diff --git a/README.md b/README.md index 8b0d95c..c40cc08 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ const mod = await loadFile("config.js"); mod.exports.default.foo.push("b"); -await writeFile(mod); +await writeFile(mod, "config.js"); ``` Updated `config.js`: @@ -164,7 +164,7 @@ function updateConfig() { We also experiment to provide a few high level helpers to make common tasks easier. You could import them from `magicast/helpers`. They might be moved to a separate package in the future. ```js -import { +import { deepMergeObject, addNuxtModule, addVitePlugin, diff --git a/src/code.ts b/src/code.ts index e030d7a..76ed658 100644 --- a/src/code.ts +++ b/src/code.ts @@ -77,13 +77,12 @@ export async function loadFile( } export async function writeFile( - node: { ast: ASTNode } | ASTNode, - filename?: string, + node: { $ast: ASTNode } | ASTNode, + filename: string, options?: ParseOptions, ): Promise { - const ast = "ast" in node ? node.ast : node; + const ast = "$ast" in node ? node.$ast : node; const { code, map } = generateCode(ast, options); - filename = filename || (ast as any).name || "output.js"; await fsp.writeFile(filename as string, code); if (map) { await fsp.writeFile(filename + ".map", map); diff --git a/src/types.ts b/src/types.ts index ab40c3c..a4e3b31 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,7 +20,6 @@ export interface Token { export interface ParsedFileNode { type: "file"; program: Program; - name?: string; loc: Loc; comments: null | any; } diff --git a/test/code.test.ts b/test/code.test.ts new file mode 100644 index 0000000..a771669 --- /dev/null +++ b/test/code.test.ts @@ -0,0 +1,50 @@ +import { promises as fsp } from "node:fs"; +import { describe, expect, it } from "vitest"; +import { loadFile, parseModule, writeFile } from "../src"; + +describe("code", () => { + it("should load and parse a file", async () => { + const stub = await loadFile("./test/stubs/config.ts"); + + expect(stub).toBeDefined(); + expect(stub.$type).toBe("module"); + expect(stub.$ast).toBeDefined(); + + expect(stub.$code).toMatchInlineSnapshot(` + "export default { + foo: [\\"a\\"], + }; + "`); + + const mod = parseModule( + ` + export default { + foo: ["a"], + }; + `, + { sourceFileName: "./test/stubs/config.ts" }, + ); + + expect(stub.exports).toEqual(mod.exports); + }); + + it("should write file from a module", async () => { + const mod = parseModule( + ` + export default { + foo: ["a"], + }; + `, + { sourceFileName: "./test/stubs/config.ts" }, + ); + + await writeFile(mod, "./test/stubs/config2.ts"); + + const stub = await fsp.readFile("./test/stubs/config2.ts", "utf8"); + + expect(stub).toMatchInlineSnapshot(` + "export default { + foo: [\\"a\\"], + };"`); + }); +}); diff --git a/test/stubs/.gitignore b/test/stubs/.gitignore new file mode 100644 index 0000000..38988bb --- /dev/null +++ b/test/stubs/.gitignore @@ -0,0 +1 @@ +config2.ts diff --git a/test/stubs/config.ts b/test/stubs/config.ts new file mode 100644 index 0000000..f9dde13 --- /dev/null +++ b/test/stubs/config.ts @@ -0,0 +1,3 @@ +export default { + foo: ["a"], +};