Skip to content

Commit ef60da7

Browse files
author
Ahmad Awais
committed
👌 IMPROVE: Block name via Commander
1 parent bf3f6de commit ef60da7

7 files changed

Lines changed: 113 additions & 84 deletions

File tree

packages/create-guten-block/app/cli.js

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,51 @@
77
'use strict';
88

99
const chalk = require( 'chalk' );
10-
const envinfo = require( 'envinfo' );
1110
const commander = require( 'commander' );
12-
const clearConsole = require( './consoleClear' );
11+
const maybeEnvInfo = require( './envInfo' );
12+
const noBlockName = require( './noBlockName' );
1313
const packageJson = require( '../package.json' );
1414

1515
// Commander.js program.
1616
module.exports = () => {
17+
// Block's name
18+
let blockName;
19+
1720
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. ' )
2621
.arguments( '<block-name>' )
2722
.usage( `${ chalk.green( '<block-name>' ) }` )
23+
.action( name => {
24+
blockName = name;
25+
} )
2826
.allowUnknownOption()
2927
.on( '--help', () => {
3028
console.log( `\n Only ${ chalk.green( '<block-name>' ) } is required.\n` );
3129
} )
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' )
3237
.parse( process.argv );
3338

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;
6157
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Environment Info
3+
*
4+
* Prints envinfo for debugging.
5+
*/
6+
7+
'use strict';
8+
9+
const chalk = require( 'chalk' );
10+
const envinfo = require( 'envinfo' );
11+
const clearConsole = require( './consoleClear' );
12+
13+
module.exports = program => {
14+
// Envinfo.
15+
if ( program.debug ) {
16+
clearConsole();
17+
18+
console.log(
19+
'\n🔰 ' +
20+
chalk.black.bgYellow( ' Printing the debug env info below: \n\n' ) +
21+
chalk.dim( ' This may take a couple of seconds...' )
22+
);
23+
24+
// Print the envinfo.
25+
envinfo.print( {
26+
packages: [ 'cgb-scripts' ],
27+
cpu: true,
28+
duplicates: true,
29+
browsers: true,
30+
noNativeIDE: true,
31+
} );
32+
33+
console.log(
34+
'\n✅ ' +
35+
chalk.black.bgGreen( ' Done ' ) +
36+
chalk.dim( ' You can copy paste this info to share it...\n' )
37+
);
38+
// Let's end the process so the app doesn't continue.
39+
process.exit( 0 );
40+
} // End Envinfo.
41+
};

packages/create-guten-block/app/getBlockDir.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
'use strict';
99

10+
const path = require( 'path' );
11+
1012
module.exports = blockName => {
11-
return `${ process.cwd() }/${ blockName }`;
13+
return path.join( process.cwd(), blockName );
1214
};

packages/create-guten-block/app/getBlockName.js

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Handle if there's no block name.
3+
*/
4+
5+
'use strict';
6+
7+
const chalk = require( 'chalk' );
8+
9+
module.exports = () => {
10+
// Stop if there's no block name to create the plugin dir.
11+
console.log(
12+
'\n❌ ',
13+
chalk.black.bgRed( ' You forgot to specify a block name: \n' )
14+
);
15+
console.log(
16+
` ${ chalk.dim( 'create-guten-block' ) } ${ chalk.green( '<block-name>' ) }`
17+
);
18+
console.log( chalk.dim( '\nFor example: \n' ) );
19+
console.log(
20+
` ${ chalk.dim( 'create-guten-block' ) } ${ chalk.green( 'my-block' ) }\n`
21+
);
22+
process.exit( 1 );
23+
};

packages/create-guten-block/app/run.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@ const prePrint = require( './prePrint' );
1313
const initBlock = require( './initBlock' );
1414
const getBlockDir = require( './getBlockDir' );
1515
const clearConsole = require( './consoleClear' );
16-
const getBlockName = require( './getBlockName' );
1716
const updateNotifier = require( './updateNotifier' );
1817
const createPluginDir = require( './createPluginDir' );
1918
const npmInstallScripts = require( './npmInstallScripts' );
2019

2120
module.exports = async() => {
22-
// 0. Set the CLI.
23-
cli();
24-
25-
// 0. Clear the console.
2621
clearConsole();
2722

2823
// 0. Update notifier.
2924
updateNotifier();
3025

31-
// 1. Get the block name and direcotry by sending in the third argument.
32-
const blockName = await getBlockName( process.argv[ 2 ] );
26+
// 1. Set the CLI and get the blockName.
27+
const blockName = cli();
28+
29+
// 2. Build the block direcotry path.
3330
const blockDir = await getBlockDir( blockName );
3431

3532
// 2. Pre print.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const clearConsole = require( './consoleClear' );
2+
const path = require( 'path' );
3+
4+
clearConsole();
5+
6+
const blockName = 'my-block';
7+
console.log( `${ process.cwd() }/${ blockName }` );
8+
9+
console.log( path.join( process.cwd(), blockName ) );

0 commit comments

Comments
 (0)