UNPKG

gia-cli

Version:

Guardian US Interactive CLI tool

126 lines (102 loc) 3.35 kB
/*global require, module, console, process */ var chalk = require( 'chalk' ); var path = require( 'path' ); var semver = require( 'semver' ); var sander = require( 'sander' ); var Promise = sander.Promise; var prompt = require( 'prompt' ); var getPath = require( './getPath' ); var copyDir = require( './copyDir' ); var checkVersion = require( '../utils/checkVersion' ); var cwd = process.cwd(); var templates = path.join( __dirname, '../../templates' ); module.exports = function ( options ) { var name, dest; if ( options.help ) { options.args.unshift( 'create' ); require( './help' )( options ); return; } var name = path.basename( cwd ).replace( /^[\d\-]+/, '' ); checkVersion( options.flags.force ).then( function () { ensureEmptyDir( cwd ) .then( function () { // copy project template, processing files as necessary return stamp( options.flags.template || options.args[0], name, cwd ); }) .then( function () { console.log( '\n...and we\'re done. Run ' + chalk.cyan( 'npm i' ) + ' to install dependencies, and ' + chalk.cyan( 'npm start' ) + ' to serve and preview your project' ); }) .catch( function ( err ) { console.log( '\nOh noes! Something went wrong' ); console.error( err ); }); }).catch( function ( err ) { console.error( err.message ); process.exit( 1 ); }); }; function ensureEmptyDir ( dir ) { return sander.readdir( dir ).then( function ( files ) { if ( files.length ) { console.log( '\nTarget folder is not empty! To create a project in a new folder, use ' + chalk.cyan( 'gia create folder-name' ) + ', otherwise type the word ' + chalk.cyan( 'nuke' ) + ' to continue (or hit Enter to abort).' ); return new Promise( function ( fulfil, reject ) { prompt.start(); prompt.get([ 'confirmation' ], function ( err, result ) { if ( !result || result.confirmation !== 'nuke' ) { console.log( 'Aborting...' ); process.exit(); } console.log( 'Deleting contents of %s...', dir ); Promise.all( files.map( function ( file ) { return sander.rimraf( dir, file ); }) ).then( fulfil, reject ); }); }); } }, function () { // directory doesn't exist, we just need to add this function // so that the error doesn't propagate }); } function stamp ( template, name, dest ) { var root, variables, varPattern = /__(.+?)__/g, varReplacer, processor; template = template || 'default'; root = path.join( templates, template, 'root' ); var rename = {}; try { rename = require( path.join( templates, template, 'rename.json' ) ); } catch ( err ) { // noop } var renamer = getRenamer( rename ); console.log( 'Copying %s template to %s...', template, dest ); variables = { NAME: name, PATH: getPath( name ), PORT: getPort( name ) }; varReplacer = function ( match, $1 ) { return variables[ $1 ] || match; }; processor = function ( content ) { return content.replace( varPattern, varReplacer ); }; return copyDir( root, dest, processor, renamer ); } function getRenamer ( rename ) { return function ( file ) { var basename = path.basename( file ); return path.join( path.dirname( file ), rename[ basename ] || basename ); } } function getPort ( name ) { var port = 4567, i; i = name.length; while ( i-- ) { port += name.charCodeAt( i ); } return port; }