Skip to content

Commit

Permalink
fix: add EOL at the end of updated lock file
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Mar 21, 2024
1 parent c9981e5 commit 7be5b78
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
8 changes: 6 additions & 2 deletions lib/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ async function writeToImportMap(
async function writeToLockfile(
update: FileUpdate<"lockfile">,
) {
const original = await parseLockFileJson(update.path);
const content = await Deno.readTextFile(update.path);
const original = parseLockFileJson(content);

for await (const dependency of update.dependencies) {
const specifier = dependency.code.specifier;
Expand Down Expand Up @@ -192,7 +193,10 @@ async function writeToLockfile(
);
}
}
await Deno.writeTextFile(update.path, JSON.stringify(original, replacer, 2));
await Deno.writeTextFile(
update.path,
JSON.stringify(original, replacer, 2) + (detectEOL(content) ?? EOL),
);
}

function replacer(
Expand Down
19 changes: 8 additions & 11 deletions lib/lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,13 @@ export interface LockPart {
* @param specifier - The URL or path to the lockfile.
* @returns The parsed JSON object of the lockfile.
*/
export async function parseLockFileJson(
specifier: URL | string,
): Promise<LockFileJson> {
export function parseLockFileJson(
content: string,
): LockFileJson {
try {
return ensure(
JSON.parse(await Deno.readTextFile(specifier)),
isLockFileJson,
);
return ensure(JSON.parse(content), isLockFileJson);
} catch (cause) {
throw new Error(`Failed to parse lockfile: ${specifier}`, { cause });
throw new Error(`Failed to parse lockfile`, { cause });
}
}

Expand All @@ -87,12 +84,12 @@ export async function parseLockFileJson(
* @param specifier - The URL or path to the lockfile.
* @returns The parsed `LockFile` object.
*/
export async function parseLockFile(
export async function readLockFile(
specifier: URL | string,
): Promise<LockFile> {
return {
path: toPath(specifier),
data: await parseLockFileJson(specifier),
data: parseLockFileJson(await Deno.readTextFile(specifier)),
};
}

Expand Down Expand Up @@ -149,7 +146,7 @@ export async function createLockPart(
}
return {
specifier: dependency,
data: await parseLockFileJson(lock.path),
data: parseLockFileJson(await Deno.readTextFile(lock.path)),
};
}

Expand Down
16 changes: 9 additions & 7 deletions lib/lockfile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import {
collectUpdateFromLockFile,
createLockPart,
createLockPartForEach,
parseLockFile,
parseLockFileJson,
readLockFile,
} from "./lockfile.ts";
import { assertEquals, assertObjectMatch } from "./std/assert.ts";

Deno.test("parseLockFileJson", async () =>
assertObjectMatch(
await parseLockFileJson(
new URL("../test/data/lockfile/deno.updated.lock", import.meta.url),
parseLockFileJson(
await Deno.readTextFile(
new URL("../test/data/lockfile/deno.updated.lock", import.meta.url),
),
),
{
version: "3",
Expand Down Expand Up @@ -55,7 +57,7 @@ Deno.test("createLockPart - npm:hono", async () => {

Deno.test("createLockPartForEach", async () => {
const updated = await createLockPartForEach(
await parseLockFile(
await readLockFile(
new URL("../test/data/lockfile/deno.lock", import.meta.url),
),
);
Expand Down Expand Up @@ -95,7 +97,7 @@ Deno.test("createLockPartForEach", async () => {

Deno.test("createLockPartForEach - no updates", async () => {
const updated = await createLockPartForEach(
await parseLockFile(
await readLockFile(
new URL("../test/data/lockfile/deno.lock", import.meta.url),
),
false,
Expand Down Expand Up @@ -143,7 +145,7 @@ Deno.test("createLockPartForEach - no updates", async () => {

Deno.test("collectUpdateFromLockFile", async () => {
const updates = await collectUpdateFromLockFile(
await parseLockFile(
await readLockFile(
new URL("../test/data/lockfile/deno.lock", import.meta.url),
),
);
Expand Down Expand Up @@ -190,7 +192,7 @@ Deno.test("collectUpdateFromLockFile", async () => {

Deno.test("collectUpdateFromLockFile - with a patch", async () => {
const updates = await collectUpdateFromLockFile(
await parseLockFile(
await readLockFile(
new URL("../test/data/lockfile/deno.lock", import.meta.url),
),
await createLockPart("npm:hono@^3"),
Expand Down
4 changes: 2 additions & 2 deletions lib/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
createLockPart,
LockFile,
LockPart,
parseLockFile,
readLockFile,
} from "./lockfile.ts";

export type SourceType = "import_map" | "module" | "lockfile";
Expand Down Expand Up @@ -186,7 +186,7 @@ export async function collect(
? await maybeFile(new URL("deno.lock", toUrl(importMapPath)))
: undefined))
: undefined;
const lockFile = lockFilePath ? await parseLockFile(lockFilePath) : undefined;
const lockFile = lockFilePath ? await readLockFile(lockFilePath) : undefined;

const urls = [from].flat().map((path) => toUrl(path));
const [jsons, esms] = partition(urls, isJsonPath);
Expand Down
9 changes: 6 additions & 3 deletions test/snapshots/file_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ snapshot[`write - lockfile_not_importable/mod.ts 2`] = `
"remote": {
"https://deno.land/std@0.218.0/version.ts": "cfb73e2f2b628978b2b70585b941c5ef5ccbf1dc06e76967206115c74ecfce49"
}
}',
}
',
]
`;

Expand Down Expand Up @@ -1017,7 +1018,8 @@ snapshot[`write - lockfile/deno.json 2`] = `
"npm:hono@^3"
]
}
}',
}
',
]
`;

Expand Down Expand Up @@ -1170,7 +1172,8 @@ snapshot[`write - lockfile/mod.ts 2`] = `
"npm:hono@^3"
]
}
}',
}
',
]
`;

Expand Down

0 comments on commit 7be5b78

Please sign in to comment.