forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk-cli.js
executable file
·155 lines (140 loc) · 4.26 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
/** @format */
// SDK bundles are for the browser. Ensure that babel preset reflects this.
process.env.TARGET_BROWSER = 'true';
/**
* External dependencies
*/
const chalk = require( 'chalk' );
const path = require( 'path' );
const yargsModule = require( 'yargs' );
const webpack = require( 'webpack' );
/**
* Internal dependencies
*/
const gutenberg = require( './sdk/gutenberg.js' );
const notifications = require( './sdk/notifications.js' );
const generic = require( './sdk/generic.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 sdk' : calleeScript;
const delimit = scriptName.substring( 0, 3 ) === 'npm' ? '-- ' : '';
const calypsoRoot = path.resolve( __dirname, '..' );
const getBaseConfig = ( options = {} ) => {
const getConfig = require( path.join( calypsoRoot, '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, calypsoRoot } );
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' ) );
}
} );
};
// Generic "watch" option you can add to any command
const watch = {
alias: 'w',
description: 'Whether to watch for changes and automatically rebuild.',
type: 'boolean',
};
yargsModule
.scriptName( scriptName )
.usage( `Usage: $0 <command> ${ delimit }[options]` )
.example( `$0 gutenberg ${ delimit }client/gutenberg/extensions/hello-dolly` )
.command( {
command: 'gutenberg <input-dir>',
desc: 'Build a Gutenberg extension',
builder: yargs =>
yargs.positional('input-dir', {
description: 'Directory containing entry point files editor.js and (optionally) view.js ' +
'(for editor and frontend view modes, respectively)',
type: 'string',
required: true,
coerce: path.resolve,
})
.options( {
'output-dir': {
alias: 'o',
description:
'Output directory for the built assets. Intermediate directories are created as required.',
type: 'string',
coerce: path.resolve,
requiresArg: true,
},
watch,
} ),
handler: argv => build( gutenberg, argv ),
} )
.command( {
command: 'notifications',
desc: 'Build the standalone notifications client',
builder: yargs =>
yargs.options( {
'output-dir': {
alias: 'o',
description:
'Output directory for the built assets. Intermediate directories are created as required.',
type: 'string',
coerce: path.resolve,
required: true,
requiresArg: true,
},
} ),
handler: argv => build( notifications, argv ),
} )
.command( {
command: 'generic <entry-point> <output-name>',
desc: 'Build generic JavaScript code',
builder: yargs => yargs
.positional( 'entry-point', {
description: 'Entry-point for your code',
type: 'string',
required: true,
coerce: path.resolve,
} )
.positional( 'output-name', {
description: 'Output filename',
type: 'string',
required: true,
coerce: path.resolve,
} )
.options( {
'global-wp': {
description: 'Externalize the @wordpress packages as globals',
type: 'boolean',
},
watch,
} ),
handler: argv => build( generic, argv ),
} )
.demandCommand( 1, chalk.red( 'You must provide a valid command!' ) )
.alias( 'help', 'h' )
.version( false ).argv;