forked from Himenon/openapi-typescript-code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
51 lines (43 loc) · 1.52 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import "./clean";
import pkg from "../package.json";
import * as fs from "fs";
import { EOL } from "os";
import { cherryPick } from "./tools/cherry-pick";
import { copyPackageSet } from "./tools/copyPackageSet";
import { generateExportsField } from "./tools/dualPackageSupport";
import { shell } from "./tools/shell";
const generateVersionTsFile = () => {
const codes: string[] = [`export const Name = "${pkg.name}";`, `export const Version = "${pkg.version}";`];
const tscCode = codes.join(EOL);
console.log("Update src/meta.ts file.");
const snapshot = fs.readFileSync("src/meta.ts", "utf-8");
fs.writeFileSync("src/meta.ts", tscCode, "utf-8");
return () => {
fs.writeFileSync("src/meta.ts", snapshot, "utf-8");
};
};
const main = async () => {
const recovery = generateVersionTsFile();
await Promise.all([
shell("pnpm tsc -p tsconfig.esm.json -d --emitDeclarationOnly --outDir ./lib/\\$types"),
shell("pnpm tsc -p tsconfig.cjs.json"),
shell("pnpm tsc -p tsconfig.esm.json"),
]);
await cherryPick({ inputDir: "../src", cwd: "./lib", typesDir: "./$types", cjsDir: "./$cjs", esmDir: "./$esm" });
const exportsFiled = generateExportsField("./src", {
directory: {
import: "./$esm",
// require: "./$cjs", // OFFにするとwebpack 5でesmを読んでくれる
node: "./$cjs",
browser: "./$esm",
default: "./$cjs",
types: "./$types",
},
});
await copyPackageSet(exportsFiled);
recovery();
};
main().catch(error => {
console.error(error);
process.exit(1);
});