forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
119 lines (103 loc) · 3.57 KB
/
utils.js
File metadata and controls
119 lines (103 loc) · 3.57 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
#!/usr/bin/env node
/**
* Gutenberg build utilities.
*
* Shared helpers used by the Gutenberg download script. When run directly,
* verifies that the installed Gutenberg build matches the SHA in package.json,
* and automatically downloads the correct version when needed.
*
* @package WordPress
*/
const { spawnSync } = require( 'child_process' );
const fs = require( 'fs' );
const path = require( 'path' );
// Paths.
const rootDir = path.resolve( __dirname, '../..' );
const gutenbergDir = path.join( rootDir, 'gutenberg' );
const hashFilePath = path.join( gutenbergDir, '.gutenberg-hash' );
/**
* Read Gutenberg configuration from package.json.
*
* @return {{ sha: string, ghcrRepo: string }} The Gutenberg configuration.
* @throws {Error} If the configuration is missing or invalid.
*/
function readGutenbergConfig() {
const packageJson = require( path.join( rootDir, 'package.json' ) );
const sha = packageJson.gutenberg?.sha;
const ghcrRepo = packageJson.gutenberg?.ghcrRepo;
if ( ! sha ) {
throw new Error( 'Missing "gutenberg.sha" in package.json' );
}
if ( ! ghcrRepo ) {
throw new Error( 'Missing "gutenberg.ghcrRepo" in package.json' );
}
return { sha, ghcrRepo };
}
/**
* Trigger a fresh download of the Gutenberg artifact by spawning download.js.
* Exits the process if the download fails.
*/
function downloadGutenberg() {
const result = spawnSync( 'node', [ path.join( __dirname, 'download.js' ) ], { stdio: 'inherit' } );
if ( result.status !== 0 ) {
process.exit( result.status ?? 1 );
}
}
/**
* Verify that the installed Gutenberg version matches the expected SHA in
* package.json. Automatically downloads the correct version when the directory
* is missing, the hash file is absent, or the hash does not match. Logs
* progress to the console and exits with a non-zero code on failure.
*/
function verifyGutenbergVersion() {
console.log( '\n🔍 Verifying Gutenberg version...' );
let sha;
try {
( { sha } = readGutenbergConfig() );
} catch ( error ) {
console.error( '❌ Error reading package.json:', error.message );
process.exit( 1 );
}
// Check for conditions that require a fresh download.
if ( ! fs.existsSync( gutenbergDir ) ) {
console.log( 'ℹ️ Gutenberg directory not found. Downloading...' );
downloadGutenberg();
} else {
let installedHash = null;
try {
installedHash = fs.readFileSync( hashFilePath, 'utf8' ).trim();
} catch ( error ) {
if ( error.code !== 'ENOENT' ) {
console.error( `❌ ${ error.message }` );
process.exit( 1 );
}
}
if ( installedHash === null ) {
console.log( 'ℹ️ Hash file not found. Downloading expected version...' );
downloadGutenberg();
} else if ( installedHash !== sha ) {
console.log( `ℹ️ Hash mismatch (found ${ installedHash }, expected ${ sha }). Downloading expected version...` );
downloadGutenberg();
}
}
// Final verification — confirms the download (if any) produced the correct version.
try {
const installedHash = fs.readFileSync( hashFilePath, 'utf8' ).trim();
if ( installedHash !== sha ) {
console.error( `❌ SHA mismatch after download: expected ${ sha } but found ${ installedHash }.` );
process.exit( 1 );
}
} catch ( error ) {
if ( error.code === 'ENOENT' ) {
console.error( '❌ .gutenberg-hash not found after download. This is unexpected.' );
} else {
console.error( `❌ ${ error.message }` );
}
process.exit( 1 );
}
console.log( '✅ Version verified' );
}
module.exports = { rootDir, gutenbergDir, readGutenbergConfig, verifyGutenbergVersion };
if ( require.main === module ) {
verifyGutenbergVersion();
}