1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | 'use strict';
|
8 |
|
9 | const chalk = require( 'chalk' );
|
10 | const commander = require( 'commander' );
|
11 | const maybeEnvInfo = require( './envInfo' );
|
12 | const noBlockName = require( './noBlockName' );
|
13 | const invalidBlockName = require( './invalidBlockName' );
|
14 | const packageJson = require( '../package.json' );
|
15 |
|
16 |
|
17 | module.exports = () => {
|
18 |
|
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 |
|
41 | if ( typeof blockName === 'undefined' ) {
|
42 |
|
43 | maybeEnvInfo( program );
|
44 |
|
45 |
|
46 | noBlockName();
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const formatBlockName = blockName
|
53 | .toLowerCase()
|
54 | .split( ' ' )
|
55 | .join( '-' );
|
56 |
|
57 |
|
58 | const blockNameRegex = /^[a-z][a-z0-9-]/;
|
59 | if ( ! blockNameRegex.test( formatBlockName ) ) {
|
60 | invalidBlockName();
|
61 | }
|
62 |
|
63 | return formatBlockName;
|
64 | };
|