Skip to content

Commit

Permalink
replace fs-extra by graceful-fs
Browse files Browse the repository at this point in the history
  • Loading branch information
erikian committed Feb 23, 2025
1 parent 20747f8 commit f4e4911
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 131 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"chalk": "^4.0.0",
"debug": "^4.1.1",
"detect-libc": "^2.0.1",
"fs-extra": "^10.0.0",
"got": "^11.7.0",
"graceful-fs": "^4.2.11",
"node-abi": "^3.45.0",
"node-api-version": "^0.2.0",
"ora": "^5.1.0",
Expand All @@ -66,7 +66,7 @@
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.1",
"@types/graceful-fs": "^4.1.9",
"@types/mocha": "^10.0.10",
"@types/node": "~22.10.7",
"@types/node-abi": "^3.0.0",
Expand Down
30 changes: 15 additions & 15 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'node:crypto';
import debug from 'debug';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';
import zlib from 'node:zlib';

Expand Down Expand Up @@ -31,14 +31,14 @@ type CacheOptions = {

const takeSnapshot = async (dir: string, relativeTo = dir): Promise<Snapshot> => {
const snap: Snapshot = {};
await Promise.all((await fs.readdir(dir)).map(async (child) => {
await Promise.all((await fs.promises.readdir(dir)).map(async (child) => {
if (child === 'node_modules') return;
const childPath = path.resolve(dir, child);
const relative = path.relative(relativeTo, childPath);
if ((await fs.stat(childPath)).isDirectory()) {
if ((await fs.promises.stat(childPath)).isDirectory()) {
snap[relative] = await takeSnapshot(childPath, relativeTo);
} else {
const data = await fs.readFile(childPath);
const data = await fs.promises.readFile(childPath);
snap[relative] = new Snap(
crypto.createHash('SHA256').update(data).digest('hex'),
data,
Expand All @@ -51,10 +51,10 @@ const takeSnapshot = async (dir: string, relativeTo = dir): Promise<Snapshot> =>
const writeSnapshot = async (diff: Snapshot, dir: string): Promise<void> => {
for (const key in diff) {
if (diff[key] instanceof Snap) {
await fs.mkdirp(path.dirname(path.resolve(dir, key)));
await fs.writeFile(path.resolve(dir, key), (diff[key] as Snap).data);
await fs.promises.mkdir(path.dirname(path.resolve(dir, key)), { recursive: true });
await fs.promises.writeFile(path.resolve(dir, key), (diff[key] as Snap).data);
} else {
await fs.mkdirp(path.resolve(dir, key));
await fs.promises.mkdir(path.resolve(dir, key), { recursive: true });
await writeSnapshot(diff[key] as Snapshot, dir);
}
}
Expand Down Expand Up @@ -99,17 +99,17 @@ export const cacheModuleState = async (dir: string, cachePath: string, key: stri
const snap = await takeSnapshot(dir);

const moduleBuffer = Buffer.from(JSON.stringify(serialize(snap)));
const zipped = await new Promise(resolve => zlib.gzip(moduleBuffer, (_, result) => resolve(result)));
await fs.mkdirp(cachePath);
await fs.writeFile(path.resolve(cachePath, key), zipped);
const zipped = await new Promise<Buffer>(resolve => zlib.gzip(moduleBuffer, (_, result) => resolve(result)));
await fs.promises.mkdir(cachePath, { recursive: true });
await fs.promises.writeFile(path.resolve(cachePath, key), zipped);
};

type ApplyDiffFunction = (dir: string) => Promise<void>;

export const lookupModuleState = async (cachePath: string, key: string): Promise<ApplyDiffFunction | boolean> => {
if (await fs.pathExists(path.resolve(cachePath, key))) {
if (fs.existsSync(path.resolve(cachePath, key))) {
return async function applyDiff(dir: string): Promise<void> {
const zipped = await fs.readFile(path.resolve(cachePath, key));
const zipped = await fs.promises.readFile(path.resolve(cachePath, key));
const unzipped: Buffer = await new Promise(resolve => { zlib.gunzip(zipped, (_, result) => resolve(result)); });
const diff = unserialize(JSON.parse(unzipped.toString()));
await writeSnapshot(diff, dir);
Expand All @@ -133,7 +133,7 @@ async function hashDirectory(dir: string, relativeTo?: string): Promise<HashTree
relativeTo ??= dir;
d('hashing dir', dir);
const dirTree: HashTree = {};
await Promise.all((await fs.readdir(dir)).map(async (child) => {
await Promise.all((await fs.promises.readdir(dir)).map(async (child) => {
d('found child', child, 'in dir', dir);
// Ignore output directories
if (dir === relativeTo && (child === 'build' || child === 'bin')) return;
Expand All @@ -143,10 +143,10 @@ async function hashDirectory(dir: string, relativeTo?: string): Promise<HashTree
const childPath = path.resolve(dir, child);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const relative = path.relative(relativeTo!, childPath);
if ((await fs.stat(childPath)).isDirectory()) {
if ((await fs.promises.stat(childPath)).isDirectory()) {
dirTree[relative] = await hashDirectory(childPath, relativeTo);
} else {
dirTree[relative] = crypto.createHash('SHA256').update(await fs.readFile(childPath)).digest('hex');
dirTree[relative] = crypto.createHash('SHA256').update(await fs.promises.readFile(childPath)).digest('hex');
}
}));

Expand Down
16 changes: 8 additions & 8 deletions src/clang-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cp from 'node:child_process';
import debug from 'debug';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';
import tar from 'tar';
import zlib from 'node:zlib';
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function getClangEnvironmentVars(electronVersion: string, targetArc

const gypArgs = [];
if (process.platform === 'win32') {
console.log(fs.readdirSync(clangDir));
console.log(await fs.promises.readdir(clangDir));
gypArgs.push(`/p:CLToolExe=clang-cl.exe`, `/p:CLToolPath=${clangDir}`);
}

Expand Down Expand Up @@ -84,8 +84,8 @@ function clangVersionFromSVN(update: string): string | null {
async function downloadClangVersion(electronVersion: string) {
d('fetching clang for Electron:', electronVersion);
const clangDirPath = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-clang`);
if (await fs.pathExists(path.resolve(clangDirPath, 'bin', 'clang'))) return clangDirPath;
if (!await fs.pathExists(ELECTRON_GYP_DIR)) await fs.mkdirp(ELECTRON_GYP_DIR);
if (fs.existsSync(path.resolve(clangDirPath, 'bin', 'clang'))) return clangDirPath;
await fs.promises.mkdir(ELECTRON_GYP_DIR, { recursive: true });

const electronDeps = await fetch(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/DEPS`, 'text');
const chromiumRevisionExtractor = /'chromium_version':\n\s+'([^']+)/g;
Expand All @@ -107,15 +107,15 @@ async function downloadClangVersion(electronVersion: string) {
d('deflating clang');
zlib.deflateSync(contents);
const tarPath = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-clang.tar`);
if (await fs.pathExists(tarPath)) await fs.remove(tarPath);
await fs.writeFile(tarPath, Buffer.from(contents));
await fs.mkdirp(clangDirPath);
if (fs.existsSync(tarPath)) await fs.promises.rm(tarPath, { recursive: true, force: true });
await fs.promises.writeFile(tarPath, Buffer.from(contents));
await fs.promises.mkdir(clangDirPath, { recursive: true });
d('tar running on clang');
await tar.x({
file: tarPath,
cwd: clangDirPath,
});
await fs.remove(tarPath);
await fs.promises.rm(tarPath, { recursive: true, force: true });
d('cleaning up clang tar file');
return clangDirPath;
}
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import chalk from 'chalk';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';
import ora = require('ora');
import yargs from 'yargs/yargs';
Expand Down Expand Up @@ -83,10 +83,10 @@ process.on('unhandledRejection', handler);
// node modules, which might not always be the case but it's at least a
// good guess
rootDirectory = path.resolve(__dirname, '../../..');
if (!await fs.pathExists(rootDirectory) || !await fs.pathExists(path.resolve(rootDirectory, 'package.json'))) {
if (!fs.existsSync(rootDirectory) || !fs.existsSync(path.resolve(rootDirectory, 'package.json'))) {
// Then we try the CWD
rootDirectory = process.cwd();
if (!await fs.pathExists(rootDirectory) || !await fs.pathExists(path.resolve(rootDirectory, 'package.json'))) {
if (!fs.existsSync(rootDirectory) || !fs.existsSync(path.resolve(rootDirectory, 'package.json'))) {
throw new Error('Unable to find parent node_modules directory, specify it via --module-dir, E.g. "--module-dir ." for the current directory');
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/electron-locator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';
import { searchForModule } from './search-module';

Expand All @@ -8,7 +8,7 @@ async function locateModuleByRequire(): Promise<string | null> {
for (const moduleName of electronModuleNames) {
try {
const modulePath = path.resolve(require.resolve(path.join(moduleName, 'package.json')), '..');
if (await fs.pathExists(path.join(modulePath, 'package.json'))) {
if (fs.existsSync(path.join(modulePath, 'package.json'))) {
return modulePath;
}
} catch { // eslint-disable-line no-empty
Expand All @@ -26,7 +26,7 @@ export async function locateElectronModule(

for (const moduleName of electronModuleNames) {
const electronPaths = await searchForModule(startDir, moduleName, projectRootPath);
const electronPath = electronPaths.find(async (ePath: string) => await fs.pathExists(path.join(ePath, 'package.json')));
const electronPath = electronPaths.find((ePath: string) => fs.existsSync(path.join(ePath, 'package.json')));

if (electronPath) {
return electronPath;
Expand Down
17 changes: 9 additions & 8 deletions src/module-rebuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';

import { cacheModuleState } from './cache';
Expand Down Expand Up @@ -38,8 +38,8 @@ export class ModuleRebuilder {
}

async alreadyBuiltByRebuild(): Promise<boolean> {
if (await fs.pathExists(this.metaPath)) {
const meta = await fs.readFile(this.metaPath, 'utf8');
if (fs.existsSync(this.metaPath)) {
const meta = await fs.promises.readFile(this.metaPath, 'utf8');
return meta === this.metaData;
}

Expand Down Expand Up @@ -120,25 +120,26 @@ export class ModuleRebuilder {
const buildLocation = path.resolve(this.modulePath, 'build', this.rebuilder.buildType);

d('searching for .node file', buildLocation);
const buildLocationFiles = await fs.readdir(buildLocation);
const buildLocationFiles = await fs.promises.readdir(buildLocation);
d('testing files', buildLocationFiles);

const nodeFile = buildLocationFiles.find((file) => file !== '.node' && file.endsWith('.node'));
const nodePath = nodeFile ? path.resolve(buildLocation, nodeFile) : undefined;

if (nodePath && await fs.pathExists(nodePath)) {
if (nodePath && fs.existsSync(nodePath)) {
d('found .node file', nodePath);
if (!this.rebuilder.disablePreGypCopy) {
const abiPath = path.resolve(this.modulePath, `bin/${this.rebuilder.platform}-${this.rebuilder.arch}-${this.rebuilder.ABI}`);
d('copying to prebuilt place:', abiPath);
await fs.mkdir(abiPath, { recursive: true });
await fs.copyFile(nodePath, path.join(abiPath, `${this.nodeGyp.moduleName}.node`));
await fs.promises.mkdir(abiPath, { recursive: true });
await fs.promises.copyFile(nodePath, path.join(abiPath, `${this.nodeGyp.moduleName}.node`));
}
}
}

async writeMetadata(): Promise<void> {
await fs.outputFile(this.metaPath, this.metaData);
await fs.promises.mkdir(path.dirname(this.metaPath), { recursive: true });
await fs.promises.writeFile(this.metaPath, this.metaData);
}

async rebuild(cacheKey: string): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions src/module-type/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';

import { NodeAPI } from '../node-api';
Expand Down Expand Up @@ -80,7 +80,7 @@ export async function locateBinary(basePath: string, suffix: string): Promise<st
while (testPath !== parentPath) {
testPath = parentPath;
const checkPath = path.resolve(testPath, suffix);
if (await fs.pathExists(checkPath)) {
if (fs.existsSync(checkPath)) {
return checkPath;
}
parentPath = path.resolve(testPath, '..');
Expand Down
4 changes: 2 additions & 2 deletions src/module-type/prebuild-install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';
import { spawn } from '@malept/cross-spawn-promise';

Expand Down Expand Up @@ -63,7 +63,7 @@ export class PrebuildInstall extends NativeModule {
* Whether a prebuild-install-based native module exists.
*/
async prebuiltModuleExists(): Promise<boolean> {
return fs.pathExists(path.resolve(this.modulePath, 'prebuilds', `${this.rebuilder.platform}-${this.rebuilder.arch}`, `electron-${this.rebuilder.ABI}.node`));
return fs.existsSync(path.resolve(this.modulePath, 'prebuilds', `${this.rebuilder.platform}-${this.rebuilder.arch}`, `electron-${this.rebuilder.ABI}.node`));
}

async getPrebuildInstallRuntimeArgs(): Promise<string[]> {
Expand Down
8 changes: 4 additions & 4 deletions src/module-type/prebuildify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';

import { ConfigVariables, getNodeArch } from '../arch';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class Prebuildify extends NativeModule {
d(`Checking for prebuilds for "${this.moduleName}"`);

const prebuildsDir = path.join(this.modulePath, 'prebuilds');
if (!(await fs.pathExists(prebuildsDir))) {
if (!(fs.existsSync(prebuildsDir))) {
d(`Could not find the prebuilds directory at "${prebuildsDir}"`);
return false;
}
Expand All @@ -52,10 +52,10 @@ export class Prebuildify extends NativeModule {
const nodejsNapiModuleFilename = path.join(prebuiltModuleDir, `node.napi.${nativeExt}`);
const abiModuleFilename = path.join(prebuiltModuleDir, `electron.abi${this.rebuilder.ABI}.${nativeExt}`);

if (await fs.pathExists(electronNapiModuleFilename) || await fs.pathExists(nodejsNapiModuleFilename)) {
if (fs.existsSync(electronNapiModuleFilename) || fs.existsSync(nodejsNapiModuleFilename)) {
this.nodeAPI.ensureElectronSupport();
d(`Found prebuilt Node-API module in ${prebuiltModuleDir}"`);
} else if (await fs.pathExists(abiModuleFilename)) {
} else if (fs.existsSync(abiModuleFilename)) {
d(`Found prebuilt module: "${abiModuleFilename}"`);
} else {
d(`Could not locate "${electronNapiModuleFilename}", "${nodejsNapiModuleFilename}", or "${abiModuleFilename}"`);
Expand Down
12 changes: 6 additions & 6 deletions src/module-walker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';

import { readPackageJson } from './read-package-json';
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ModuleWalker {
}

async markChildrenAsProdDeps(modulePath: string): Promise<void> {
if (!await fs.pathExists(modulePath)) {
if (!fs.existsSync(modulePath)) {
return;
}

Expand Down Expand Up @@ -113,20 +113,20 @@ export class ModuleWalker {
// Some package managers use symbolic links when installing node modules
// we need to be sure we've never tested the a package before by resolving
// all symlinks in the path and testing against a set
const realNodeModulesPath = await fs.realpath(nodeModulesPath);
const realNodeModulesPath = await fs.promises.realpath(nodeModulesPath);
if (this.realNodeModulesPaths.has(realNodeModulesPath)) {
return;
}
this.realNodeModulesPaths.add(realNodeModulesPath);

d('scanning:', realNodeModulesPath);

for (const modulePath of await fs.readdir(realNodeModulesPath)) {
for (const modulePath of await fs.promises.readdir(realNodeModulesPath)) {
// Ignore the magical .bin directory
if (modulePath === '.bin') continue;
// Ensure that we don't mark modules as needing to be rebuilt more than once
// by ignoring / resolving symlinks
const realPath = await fs.realpath(path.resolve(nodeModulesPath, modulePath));
const realPath = await fs.promises.realpath(path.resolve(nodeModulesPath, modulePath));

if (this.realModulePaths.has(realPath)) {
continue;
Expand All @@ -141,7 +141,7 @@ export class ModuleWalker {
await this.findAllModulesIn(realPath, `${modulePath}/`);
}

if (await fs.pathExists(path.resolve(nodeModulesPath, modulePath, 'node_modules'))) {
if (fs.existsSync(path.resolve(nodeModulesPath, modulePath, 'node_modules'))) {
await this.findAllModulesIn(path.resolve(realPath, 'node_modules'));
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/read-package-json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import fs from 'fs-extra';
import fs from 'graceful-fs';
import path from 'node:path';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function readPackageJson(dir: string, safe = false): Promise<any> {
try {
return await fs.readJson(path.resolve(dir, 'package.json'));
return JSON.parse(await fs.promises.readFile(path.resolve(dir, 'package.json'), {
encoding: 'utf-8',
}));
} catch (err) {
if (safe) {
return {};
Expand Down
4 changes: 2 additions & 2 deletions src/rebuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import debug from 'debug';
import { EventEmitter } from 'node:events';
import fs from 'fs-extra';
import fs from 'graceful-fs';
import nodeAbi from 'node-abi';
import os from 'node:os';
import path from 'node:path';
Expand Down Expand Up @@ -262,7 +262,7 @@ export class Rebuilder implements IRebuilder {
}

async rebuildModuleAt(modulePath: string): Promise<void> {
if (!(await fs.pathExists(path.resolve(modulePath, 'binding.gyp')))) {
if (!(fs.existsSync(path.resolve(modulePath, 'binding.gyp')))) {
return;
}

Expand Down
Loading

0 comments on commit f4e4911

Please sign in to comment.