pmint
Version:
PepperMint CMS command-line tools
126 lines (109 loc) • 3.71 kB
JavaScript
var fs = require('fs');
var async = require('async');
var inquirer = require('inquirer');
var gh = require('download-github-repo');
var request = require('request');
require('colors');
var AdmZip = require('adm-zip');
var ncp = require('ncp').ncp;
ncp.limit = 30;
var isValidSite = function(sitename) {
return /^[\d\w\-_\.,]+$/.test(sitename);
}
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
module.exports = function (options) {
var config = {
siteName : options.siteName,
localUpdates : options.localUpdates,
firebase : options.firebase
}
async.series([
function(step) {
if(!isValidSite(config.siteName)) {
console.log('Invalid sitename. Sitename can only contain letters, numbers, and dashes.'.red);
process.exit(1);
}
step();
},
function (step) {
if(fs.existsSync(config.siteName)) {
inquirer.prompt({
type: 'confirm',
name: 'confirm',
message: 'There is already a directory with the name of ' + config.siteName + '. This will overwrite that directory permanently and make PepperMint in it. Continue?'
}, function(answer) {
if(answer.confirm) {
deleteFolderRecursive(config.siteName);
step();
} else {
process.exit(0);
}
});
} else {
step();
}
},
function(step) {
fs.mkdirSync(config.siteName);
console.log("Created site directory...".green);
fs.writeFile(config.siteName+"/config.json",JSON.stringify(config),function(err){
if(!err) {
console.log("Wrote config file...".green);
/*gh("PepperMintCMS/PepperMint-cli",config.siteName,function(){
console.log("Boilerplate init in site folder successful...".green);
})*/
var out = fs.createWriteStream(config.siteName+"/temp.zip"); // For saving NSE Equity bhavcopy
var req = request(
{
method: 'GET',
uri: 'https://github.com/PepperMintCMS/PepperMint-Boilerplate/archive/master.zip',
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
"Referer": "https://github.com/PepperMintCMS/PepperMint-Boilerplate/archive/master.zip",
"Accept-Encoding": "gzip,deflate,sdch",
"encoding": "null",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "cookie"
}
}
);
req.pipe(out);
req.on('end', function() {
var zip = new AdmZip(config.siteName+"/temp.zip"),
zipEntries = zip.getEntries();
zip.extractAllTo(/*target path*/config.siteName, /*overwrite*/true);
ncp(config.siteName+"/PepperMint-Boilerplate-master", config.siteName, function (err) {
if (err) {
return console.error(err);
}
console.log('Fixing downloaded weirdness...'.green);
step();
});
});
}
});
},
function(step) {
deleteFolderRecursive(config.siteName+"/PepperMint-Boilerplate-master");
fs.unlink(config.siteName+"/temp.zip");
console.log("Clearing up...".green);
step();
},
function() {
console.log("All done!".green);
console.log("Now you can run 'npm install' in the site dir to install dependencies.".blue);
console.log("Now you can run 'node runlocal.js' in the site dir to serve the site locally.".blue);
}
]);
}