craft-maker
Version:
249 lines (203 loc) • 6.42 kB
JavaScript
const library = require('../lib/index.js'),
fs = require('file-system'),
fse = require('fs-extra'),
clone = require('gitclone'),
mv = require('mv'),
path = require('path'),
notifier = require('node-notifier'),
log = require('fancy-log'),
chalk = require('chalk'),
inquirer = require('inquirer'),
userHome = require('user-home'),
shell = require('shelljs');
// Delete the 0 and 1 argument (node and script.js)
let args = process.argv.splice(process.execArgv.length + 2);
// Retrieve the first argument
let name = args[0];
let options = {
repo : 'marknotton/craft-master',
branch : 'experiments',
}
let cwd = process.cwd();
let tempFolder = 'temp';
let tempPath = path.resolve(cwd, tempFolder);
let allFiles = [];
notifier.on('click', function(notifierObject, options) {
console.log('One day, a fancy checklist will be here. ');
});
var Spinner = require('cli-spinner').Spinner;
const _progress = require('cli-progress');
// start the progress bar with a total value of 200 and start value of 0
function fileCount() {
var files = fs.readdirSync(cwd);
return files.length;
}
//
// const envmod = require('gulp-env-modify')
// envmod.setVariable('DB_DATABASE', '222')
// update the current value in your application..
var counter = 0;
function install() {
if (fileCount()) {
if ( cwd == userHome) {
console.log(`Jesus, you can't be doing stuff like this in your home directory.`)
return false;
}
inquirer.prompt([
{
type: 'confirm',
name: 'whatToDelete1',
message: `It looks like your working directory is not empty. \nIt needs to be empty before we can continue. Would you like to delete everything in: \n${cwd}`,
default: false
},
{
type: 'confirm',
name: 'whatToDelete2',
message: `Whoah! There are ${fileCount()}+ files in there. \nAre you absolutely positive you want to do this?`,
default: false,
when : (answer) => {
return answer.whatToDelete1
}
},
{
type: 'confirm',
name: 'whatToDelete3',
message: `Holy shit... you're really doing this. Everything in \n${cwd} is going to be removed. \nThis can't be undone. Nothing is moved to Trash... it's just GONE! Still want to do this?!`,
default: false,
when : (answer) => {
return answer.whatToDelete2
}
},
]).then(answers => {
if ( answers.whatToDelete1 && answers.whatToDelete2 && answers.whatToDelete3 ) {
var timeleft = 10;
var downloadTimer = setInterval(function(){
msg = "Deleting files in " + (timeleft - 5);
switch (timeleft) {
case 6:
msg = `God help you man!`;
break;
case 5:
msg = 'Deleting files in 2';
break;
case 4:
msg = 'Deleting files in 1 and a half';
break;
case 3:
msg = "Deleting files in 1 and 3/4";
break;
case 2:
msg = "FINE!";
break;
}
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(msg);
timeleft = timeleft - 1;
if(timeleft <= 0) {
emptyWorkingDirectory(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`DONE!!\n`);
cloneReo()
});
clearInterval(downloadTimer);
}
},1000);
} else {
console.log('Do nothing');
}
});
} else {
console.log("It's empty ya'll")
cloneReo()
}
}
function cloneReo() {
var spinner = new Spinner(`${chalk.cyan("Installing Craft CMS (" + options.branch + ")")} ${chalk.green('Please wait...')} %s`);
spinner.setSpinnerString('|/-\\');
spinner.start();
// create a new progress bar instance and use shades_classic theme
// const bar = new _progress.Bar({}, _progress.Presets.shades_classic);
const bar = new _progress.Bar({
format: `Downloading | ${chalk.cyan('[{bar}]')} {percentage}% | ETA: {eta}s | {value}/{total} Files`,
barCompleteChar: '=',
barIncompleteChar: '-'
});
clone(options.repo, { branch: options.branch, dest: tempFolder }, (err) => {
if ( err ) {
return console.error(err);
} else {
fs.recurseSync(tempPath, ['**/*.*', '!.git'], (filepath, relative, filename) => {
if ( typeof filename !== 'undefined' && (!relative.includes('.git') || relative.includes('.gitignore'))) {
allFiles.push({
'filepath':filepath,
'relative':relative,
'filename':filename
})
}
})
if ( allFiles.length ) {
spinner.stop(true);
bar.start(allFiles.length, counter);
allFiles.forEach((file, i) => {
fse.move(file.filepath, path.resolve(cwd, file.relative), {mkdirp: true}, function(err) {
if ( err ) {
return console.error(err);
} else {
counter = counter + 1;
bar.update(counter);
if (i === allFiles.length - 1) {
bar.update(allFiles.length);
notifier.notify({
title: `Craft CMS (${options.branch}) Installed`,
message: `Click here for what's next checklist`,
icon: `https://github.com/marknotton/craft-master/blob/${options.branch}/icon.png?raw=true`,
wait: true
});
bar.stop();
log(`${chalk.cyan("Craft CMS has succefully been cloned to ")} ${chalk.green(cwd)}`);
setTimeout(function(){
fs.rmdirSync(tempPath);
fs.copyFile(path.resolve(cwd, '.env.example'), path.resolve(cwd, '.env'), {
done: function(err) {
if (shell.exec('sudo composer update && npm install').code !== 0) {
shell.echo('Error: Git commit failed');
shell.exit(1);
}
}
});
}, 1000);
}
}
})
});
}
}
})
}
function emptyWorkingDirectory(cb) {
filesToDelete = [];
counter = 0;
fs.readdir(cwd, (err, files) => {
if (err) { throw err; }
for (const file of files) {
if ( file != '.git') {
fse.remove(path.resolve(cwd, file), err => {
if (err) { throw err; }
counter = counter + 1;
if (counter === files.length - 1 && typeof cb !== 'undefined') {
console.log('last');
cb();
}
});
}
}
});
}
if ( name == 'install') {
install();
}
// Displays the text in the console
// library.say(name + ', get back, come on before we crack Lose your blues, everybody cut footloose');