generated from markthree/deno-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease-npm.ts
122 lines (112 loc) · 2.81 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
import { version } from "./src/version.ts"
import { description } from "./src/constant.ts"
import * as esbuild from "https://deno.land/x/esbuild@v0.20.1/mod.js"
import { build, emptyDir } from "https://deno.land/x/dnt@0.40.0/mod.ts"
import { execa } from "https://deno.land/x/easy_std@v0.7.0/src/process.ts"
import {
dirname,
fromFileUrl,
resolve,
} from "https://deno.land/std@0.218.2/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,
description,
name: "ndeno",
bin: {
n: "dist/mod.js",
ndeno: "dist/mod.js",
},
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/markthree/ndeno.git",
},
files: ["dist"],
author: {
name: "markthree",
email: "1801982702@qq.com",
url: "https://github.com/markthree",
},
bugs: {
email: "1801982702@qq.com",
url: "https://github.com/markthree/ndeno/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,
})
}