Skip to content

Commit

Permalink
fix: add EOL at the end of a JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Mar 6, 2024
1 parent e4cbc03 commit b280f3c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
15 changes: 10 additions & 5 deletions lib/file.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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,
);
}
11 changes: 10 additions & 1 deletion lib/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,21 @@ export async function readImportMapJson(
): Promise<ImportMapJson> {
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.
Expand Down
1 change: 1 addition & 0 deletions lib/std/fs.ts
Original file line number Diff line number Diff line change
@@ -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";
6 changes: 4 additions & 2 deletions test/snapshots/file_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ snapshot[`write - import_map_referred/mod.ts 1`] = `
"imports": {
"dax": "https://deno.land/x/dax@123.456.789/mod.ts"
}
}',
}
',
]
`;

Expand Down Expand Up @@ -418,7 +419,8 @@ snapshot[`write - import_map/mod.ts 1`] = `
"flag": "jsr:@luca/flag@123.456.789",
"/": "./"
}
}',
}
',
]
`;

Expand Down

0 comments on commit b280f3c

Please sign in to comment.