-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_npm.ts
223 lines (205 loc) · 5.34 KB
/
build_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { emptyDir } from "https://deno.land/x/[email protected]/mod.ts";
import { transform } from "https://deno.land/x/[email protected]/transform.ts";
import {
common,
dirname,
join,
} from "https://deno.land/[email protected]/path/mod.ts";
import { copy } from "https://deno.land/[email protected]/fs/mod.ts";
import { bundle } from "https://deno.land/x/[email protected]/mod.ts";
const VERSION = "0.1.1";
console.log(`Building version ${VERSION}`);
console.log("Clearing npm directory...");
await emptyDir("./npm");
console.log("Building...");
const outputResult = await transform({
entryPoints: [
"./mod.ts",
"./client.ts",
"./server.ts",
],
target: "Latest",
shims: [
{
package: {
name: "undici",
version: "^5.3.0",
},
globalNames: [
"fetch",
"File",
"FormData",
"Headers",
"Request",
"Response",
],
},
{
package: {
name: "ws",
version: "^8.7.0",
},
globalNames: [{
name: "WebSocket",
exportName: "WebSocket",
}, {
name: "MessageEvent",
exportName: "MessageEvent",
typeOnly: true,
}, {
name: "WebSocketServer",
exportName: "WebSocketServer",
}],
},
{
package: {
name: "@deno/shim-crypto",
version: "~0.3.0",
},
globalNames: [
"crypto",
...[
"Crypto",
"SubtleCrypto",
"AlgorithmIdentifier",
"Algorithm",
"RsaOaepParams",
"BufferSource",
"AesCtrParams",
"AesCbcParams",
"AesGcmParams",
"CryptoKey",
"KeyAlgorithm",
"KeyType",
"KeyUsage",
"EcdhKeyDeriveParams",
"HkdfParams",
"HashAlgorithmIdentifier",
"Pbkdf2Params",
"AesDerivedKeyParams",
"HmacImportParams",
"JsonWebKey",
"RsaOtherPrimesInfo",
"KeyFormat",
"RsaHashedKeyGenParams",
"RsaKeyGenParams",
"BigInteger",
"EcKeyGenParams",
"NamedCurve",
"CryptoKeyPair",
"AesKeyGenParams",
"HmacKeyGenParams",
"RsaHashedImportParams",
"EcKeyImportParams",
"AesKeyAlgorithm",
"RsaPssParams",
"EcdsaParams",
].map((name) => ({
name,
typeOnly: true,
})),
],
},
],
mappings: {
"./src/deps.ts": "./src/deps.node.ts",
"./src/lib/serve.ts": "./src/lib/serve.node.ts",
},
});
console.log("Building done.");
console.log("Writing files...");
// we don't need those
outputResult.main.files = outputResult.main.files.filter((file) =>
!["deps/cdn.skypack.dev/ws.d.ts", "deps/cdn.skypack.dev/ws.js"].includes(
file.filePath,
) &&
!(common([`./${file.filePath}`, "./deps/deno.land/"]) === "./deps/deno.land/")
);
for (const file of outputResult.main.files) {
const text = file.fileText
// remove lines after a comment with "// dnt-node-remove-line"
.replace(/^\s*\/\/\s*dnt-node-remove-line\s*\n.*$/gm, "")
// uncomment line after a comment with "// dnt-node-uncomment-line"
.replace(
/^\s*\/\/\s*dnt-node-uncomment-line\s*\n\s*\/\/\s{0,1}/gm,
"",
);
await Deno.mkdir(join("./npm", dirname(file.filePath)), { recursive: true });
const path = join("./npm", file.filePath);
await Deno.writeTextFile(path, text);
}
const dependencies: Record<string, string> = {};
for (const dep of outputResult.main.dependencies) {
if (dep.name.startsWith("-/")) continue; // idk skypack bug
dependencies[dep.name] = dep.version;
}
// write package.json
const packageJson = {
name: "@ngruychev/json_rpc_controllers",
version: VERSION,
description:
"Create class-based JSON-RPC services and use them seamlessly on the client-side",
main: "./mod.js",
types: "./mod.d.ts",
browser: "./browser.js",
exports: {
".": "./mod.js",
"./client": "./client.js",
"./server": "./server.js",
},
license: "MIT",
dependencies,
devDependencies: {
"typescript": "^4.7.2",
"@types/node": "^17.0.38",
},
scripts: {
"tsc": "tsc",
},
keywords: [
"json-rpc",
"rpc",
"json",
"remote",
"node",
"controller",
"decorator",
],
repository: "github:ngruychev/json_rpc_controllers",
};
console.log("Writing package.json...");
await Deno.writeTextFile(
"./npm/package.json",
JSON.stringify(packageJson, null, 2),
);
console.log("Copying tsconfig.node.json...");
await copy("./tsconfig.node.json", "./npm/tsconfig.json");
console.log("Copying README.md...");
await copy("./README.md", "./npm/README.md");
console.log("Running npm install...");
const installProcess = Deno.run({
cmd: ["npm", "--prefix", "./npm", "install"],
});
try {
if (!(await installProcess.status()).success) {
throw new Error("npm install failed");
}
} finally {
installProcess.close();
}
console.log("Running tsc...");
const tscProcess = Deno.run({
cmd: ["npm", "--prefix", "./npm", "run", "tsc"],
});
try {
if (!(await tscProcess.status()).success) {
throw new Error("tsc failed");
}
} finally {
tscProcess.close();
}
console.log("Bundling browser.js...");
const browserBundle = await bundle("./client.ts");
await Deno.writeTextFile("./npm/browser.js", browserBundle.code);
await copy("./npm/client.d.ts", "./npm/browser.d.ts");
console.log("All done!");