Skip to content

Commit ae7f613

Browse files
committed
Build: migrate more uses of fs.promises; use node: protocol
Ref gh-5440
1 parent 822362e commit ae7f613

12 files changed

+65
-56
lines changed

build/release.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var fs = require( "fs" );
3+
const fs = require( "node:fs" );
44

55
module.exports = function( Release ) {
66

build/release/authors.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const fs = require( "node:fs" );
3+
const fs = require( "node:fs/promises" );
44
const util = require( "node:util" );
55
const exec = util.promisify( require( "node:child_process" ).exec );
66
const rnewline = /\r?\n/;
@@ -40,7 +40,7 @@ function cloneSizzle() {
4040
}
4141

4242
async function getLastAuthor() {
43-
const authorsTxt = await fs.promises.readFile( "AUTHORS.txt", "utf8" );
43+
const authorsTxt = await fs.readFile( "AUTHORS.txt", "utf8" );
4444
return authorsTxt.trim().split( rnewline ).pop();
4545
}
4646

@@ -93,7 +93,7 @@ async function updateAuthors() {
9393
const authors = await getAuthors();
9494

9595
const authorsTxt = "Authors ordered by first contribution.\n\n" + authors.join( "\n" ) + "\n";
96-
await fs.promises.writeFile( "AUTHORS.txt", authorsTxt );
96+
await fs.writeFile( "AUTHORS.txt", authorsTxt );
9797

9898
console.log( "AUTHORS.txt updated" );
9999
}

build/release/cdn.js

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
const fs = require( "fs" );
3+
const fs = require( "node:fs" );
44
const shell = require( "shelljs" );
5-
const path = require( "path" );
6-
const os = require( "os" );
5+
const path = require( "node:path" );
6+
const os = require( "node:os" );
77

88
const cdnFolderContainer = "dist/cdn";
99
const cdnFolderVersioned = `${ cdnFolderContainer }/versioned`;
@@ -49,7 +49,7 @@ function makeReleaseCopies( Release ) {
4949
].forEach( ( { filesMap, cdnFolder } ) => {
5050
shell.mkdir( "-p", cdnFolder );
5151

52-
Object.keys( filesMap ).forEach( key => {
52+
Object.keys( filesMap ).forEach( ( key ) => {
5353
let text;
5454
const builtFile = filesMap[ key ];
5555
const unpathedFile = key.replace( /@VER/g, Release.newVersion );
@@ -60,28 +60,33 @@ function makeReleaseCopies( Release ) {
6060
// Map files need to reference the new uncompressed name;
6161
// assume that all files reside in the same directory.
6262
// "file":"jquery.min.js" ... "sources":["jquery.js"]
63-
text = fs.readFileSync( builtFile, "utf8" )
64-
.replace( /"file":"([^"]+)"/,
65-
"\"file\":\"" + unpathedFile.replace( /\.min\.map/, ".min.js\"" ) )
66-
.replace( /"sources":\["([^"]+)"\]/,
67-
"\"sources\":[\"" + unpathedFile.replace( /\.min\.map/, ".js" ) + "\"]" );
63+
text = fs
64+
.readFileSync( builtFile, "utf8" )
65+
.replace(
66+
/"file":"([^"]+)"/,
67+
`"file":"${ unpathedFile.replace( /\.min\.map/, ".min.js" ) }"`
68+
)
69+
.replace(
70+
/"sources":\["([^"]+)"\]/,
71+
`"sources":["${ unpathedFile.replace( /\.min\.map/, ".js" ) }"]`
72+
);
6873
fs.writeFileSync( releaseFile, text );
6974
} else if ( builtFile !== releaseFile ) {
7075
shell.cp( "-f", builtFile, releaseFile );
7176
}
7277
} );
73-
7478
} );
7579
}
7680

7781
async function makeArchives( Release ) {
78-
7982
Release.chdir( Release.dir.repo );
8083

8184
async function makeArchive( { cdn, filesMap, cdnFolder } ) {
8285
return new Promise( ( resolve, reject ) => {
8386
if ( Release.preRelease ) {
84-
console.log( "Skipping archive creation for " + cdn + "; this is a beta release." );
87+
console.log(
88+
`Skipping archive creation for ${ cdn }; this is a beta release.`
89+
);
8590
resolve();
8691
return;
8792
}
@@ -99,41 +104,45 @@ async function makeArchives( Release ) {
99104

100105
output.on( "close", resolve );
101106

102-
output.on( "error", err => {
107+
output.on( "error", ( err ) => {
103108
reject( err );
104109
} );
105110

106111
archiver.pipe( output );
107112

108113
let finalFilesMap = Object.create( null );
109114
for ( const [ releaseFile, builtFile ] of Object.entries( filesMap ) ) {
110-
finalFilesMap[ releaseFile.replace( rver, Release.newVersion ) ] = builtFile;
115+
finalFilesMap[ releaseFile.replace( rver, Release.newVersion ) ] =
116+
builtFile;
111117
}
112118

113-
const files = Object
114-
.keys( filesMap )
115-
.map( item => `${ cdnFolder }/${
116-
item.replace( rver, Release.newVersion )
117-
}` );
119+
const files = Object.keys( filesMap ).map(
120+
( item ) => `${ cdnFolder }/${ item.replace( rver, Release.newVersion ) }`
121+
);
118122

119123
if ( os.platform() === "win32" ) {
120124
sum = [];
121125
for ( i = 0; i < files.length; i++ ) {
122126
result = Release.exec(
123-
"certutil -hashfile " + files[ i ] + " MD5", "Error retrieving md5sum"
127+
"certutil -hashfile " + files[ i ] + " MD5",
128+
"Error retrieving md5sum"
124129
);
125130
sum.push( rmd5.exec( result )[ 0 ] + " " + files[ i ] );
126131
}
127132
sum = sum.join( "\n" );
128133
} else {
129-
sum = Release.exec( "md5 -r " + files.join( " " ), "Error retrieving md5sum" );
134+
sum = Release.exec(
135+
"md5 -r " + files.join( " " ),
136+
"Error retrieving md5sum"
137+
);
130138
}
131139
fs.writeFileSync( md5file, sum );
132140
files.push( md5file );
133141

134-
files.forEach( file => {
135-
archiver.append( fs.createReadStream( file ),
136-
{ name: path.basename( file ) } );
142+
files.forEach( ( file ) => {
143+
archiver.append( fs.createReadStream( file ), {
144+
name: path.basename( file )
145+
} );
137146
} );
138147

139148
archiver.finalize();

build/release/dist.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = function( Release, files, complete ) {
44

5-
const fs = require( "fs" ).promises;
5+
const fs = require( "node:fs/promises" );
66
const shell = require( "shelljs" );
77
const inquirer = require( "inquirer" );
88
const pkg = require( `${ Release.dir.repo }/package.json` );

build/tasks/compare_size.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import chalk from "chalk";
2-
import fs from "node:fs";
2+
import fs from "node:fs/promises";
33
import { promisify } from "node:util";
44
import zlib from "node:zlib";
55
import { exec as nodeExec } from "node:child_process";
@@ -34,7 +34,7 @@ function getBranchHeader( branch, commit ) {
3434
async function getCache( loc ) {
3535
let cache;
3636
try {
37-
const contents = await fs.promises.readFile( loc, "utf8" );
37+
const contents = await fs.readFile( loc, "utf8" );
3838
cache = JSON.parse( contents );
3939
} catch ( err ) {
4040
return {};
@@ -60,7 +60,7 @@ function cacheResults( results ) {
6060
}
6161

6262
function saveCache( loc, cache ) {
63-
return fs.promises.writeFile( loc, JSON.stringify( cache ) );
63+
return fs.writeFile( loc, JSON.stringify( cache ) );
6464
}
6565

6666
function compareSizes( existing, current, padLength ) {
@@ -104,7 +104,7 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
104104
const results = await Promise.all(
105105
files.map( async function( filename ) {
106106

107-
let contents = await fs.promises.readFile( filename, "utf8" );
107+
let contents = await fs.readFile( filename, "utf8" );
108108

109109
// Remove the short SHA and .dirty from comparisons.
110110
// The short SHA so commits can be compared against each other

build/tasks/lib/isCleanWorkingDir.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
const util = require( "util" );
4-
const exec = util.promisify( require( "child_process" ).exec );
3+
const util = require( "node:util" );
4+
const exec = util.promisify( require( "node:child_process" ).exec );
55

66
module.exports = async function isCleanWorkingDir() {
77
const { stdout } = await exec( "git status --untracked-files=no --porcelain" );

build/tasks/minify.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22

33
const swc = require( "@swc/core" );
4-
const fs = require( "fs" );
5-
const path = require( "path" );
4+
const fs = require( "node:fs/promises" );
5+
const path = require( "node:path" );
66
const processForDist = require( "./dist" );
77
const getTimestamp = require( "./lib/getTimestamp" );
88

99
const rjs = /\.js$/;
1010

1111
module.exports = async function minify( { filename, dir, esm } ) {
12-
const contents = await fs.promises.readFile( path.join( dir, filename ), "utf8" );
12+
const contents = await fs.readFile( path.join( dir, filename ), "utf8" );
1313
const version = /jQuery JavaScript Library ([^\n]+)/.exec( contents )[ 1 ];
1414

1515
const { code, map: incompleteMap } = await swc.minify(
@@ -48,11 +48,11 @@ module.exports = async function minify( { filename, dir, esm } ) {
4848
} );
4949

5050
await Promise.all( [
51-
fs.promises.writeFile(
51+
fs.writeFile(
5252
path.join( dir, minFilename ),
5353
code
5454
),
55-
fs.promises.writeFile(
55+
fs.writeFile(
5656
path.join( dir, mapFilename ),
5757
map
5858
)

build/tasks/node_smoke_tests.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22

3-
const fs = require( "fs" );
4-
const util = require( "util" );
5-
const exec = util.promisify( require( "child_process" ).exec );
3+
const fs = require( "node:fs/promises" );
4+
const util = require( "node:util" );
5+
const exec = util.promisify( require( "node:child_process" ).exec );
66
const verifyNodeVersion = require( "./lib/verifyNodeVersion" );
77

88
const allowedLibraryTypes = [ "regular", "factory" ];
@@ -26,7 +26,7 @@ async function runTests( { libraryType, sourceType, module } ) {
2626
} ${ sourceType } "${ module }"` );
2727
}
2828
const dir = `./test/node_smoke_tests/${ sourceType }/${ libraryType }`;
29-
const files = await fs.promises.readdir( dir, { withFileTypes: true } );
29+
const files = await fs.readdir( dir, { withFileTypes: true } );
3030
const testFiles = files.filter( ( testFilePath ) => testFilePath.isFile() );
3131

3232
if ( !testFiles.length ) {

build/tasks/npmcopy.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
const fs = require( "fs" );
4-
const path = require( "path" );
3+
const fs = require( "node:fs/promises" );
4+
const path = require( "node:path" );
55

66
const projectDir = path.resolve( __dirname, "..", ".." );
77

@@ -26,15 +26,15 @@ const files = {
2626
};
2727

2828
async function npmcopy() {
29-
await fs.promises.mkdir( path.resolve( projectDir, "external" ), {
29+
await fs.mkdir( path.resolve( projectDir, "external" ), {
3030
recursive: true
3131
} );
3232
for ( const [ dest, source ] of Object.entries( files ) ) {
3333
const from = path.resolve( projectDir, "node_modules", source );
3434
const to = path.resolve( projectDir, "external", dest );
3535
const toDir = path.dirname( to );
36-
await fs.promises.mkdir( toDir, { recursive: true } );
37-
await fs.promises.copyFile( from, to );
36+
await fs.mkdir( toDir, { recursive: true } );
37+
await fs.copyFile( from, to );
3838
console.log( `${ source }${ dest }` );
3939
}
4040
}

build/tasks/promises_aplus_tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
const { spawn } = require( "child_process" );
3+
const { spawn } = require( "node:child_process" );
44
const verifyNodeVersion = require( "./lib/verifyNodeVersion" );
5-
const path = require( "path" );
6-
const os = require( "os" );
5+
const path = require( "node:path" );
6+
const os = require( "node:os" );
77

88
if ( !verifyNodeVersion() ) {
99
return;

build/tasks/qunit-fixture.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
const fs = require( "fs" );
3+
const fs = require( "node:fs/promises" );
44

55
async function generateFixture() {
6-
const fixture = await fs.promises.readFile( "./test/data/qunit-fixture.html", "utf8" );
7-
await fs.promises.writeFile(
6+
const fixture = await fs.readFile( "./test/data/qunit-fixture.html", "utf8" );
7+
await fs.writeFile(
88
"./test/data/qunit-fixture.js",
99
"// Generated by build/tasks/qunit-fixture.js\n" +
1010
"QUnit.config.fixture = " +

test/runner/createTestServer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bodyParser from "body-parser";
22
import express from "express";
33
import bodyParserErrorHandler from "express-body-parser-error-handler";
4-
import { readFile } from "fs/promises";
4+
import { readFile } from "node:fs/promises";
55
import mockServer from "../middleware-mockserver.cjs";
66

77
export async function createTestServer( report ) {

0 commit comments

Comments
 (0)