-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
220 lines (210 loc) · 5.56 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* External dependencies
*/
const { confirm, select } = require( '@inquirer/prompts' );
const { capitalCase } = require( 'change-case' );
const program = require( 'commander' );
/**
* 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 {
getDefaultValues,
getProjectTemplate,
runPrompts,
} = 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: "standard", "es5", the name of an external npm package, or the path to a local directory',
'standard'
)
.option( '--variant <variant>', 'the variant of the template to use' )
.option( '--no-plugin', 'scaffold only block files' )
.option(
'--target-dir <directory>',
'the directory where the files will be scaffolded, defaults to the slug'
)
.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' )
.action(
async (
slug,
{
plugin,
category,
namespace,
shortDescription: description,
template: templateName,
title,
wpScripts,
wpEnv,
variant,
targetDir,
}
) => {
try {
await checkSystemRequirements( engines );
const projectTemplate =
await getProjectTemplate( templateName );
const availableVariants = Object.keys(
projectTemplate.variants
);
if ( variant && ! availableVariants.includes( variant ) ) {
if ( ! availableVariants.length ) {
throw new CLIError(
`"${ variant }" variant was selected. This template does not have any variants!`
);
}
throw new CLIError(
`"${ variant }" is not a valid variant for this template. Available variants are: ${ availableVariants.join(
', '
) }.`
);
}
const optionsValues = Object.fromEntries(
Object.entries( {
plugin,
category,
description,
namespace,
title,
wpScripts,
wpEnv,
targetDir,
} ).filter( ( [ , value ] ) => value !== undefined )
);
if ( slug ) {
const defaultValues = getDefaultValues(
projectTemplate,
variant
);
const answers = {
...defaultValues,
slug,
// Transforms slug to title as a fallback.
title: capitalCase( slug ),
...optionsValues,
};
await scaffold( projectTemplate, 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:"
);
if ( ! variant && availableVariants.length > 1 ) {
variant = await select( {
message:
'The template variant to use for this block:',
choices: availableVariants.map( ( value ) => ( {
value,
} ) ),
} );
}
const defaultValues = getDefaultValues(
projectTemplate,
variant
);
const blockAnswers = await runPrompts(
projectTemplate,
[
'slug',
'namespace',
'title',
'description',
'dashicon',
'category',
! plugin && 'textdomain',
].filter( Boolean ),
variant,
optionsValues
);
const pluginAnswers =
plugin &&
( await confirm( {
message:
'Do you want to customize the WordPress plugin?',
default: false,
} ) )
? await runPrompts(
projectTemplate,
[
'pluginURI',
'version',
'author',
'license',
'licenseURI',
'domainPath',
'updateURI',
],
variant,
optionsValues
)
: {};
await scaffold( projectTemplate, {
...defaultValues,
...optionsValues,
variant,
...blockAnswers,
...pluginAnswers,
} );
}
} catch ( error ) {
if ( error instanceof CLIError ) {
log.error( error.message );
process.exit( 1 );
} else if ( error.name === 'ExitPromptError' ) {
log.info( 'Cancelled.' );
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 );