diff --git a/src/cli.ts b/src/cli.ts index 50b2d32..d31db0e 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,6 @@ #! /usr/bin/env node -import {version, description} from '../package.json'; +import {version} from '../package.json'; import {log} from 'console'; import chalk from 'chalk'; import arg from 'arg'; @@ -12,51 +12,44 @@ const args = arg({ '-v': '--version' }, {permissive: true}); -if (args['--version']) { +const commands = { + init: async () => import('./gridtastic-init').then(i => i.default), + scaffold: async () => import('./gridtastic-scaffold').then(async i => i.default), + override: async () => import('./gridtastic-override').then(async i => i.default), + fresh: async () => import('./gridtastic-fresh').then(async i => i.default) +}; + +const foundCommand = Boolean(commands[args._[0]]); + +if (!foundCommand && args['--version']) { log(version); process.exit(0); } -if (args['--help']) { - const help = chalk` +if (!foundCommand && args['--help']) { + log(chalk` + {bold Usage} + $ gridtastic {bold } - ${description} + {bold Available commands} + ${Object.keys(commands).join(', ')} - {bold Usage} - {dim $} {bold gridtastic} + {bold Options} + --version, -v Show version + --help, -h Show help - init --repo REPO --dest DEST Download Gridsome starter - override --html --vue Override App.vue and/or index.html - scaffold --TYPE --name NAME Scaffold out a new file - fresh Delete Gridsome boilerplate pages and folder-specific README.md files - - {bold Options} - --repo, -r GITHUB_USER/REPO_NAME - --dest, -d Folder to clone Gridsome starter project to - --html Denotes index.html - --vue Denotes App.vue - --template, -t /templates - --page, -p /pages - --component, -c /components - --layout, -l /layouts - --name, -n SomeFilename Filename to be used (will be pascal cased by CLI) - --version, -v Show version - --help, -h Show help -`; - log(help); + For more information run a command with the --help flag + $ gridtastic scaffold --help +`); process.exit(0); } -let forwardedArgs = args._.slice(1); - -const commands = { - init: async () => import('./gridtastic-init').then(i => i.default), - scaffold: async () => import('./gridtastic-scaffold').then(async i => i.default), - override: async () => import('./gridtastic-override').then(async i => i.default), - fresh: async () => import('./gridtastic-fresh').then(async i => i.default) -}; - const command = commands[args._[0]]; +const forwardedArgs = args._.slice(1); + +if (args['--help']) { + forwardedArgs.push('--help'); +} if (command) { command().then(exec => exec(forwardedArgs)); diff --git a/src/gridtastic-fresh.ts b/src/gridtastic-fresh.ts index c575264..85d8a76 100644 --- a/src/gridtastic-fresh.ts +++ b/src/gridtastic-fresh.ts @@ -2,8 +2,25 @@ import del from 'del'; import chalk from 'chalk'; import scaffold from './gridtastic-scaffold'; import isGridsomeProject from './isGridsomeProject'; +import arg from 'arg'; + +export default async (argv: string[]): Promise => { + const args = arg({ + '--help': Boolean, + '-h': '--help' + }, {argv}); + + if (args['--help']) { + console.log(chalk` + {bold Description} + Removes boilerplate files like README.md and About.vue typically included for new developers. + + {bold Usage} + $ gridtastic fresh + `); + process.exit(0); + } -export default async (): Promise => { isGridsomeProject(); const deletedPaths = await del([ diff --git a/src/gridtastic-init.ts b/src/gridtastic-init.ts index d6ea567..07b1edb 100644 --- a/src/gridtastic-init.ts +++ b/src/gridtastic-init.ts @@ -2,14 +2,33 @@ import degit from 'degit'; import chalk from 'chalk'; import arg from 'arg'; -export default (argv): void => { +export default (argv: string[]): void => { + const args = arg({ + '--repo': String, + '--dest': String, + '--help': Boolean, + '-h': '--help' + }, {argv}); + + if (args['--help']) { + console.log(chalk` + {bold Description} + Creates overrides for App.vue and/or index.html, with boilerplate code. + + {bold Usage} + $ gridtastic init [--repo repo] [--dest dest] + + [repo] refers to a GitHub repository containing a starter project. [dest] + is the desired directory name for the clone repository. [repo] defaults to + "brandonpittman/gridsome-starter-default" and [dest] defaults to "gridsome-starter-default". + `); + process.exit(0); + } + let { '--repo': repo = 'brandonpittman/gridsome-starter-default', '--dest': dest = 'gridsome-starter-default' - } = arg({ - '--repo': String, - '--dest': String - }, {argv}); + } = args; let emitter = degit(repo, { cache: false, diff --git a/src/gridtastic-override.ts b/src/gridtastic-override.ts index 729f2e5..0996a84 100644 --- a/src/gridtastic-override.ts +++ b/src/gridtastic-override.ts @@ -5,16 +5,31 @@ import isGridsomeProject from './isGridsomeProject'; import pkgDir from 'pkg-dir'; import arg from 'arg'; -export default async (argv): Promise => { +export default async (argv: string[]): Promise => { + const args = arg({ + '--html': Boolean, + '--vue': Boolean, + '--help': Boolean, + '-h': '--help' + }, {argv}); + + if (args['--help']) { + console.log(chalk` + {bold Description} + Creates overrides for App.vue and/or index.html, with boilerplate code. + + {bold Usage} + $ gridtastic override --vue --html + `); + process.exit(0); + } + isGridsomeProject(); let { '--html': html = false, '--vue': vue = false - } = arg({ - '--html': Boolean, - '--vue': Boolean - }, {argv}); + } = args; if (!fs.existsSync('./src')) { log(chalk.blue('Creating src directory')); diff --git a/src/gridtastic-scaffold.ts b/src/gridtastic-scaffold.ts index aff62b3..7be3643 100644 --- a/src/gridtastic-scaffold.ts +++ b/src/gridtastic-scaffold.ts @@ -6,16 +6,15 @@ import pascalcase from 'pascalcase'; import isGridsomeProject from './isGridsomeProject'; import arg from 'arg'; -export default async (argv): Promise => { - isGridsomeProject(); +// Scaffold --TYPE --name NAME Scaffold out a new file +// --template, -t /templates +// --page, -p /pages +// --component, -c /components +// --layout, -l /layouts +// --name, -n SomeFilename Filename to be used (will be pascal cased by CLI) - let { - '--name': name = null, - '--component': component = false, - '--template': template = false, - '--page': page = false, - '--layout': layout = false - } = arg({ +export default async (argv: string[]): Promise => { + const args = arg({ '--template': Boolean, '--page': Boolean, '--component': Boolean, @@ -25,9 +24,34 @@ export default async (argv): Promise => { '-c': '--component', '-p': '--page', '-t': '--template', - '-n': '--name' + '-n': '--name', + '--help': Boolean, + '-h': '--help' }, {argv}); + if (args['--help']) { + console.log(chalk` + {bold Description} + Creates a new Vue file of page, component, template, or layout types. + + {bold Usage} + $ gridtastic scaffold --page --name + + will converted to PascalCase automatically. + `); + process.exit(0); + } + + isGridsomeProject(); + + let { + '--name': name = null, + '--component': component = false, + '--template': template = false, + '--page': page = false, + '--layout': layout = false + } = args; + if (!(component || page || template || layout)) { console.log(chalk.bold.red('Please provide one of the filetype options.')); process.exit(1);