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

feat: auto detect and preserve format #175

Merged
merged 2 commits into from Apr 17, 2024
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
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
test/fixture/tsconfig.json
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"test:types": "tsc --noEmit --module esnext --skipLibCheck --moduleResolution node ./test/*.test.ts"
},
"dependencies": {
"confbox": "^0.1.6",
"confbox": "^0.1.7",
"mlly": "^1.6.1",
"pathe": "^1.1.2"
},
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/index.ts
Expand Up @@ -3,7 +3,7 @@ import { dirname, resolve, isAbsolute } from "pathe";
import { ResolveOptions as _ResolveOptions, resolvePath } from "mlly";
import { findFile, FindFileOptions, findNearestFile } from "./utils";
import type { PackageJson, TSConfig } from "./types";
import { parseJSONC } from "confbox";
import { parseJSONC, parseJSON, stringifyJSON, stringifyJSONC } from "confbox";

export * from "./types";
export * from "./utils";
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function readPackageJSON(
return cache.get(resolvedPath)!;
}
const blob = await fsp.readFile(resolvedPath, "utf8");
const parsed = JSON.parse(blob) as PackageJson;
const parsed = parseJSON(blob) as PackageJson;
cache.set(resolvedPath, parsed);
return parsed;
}
Expand All @@ -77,7 +77,7 @@ export async function writePackageJSON(
path: string,
package_: PackageJson,
): Promise<void> {
await fsp.writeFile(path, JSON.stringify(package_, undefined, 2));
await fsp.writeFile(path, stringifyJSON(package_));
}

/**
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function writeTSConfig(
path: string,
tsconfig: TSConfig,
): Promise<void> {
await fsp.writeFile(path, JSON.stringify(tsconfig, undefined, 2));
await fsp.writeFile(path, stringifyJSONC(tsconfig));
}

/**
Expand Down
11 changes: 7 additions & 4 deletions test/fixture/tsconfig.json
@@ -1,14 +1,17 @@
{
// Comment
"compilerOptions": {
"compilerOptions": { // Comment
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true,
"outDir": "dist",
"strict": true,
"declaration": true,
"types": ["node"]
"types": [
"node"
]
},
"include": ["src"]
"include": [
"src"
]
}
27 changes: 27 additions & 0 deletions test/index.test.ts
Expand Up @@ -14,6 +14,7 @@ import {
resolveLockfile,
findWorkspaceDir,
} from "../src";
import { readFile } from "node:fs/promises";

const fixtureDir = resolve(dirname(fileURLToPath(import.meta.url)), "fixture");

Expand Down Expand Up @@ -90,6 +91,19 @@ describe("package.json", () => {
"string",
);
});

it("styles are preserved", async () => {
const originalContent = await readFile(rFixture("package.json"), "utf8");
await writePackageJSON(
rFixture("package.json") + ".tmp",
await readPackageJSON(rFixture("package.json")),
);
const newContent = await readFile(
rFixture("package.json") + ".tmp",
"utf8",
);
expect(newContent).toBe(originalContent);
});
});

describe("tsconfig.json", () => {
Expand All @@ -112,6 +126,19 @@ describe("tsconfig.json", () => {
// TODO: type check this file.
// expectTypeOf(options.maxNodeModuleJsDepth).toEqualTypeOf<number | undefined>()
});

it("styles are preserved", async () => {
const originalContent = await readFile(rFixture("tsconfig.json"), "utf8");
await writeTSConfig(
rFixture("tsconfig.json") + ".tmp",
await readTSConfig(rFixture("tsconfig.json")),
);
const newContent = await readFile(
rFixture("tsconfig.json") + ".tmp",
"utf8",
);
expect(newContent).toBe(originalContent.replace(/\s*\/\/\s*.+/g, ""));
});
});

describe("resolveLockfile", () => {
Expand Down