Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download and generate using remote template feature #38

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/p5-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ program
.alias('g')
.option('-e, --es6', 'ES6 template')
.option('-b, --bundle', 'Generate a bundled project')
.option(
'-t, --template <remote>',
'Generate a project with template from a github repo'
)
.description('Generate a p5 project')
.action(function(req, opt) {
generator.project(req, opt);
Expand Down
115 changes: 112 additions & 3 deletions generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ var fs = require('fs');
var path = require('path');
var request = require('request');
var download = require('download');
const imageExtensions = require('image-extensions');

var client_id = "<client_id>";
var client_secret = "<client_secret>";

var templates = {
sketchjs: loadFile('templates/sketch.js'),
Expand Down Expand Up @@ -36,10 +40,26 @@ var generator = {

mkdir(project, function() {
if (opt.bundle) {
createLibraries(project)
createLibraries(project);
write(project + '/sketch.js', templates.sketchjs);
write(project + '/index.html', templates.indexhtmlb);
} else {
}
else if (opt.template) {
// template info format: owner/repo@branch
let templateInfo = opt.template;
let regex = /\w+\/\w+(\@\w+)?/i
if (!regex.test(templateInfo)) {
console.log("Please specify owner and repo !");
fs.rmdirSync(project);
return;
}
let owner = templateInfo.split("/")[0];
let repo = templateInfo.split("/")[1].split("@")[0];
let branch = templateInfo.split("/")[1].split("@")[1];
branch = branch ? branch : 'master'; // default value for branch
downloadRemoteTemplate(project, owner, repo, branch);
}
else {
var p5rc = JSON.parse(fs.readFileSync('.p5rc', 'utf-8'));
p5rc.projects.push(project);
write('.p5rc', JSON.stringify(p5rc, null, 2));
Expand Down Expand Up @@ -112,7 +132,7 @@ var generator = {
var option = {
url: 'https://api.github.com/repos/processing/p5.js/releases/latest',
headers: {
'User-Agent': 'chiunhau/p5-manager'
'User-Agent': 'chiunhau/p5-manager',
}
}

Expand Down Expand Up @@ -150,6 +170,95 @@ function createLibraries(dirName) {
});
}

function downloadRemoteTemplate(project, owner, repo, branch) {
var url = `https://api.github.com/repos/${owner}/${repo}/git/trees/${branch}`;
// using recursive to get all files in all sub directories
url += "?recursive=1";
// auth
url += "&client_id=" + client_id;
url += "&client_secret=" + client_secret;
var option = {
url: url,
headers: {
'User-Agent': 'chiunhau/p5-manager'
}
}
request(option, function(error, res, body) {
if (error) {
console.log("Error while downloading the template");
console.log("Error: " + error);
return;
}
else {
if (res.statusCode == 200) {
var data = JSON.parse(body);
var items = data.tree;
for(var i = 0; i < items.length; i++) {
let currentItem = items[i];
if (currentItem.type == 'tree') {
// this is a directory
mkdir(project + '/' + currentItem.path);
}
else {
var currentFilePath = currentItem.path;
var currentFileURL = currentItem.url;
currentFileURL += "?client_id=" + client_id;
currentFileURL += "&client_secret=" + client_secret;
downloadFile(project, currentFilePath, currentFileURL);
}
}
}
else {
console.log("Error while downloading the template");
console.log("Response code: " + res.statusCode);
fs.rmdirSync(project);
return;
}
}
});
}

function downloadFile(project, filePath, url) {
var option = {
url: url,
headers: {
'User-Agent': 'chiunhau/p5-manager',
'accept': 'application/vnd.github.VERSION.raw'
}
}
var fileExtension = path.extname(filePath).replace(".", "");
var isImage = imageExtensions.indexOf(fileExtension) != -1;
// if a file is an image then download the base64 version and decode it
if (isImage) {
option.headers.accept = 'application/vnd.github.VERSION+json';
}

request(option, function(err, resp, fileContent) {
if (err) {
console.log("Error while downloading the template");
console.log("Error: " + err);
fs.rmdirSync(project);
return;
}
else {

if (!fileContent) {
console.log("Error while downloading the template");
console.log("Undefined content on file : " + filePath);
fs.rmdirSync(project);
return;
}

if (isImage) {
fileContent = JSON.parse(fileContent).content;
fileContent = new Buffer(fileContent, 'base64');
}

write(project + '/' + filePath, fileContent);
}
});
}

// the following code are taken from https://github.com/expressjs/generator

function loadFile(name) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"debug": "~2.2.0",
"download": "^5.0.2",
"express": "~4.13.4",
"image-extensions": "^1.1.0",
"livereload": "^0.6.3",
"morgan": "~1.7.0",
"request": "^2.74.0",
Expand Down