Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import from node:fs/promises #4777

Merged
merged 3 commits into from Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions browser/src/fs.ts
@@ -1,7 +1,5 @@
import { throwNoFileSystem } from './error';

export const promises = {
mkdir: throwNoFileSystem('fs.mkdir'),
readFile: throwNoFileSystem('fs.readFile'),
writeFile: throwNoFileSystem('fs.writeFile')
};
export const mkdir = throwNoFileSystem('fs.mkdir');
export const readFile = throwNoFileSystem('fs.readFile');
export const writeFile = throwNoFileSystem('fs.writeFile');
8 changes: 4 additions & 4 deletions build-plugins/generate-license-file.ts
@@ -1,4 +1,4 @@
import { promises as fs } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import type { PluginImpl } from 'rollup';
import license, { type Dependency, type Person } from 'rollup-plugin-license';
Expand All @@ -7,7 +7,7 @@ async function generateLicenseFile(
directory: string,
dependencies: readonly Dependency[]
): Promise<void> {
const coreLicense = await fs.readFile('LICENSE-CORE.md', 'utf8');
const coreLicense = await readFile('LICENSE-CORE.md', 'utf8');
const licenses = new Set<string>();
const dependencyLicenseTexts = [...dependencies]
.filter(({ name }) => name !== '@rollup/browser')
Expand Down Expand Up @@ -58,9 +58,9 @@ async function generateLicenseFile(
`# Bundled dependencies:\n` +
dependencyLicenseTexts;
const licenseFile = join(directory, 'LICENSE.md');
const existingLicenseText = await fs.readFile(licenseFile, 'utf8');
const existingLicenseText = await readFile(licenseFile, 'utf8');
if (existingLicenseText !== licenseText) {
await fs.writeFile(licenseFile, licenseText);
await writeFile(licenseFile, licenseText);
console.warn('LICENSE.md updated. You should commit the updated file.');
}
}
Expand Down
4 changes: 2 additions & 2 deletions build-plugins/get-banner.ts
@@ -1,5 +1,5 @@
import { exec } from 'node:child_process';
import { promises as fs } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { env } from 'node:process';
import { promisify } from 'node:util';

Expand Down Expand Up @@ -31,6 +31,6 @@ export default function getBanner(): Promise<string> {
console.error('Could not determine commit hash:', error);
return 'unknown';
}),
fs.readFile(new URL('../package.json', import.meta.url), 'utf8')
readFile(new URL('../package.json', import.meta.url), 'utf8')
]).then(([commit, package_]) => generateBanner(commit, JSON.parse(package_).version)));
}
4 changes: 2 additions & 2 deletions cli/run/getConfigPath.ts
@@ -1,4 +1,4 @@
import { promises as fs } from 'node:fs';
import { readdir } from 'node:fs/promises';
import { resolve } from 'node:path';
import { cwd } from 'node:process';
import { errorMissingExternalConfig } from '../../src/utils/error';
Expand Down Expand Up @@ -31,7 +31,7 @@ export async function getConfigPath(commandConfig: string | true): Promise<strin
}

async function findConfigFileNameInCwd(): Promise<string> {
const filesInWorkingDirectory = new Set(await fs.readdir(cwd()));
const filesInWorkingDirectory = new Set(await readdir(cwd()));
for (const extension of ['mjs', 'cjs', 'ts']) {
const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`;
if (filesInWorkingDirectory.has(fileName)) return fileName;
Expand Down
6 changes: 3 additions & 3 deletions cli/run/loadConfigFile.ts
@@ -1,4 +1,4 @@
import { promises as fs } from 'node:fs';
import { unlink, writeFile } from 'node:fs/promises';
import { dirname, isAbsolute, join } from 'node:path';
import process from 'node:process';
import { pathToFileURL } from 'node:url';
Expand Down Expand Up @@ -132,12 +132,12 @@ async function loadConfigFromWrittenFile(
bundledFileName: string,
bundledCode: string
): Promise<unknown> {
await fs.writeFile(bundledFileName, bundledCode);
await writeFile(bundledFileName, bundledCode);
try {
return (await import(pathToFileURL(bundledFileName).href)).default;
} finally {
// Not awaiting here saves some ms while potentially hiding a non-critical error
fs.unlink(bundledFileName);
unlink(bundledFileName);
}
}

Expand Down
5 changes: 3 additions & 2 deletions cli/run/watch-cli.ts
@@ -1,4 +1,5 @@
import { promises as fs, type FSWatcher } from 'node:fs';
import type { FSWatcher } from 'node:fs';
import { readFile } from 'node:fs/promises';
import process from 'node:process';
import chokidar from 'chokidar';
import dateTime from 'date-time';
Expand Down Expand Up @@ -43,7 +44,7 @@ export async function watch(command: Record<string, any>): Promise<void> {

async function reloadConfigFile() {
try {
const newConfigFileData = await fs.readFile(configFile, 'utf8');
const newConfigFileData = await readFile(configFile, 'utf8');
if (newConfigFileData === configFileData) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/find-config.js
@@ -1,8 +1,8 @@
import { promises as fs } from 'node:fs';
import { readdir } from 'node:fs/promises';
import { resolve } from 'node:path';

export async function findConfigFileName(targetDirectory) {
const filesInWorkingDirectory = new Set(await fs.readdir(targetDirectory));
const filesInWorkingDirectory = new Set(await readdir(targetDirectory));
for (const extension of ['mjs', 'cjs', 'ts', 'js']) {
const fileName = `rollup.config.${extension}`;
if (filesInWorkingDirectory.has(fileName)) return resolve(targetDirectory, fileName);
Expand Down
4 changes: 2 additions & 2 deletions src/ModuleLoader.ts
Expand Up @@ -30,7 +30,7 @@ import {
errorUnresolvedImport,
errorUnresolvedImportTreatedAsExternal
} from './utils/error';
import { promises as fs } from './utils/fs';
import { readFile } from './utils/fs';
import { doAssertionsDiffer, getAssertionsFromImportExpression } from './utils/parseAssertions';
import { isAbsolute, isRelative, resolve } from './utils/path';
import relativeId from './utils/relativeId';
Expand Down Expand Up @@ -259,7 +259,7 @@ export class ModuleLoader {
try {
source = await this.graph.fileOperationQueue.run(
async () =>
(await this.pluginDriver.hookFirst('load', [id])) ?? (await fs.readFile(id, 'utf8'))
(await this.pluginDriver.hookFirst('load', [id])) ?? (await readFile(id, 'utf8'))
);
} catch (error_: any) {
let message = `Could not load ${id}`;
Expand Down
6 changes: 3 additions & 3 deletions src/rollup/rollup.ts
Expand Up @@ -10,7 +10,7 @@ import {
// eslint-disable-next-line unicorn/prevent-abbreviations
errorMissingFileOrDirOption
} from '../utils/error';
import { promises as fs } from '../utils/fs';
import { mkdir, writeFile } from '../utils/fs';
import { catchUnfinishedHookActions } from '../utils/hookActions';
import { normalizeInputOptions } from '../utils/options/normalizeInputOptions';
import { normalizeOutputOptions } from '../utils/options/normalizeOutputOptions';
Expand Down Expand Up @@ -268,9 +268,9 @@ async function writeOutputFile(
const fileName = resolve(outputOptions.dir || dirname(outputOptions.file!), outputFile.fileName);

// 'recursive: true' does not throw if the folder structure, or parts of it, already exist
await fs.mkdir(dirname(fileName), { recursive: true });
await mkdir(dirname(fileName), { recursive: true });

return fs.writeFile(fileName, outputFile.type === 'asset' ? outputFile.source : outputFile.code);
return writeFile(fileName, outputFile.type === 'asset' ? outputFile.source : outputFile.code);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fs.ts
@@ -1 +1 @@
export * from 'node:fs';
export * from 'node:fs/promises';
8 changes: 4 additions & 4 deletions src/utils/resolveId.ts
@@ -1,7 +1,7 @@
import type { ModuleLoaderResolveId } from '../ModuleLoader';
import type { CustomPluginOptions, Plugin, ResolveIdResult } from '../rollup/types';
import type { PluginDriver } from './PluginDriver';
import { promises as fs } from './fs';
import { lstat, readdir, realpath } from './fs';
import { basename, dirname, isAbsolute, resolve } from './path';
import { resolveIdViaPlugins } from './resolveIdViaPlugins';

Expand Down Expand Up @@ -55,13 +55,13 @@ async function addJsExtensionIfNecessary(

async function findFile(file: string, preserveSymlinks: boolean): Promise<string | undefined> {
try {
const stats = await fs.lstat(file);
const stats = await lstat(file);
if (!preserveSymlinks && stats.isSymbolicLink())
return await findFile(await fs.realpath(file), preserveSymlinks);
return await findFile(await realpath(file), preserveSymlinks);
if ((preserveSymlinks && stats.isSymbolicLink()) || stats.isFile()) {
// check case
const name = basename(file);
const files = await fs.readdir(dirname(file));
const files = await readdir(dirname(file));

if (files.includes(name)) return file;
}
Expand Down
12 changes: 6 additions & 6 deletions test/cli/samples/watch/close-stdin/wrapper.js
@@ -1,21 +1,21 @@
#!/usr/bin/env node

const stream = require('stream');
const { mkdirSync, readFileSync, writeFileSync } = require('fs');
const { mkdirSync, readFileSync, writeFileSync } = require('node:fs');
const { resolve } = require('node:path');
const { Readable } = require('node:stream');
const chokidar = require('chokidar');
const path = require('path');

delete process.stdin;
process.stdin = new stream.Readable({
process.stdin = new Readable({
encoding: 'utf8',
read() {
return null;
}
});

const outputDir = path.resolve(__dirname, '_actual');
const outputDir = resolve(__dirname, '_actual');
mkdirSync(outputDir);
const outputFile = path.resolve(outputDir, 'out.js');
const outputFile = resolve(outputDir, 'out.js');
const INITIAL_OUTPUT = 'NOT WRITTEN';
writeFileSync(outputFile, INITIAL_OUTPUT);

Expand Down