forked from WordPress/gutenberg
/
index.js
180 lines (173 loc) · 4.69 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* External dependencies
*/
const inquirer = require( 'inquirer' );
const program = require( 'commander' );
const { pickBy, startCase } = require( 'lodash' );
/**
* Internal dependencies
*/
const checkSystemRequirements = require( './check-system-requirements' );
const CLIError = require( './cli-error' );
const log = require( './log' );
const { engines, version } = require( '../package.json' );
const scaffold = require( './scaffold' );
const {
getPluginTemplate,
getDefaultValues,
getPrompts,
} = require( './templates' );
const commandName = `wp-create-block`;
program
.name( commandName )
.description(
'Generates PHP, JS and CSS code for registering a WordPress plugin with blocks.\n\n' +
'[slug] is optional. When provided, it triggers the quick mode where ' +
'it is used as the block slug used for its identification, the output ' +
'location for scaffolded files, and the name of the WordPress plugin.' +
'The rest of the configuration is set to all default values unless ' +
'overridden with some options listed below.'
)
.version( version )
.arguments( '[slug]' )
.option(
'-t, --template <name>',
'project template type name; allowed values: "static", "es5", the name of an external npm package, or the path to a local directory',
'static'
)
.option( '--namespace <value>', 'internal namespace for the block name' )
.option(
'--title <value>',
'display title for the block and the WordPress plugin'
)
// The name "description" is used internally so it couldn't be used.
.option(
'--short-description <value>',
'short description for the block and the WordPress plugin'
)
.option( '--category <name>', 'category name for the block' )
.option(
'--wp-scripts',
'enable integration with `@wordpress/scripts` package'
)
.option(
'--no-wp-scripts',
'disable integration with `@wordpress/scripts` package'
)
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
.option( '--no-plugin', 'scaffold only block files' )
.action(
async (
slug,
{
plugin,
category,
namespace,
shortDescription: description,
template: templateName,
title,
wpScripts,
wpEnv,
}
) => {
await checkSystemRequirements( engines );
try {
const pluginTemplate = await getPluginTemplate( templateName );
const defaultValues = getDefaultValues( pluginTemplate );
const optionsValues = pickBy(
{
plugin,
category,
description,
namespace,
title,
wpScripts,
wpEnv,
},
( value ) => value !== undefined
);
if ( slug ) {
const answers = {
...defaultValues,
slug,
// Transforms slug to title as a fallback.
title: startCase( slug ),
...optionsValues,
};
await scaffold( pluginTemplate, answers );
} else {
log.info( '' );
log.info(
plugin
? "Let's customize your WordPress plugin with blocks:"
: "Let's add a new block to your existing WordPress plugin:"
);
const filterOptionsProvided = ( { name } ) =>
! Object.keys( optionsValues ).includes( name );
const blockPrompts = getPrompts( pluginTemplate, [
'slug',
'namespace',
'title',
'description',
'dashicon',
'category',
] ).filter( filterOptionsProvided );
const blockAnswers = await inquirer.prompt( blockPrompts );
const pluginAnswers = plugin
? await inquirer
.prompt( {
type: 'confirm',
name: 'configurePlugin',
message:
'Do you want to customize the WordPress plugin?',
default: false,
} )
.then( async ( { configurePlugin } ) => {
if ( ! configurePlugin ) {
return {};
}
const pluginPrompts = getPrompts(
pluginTemplate,
[
'pluginURI',
'version',
'author',
'license',
'licenseURI',
'domainPath',
'updateURI',
]
).filter( filterOptionsProvided );
const result = await inquirer.prompt(
pluginPrompts
);
return result;
} )
: {};
await scaffold( pluginTemplate, {
...defaultValues,
...optionsValues,
...blockAnswers,
...pluginAnswers,
} );
}
} catch ( error ) {
if ( error instanceof CLIError ) {
log.error( error.message );
process.exit( 1 );
} else {
throw error;
}
}
}
)
.on( '--help', () => {
log.info( '' );
log.info( 'Examples:' );
log.info( ` $ ${ commandName }` );
log.info( ` $ ${ commandName } todo-list` );
log.info(
` $ ${ commandName } todo-list --template es5 --title "TODO List"`
);
} )
.parse( process.argv );