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

refactor: use fs.promises in cli/run, Part 1 #4371

Merged
merged 1 commit into from Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions cli/run/getConfigPath.ts
@@ -1,13 +1,13 @@
import { readdirSync } from 'fs';
import { promises as fs } from 'fs';
import { resolve } from 'path';
import { cwd } from 'process';
import { handleError } from '../logging';

const DEFAULT_CONFIG_BASE = 'rollup.config';

export function getConfigPath(commandConfig: string | true): string {
export async function getConfigPath(commandConfig: string | true): Promise<string> {
if (commandConfig === true) {
return resolve(findConfigFileNameInCwd());
return resolve(await findConfigFileNameInCwd());
}
if (commandConfig.slice(0, 5) === 'node:') {
const pkgName = commandConfig.slice(5);
Expand All @@ -30,8 +30,8 @@ export function getConfigPath(commandConfig: string | true): string {
return resolve(commandConfig);
}

function findConfigFileNameInCwd(): string {
const filesInWorkingDir = new Set(readdirSync(cwd()));
async function findConfigFileNameInCwd(): Promise<string> {
const filesInWorkingDir = new Set(await fs.readdir(cwd()));
for (const extension of ['mjs', 'cjs', 'ts']) {
const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`;
if (filesInWorkingDir.has(fileName)) return fileName;
Expand Down
2 changes: 1 addition & 1 deletion cli/run/index.ts
Expand Up @@ -89,7 +89,7 @@ async function getConfigs(
command: any
): Promise<{ options: MergedRollupOptions[]; warnings: BatchWarnings }> {
if (command.config) {
const configFile = getConfigPath(command.config);
const configFile = await getConfigPath(command.config);
const { options, warnings } = await loadAndParseConfigFile(configFile, command);
return { options, warnings };
}
Expand Down
6 changes: 3 additions & 3 deletions cli/run/loadConfigFile.ts
@@ -1,4 +1,4 @@
import { realpathSync } from 'fs';
import { promises as fs } from 'fs';
import { extname, isAbsolute } from 'path';
import { pathToFileURL } from 'url';
import * as rollup from '../../src/node-entry';
Expand Down Expand Up @@ -57,7 +57,7 @@ async function loadConfigFile(
return getConfigList(configFileExport, commandOptions);
}

function getDefaultFromCjs(namespace: GenericConfigObject) {
function getDefaultFromCjs(namespace: GenericConfigObject): unknown {
return namespace.__esModule ? namespace.default : namespace;
}

Expand Down Expand Up @@ -103,7 +103,7 @@ async function getDefaultFromTranspiledConfigFile(
}

async function loadConfigFromBundledFile(fileName: string, bundledCode: string): Promise<unknown> {
const resolvedFileName = realpathSync(fileName);
const resolvedFileName = await fs.realpath(fileName);
const extension = extname(resolvedFileName);
const defaultLoader = require.extensions[extension];
require.extensions[extension] = (module: NodeModule, requiredFileName: string) => {
Expand Down
6 changes: 3 additions & 3 deletions cli/run/watch-cli.ts
@@ -1,4 +1,4 @@
import { type FSWatcher, readFileSync } from 'fs';
import { promises as fs, type FSWatcher } from 'fs';
import process from 'process';
import chokidar from 'chokidar';
import dateTime from 'date-time';
Expand All @@ -23,7 +23,7 @@ export async function watch(command: Record<string, any>): Promise<void> {
let watcher: RollupWatcher;
let configWatcher: FSWatcher;
let resetScreen: (heading: string) => void;
const configFile = command.config ? getConfigPath(command.config) : null;
const configFile = command.config ? await getConfigPath(command.config) : null;

onExit(close);
process.on('uncaughtException', close);
Expand All @@ -41,7 +41,7 @@ export async function watch(command: Record<string, any>): Promise<void> {

async function reloadConfigFile() {
try {
const newConfigFileData = readFileSync(configFile, 'utf-8');
const newConfigFileData = await fs.readFile(configFile, 'utf8');
if (newConfigFileData === configFileData) {
return;
}
Expand Down