diff --git a/packages/cli/lib/commands/create.js b/packages/cli/lib/commands/create.js index d420dd81a..b399b5e1d 100644 --- a/packages/cli/lib/commands/create.js +++ b/packages/cli/lib/commands/create.js @@ -8,7 +8,15 @@ const { green } = require('kleur'); const { resolve, join } = require('path'); const { prompt } = require('prompts'); const isValidName = require('validate-npm-package-name'); -const { info, isDir, hasCommand, error, trim, warn } = require('../util'); +const { + info, + isDir, + hasCommand, + error, + trim, + warn, + dirExists, +} = require('../util'); const { addScripts, install, initGit } = require('../lib/setup'); const ORG = 'preactjs-templates'; @@ -16,14 +24,6 @@ const RGX = /\.(woff2?|ttf|eot|jpe?g|ico|png|gif|webp|mp4|mov|ogg|webm)(\?.*)?$/ const isMedia = str => RGX.test(str); const capitalize = str => str.charAt(0).toUpperCase() + str.substring(1); -function directoryExists(workingDir, destDir) { - if (workingDir && destDir) { - const target = resolve(workingDir, destDir); - return isDir(target); - } - return false; -} - // Formulate Questions if `create` args are missing function requestParams(argv) { const cwd = resolve(argv.cwd); @@ -80,8 +80,7 @@ function requestParams(argv) { message: 'Directory to create the app', }, { - type: prev => - !directoryExists(cwd, prev || argv.dest) ? null : 'confirm', + type: prev => (!dirExists(cwd, prev || argv.dest) ? null : 'confirm'), name: 'force', message: 'The destination directory exists. Overwrite?', initial: false, @@ -222,12 +221,20 @@ module.exports = async function(repo, dest, argv) { if (keeps.length) { // eslint-disable-next-line - let dict = new Map(); + const dict = new Map(); + const templateVar = str => new RegExp(`{{\\s?${str}\\s}}`, 'g'); + + dict.set(templateVar('pkg-install'), isYarn ? 'yarn' : 'npm install'); + dict.set(templateVar('pkg-run'), isYarn ? 'yarn' : 'npm run'); + dict.set(templateVar('pkg-add'), isYarn ? 'yarn add' : 'npm install'); + dict.set(templateVar('now-year'), new Date().getFullYear()); + dict.set(templateVar('license'), argv.license || 'MIT'); + // TODO: concat author-driven patterns ['name'].forEach(str => { // if value is defined if (argv[str] !== void 0) { - dict.set(new RegExp(`{{\\s?${str}\\s}}`, 'g'), argv[str]); + dict.set(templateVar(str), argv[str]); } }); // Update each file's contents diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index 97f32799c..04a270308 100755 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -61,11 +61,12 @@ prog .describe('Create a new application') .option('--name', 'The application name') .option('--cwd', 'A directory to use instead of $PWD', '.') - .option('--force', 'Force destination output; will override!') + .option('--force', 'Force destination output; will override!', false) .option('--install', 'Install dependencies', true) - .option('--yarn', 'Use `yarn` instead of `npm`') - .option('--git', 'Initialize git repository') - .option('-v, --verbose', 'Verbose output') + .option('--yarn', 'Use `yarn` instead of `npm`', false) + .option('--git', 'Initialize git repository', false) + .option('--license', 'License type', 'MIT') + .option('-v, --verbose', 'Verbose output', false) .action(commands.create); prog diff --git a/packages/cli/lib/util.js b/packages/cli/lib/util.js index 555db47d6..8e8a5b40d 100644 --- a/packages/cli/lib/util.js +++ b/packages/cli/lib/util.js @@ -1,5 +1,5 @@ const { blue, yellow, red } = require('kleur'); -const { normalize } = require('path'); +const { normalize, resolve } = require('path'); const { statSync, existsSync } = require('fs'); const symbols = require('./symbols'); const which = require('which'); @@ -8,6 +8,14 @@ exports.isDir = function(str) { return existsSync(str) && statSync(str).isDirectory(); }; +exports.dirExists = function(workingDir, destDir) { + if (workingDir && destDir) { + const target = resolve(workingDir, destDir); + return exports.isDir(target); + } + return false; +}; + exports.hasCommand = function(str) { return !!which.sync(str, { nothrow: true }); };