UNPKG

1.38 kBJavaScriptView Raw
1/**
2 * Commande.js CLI
3 *
4 * Set the options for -v --help etc.
5 */
6
7'use strict';
8
9const chalk = require( 'chalk' );
10const commander = require( 'commander' );
11const maybeEnvInfo = require( './envInfo' );
12const noBlockName = require( './noBlockName' );
13const packageJson = require( '../package.json' );
14
15// Commander.js program.
16module.exports = () => {
17 // Block's name
18 let blockName;
19
20 const program = new commander.Command( packageJson.name )
21 .arguments( '<block-name>' )
22 .usage( `${ chalk.green( '<block-name>' ) }` )
23 .action( name => {
24 blockName = name;
25 } )
26 .allowUnknownOption()
27 .on( '--help', () => {
28 console.log( `\n Only ${ chalk.green( '<block-name>' ) } is required.\n` );
29 } )
30 .option( '-d, --debug', 'Prints envinfo for debugging' )
31 .description(
32 `CGB ${ chalk.dim(
33 '(create-guten-block)'
34 ) } is a Zero-Config #OCJS for builing WordPress Gutenberg Blocks.`
35 )
36 .version( packageJson.version, '-v, --version' )
37 .parse( process.argv );
38
39 // If no blockName.
40 if ( typeof blockName === 'undefined' ) {
41 // Maybe user asked for debug info.
42 maybeEnvInfo( program );
43
44 // If still running then tell user to provide blockName.
45 noBlockName();
46 } // End.
47
48 // We must have a blockName by now.
49
50 // Format the blockName.
51 const formatBlockName = blockName
52 .toLowerCase()
53 .split( ' ' )
54 .join( '-' );
55
56 return formatBlockName;
57};