Skip to content

Commit

Permalink
moved functions around and added auto-generating help
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Wilkowski <Hi@Dominik-Wilkowski.com>
  • Loading branch information
dominikwilkowski committed Mar 12, 2019
1 parent afb4032 commit b65d471
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 61 deletions.
90 changes: 72 additions & 18 deletions packages/create-keystone-app/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@

const arg = require('arg');
const chalk = require('chalk');
const generator = require('./generator');
const { exec, createAppName, checkEmptyDir } = require('./generator');

const pkgInfo = require('../package.json');

const info = {
exeName: Object.keys(pkgInfo.bin)[0],
version: pkgInfo.version,
};

// Setup all the args
const argSpec = {
'--help': Boolean,
'-h': '--help',
'--version': Boolean,
'-V': '--version',
const argSpecDescription = [
{
description: 'Version number',
command: '--help',
alias: '-h',
value: Boolean,
},
{
description: 'Displays help',
command: '--version',
alias: '-v',
value: Boolean,
},
];

// Translate args to arg format
const argSpec = {};
argSpecDescription.map(arg => {
argSpec[arg.command] = arg.value;
argSpec[arg.alias] = arg.command;
});

// Generate help from our arg specs
const help = args => {
return `
╦╔═ ╔═╗ ╦ ╦ ╔═╗ ╔╦╗ ╔═╗ ╔╗╔ ╔═╗ ╦ ╔═╗
╠╩╗ ║╣ ╚╦╝ ╚═╗ ║ ║ ║ ║║║ ║╣ ║ ╚═╗
╩ ╩ ╚═╝ ╩ ╚═╝ ╩ ╚═╝ ╝╚╝ ╚═╝ ╚╝ ╚═╝
${chalk.bold('Usage')}
${chalk.gray('$')} ${info.exeName} ${chalk.gray('<project name>')}
${chalk.bold('Common Options')}
${argSpecDescription
.map(arg => ` ${`${arg.command}, ${arg.alias}`.padEnd(20, ' ')} ${arg.description}`)
.join('\n')}\n
`;
};

let args;
Expand All @@ -22,39 +61,54 @@ try {
} catch (error) {
if (error.code === 'ARG_UNKNOWN_OPTION') {
console.error(chalk.red(`\nError: ${error.message}`));
console.info(generator.help());
console.info(help());
process.exit(0);
}
}

if (args['--version']) {
console.info(generator.version());
console.info(info.version);
process.exit(0);
}

if (args['--help']) {
console.info(generator.help());
console.info(help());
process.exit(0);
}

const name = createAppName(args._.join(' '));

// if project name is missing print help
if (args._.length === 0) {
console.info(generator.help());
if (args._.length === 0 || name === '') {
console.info(help());
process.exit(0);
}

// check if folder exists and is not empty
const name = generator.createAppName(args._.join(' '));
try {
generator.checkEmptyDir(name);
checkEmptyDir(name);
} catch (error) {
console.error(chalk.red(`\n${error}`));
console.info(generator.help());
console.info(help());
process.exit(0);
}

// Everything else is assumed to be a command we want to execute - more options added
generator.exec(name).catch(error => {
console.error(error);
process.exit(1);
});
exec(name)
.catch(error => {
console.error(error);
process.exit(1);
})
.then(({ name, appName }) => {
console.log();
console.log(chalk.green(`Your app "${name}" is ready in ${appName}/`));
console.log(
chalk.green(
`You can start your app with ${chalk.yellow(`cd ${appName}`)} and ${chalk.yellow(
`yarn start`
)}`
)
);
console.log();
process.exit(0);
});
48 changes: 5 additions & 43 deletions packages/create-keystone-app/bin/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
var ejs = require('ejs');
const endent = require('endent');

const pkgInfo = require('../package.json');

const info = {
exeName: Object.keys(pkgInfo.bin)[0],
version: pkgInfo.version,
};

const templateDir = path.join(__dirname, '..', 'templates');

Expand Down Expand Up @@ -46,7 +38,7 @@ function generate(name) {
appName,
});
installDependencies(projectDir);
done(name, appName);
return { name, appName };
}

/**
Expand Down Expand Up @@ -86,7 +78,9 @@ function copyTemplate(templatePath, projectDir, templateData) {
* @param {String} projectDir project drectory
*/
function installDependencies(projectDir) {
console.log(chalk.green(`Created app in ${projectDir}, installing project dependencies now`));
console.log(
chalk.green(`\nCreated app in ${chalk.yellow(projectDir)}\nInstalling project dependencies now`)
);
const currentDir = process.cwd();
process.chdir(projectDir);
child_process.spawnSync('yarnpkg', {
Expand All @@ -95,22 +89,6 @@ function installDependencies(projectDir) {
process.chdir(currentDir);
}

/**
* prints success message after completion
* @param {String} name name of the project
* @param {String} appName npm friendly name of the project
*/
function done(name, appName) {
console.log(chalk.green(`Your app "${name}" is ready in ${appName}/`));
console.log(
chalk.green(
`You can start your app with ${chalk.yellow(`cd ${appName}`)} and ${chalk.yellow(
`yarn start`
)}`
)
);
}

/**
* Create an app name from a directory path, fitting npm naming requirements.
*
Expand All @@ -126,29 +104,13 @@ function createAppName(pathName) {
}

module.exports = {
version: () => info.version,

help: () => endent`\n
╦╔═ ╔═╗ ╦ ╦ ╔═╗ ╔╦╗ ╔═╗ ╔╗╔ ╔═╗ ╦ ╔═╗
╠╩╗ ║╣ ╚╦╝ ╚═╗ ║ ║ ║ ║║║ ║╣ ║ ╚═╗
╩ ╩ ╚═╝ ╩ ╚═╝ ╩ ╚═╝ ╝╚╝ ╚═╝ ╚╝ ╚═╝
Usage
${chalk.gray('$')} ${info.exeName} ${chalk.gray('<project name>')}
Common Options
--version, -V Version number
--help, -h Displays help\n`,

exec: name => {
try {
generate(name);
return Promise.resolve();
return Promise.resolve(generate(name));
} catch (error) {
return Promise.reject(error);
}
},

createAppName,
checkEmptyDir,
};

0 comments on commit b65d471

Please sign in to comment.