From b280f3c1739ba7dfb0ff0c0edd88d6d8ea6445de Mon Sep 17 00:00:00 2001 From: hasundue Date: Wed, 6 Mar 2024 12:59:00 +0900 Subject: [PATCH] fix: add EOL at the end of a JSON file --- lib/file.ts | 15 ++++++++++----- lib/import_map.ts | 11 ++++++++++- lib/std/fs.ts | 1 + test/snapshots/file_test.ts.snap | 6 ++++-- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/file.ts b/lib/file.ts index 8f4f5dfc..80e71b9e 100644 --- a/lib/file.ts +++ b/lib/file.ts @@ -1,7 +1,7 @@ -import { detectEOL } from "./std/fs.ts"; +import { detectEOL, EOL, formatEOL } from "./std/fs.ts"; import { toUrl } from "./dependency.ts"; import { type DependencyUpdate } from "./update.ts"; -import { readImportMapJson } from "./import_map.ts"; +import { parseImportMapJson } from "./import_map.ts"; /** * Write the given array of DependencyUpdate to files. @@ -102,7 +102,7 @@ async function writeToModule( ) => [dependency.code.span.start.line, dependency]), ); const content = await Deno.readTextFile(update.path); - const eol = detectEOL(content) ?? "\n"; + const eol = detectEOL(content) ?? EOL; await Deno.writeTextFile( update.path, content @@ -127,9 +127,14 @@ async function writeToImportMap( /** The dependency update to apply. */ update: FileUpdate<"import_map">, ) { - const json = await readImportMapJson(update.path); + const content = await Deno.readTextFile(update.path); + const json = parseImportMapJson(content); for (const dependency of update.dependencies) { json.imports[dependency.map.key] = toUrl(dependency.to); } - await Deno.writeTextFile(update.path, JSON.stringify(json, null, 2)); + const eol = detectEOL(content) ?? EOL; + await Deno.writeTextFile( + update.path, + formatEOL(JSON.stringify(json, null, 2), eol) + eol, + ); } diff --git a/lib/import_map.ts b/lib/import_map.ts index 12aeb060..1a4ca823 100644 --- a/lib/import_map.ts +++ b/lib/import_map.ts @@ -53,12 +53,21 @@ export async function readImportMapJson( ): Promise { const data = await Deno.readTextFile(url); try { - return ensure(parseJsonc(data), isImportMapJson); + return parseImportMapJson(data); } catch { throw new SyntaxError(`${url} does not have a valid import map`); } } +/** + * Parse a JSON including import maps from the given string. + */ +export function parseImportMapJson( + data: string, +): ImportMapJson { + return ensure(parseJsonc(data), isImportMapJson); +} + /** * Read an import map from the given file path or URL. * @param url - The URL of the import map. diff --git a/lib/std/fs.ts b/lib/std/fs.ts index 05227ad0..09000b18 100644 --- a/lib/std/fs.ts +++ b/lib/std/fs.ts @@ -1,6 +1,7 @@ export { existsSync } from "https://deno.land/std@0.218.2/fs/exists.ts"; export { detect as detectEOL, + EOL, format as formatEOL, LF, } from "https://deno.land/std@0.218.2/fs/eol.ts"; diff --git a/test/snapshots/file_test.ts.snap b/test/snapshots/file_test.ts.snap index aece0c17..b8182476 100644 --- a/test/snapshots/file_test.ts.snap +++ b/test/snapshots/file_test.ts.snap @@ -224,7 +224,8 @@ snapshot[`write - import_map_referred/mod.ts 1`] = ` "imports": { "dax": "https://deno.land/x/dax@123.456.789/mod.ts" } -}', +} +', ] `; @@ -418,7 +419,8 @@ snapshot[`write - import_map/mod.ts 1`] = ` "flag": "jsr:@luca/flag@123.456.789", "/": "./" } -}', +} +', ] `;