-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.ts
executable file
·63 lines (57 loc) · 1.83 KB
/
init.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
#!/usr/bin/env -S deno run --allow-read --allow-write='.'
import * as fs from "node:fs/promises";
const shimFile = "// See https://github.com/fromdeno/deno2node#shimming";
const gitignore = "/lib/\n/node_modules/\n/src/vendor/";
async function download(url: URL, target: string) {
const response = await fetch(url);
await fs.writeFile(target, await response.text(), { flag: "wx" });
}
export async function getVersion(): Promise<string> {
const packageUrl = new URL("../package.json", import.meta.url);
const response = await fetch(packageUrl);
const { version } = await response.json();
return version;
}
async function createPackageJson() {
const pkg = {
"type": "module",
"version": "0.0.0",
"exports": "./lib/mod.js",
"typings": "./lib/mod.d.ts",
"files": [
"lib/",
"!lib/**/*.test.*",
"!*/vendor/**/*.ts*",
],
"scripts": {
"fmt": "deno fmt",
"lint": "deno lint",
"test": "deno test",
"prepare": "deno2node",
"postprepare": "node --test lib/",
"clean": "git clean -fXde !node_modules/",
},
"devDependencies": {
"deno2node": `~${await getVersion()}`,
},
};
await fs.writeFile("package.json", JSON.stringify(pkg, null, 2), {
flag: "wx",
});
}
export async function initializeProject() {
const denoJsonUrl = new URL("../deno.json", import.meta.url);
const tsconfigUrl = new URL("../tsconfig.json", import.meta.url);
await fs.mkdir("src/");
await Promise.all([
createPackageJson(),
download(denoJsonUrl, "deno.json"),
download(tsconfigUrl, "tsconfig.json"),
fs.writeFile(".gitignore", gitignore, { flag: "wx" }),
fs.writeFile("src/shim.node.ts", shimFile, { flag: "wx" }),
]);
}
// @ts-ignore not available on Node.js: https://github.com/nodejs/modules/issues/274
if (import.meta.main) {
await initializeProject();
}