UNPKG

adgile

Version:

An easy-to-use automated front-end setup.

144 lines (123 loc) 3.62 kB
'use strict' /** * A module for running the `adg init` task * @module task.init */ let settings = require('../settings.default'), gulpfile = require('../gulpfile.js'); const chalk = require('chalk'), _ = require('lodash'), gulp = require('gulp'), fs = require('fs-extra'), prompt = require('inquirer').prompt, git = require('simple-git'); /** * Run the initialise task in response to Gulp. * * First selects all files in the cwd, except for . files (.DS_Store,...) * * If files are found: inform the user to delete all files or switch directory * If no files found: download boilerplate files * * @param {*} callback */ function runtask(cb) { let cwdFiles = _.remove(fs.readdirSync(settings.cwd), function (file) { return file.substring(0,1) !== '.'; }); if (cwdFiles.length > 0) { console.log(chalk.red.inverse('The current directory is not empty! Remove all files and folders or switch directory and try again.')); } else { console.log(' '); downloadBoilerplate(); } cb(null); } /** * Download the boilerplate files * * First check if --base flag was given and validate the git url, * then clone git files to a .tmp folder in cwd. * * Finally copy files from .tmp folder to cwd and remove .tmp folder. */ function downloadBoilerplate () { if (settings.isRepo) { let gitUrl = process.argv[4]; if(!gitUrl) { return console.log(chalk.red('Please provide a git repository url.')); } if (!isGitUrl(gitUrl)) { return console.log(chalk.red('The provided url is not a valid git repository.')); } settings.gitConfig.url = gitUrl; } console.log(chalk.grey('Downloading boilerplate files...')); git().silent(true).clone(settings.gitConfig.url, settings.cwd + '/.tmp', function(err){ if (err) { return console.log(chalk.red('Download failed: Invalid username or password.')); } console.log(chalk.grey('Download finished, setting up project now...')); let files = _.remove(fs.readdirSync(settings.cwd + '/.tmp'), function (file) { return _.indexOf(['.git', '.DS_Store'], file); }); files.forEach((file, index) => { fs.copySync(settings.cwd + '/.tmp/' + file, settings.cwd + '/' + file); const done = index >= files.length -1; if(done) { fs.remove(settings.cwd + '/.tmp', err => { if(err) return console.error(err); console.log(chalk.green.bold('✓ Done! Your Adgile project is ready to go.')); finishInit(); }); } }); }); } /** * Finish the init process by prompting the user for serve, open and edit options. */ function finishInit() { prompt({ type: 'confirm', message: 'Would you like to have these files served right away?', name: 'build', default: true }).then(answer => { if (answer.build) { console.log(' '); settings.isServe = true; prompt({ type: 'confirm', message: 'Open the project in a browser?', name: 'open', default: true }).then(answers => { console.log(' '); if (answers.open) settings.isOpen = true; prompt({ type: 'confirm', message: 'Open the project in an editor?', name: 'edit', default: true }).then(answers => { if (answers.edit) settings.isEdit = true; gulp.start('build'); }); }); } else { return console.log(' '); } }); } /** * Check if given string is a Git repository * @param {string} str */ function isGitUrl(str) { var regex = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/; return regex.test(str); }; module.exports = { runtask }