Skip to content

Commit

Permalink
chore(packages): update fs-extra import (#9752)
Browse files Browse the repository at this point in the history
### Description

Update how we're importing CJS (this PR moves fs-extra only) libraries
to prepare to move our packages to ESM
  • Loading branch information
tknickman authored Jan 22, 2025
1 parent a6e1f4c commit 53f3ee3
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 131 deletions.
12 changes: 6 additions & 6 deletions packages/create-turbo/src/transforms/official-starter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, writeJsonSync, rmSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import type { PackageJson } from "@turbo/utils";
import { isDefaultExample } from "../utils/isDefaultExample";
import type { TransformInput, TransformResult, MetaJson } from "./types";
Expand Down Expand Up @@ -30,22 +30,22 @@ export async function transform(args: TransformInput): TransformResult {
// paths
const rootPackageJsonPath = path.join(prompts.root, "package.json");
const rootMetaJsonPath = path.join(prompts.root, "meta.json");
const hasPackageJson = existsSync(rootPackageJsonPath);
const hasPackageJson = fs.existsSync(rootPackageJsonPath);

let metaJson: MetaJson | undefined;

// 1. remove meta file (used for generating the examples page on turbo.build)
try {
metaJson = readJsonSync(rootMetaJsonPath) as MetaJson;
rmSync(rootMetaJsonPath, { force: true });
metaJson = fs.readJsonSync(rootMetaJsonPath) as MetaJson;
fs.rmSync(rootMetaJsonPath, { force: true });
} catch (_err) {
// do nothing
}

if (hasPackageJson) {
let packageJsonContent;
try {
packageJsonContent = readJsonSync(rootPackageJsonPath) as
packageJsonContent = fs.readJsonSync(rootPackageJsonPath) as
| PackageJson
| undefined;
} catch {
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function transform(args: TransformInput): TransformResult {
}

try {
writeJsonSync(rootPackageJsonPath, packageJsonContent, {
fs.writeJsonSync(rootPackageJsonPath, packageJsonContent, {
spaces: 2,
});
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions packages/create-turbo/src/transforms/pnpm-eslint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeJson, mkdir } from "fs-extra";
import fs from "fs-extra";
import type { TransformInput, TransformResult } from "./types";

const meta = {
Expand All @@ -15,9 +15,9 @@ export async function transform(args: TransformInput): TransformResult {

if (packageManager?.name === "pnpm") {
// write the settings directory
await mkdir(`${project.paths.root}/.vscode`, { recursive: true });
await fs.mkdir(`${project.paths.root}/.vscode`, { recursive: true });
// write .vscode settings =- required for eslint plugin for work with pnpm workspaces
await writeJson(
await fs.writeJson(
`${project.paths.root}/.vscode/settings.json`,
VSCODE_ESLINT_CONFIG,
{
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
"error",
{ peerDependencies: true, includeTypes: true },
],
"import/no-named-as-default-member": ["off"],
},
overrides: [
{
Expand Down
6 changes: 3 additions & 3 deletions packages/turbo-benchmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
writeSync,
} from "node:fs";
import path from "node:path";
import { copySync } from "fs-extra";
import fs from "fs-extra";
import { stringify } from "ndjson";
import {
DEFAULT_EXEC_OPTS,
Expand Down Expand Up @@ -99,7 +99,7 @@ function saveCache() {
}
// copy the current cache to the backup
if (existsSync(DEFAULT_CACHE_PATH)) {
copySync(DEFAULT_CACHE_PATH, ALT_CACHE_PATH, { recursive: true });
fs.copySync(DEFAULT_CACHE_PATH, ALT_CACHE_PATH, { recursive: true });
} else {
// make an empty cache
mkdirSync(ALT_CACHE_PATH, { recursive: true });
Expand All @@ -112,7 +112,7 @@ function restoreSavedCache() {
rmSync(DEFAULT_CACHE_PATH, { recursive: true });
}
// Copy the backed-up cache to the real cache
copySync(ALT_CACHE_PATH, DEFAULT_CACHE_PATH, { recursive: true });
fs.copySync(ALT_CACHE_PATH, DEFAULT_CACHE_PATH, { recursive: true });
}

function cachedBuildWithDelta(): Array<Timing> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { gte } from "semver";
import {
getAvailablePackageManagers,
Expand Down Expand Up @@ -94,12 +94,12 @@ function getLocalUpgradeCommand({
function getInstallType({ root }: { root: string }): InstallType | undefined {
// read package.json to make sure we have a reference to turbo
const packageJsonPath = path.join(root, "package.json");
if (!existsSync(packageJsonPath)) {
if (!fs.existsSync(packageJsonPath)) {
logger.error(`Unable to find package.json at ${packageJsonPath}`);
return undefined;
}

const packageJson = readJsonSync(packageJsonPath) as PackageJson;
const packageJson = fs.readJsonSync(packageJsonPath) as PackageJson;
const isDevDependency =
packageJson.devDependencies && "turbo" in packageJson.devDependencies;
const isDependency =
Expand All @@ -126,7 +126,7 @@ export async function getTurboUpgradeCommand({
project: Project;
to?: string;
}) {
const turboBinaryPathFromGlobal = exec(`turbo bin`, {
const turboBinaryPathFromGlobal = exec("turbo bin", {
cwd: project.paths.root,
stdio: "pipe",
});
Expand Down
4 changes: 2 additions & 2 deletions packages/turbo-codemod/src/transforms/add-package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync } from "fs-extra";
import fs from "fs-extra";
import { getWorkspaceDetails, type Project } from "@turbo/workspaces";
import { type PackageJson, getAvailablePackageManagers } from "@turbo/utils";
import { getTransformerHelpers } from "../utils/getTransformerHelpers";
Expand All @@ -22,7 +22,7 @@ export async function transformer({
});

const rootPackageJsonPath = path.join(root, "package.json");
const rootPackageJson = readJsonSync(rootPackageJsonPath) as PackageJson;
const rootPackageJson = fs.readJsonSync(rootPackageJsonPath) as PackageJson;
if ("packageManager" in rootPackageJson) {
log.info(`"packageManager" already set in root "package.json"`);
return runner.finish();
Expand Down
4 changes: 2 additions & 2 deletions packages/turbo-codemod/src/transforms/add-package-names.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import { getWorkspaceDetails, type Project } from "@turbo/workspaces";
import { readJson } from "fs-extra";
import fs from "fs-extra";
import type { Transformer, TransformerArgs } from "../types";
import type { TransformerResults } from "../runner";
import { getTransformerHelpers } from "../utils/getTransformerHelpers";
Expand All @@ -18,7 +18,7 @@ async function readPkgJson(
pkgJsonPath: string
): Promise<PartialPackageJson | null> {
try {
return (await readJson(pkgJsonPath)) as { name?: string };
return (await fs.readJson(pkgJsonPath)) as { name?: string };
} catch (e) {
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/turbo-codemod/src/transforms/create-turbo-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { type PackageJson } from "@turbo/utils";
import type { Schema } from "@turbo/types";
import type { TransformerResults } from "../runner";
Expand All @@ -25,17 +25,17 @@ export function transformer({
log.info(`Migrating "package.json" "turbo" key to "turbo.json" file...`);
const turboConfigPath = path.join(root, "turbo.json");
const rootPackageJsonPath = path.join(root, "package.json");
if (!existsSync(rootPackageJsonPath)) {
if (!fs.existsSync(rootPackageJsonPath)) {
return runner.abortTransform({
reason: `No package.json found at ${root}. Is the path correct?`,
});
}

// read files
const rootPackageJson = readJsonSync(rootPackageJsonPath) as PackageJson;
const rootPackageJson = fs.readJsonSync(rootPackageJsonPath) as PackageJson;
let rootTurboJson = null;
try {
rootTurboJson = readJsonSync(turboConfigPath) as Schema;
rootTurboJson = fs.readJsonSync(turboConfigPath) as Schema;
} catch (err) {
rootTurboJson = null;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/turbo-codemod/src/transforms/migrate-dot-env.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import {
type PackageJson,
getTurboConfigs,
forEachTaskDef,
} from "@turbo/utils";
import { type SchemaV1 } from "@turbo/types";
import type { SchemaV1 } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
import { getTransformerHelpers } from "../utils/getTransformerHelpers";
import type { TransformerResults } from "../runner";
Expand Down Expand Up @@ -58,7 +58,7 @@ export function transformer({
let packageJSON = {};

try {
packageJSON = readJsonSync(packageJsonPath) as PackageJson;
packageJSON = fs.readJsonSync(packageJsonPath) as PackageJson;
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
Expand All @@ -70,9 +70,9 @@ export function transformer({
});
}

log.info(`Moving entries in \`dotEnv\` key in task config to \`inputs\``);
log.info("Moving entries in `dotEnv` key in task config to `inputs`");
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { type PackageJson, getTurboConfigs } from "@turbo/utils";
import type { Pipeline, SchemaV1 } from "@turbo/types";
import { getTransformerHelpers } from "../utils/getTransformerHelpers";
Expand Down Expand Up @@ -125,7 +125,7 @@ export function transformer({
const packageJsonPath = path.join(root, "package.json");
let packageJSON = {};
try {
packageJSON = readJsonSync(packageJsonPath) as PackageJson;
packageJSON = fs.readJsonSync(packageJsonPath) as PackageJson;
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
Expand All @@ -139,7 +139,7 @@ export function transformer({

// validate we have a root config
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
8 changes: 4 additions & 4 deletions packages/turbo-codemod/src/transforms/rename-output-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { type PackageJson, getTurboConfigs } from "@turbo/utils";
import type { PipelineV2, SchemaV1 } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
Expand Down Expand Up @@ -40,7 +40,7 @@ export function transformer({
let packageJSON = {};

try {
packageJSON = readJsonSync(packageJsonPath) as PackageJson;
packageJSON = fs.readJsonSync(packageJsonPath) as PackageJson;
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
Expand All @@ -52,9 +52,9 @@ export function transformer({
});
}

log.info(`Renaming \`outputMode\` key in task config to \`outputLogs\``);
log.info("Renaming `outputMode` key in task config to `outputLogs`");
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/turbo-codemod/src/transforms/rename-pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { existsSync } from "fs-extra";
import fs from "fs-extra";
import { getTurboConfigs } from "@turbo/utils";
import type { SchemaV2, SchemaV1 } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
Expand Down Expand Up @@ -28,9 +28,9 @@ export function transformer({
options,
});

log.info(`Renaming \`pipeline\` key in turbo.json to \`tasks\``);
log.info("Renaming `pipeline` key in turbo.json to `tasks`");
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/turbo-codemod/src/transforms/set-default-outputs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { type PackageJson, getTurboConfigs } from "@turbo/utils";
import type { SchemaV1 } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
Expand Down Expand Up @@ -49,7 +49,7 @@ export function transformer({
let packageJSON = {};

try {
packageJSON = readJsonSync(packageJsonPath) as PackageJson;
packageJSON = fs.readJsonSync(packageJsonPath) as PackageJson;
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
Expand All @@ -63,7 +63,7 @@ export function transformer({

log.info(`Adding default \`outputs\` key into tasks if it doesn't exist`);
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/turbo-codemod/src/transforms/stabilize-env-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { type PackageJson, getTurboConfigs } from "@turbo/utils";
import type { SchemaV1, RootSchemaV1, Pipeline } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
Expand Down Expand Up @@ -110,7 +110,7 @@ export function transformer({
let packageJSON = {};

try {
packageJSON = readJsonSync(packageJsonPath) as PackageJson;
packageJSON = fs.readJsonSync(packageJsonPath) as PackageJson;
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
Expand All @@ -126,7 +126,7 @@ export function transformer({
"Rewriting `experimentalPassThroughEnv` and `experimentalGlobalPassThroughEnv`"
);
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/turbo-codemod/src/transforms/stabilize-ui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { existsSync } from "fs-extra";
import fs from "fs-extra";
import type { RootSchema } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
import { getTransformerHelpers } from "../utils/getTransformerHelpers";
Expand Down Expand Up @@ -37,7 +37,7 @@ export function transformer({

log.info(`Renaming \`experimentalUI\` key in turbo.json to \`ui\``);
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { readJsonSync, existsSync } from "fs-extra";
import fs from "fs-extra";
import { type PackageJson, getTurboConfigs } from "@turbo/utils";
import type { RootSchemaV1, SchemaV1, EnvWildcard } from "@turbo/types";
import type { Transformer, TransformerArgs } from "../types";
Expand Down Expand Up @@ -71,7 +71,7 @@ export function transformer({
let packageJSON = {};

try {
packageJSON = readJsonSync(packageJsonPath) as PackageJson;
packageJSON = fs.readJsonSync(packageJsonPath) as PackageJson;
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
Expand All @@ -85,7 +85,7 @@ export function transformer({

log.info("Rewriting env vars to support wildcards");
const turboConfigPath = path.join(root, "turbo.json");
if (!existsSync(turboConfigPath)) {
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/turbo-codemod/src/utils/loadTurboJson.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from "fs-extra";
import fs from "fs-extra";
import { parse as JSON5Parse } from "json5";

export function loadTurboJson<T>(filePath: string): T {
const contents = readFileSync(filePath, "utf8");
const contents = fs.readFileSync(filePath, "utf8");
return JSON5Parse(contents);
}
Loading

0 comments on commit 53f3ee3

Please sign in to comment.