Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project generation enhancements #874

Merged
merged 4 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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