Skip to content

Commit

Permalink
Project generation enhancements (#874)
Browse files Browse the repository at this point in the history
* add more template variables

* move dirExits to the utils

* create defaults, license type
  • Loading branch information
DenysVuika authored and ForsakenHarmony committed Aug 21, 2019
1 parent 4a891ee commit 9cef786
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
33 changes: 20 additions & 13 deletions packages/cli/lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ 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';
const RGX = /\.(woff2?|ttf|eot|jpe?g|ico|png|gif|webp|mp4|mov|ogg|webm)(\?.*)?$/i;
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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/lib/util.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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 });
};
Expand Down

0 comments on commit 9cef786

Please sign in to comment.