Skip to content

Commit

Permalink
fix!: writeFile now requires filename (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
Barbapapazes and antfu authored Sep 15, 2023
1 parent a439ffa commit d7f7d19
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ export async function loadFile<Exports extends object = any>(
}

export async function writeFile(
node: { ast: ASTNode } | ASTNode,
filename?: string,
node: { $ast: ASTNode } | ASTNode,
filename: string,
options?: ParseOptions,
): Promise<void> {
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);
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface Token {
export interface ParsedFileNode {
type: "file";
program: Program;
name?: string;
loc: Loc;
comments: null | any;
}
Expand Down
50 changes: 50 additions & 0 deletions test/code.test.ts
Original file line number Diff line number Diff line change
@@ -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\\"],
};"`);
});
});
1 change: 1 addition & 0 deletions test/stubs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config2.ts
3 changes: 3 additions & 0 deletions test/stubs/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
foo: ["a"],
};

0 comments on commit d7f7d19

Please sign in to comment.