Skip to content

Commit f9c8d2b

Browse files
committed
Fix replaceInFile import
1 parent d52aac6 commit f9c8d2b

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

bundler/build/build-tools.mjs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import path from 'path';
2-
import fs from 'fs-extra';
2+
import pkg from 'fs-extra';
3+
const { readFileSync, writeFileSync, existsSync, mkdirp, copy, chmodSync } =
4+
pkg;
35
import { camelcase, spinalcase } from 'stringcase';
46

57
// Create an async function to handle the replace operations
68
async function replaceInFile( options ) {
7-
const { default: replace } = await import( 'replace-in-file' );
8-
return replace.sync( options );
9+
const { replaceInFile } = await import( 'replace-in-file' );
10+
return replaceInFile( options );
911
}
1012

1113
function buildIndexPhp( {
@@ -16,7 +18,7 @@ function buildIndexPhp( {
1618
resource,
1719
locale,
1820
} ) {
19-
let contents = fs.readFileSync(
21+
let contents = readFileSync(
2022
path.join( 'bundler', 'template', 'index.php' ),
2123
'utf-8'
2224
);
@@ -36,9 +38,7 @@ function buildIndexPhp( {
3638
"include_once __DIR__ . '/blocks/" + blocks[ index ] + ".php';\n";
3739

3840
if (
39-
fs.existsSync(
40-
path.join( 'blocks', blocks[ index ], 'rest-api.php' )
41-
)
41+
existsSync( path.join( 'blocks', blocks[ index ], 'rest-api.php' ) )
4242
) {
4343
contents += "include_once __DIR__ . '/blocks/rest-api.php';\n";
4444
}
@@ -48,7 +48,7 @@ function buildIndexPhp( {
4848
}
4949

5050
function buildIndexJs( blocks, isLabs ) {
51-
let contents = fs.readFileSync(
51+
let contents = readFileSync(
5252
path.join( 'bundler', 'template', 'index.js' ),
5353
'utf-8'
5454
);
@@ -80,32 +80,33 @@ function buildStyle( blocks, title, fileType ) {
8080
for ( let index = 0; index < blocks.length; index++ ) {
8181
const styleName = `./blocks/${ blocks[ index ] }/${ fileType }.scss`;
8282

83-
if ( fs.existsSync( styleName ) ) {
83+
if ( existsSync( styleName ) ) {
8484
contents += `@import '${ styleName }';\n`;
8585
}
8686
}
8787

8888
return contents;
8989
}
9090

91-
function storeFile( contents, fileName ) {
92-
fs.mkdirp( path.dirname( fileName ) )
93-
.then( function () {
94-
fs.writeFileSync( fileName, contents );
95-
} )
96-
.catch( function () {
97-
console.error( 'Unable to create directory: ' + fileName );
98-
} );
91+
async function storeFile( contents, fileName ) {
92+
try {
93+
await mkdirp( path.dirname( fileName ) );
94+
writeFileSync( fileName, contents );
95+
} catch ( error ) {
96+
console.error(
97+
`storeFile: Unable to create directory: ${ fileName }. Error: ${ error.message }`
98+
);
99+
}
99100
}
100101

101-
function copyExtra( sourceDir, targetDir ) {
102+
async function copyExtra( sourceDir, targetDir ) {
102103
const manifest = path.join( sourceDir, 'index.json' );
103104

104-
if ( fs.existsSync( manifest ) ) {
105-
const json = JSON.parse( fs.readFileSync( manifest, 'utf8' ) );
105+
if ( existsSync( manifest ) ) {
106+
const json = JSON.parse( readFileSync( manifest, 'utf8' ) );
106107

107108
for ( let index = 0; index < json.length; index++ ) {
108-
fs.copySync(
109+
await copy(
109110
path.join( sourceDir, json[ index ] ),
110111
path.join( targetDir, json[ index ] )
111112
);
@@ -124,18 +125,22 @@ async function copyBlocks( { blocks, resource }, targetDir ) {
124125
const manifest = path.join( 'blocks', blocks[ index ] );
125126

126127
try {
127-
await fs.mkdirp( path.dirname( targetFile ) );
128-
fs.copySync( sourceFile, targetFile );
129-
128+
await mkdirp( path.dirname( targetFile ) );
129+
if ( ! existsSync( sourceFile ) ) {
130+
console.error( `Source file does not exist: ${ sourceFile }` );
131+
continue;
132+
}
133+
await copy( sourceFile, targetFile );
130134
await replaceInFile( {
131135
files: targetFile,
132136
from: /block-experiments/g,
133137
to: spinalcase( resource ),
134138
} );
135-
136-
copyExtra( manifest, path.join( targetDir, 'blocks' ) );
139+
await copyExtra( manifest, path.join( targetDir, 'blocks' ) );
137140
} catch ( error ) {
138-
console.error( 'Unable to create directory: ' + targetFile );
141+
console.error(
142+
`copyBlocks: Unable to create directory: ${ targetFile }. Error: ${ error.message }`
143+
);
139144
}
140145
}
141146
}
@@ -161,9 +166,9 @@ function packageBundle( { resource, version } ) {
161166
`rm -rf ${ assets }`,
162167
];
163168

164-
fs.mkdirp( 'build' ).then( function () {
165-
fs.writeFileSync( './build/bundle.sh', lines.join( '\n' ) );
166-
fs.chmodSync( './build/bundle.sh', 0o755 );
169+
mkdirp( 'build' ).then( function () {
170+
writeFileSync( './build/bundle.sh', lines.join( '\n' ) );
171+
chmodSync( './build/bundle.sh', 0o755 );
167172
} );
168173
}
169174

0 commit comments

Comments
 (0)