-
Notifications
You must be signed in to change notification settings - Fork 22
/
publish-to-npm.ts
164 lines (149 loc) · 4.09 KB
/
publish-to-npm.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { emptyDir, ensureDir } from "https://deno.land/std@0.107.0/fs/mod.ts";
import { walk } from "https://deno.land/std@0.107.0/fs/walk.ts";
import {
dirname,
join,
relative,
} from "https://deno.land/std@0.107.0/path/mod.ts";
import { replaceTsFileExtensionInImportStatement } from "../misc/compat/tsc.ts";
import { zip } from "../misc/archive/zip.ts";
import { getVendorDir } from "../cli/pb/config.ts";
await emptyDir("tmp/npm");
const latestTag = new TextDecoder().decode(
await Deno.run({
cmd: ["git", "describe", "--tags", "--abbrev=0"],
stdout: "piped",
}).output(),
);
const version = latestTag.substr(1).trim();
const packageJson = {
name: "pbkit",
version,
author: "JongChan Choi <jong@chan.moe>",
license: "(MIT OR Apache-2.0)",
repository: {
type: "git",
url: "git+https://github.com/pbkit/pbkit.git",
},
bin: {
"pb-gen-ts": "node/cli/pb-gen-ts.js",
"pb-gen-ts-bundle": "node/cli/pb-gen-ts-bundle.js",
},
preferUnplugged: true,
dependencies: {
"@yarnpkg/fslib": "^2.6.0-rc.8",
"@yarnpkg/libzip": "^2.2.2",
"core-js": "3.18.1",
mri: "^1.2.0",
},
};
await ensureDir("tmp/npm/dist");
await Deno.writeTextFile(
"tmp/npm/dist/package.json",
JSON.stringify(packageJson, null, 2) + "\n",
);
{ // copy core files
const entries = walk("core", { includeDirs: false, exts: [".ts"] });
for await (const { path: fromPath } of entries) {
if (fromPath.endsWith(".test.ts")) continue;
if (/\bdeno\b/.test(fromPath)) continue;
const toPath = join("tmp/npm/ts", fromPath);
await ensureDir(dirname(toPath));
const code = await Deno.readTextFile(fromPath);
await Deno.writeTextFile(
toPath,
replaceTsFileExtensionInImportStatement(code, ""),
);
}
}
{ // tsc
const entries = walk("tmp/npm/ts", { includeDirs: false, exts: [".ts"] });
const tsFiles: string[] = [];
for await (const { path } of entries) tsFiles.push(path);
await Deno.run({
cmd: [
"tsc",
"-m",
"commonjs",
"--target",
"es2019",
"--lib",
"es2019,dom",
"--declaration",
"--rootDir",
"tmp/npm/ts",
"--outDir",
"tmp/npm/dist",
...tsFiles,
],
}).status();
}
{ // bundle codegen logic
await ensureDir("tmp/npm/dist/codegen/ts");
await Deno.run({
cmd: [
"deno",
"bundle",
"--unstable",
"codegen/ts/index.ts",
"tmp/npm/dist/codegen/ts/index.mjs",
],
}).status();
}
{ // copy nodejs-specific files
const entries = walk("node", { includeDirs: false, exts: [".js"] });
for await (const { path: fromPath } of entries) {
const toPath = join("tmp/npm/dist", fromPath);
await ensureDir(dirname(toPath));
const code = await Deno.readTextFile(fromPath);
await Deno.writeTextFile(toPath, code);
}
}
await Deno.writeFile(
"tmp/npm/dist/runtime.zip",
await zip(filesInDir("core/runtime", ".ts", (p) => !p.endsWith(".test.ts"))),
);
console.log("writing 'vendor.zip'. it takes few minutes...");
await Deno.writeFile(
"tmp/npm/dist/vendor.zip",
await zip(filesInDir(getVendorDir(), ".proto")),
);
await Deno.run({
cwd: "tmp/npm/dist",
cmd: ["npm", "publish"],
}).status();
const runtimePackageJson = {
name: "@pbkit/runtime",
version,
author: "JongChan Choi <jong@chan.moe>",
license: "(MIT OR Apache-2.0)",
repository: {
type: "git",
url: "git+https://github.com/pbkit/pbkit.git",
},
};
await ensureDir("tmp/npm/dist-runtime");
await Deno.writeTextFile(
"tmp/npm/dist-runtime/package.json",
JSON.stringify(runtimePackageJson, null, 2) + "\n",
);
await Deno.run({
cwd: "tmp/npm",
cmd: ["cp", "-R", "dist/core/runtime/", "dist-runtime"],
}).status();
await Deno.run({
cwd: "tmp/npm/dist-runtime",
cmd: ["npm", "publish"],
}).status();
async function* filesInDir(
dir: string,
ext: string,
filter?: (path: string) => boolean,
): AsyncGenerator<[string, Uint8Array]> {
const entries = walk(dir, { includeDirs: false, exts: [ext] });
for await (const { path } of entries) {
if (filter && !filter(path)) continue;
const file = await Deno.readFile(path);
yield [relative(dir, path), file];
}
}