|
7 | 7 | 'use strict'; |
8 | 8 |
|
9 | 9 | const chalk = require( 'chalk' ); |
10 | | -const envinfo = require( 'envinfo' ); |
11 | 10 | const commander = require( 'commander' ); |
12 | | -const clearConsole = require( './consoleClear' ); |
| 11 | +const maybeEnvInfo = require( './envInfo' ); |
| 12 | +const noBlockName = require( './noBlockName' ); |
13 | 13 | const packageJson = require( '../package.json' ); |
14 | 14 |
|
15 | 15 | // Commander.js program. |
16 | 16 | module.exports = () => { |
| 17 | + // Block's name |
| 18 | + let blockName; |
| 19 | + |
17 | 20 | const program = new commander.Command( packageJson.name ) |
18 | | - .version( packageJson.version, '-v, --version' ) |
19 | | - .description( |
20 | | - `CGB ${ chalk.dim( |
21 | | - '(create-guten-block)' |
22 | | - ) } is a Zero-Config #OCJS for builing WordPress Gutenberg Blocks.` |
23 | | - ); |
24 | | - program |
25 | | - .option( '-d, --debug', 'Prints envinfo for debugging. ' ) |
26 | 21 | .arguments( '<block-name>' ) |
27 | 22 | .usage( `${ chalk.green( '<block-name>' ) }` ) |
| 23 | + .action( name => { |
| 24 | + blockName = name; |
| 25 | + } ) |
28 | 26 | .allowUnknownOption() |
29 | 27 | .on( '--help', () => { |
30 | 28 | console.log( `\n Only ${ chalk.green( '<block-name>' ) } is required.\n` ); |
31 | 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' ) |
32 | 37 | .parse( process.argv ); |
33 | 38 |
|
34 | | - // Envinfo. |
35 | | - if ( program.debug ) { |
36 | | - clearConsole(); |
37 | | - |
38 | | - console.log( |
39 | | - '\n🔰 ' + |
40 | | - chalk.black.bgYellow( ' Printing the debug env info below: \n\n' ) + |
41 | | - chalk.dim( ' This may take a couple of seconds...' ) |
42 | | - ); |
43 | | - |
44 | | - // Print the envinfo. |
45 | | - envinfo.print( { |
46 | | - packages: [ 'cgb-scripts' ], |
47 | | - cpu: true, |
48 | | - duplicates: true, |
49 | | - browsers: true, |
50 | | - noNativeIDE: true, |
51 | | - } ); |
52 | | - |
53 | | - console.log( |
54 | | - '\n✅ ' + |
55 | | - chalk.black.bgGreen( ' Done ' ) + |
56 | | - chalk.dim( ' You can copy paste this info to share it...\n' ) |
57 | | - ); |
58 | | - // Let's end the process so the app doesn't continue. |
59 | | - process.exit(); |
60 | | - } |
| 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; |
61 | 57 | }; |
0 commit comments