-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathscaffold.js
215 lines (202 loc) · 4.69 KB
/
scaffold.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
/**
* External dependencies
*/
const { pascalCase, snakeCase } = require( 'change-case' );
const { join } = require( 'path' );
/**
* Internal dependencies
*/
const initBlock = require( './init-block' );
const initPackageJSON = require( './init-package-json' );
const initWPScripts = require( './init-wp-scripts' );
const initWPEnv = require( './init-wp-env' );
const { code, info, success, error } = require( './log' );
const { writeOutputAsset, writeOutputTemplate } = require( './output' );
module.exports = async (
{ blockOutputTemplates, pluginOutputTemplates, outputAssets },
{
$schema,
apiVersion,
plugin,
namespace,
slug,
title,
description,
dashicon,
category,
textdomain,
attributes,
supports,
author,
pluginURI,
license,
licenseURI,
domainPath,
updateURI,
version,
wpScripts,
wpEnv,
npmDependencies,
npmDevDependencies,
customScripts,
folderName,
targetDir,
editorScript,
editorStyle,
style,
viewStyle,
render,
viewScriptModule,
viewScript,
variantVars,
customPackageJSON,
customBlockJSON,
example,
transformer,
}
) => {
slug = slug.toLowerCase();
const rootDirectory = join( process.cwd(), targetDir || slug );
const transformedValues = transformer( {
$schema,
apiVersion,
plugin,
namespace: namespace.toLowerCase(),
slug,
title,
description,
dashicon,
category,
attributes,
supports,
author,
pluginURI,
license,
licenseURI,
domainPath,
updateURI,
version,
wpScripts,
wpEnv,
npmDependencies,
npmDevDependencies,
customScripts,
folderName: folderName.replace( /\$slug/g, slug ),
editorScript,
editorStyle,
style,
viewStyle,
render,
viewScriptModule,
viewScript,
variantVars,
customPackageJSON,
customBlockJSON,
example,
textdomain: textdomain || slug,
rootDirectory,
} );
const view = {
...transformedValues,
namespaceSnakeCase: snakeCase( transformedValues.namespace ),
namespacePascalCase: pascalCase( transformedValues.namespace ),
slugSnakeCase: snakeCase( transformedValues.slug ),
slugPascalCase: pascalCase( transformedValues.slug ),
...variantVars,
};
/**
* --no-plugin relies on the used template supporting the [blockTemplatesPath property](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#blocktemplatespath).
* If the blockOutputTemplates object has no properties, we can assume that there was a custom --template passed that
* doesn't support it.
*/
if ( ! plugin && Object.keys( blockOutputTemplates ) < 1 ) {
error(
'No block files found in the template. Please ensure that the template supports the blockTemplatesPath property.'
);
return;
}
const projectType = plugin ? 'plugin' : 'block';
info( '' );
info(
`Creating a new WordPress ${ projectType } in the ${ rootDirectory } directory.`
);
if ( plugin ) {
await Promise.all(
Object.keys( pluginOutputTemplates ).map(
async ( outputFile ) =>
await writeOutputTemplate(
pluginOutputTemplates[ outputFile ],
outputFile,
view
)
)
);
}
await Promise.all(
Object.keys( outputAssets ).map(
async ( outputFile ) =>
await writeOutputAsset(
outputAssets[ outputFile ],
outputFile,
view
)
)
);
await initBlock( blockOutputTemplates, view );
if ( plugin ) {
await initPackageJSON( view );
if ( wpScripts ) {
await initWPScripts( view );
}
if ( wpEnv ) {
await initWPEnv( view );
}
}
info( '' );
success(
`Done: WordPress ${ projectType } ${ title } bootstrapped in the ${ rootDirectory } directory.`
);
if ( plugin && wpScripts ) {
info( '' );
info( 'You can run several commands inside:' );
info( '' );
code( ' $ npm start' );
info( ' Starts the build for development.' );
info( '' );
code( ' $ npm run build' );
info( ' Builds the code for production.' );
info( '' );
code( ' $ npm run format' );
info( ' Formats files.' );
info( '' );
code( ' $ npm run lint:css' );
info( ' Lints CSS files.' );
info( '' );
code( ' $ npm run lint:js' );
info( ' Lints JavaScript files.' );
info( '' );
code( ' $ npm run plugin-zip' );
info( ' Creates a zip file for a WordPress plugin.' );
info( '' );
code( ' $ npm run packages-update' );
info( ' Updates WordPress packages to the latest version.' );
info( '' );
info( 'To enter the directory type:' );
info( '' );
code( ` $ cd ${ slug }` );
}
if ( plugin && wpScripts ) {
info( '' );
info( 'You can start development with:' );
info( '' );
code( ' $ npm start' );
}
if ( plugin && wpEnv ) {
info( '' );
info( 'You can start WordPress with:' );
info( '' );
code( ' $ npx wp-env start' );
}
info( '' );
info( 'Code is Poetry' );
};