UNPKG

1.6 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 invalidBlockName = require( './invalidBlockName' );
14const packageJson = require( '../package.json' );
15
16// Commander.js program.
17module.exports = () => {
18 // Block's name
19 let blockName;
20
21 const program = new commander.Command( packageJson.name )
22 .arguments( '<block-name>' )
23 .usage( `${ chalk.green( '<block-name>' ) }` )
24 .action( name => {
25 blockName = name;
26 } )
27 .allowUnknownOption()
28 .on( '--help', () => {
29 console.log( `\n Only ${ chalk.green( '<block-name>' ) } is required.\n` );
30 } )
31 .option( '-d, --debug', 'Prints envinfo for debugging' )
32 .description(
33 `CGB ${ chalk.dim(
34 '(create-guten-block)'
35 ) } is a Zero-Config #OCJS for builing WordPress Gutenberg Blocks.`
36 )
37 .version( packageJson.version, '-v, --version' )
38 .parse( process.argv );
39
40 // If no blockName.
41 if ( typeof blockName === 'undefined' ) {
42 // Maybe user asked for debug info.
43 maybeEnvInfo( program );
44
45 // If still running then tell user to provide blockName.
46 noBlockName();
47 } // End.
48
49 // We must have a blockName by now.
50
51 // Format the blockName.
52 const formatBlockName = blockName
53 .toLowerCase()
54 .split( ' ' )
55 .join( '-' );
56
57 // Check if block name is valid.
58 const blockNameRegex = /^[a-z][a-z0-9-]/;
59 if ( ! blockNameRegex.test( formatBlockName ) ) {
60 invalidBlockName();
61 }
62
63 return formatBlockName;
64};