Skip to content

Commit d221034

Browse files
author
Ahmad Awais
committed
πŸ‘Œ IMPROVE: Support for Windows
1 parent 132db02 commit d221034

8 files changed

Lines changed: 109 additions & 572 deletions

File tree

β€Žpackages/cgb-scripts/package-lock.jsonβ€Ž

Lines changed: 5 additions & 528 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žpackages/cgb-scripts/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"fs-extra": "^5.0.0",
5151
"gzip-size": "^4.1.0",
5252
"inquirer": "^5.0.0",
53+
"is-windows": "^1.0.1",
5354
"node-sass": "^4.7.2",
5455
"ora": "^1.3.0",
5556
"postcss-loader": "^2.0.10",

β€Žpackages/cgb-scripts/scripts/build.jsβ€Ž

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ process.on( 'unhandledRejection', err => {
2222

2323
// Modules.
2424
const fs = require( 'fs' );
25-
const ora = require( 'ora' );
2625
const path = require( 'path' );
2726
const chalk = require( 'chalk' );
2827
const webpack = require( 'webpack' );
2928
const fileSize = require( 'filesize' );
3029
const gzipSize = require( 'gzip-size' );
3130
const resolvePkg = require( 'resolve-pkg' );
31+
const isWindows = require( 'is-windows' );
32+
const ora = isWindows() ? false : require( 'ora' );
3233
const config = require( '../config/webpack.config.prod' );
3334
const cgbDevUtilsPath = resolvePkg( 'cgb-dev-utils', { cwd: __dirname } );
3435
const clearConsole = require( cgbDevUtilsPath + '/clearConsole' );
@@ -56,10 +57,7 @@ const getFileSize = filePath => {
5657
clearConsole();
5758

5859
// Init the spinner.
59-
const spinner = new ora( {
60-
text: '',
61-
enabled: true,
62-
} );
60+
const spinner = ora ? new ora( { text: '', enabled: true } ) : false;
6361

6462
/**
6563
* Build function
@@ -70,12 +68,20 @@ const spinner = new ora( {
7068
*/
7169
async function build( webpackConfig ) {
7270
// Start the build.
73-
spinner.start( `${ chalk.dim( 'Building and compiling blocks...' ) }` );
71+
if ( spinner ) {
72+
spinner.start( `${ chalk.dim( 'Building and compiling blocks...' ) }` );
73+
} else {
74+
console.log( chalk.green( 'Building and compiling blocks...' ) );
75+
}
7476

7577
// Compiler Instance.
7678
const compiler = await webpack( webpackConfig );
77-
spinner.succeed();
7879

80+
if ( spinner ) {
81+
spinner.succeed();
82+
}
83+
84+
// Run the compiler.
7985
compiler.run( ( err, stats ) => {
8086
if ( err ) {
8187
return console.log( err );

β€Žpackages/cgb-scripts/scripts/init.jsβ€Ž

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
'use strict';
1010

11-
const ora = require( 'ora' );
11+
const fs = require( 'fs-extra' );
1212
const path = require( 'path' );
1313
const chalk = require( 'chalk' );
14-
const execa = require( 'execa' );
1514
const shell = require( 'shelljs' );
15+
const isWindows = require( 'is-windows' );
16+
const ora = isWindows() ? false : require( 'ora' );
1617

1718
// Makes the script crash on unhandled rejections instead of silently
1819
// ignoring them. In the future, promise rejections that are not handled will
@@ -28,7 +29,6 @@ process.on( 'unhandledRejection', err => {
2829
* @param {string} blockDir The block directory.
2930
* @param {string} blockNamePHPLower The block name for PHP files in lowercase.
3031
* @param {string} blockNamePHPUpper The block name for PHP files in uppercase.
31-
* @return {promise} promise resolved.
3232
*/
3333
const copyTemplateFiles = (
3434
blockName,
@@ -44,15 +44,29 @@ const copyTemplateFiles = (
4444
'template'
4545
);
4646

47-
return new Promise( resolve => {
47+
const templatePromise = new Promise( resolve => {
4848
shell.cd( blockDir );
49-
// Create a tmp folder to avoid globbing through node_modules.
50-
shell.exec( 'mkdir -p ./tmp' );
51-
shell.cp( '-RL', `${ template }/*`, './tmp' );
52-
shell.cp( '-RL', `${ template }/.*`, './tmp' );
49+
50+
// Copy the files into the plugin blockDir.
51+
if ( fs.existsSync( template ) ) {
52+
fs.copySync( template, blockDir );
53+
} else {
54+
console.error(
55+
`Could not locate supplied template: ${ chalk.green( template ) }`
56+
);
57+
return;
58+
}
59+
60+
// Build a list of files we added.
61+
const files = [
62+
...shell.ls( '**.*' ),
63+
...shell.ls( blockDir + '/src/**.*' ),
64+
...shell.ls( blockDir + '/src/block/**.*' ),
65+
];
66+
console.log( '\n\nLIST OF FILES', files, '\n\n' );
5367

5468
// Replace dynamic content for block name in the code.
55-
shell.ls( 'tmp/**/**.*' ).forEach( function( file ) {
69+
files.forEach( function( file ) {
5670
shell.sed( '-i', '<% blockName %>', `${ blockName }`, file );
5771
shell.sed( '-i', '<% blockName % >', `${ blockName }`, file );
5872
shell.sed( '-i', '<% blockNamePHPLower %>', `${ blockNamePHPLower }`, file );
@@ -61,14 +75,13 @@ const copyTemplateFiles = (
6175
shell.sed( '-i', '<% blockNamePHPUpper % >', `${ blockNamePHPUpper }`, file );
6276
} );
6377

64-
// Move all processed files back to the main folder.
65-
shell.cp( '-RL', `${ blockDir }/tmp/*`, './' );
66-
shell.cp( '-RL', `${ blockDir }/tmp/.*`, './' );
67-
68-
// Thank you, tmp β€” you've been a useful friend.
69-
shell.rm( '-rf', './tmp' );
7078
resolve( true );
7179
} );
80+
81+
templatePromise.catch( err => {
82+
console.log( 'ERROR on templatePromise β†’', err );
83+
process.exit( 1 );
84+
} );
7285
};
7386

7487
/**
@@ -157,16 +170,22 @@ module.exports = async( root, blockName, blockDir ) => {
157170

158171
// 1.Copy template to the plugin dir.
159172
// Init the spinner.
160-
const spinner = new ora( { text: '', enabled: true } );
161-
spinner.start( '3. Creating plugin files...' );
173+
const spinner = ora ? new ora( { text: '', enabled: true } ) : false;
174+
if ( spinner ) {
175+
spinner.start( '3. Creating plugin files...' );
176+
} else {
177+
console.log( chalk.green( '3. Creating plugin files...' ) );
178+
}
162179
await copyTemplateFiles(
163180
blockName,
164181
blockDir,
165182
blockNamePHPLower,
166183
blockNamePHPUpper
167184
);
168-
spinner.succeed();
169185

186+
if ( spinner ) {
187+
spinner.succeed();
188+
}
170189
// 2. Prints next steps.
171190
await printNextSteps( blockName, blockDir );
172191
};

β€Žpackages/cgb-scripts/scripts/start.jsβ€Ž

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ process.env.NODE_ENV = 'development';
1919
process.on( 'unhandledRejection', err => {
2020
throw err;
2121
} );
22-
23-
const ora = require( 'ora' );
2422
const chalk = require( 'chalk' );
2523
const webpack = require( 'webpack' );
24+
const isWindows = require( 'is-windows' );
25+
const ora = isWindows() ? false : require( 'ora' );
2626
const config = require( '../config/webpack.config.dev' );
2727
const resolvePkg = require( 'resolve-pkg' );
2828
const cgbDevUtilsPath = resolvePkg( 'cgb-dev-utils', { cwd: __dirname } );
@@ -35,18 +35,25 @@ const formatWebpackMessages = require( cgbDevUtilsPath +
3535
clearConsole();
3636

3737
// Init the spinner.
38-
const spinner = new ora( {
39-
text: '',
40-
enabled: true,
41-
} );
38+
const spinner = ora ? new ora( { text: '', enabled: true } ) : false;
39+
4240
// Create the production build and print the deployment instructions.
4341
async function build( webpackConfig ) {
44-
spinner.start( `${ chalk.dim( 'Building and compiling blocks...' ) }` );
42+
// Start the build.
43+
if ( spinner ) {
44+
spinner.start( `${ chalk.dim( 'Building and compiling blocks...' ) }` );
45+
} else {
46+
console.log( chalk.green( 'Building and compiling blocks...' ) );
47+
}
4548

4649
// Compiler Instance.
4750
const compiler = await webpack( webpackConfig );
48-
spinner.succeed();
4951

52+
if ( spinner ) {
53+
spinner.succeed();
54+
}
55+
56+
// Run the compiler.
5057
compiler.watch( {}, ( err, stats ) => {
5158
if ( err ) {
5259
return console.log( err );

β€Žpackages/create-guten-block/app/run.jsβ€Ž

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
'use strict';
88

9-
const ora = require( 'ora' );
109
const chalk = require( 'chalk' );
10+
const isWindows = require( 'is-windows' );
11+
const ora = isWindows() ? false : require( 'ora' );
1112
const clearConsole = require( './consoleClear' );
1213
const cli = require( './cli' );
1314
const getBlockName = require( './getBlockName' );
@@ -37,19 +38,39 @@ module.exports = async() => {
3738

3839
// 3. Create the plugin directory.
3940
// Init the spinner.
40-
const spinner = new ora( { text: '', enabled: true } );
41-
spinner.start(
42-
`1. Creating the plugin directory called β†’ ${ chalk.black.bgWhite(
43-
` ${ blockName } `
44-
) }`
45-
);
41+
const spinner = ora ? new ora( { text: '', enabled: true } ) : false;
42+
43+
if ( spinner ) {
44+
spinner.start(
45+
`1. Creating the plugin directory called β†’ ${ chalk.black.bgWhite(
46+
` ${ blockName } `
47+
) }`
48+
);
49+
} else {
50+
console.log(
51+
chalk.green( ' 1. Creating the plugin directory called β†’ ' ) +
52+
chalk.black.bgWhite( ` ${ blockName } ` )
53+
);
54+
}
55+
4656
await createPluginDir( blockName );
47-
spinner.succeed();
57+
58+
if ( spinner ) {
59+
spinner.succeed();
60+
}
4861

4962
// 4. NPM install cgb-scripts.
50-
spinner.start( '2. Installing npm packages...' );
63+
if ( spinner ) {
64+
spinner.start( '2. Installing npm packages...' );
65+
} else {
66+
console.log( chalk.green( '2. Installing npm packages...' ) );
67+
}
68+
5169
await npmInstallScripts( blockName, blockDir );
52-
spinner.succeed();
70+
71+
if ( spinner ) {
72+
spinner.succeed();
73+
}
5374

5475
// 5. Initialize the block.
5576
await initBlock( blockName, blockDir );

β€Žpackages/create-guten-block/package-lock.jsonβ€Ž

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žpackages/create-guten-block/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"directory-exists": "^1.0.2",
3838
"execa": "^0.9.0",
3939
"fs-extra": "^5.0.0",
40+
"is-windows": "^1.0.1",
4041
"ora": "^1.3.0",
4142
"shelljs": "^0.8.0",
4243
"update-notifier": "^2.3.0"

0 commit comments

Comments
Β (0)