forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-gutenberg.js
More file actions
167 lines (142 loc) · 4.53 KB
/
build-gutenberg.js
File metadata and controls
167 lines (142 loc) · 4.53 KB
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
#!/usr/bin/env node
/**
* Build Gutenberg Script
*
* This script builds the Gutenberg repository using its build command
* as specified in the root package.json's "gutenberg" configuration.
*
* @package WordPress
*/
const { spawn } = require( 'child_process' );
const fs = require( 'fs' );
const path = require( 'path' );
// Paths
const rootDir = path.resolve( __dirname, '../..' );
const gutenbergDir = path.join( rootDir, 'gutenberg' );
/**
* Execute a command and return a promise.
* Captures output and only displays it on failure for cleaner logs.
*
* @param {string} command - Command to execute.
* @param {string[]} args - Command arguments.
* @param {Object} options - Spawn options.
* @return {Promise} Promise that resolves when command completes.
*/
function exec( command, args, options = {} ) {
return new Promise( ( resolve, reject ) => {
let stdout = '';
let stderr = '';
const child = spawn( command, args, {
cwd: options.cwd || rootDir,
stdio: [ 'ignore', 'pipe', 'pipe' ],
shell: process.platform === 'win32', // Use shell on Windows to find .cmd files
...options,
} );
// Capture output
if ( child.stdout ) {
child.stdout.on( 'data', ( data ) => {
stdout += data.toString();
} );
}
if ( child.stderr ) {
child.stderr.on( 'data', ( data ) => {
stderr += data.toString();
} );
}
child.on( 'close', ( code ) => {
if ( code !== 0 ) {
// Show output only on failure
if ( stdout ) {
console.error( '\nCommand output:' );
console.error( stdout );
}
if ( stderr ) {
console.error( '\nCommand errors:' );
console.error( stderr );
}
reject(
new Error(
`${ command } ${ args.join(
' '
) } failed with code ${ code }`
)
);
} else {
resolve();
}
} );
child.on( 'error', reject );
} );
}
/**
* Main execution function.
*/
async function main() {
console.log( '🔍 Checking Gutenberg setup...' );
// Verify Gutenberg directory exists
if ( ! fs.existsSync( gutenbergDir ) ) {
console.error( '❌ Gutenberg directory not found at:', gutenbergDir );
console.error( ' Run: node tools/gutenberg/checkout-gutenberg.js' );
process.exit( 1 );
}
// Verify node_modules exists
const nodeModulesPath = path.join( gutenbergDir, 'node_modules' );
if ( ! fs.existsSync( nodeModulesPath ) ) {
console.error( '❌ Gutenberg dependencies not installed' );
console.error( ' Run: node tools/gutenberg/checkout-gutenberg.js' );
process.exit( 1 );
}
console.log( '✅ Gutenberg directory found' );
// Modify Gutenberg's package.json for Core build
console.log( '\n⚙️ Configuring build for WordPress Core...' );
const gutenbergPackageJsonPath = path.join( gutenbergDir, 'package.json' );
try {
const content = fs.readFileSync( gutenbergPackageJsonPath, 'utf8' );
const gutenbergPackageJson = JSON.parse( content );
// Set Core environment variables
gutenbergPackageJson.config = gutenbergPackageJson.config || {};
gutenbergPackageJson.config.IS_GUTENBERG_PLUGIN = false;
gutenbergPackageJson.config.IS_WORDPRESS_CORE = true;
// Set wpPlugin.name for Core naming convention
gutenbergPackageJson.wpPlugin = gutenbergPackageJson.wpPlugin || {};
gutenbergPackageJson.wpPlugin.name = 'wp';
fs.writeFileSync(
gutenbergPackageJsonPath,
JSON.stringify( gutenbergPackageJson, null, '\t' ) + '\n'
);
console.log( ' ✅ IS_GUTENBERG_PLUGIN = false' );
console.log( ' ✅ IS_WORDPRESS_CORE = true' );
console.log( ' ✅ wpPlugin.name = wp' );
} catch ( error ) {
console.error(
'❌ Error modifying Gutenberg package.json:',
error.message
);
process.exit( 1 );
}
// Build Gutenberg
console.log( '\n🔨 Building Gutenberg for WordPress Core...' );
console.log( ' (This may take a few minutes)' );
const startTime = Date.now();
try {
// On Windows, shell mode is used and needs the argument wrapped in quotes
// On Unix, arguments are passed directly without shell parsing
const baseUrlArg =
process.platform === 'win32'
? '--base-url="includes_url( \'build\' )"'
: "--base-url=includes_url( 'build' )";
await exec( 'npm', [ 'run', 'build', '--', '--fast', baseUrlArg ], {
cwd: gutenbergDir,
} );
const duration = Math.round( ( Date.now() - startTime ) / 1000 );
console.log( `✅ Build completed in ${ duration }s` );
} catch ( error ) {
console.error( '❌ Build failed:', error.message );
process.exit( 1 );
}
}
// Run main function
main().catch( ( error ) => {
console.error( '❌ Unexpected error:', error );
process.exit( 1 );
} );