Skip to content

Commit 0863319

Browse files
committed
chore(_tools): add npm build script
1 parent 9526649 commit 0863319

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

_tools/build_npm.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { build, emptyDir } from "https://deno.land/x/dnt@0.34.0/mod.ts";
2+
import { join } from "https://deno.land/std@0.186.0/path/mod.ts";
3+
import { makeOptions } from "./meta.ts";
4+
5+
async function buildPkg(version: string): Promise<void> {
6+
await emptyDir("./npm");
7+
const pkg = makeOptions(version);
8+
await Deno.copyFile("LICENSE", join(pkg.outDir, "LICENSE"));
9+
Deno.copyFile(
10+
join(".", "README.md"),
11+
join(pkg.outDir, "README.md"),
12+
);
13+
await build(pkg);
14+
}
15+
16+
if (import.meta.main) {
17+
const version = Deno.args[0];
18+
if (!version) {
19+
console.error("argument is required");
20+
Deno.exit(1);
21+
}
22+
await buildPkg(version);
23+
}

_tools/meta.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import {
2+
BuildOptions,
3+
EntryPoint,
4+
} from "https://deno.land/x/dnt@0.34.0/mod.ts";
5+
import { expandGlobSync } from "https://deno.land/std@0.186.0/fs/expand_glob.ts";
6+
import {
7+
fromFileUrl,
8+
join,
9+
parse,
10+
relative,
11+
} from "https://deno.land/std@0.186.0/path/mod.ts";
12+
13+
interface ModuleInfo {
14+
readonly name: string;
15+
readonly path: string;
16+
}
17+
18+
function path2EntryPoint(module: ModuleInfo): EntryPoint {
19+
const entryPoint: EntryPoint = {
20+
name: toRelative(module.name),
21+
path: toRelative(module.path),
22+
};
23+
24+
return entryPoint;
25+
}
26+
27+
function module2TypeVersions(modules: readonly ModuleInfo[]) {
28+
const entries = modules.map(({ name, path }) => {
29+
return [name, [join("types", toDts(path))]];
30+
});
31+
const map = Object.fromEntries(entries);
32+
33+
return { "*": map };
34+
}
35+
36+
function toRelative(path: string): string {
37+
return path.startsWith("./") ? path : "./" + path;
38+
}
39+
40+
function toDts(path: string): string {
41+
return path.replace(/.ts$/, ".d.ts");
42+
}
43+
44+
const root = fromFileUrl(import.meta.resolve("../"));
45+
const entries = expandGlobSync("!(_*|*_test.ts)*.ts", {
46+
includeDirs: false,
47+
root,
48+
});
49+
50+
const modules = [...entries].map(({ path }) => relative(root, path)).map(
51+
(path) => {
52+
const parsed = parse(path);
53+
const name = join(parsed.dir, parsed.name) + ".js";
54+
55+
return { name, path };
56+
},
57+
);
58+
59+
const entryPoints = modules.map(path2EntryPoint);
60+
const typesVersions = module2TypeVersions(modules);
61+
62+
export const makeOptions = (version: string): BuildOptions => ({
63+
test: false,
64+
shims: {},
65+
compilerOptions: {
66+
lib: ["esnext", "dom", "dom.iterable"],
67+
},
68+
typeCheck: false,
69+
entryPoints,
70+
outDir: "./npm",
71+
package: {
72+
name: "@httpland/request-utils",
73+
version,
74+
description: "Request utility collection",
75+
keywords: [
76+
"http",
77+
"request",
78+
"fetch-api",
79+
"utility",
80+
"utilities",
81+
"collection",
82+
],
83+
license: "MIT",
84+
homepage: "https://github.com/httpland/request-utils",
85+
repository: {
86+
type: "git",
87+
url: "git+https://github.com/httpland/request-utils.git",
88+
},
89+
bugs: {
90+
url: "https://github.com/httpland/request-utils/issues",
91+
},
92+
main: undefined,
93+
module: undefined,
94+
types: undefined,
95+
sideEffects: false,
96+
type: "module",
97+
publishConfig: {
98+
access: "public",
99+
},
100+
typesVersions,
101+
},
102+
packageManager: "pnpm",
103+
mappings: {
104+
"https://deno.land/x/headers_utils@1.0.0/equal.ts": {
105+
name: "@httpland/headers-utils",
106+
version: "1.0.0",
107+
subPath: "equal.js",
108+
},
109+
"https://deno.land/x/isx@1.3.1/is_null.ts": {
110+
name: "@miyauci/isx",
111+
version: "1.3.1",
112+
subPath: "is_null.js",
113+
},
114+
},
115+
});

_tools/publish_npm.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { prerelease, valid } from "https://deno.land/x/semver@v1.4.0/mod.ts";
2+
import { makeOptions } from "./meta.ts";
3+
4+
if (import.meta.main) {
5+
const version = Deno.args[0];
6+
if (!version) {
7+
console.error("arg of version is required");
8+
Deno.exit(1);
9+
}
10+
if (!valid(version)) {
11+
console.error("The argument of version is invalid");
12+
Deno.exit(1);
13+
}
14+
15+
const isPrerelease = prerelease(version);
16+
const tag = isPrerelease?.[0] ?? "latest";
17+
18+
const pkg = makeOptions(version);
19+
const command = new Deno.Command("npm", {
20+
args: ["publish", pkg.outDir, "--tag", String(tag)],
21+
});
22+
const result = await command.output();
23+
24+
if (!result.success) {
25+
console.error(new TextDecoder().decode(result.stderr));
26+
}
27+
}

0 commit comments

Comments
 (0)