Skip to content

Commit

Permalink
refactor: use fs.promises in cli/run (#4371)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jan 30, 2022
1 parent 5364114 commit 4611705
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
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

0 comments on commit 4611705

Please sign in to comment.