-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease-npm.ts
126 lines (115 loc) · 2.91 KB
/
release-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
import { version } from "./src/version.ts";
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";
import { execa } from "https://deno.land/x/[email protected]/src/process.ts";
import {
dirname,
fromFileUrl,
resolve,
} from "https://deno.land/[email protected]/path/mod.ts";
const npm = resolve(dirname(fromFileUrl(import.meta.url)), "npm");
await emptyDir(npm);
await build({
outDir: npm,
typeCheck: false,
declaration: false,
scriptModule: "cjs",
entryPoints: ["./mod.ts"],
compilerOptions: {
target: "ES2019",
},
shims: {
deno: true,
},
test: false,
esModule: false,
package: {
version,
name: "deno-nrm",
bin: {
dnrm: "dist/mod.js",
"deno-nrm": "dist/mod.js",
},
description: "deno 实现的 nrm,每次切换源都在 100ms 内,速度超级快",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/markthree/dnrm.git",
},
files: [
"dist",
],
author: {
name: "markthree",
email: "[email protected]",
url: "https://github.com/markthree",
},
bugs: {
email: "[email protected]",
url: "https://github.com/markthree/dnrm/issues",
},
},
async postBuild() {
await sanitiseDeps();
await Promise.all(
[
shebang(resolve(npm, "script/mod.js")),
Deno.copyFile(".npmrc", resolve(npm, ".npmrc")),
Deno.copyFile("LICENSE", resolve(npm, "LICENSE")),
Deno.copyFile("README.md", resolve(npm, "README.md")),
],
);
await bundle();
await npmPublish();
},
});
async function shebang(mod: string) {
const code = await Deno.readTextFile(mod);
await Deno.writeTextFile(mod, `#!/usr/bin/env node\n${code}`);
}
async function sanitiseDeps() {
const packageJsonPath = resolve(npm, "package.json");
const packageJsonText = await Deno.readTextFile(packageJsonPath);
const packageJson = JSON.parse(packageJsonText) ?? {};
for (const key in packageJson) {
if (key === "dependencies") {
const dependencies = packageJson[key];
if (packageJson["devDependencies"]) {
packageJson["devDependencies"] = Object.assign(
packageJson["devDependencies"],
dependencies,
);
}
delete packageJson[key];
break;
}
}
await Deno.writeTextFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2),
);
await execa(["npm", "install"], {
cwd: npm,
});
}
async function bundle() {
await esbuild.build({
bundle: true,
format: "cjs",
minify: true,
target: "ES2019",
platform: "node",
sourcemap: false,
treeShaking: true,
absWorkingDir: npm,
legalComments: "none",
outfile: "./dist/mod.js",
entryPoints: ["./script/mod.js"],
});
esbuild.stop();
}
async function npmPublish() {
await execa(["npm", "publish"], {
cwd: npm,
});
}