/
sdk-cli.js
executable file
·120 lines (108 loc) · 3.4 KB
/
sdk-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env node
/** @format */
/**
* External dependencies
*/
const chalk = require( 'chalk' );
const path = require( 'path' );
const yargs = require( 'yargs' );
const webpack = require( 'webpack' );
/**
* Internal dependencies
*/
const gutenberg = require( './sdk/gutenberg.js' );
// Script name is used in help instructions;
// pick between `npm run calypso-sdk` and `npx calypso-sdk`.
// Show also how npm scripts require delimiter to pass arguments.
const calleeScript = path.basename( process.argv[ 1 ] );
const scriptName =
calleeScript === path.basename( __filename ) ? 'npm run calypso-sdk' : calleeScript;
const delimit = scriptName.substring( 0, 3 ) === 'npm' ? '-- ' : '';
const __rootDir = path.resolve( __dirname, '..' );
const getBaseConfig = ( options = {} ) => {
const getConfig = require( path.join( __rootDir, 'webpack.config.js' ) );
const config = getConfig( options );
// these are currently Calypso-specific
const omitPlugins = [
webpack.HotModuleReplacementPlugin,
];
return {
...config,
optimization: {
splitChunks: false,
},
plugins: config.plugins.filter( plugin => ! omitPlugins.includes( plugin.constructor ) ),
};
};
const build = ( target, argv ) => {
const config = target.config( { argv, getBaseConfig } );
const compiler = webpack( config );
// watch takes an additional argument, adjust accordingly
const runner = f => argv.watch ? compiler.watch( {}, f ) : compiler.run( f );
runner( ( error, stats ) => {
if ( error ) {
console.error( error );
console.log( chalk.red( 'Failed to build' ) );
process.exit( 1 );
}
console.log( stats.toString() );
if ( stats.hasErrors() ) {
console.log( chalk.red( 'Built with errors' ) );
} else if ( stats.hasWarnings() ) {
console.log( chalk.yellow( 'Built with warnings' ) );
} else {
console.log( chalk.green( 'Built successfully' ) );
}
} );
};
yargs
.scriptName( scriptName )
.usage( `Usage: $0 <command> ${ delimit }[options]` )
.example( `$0 gutenberg ${ delimit }--editor-script=hello-dolly.js` )
.command( {
command: 'gutenberg',
desc: 'Build a Gutenberg extension',
builder: yargs =>
yargs.options( {
'editor-script': {
description: 'Entry for editor-side JavaScript file',
type: 'string',
required: true,
coerce: value => path.resolve( __dirname, '../', value ),
requiresArg: true,
},
'view-script': {
description: 'Entry for rendered-page-side JavaScript file',
type: 'string',
coerce: value => path.resolve( __dirname, '../', value ),
requiresArg: true,
},
'output-dir': {
alias: 'o',
description:
'Output directory for the built assets. Intermediate directories are created as required.',
type: 'string',
coerce: path.resolve,
requiresArg: true,
},
'output-editor-file': {
description: 'Name of the built editor script output file (without the file extension).',
type: 'string',
requiresArg: true,
},
'output-view-file': {
description: 'Name of the built view script output file (without the file extension).',
type: 'string',
requiresArg: true,
},
watch: {
alias: 'w',
description: 'Whether to watch for changes and automatically rebuild.',
type: 'boolean',
},
} ),
handler: argv => build( gutenberg, argv ),
} )
.demandCommand( 1, chalk.red( 'You must provide a valid command!' ) )
.alias( 'help', 'h' )
.version( false ).argv;