Skip to content

Commit

Permalink
working on #813
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 bef0e49 commit 9be302d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
42 changes: 32 additions & 10 deletions packages/create-keystone-app/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,62 @@
#!/usr/bin/env node

const fs = require('fs');
const arg = require('arg');

const chalk = require('chalk');
const generator = require('./generator');

// Setup all the args
const argSpec = {
'--help': Boolean,
'-h': '--help',
'--version': Boolean,
'-V': '--version',
};

// Parse the command line args coming in
const args = arg(argSpec, {
// Capture the project name only
permissive: false,
});
let args;
try {
// Parse the command line args coming in
args = arg(argSpec, {
// Capture the project name only
permissive: false,
});
} catch (error) {
if (error.code === 'ARG_UNKNOWN_OPTION') {
console.error(chalk.red(`\nError: ${error.message}`));
console.info(generator.help());
process.exit(0);
}
}

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

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

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

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

// Everything else is assumed to be a command we want to execute - more options added
generator.exec(args).catch(error => {
generator.exec(name).catch(error => {
console.error(error);
process.exit(1);
});
25 changes: 15 additions & 10 deletions packages/create-keystone-app/bin/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function checkEmptyDir(dir) {
try {
const readDir = fs.readdirSync(dir);
if (readDir && readDir.length > 0) {
throw new Error(`Project directory is not empty: ${path.basename(dir)}`);
throw new Error(`Project directory is not empty: ${path.basename(dir)}/`);
}
} catch (error) {
if (error.code !== 'ENOENT') throw error;
Expand Down Expand Up @@ -101,8 +101,8 @@ function installDependencies(projectDir) {
* @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 by 'cd ${appName}' and 'yarn start'`));
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`)}`));
}

/**
Expand All @@ -122,22 +122,27 @@ function createAppName(pathName) {
module.exports = {
version: () => info.version,

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

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

createAppName,
checkEmptyDir,
};

0 comments on commit 9be302d

Please sign in to comment.