-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcommands.ts
More file actions
101 lines (91 loc) · 2.61 KB
/
commands.ts
File metadata and controls
101 lines (91 loc) · 2.61 KB
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
import * as path from "node:path";
import * as fs from "node:fs";
import * as kl from "kolorist";
import { JsrPackage } from "./utils";
import { getPkgManager } from "./pkg_manager";
const JSR_NPMRC = `@jsr:registry=https://npm.jsr.io\n`;
export async function setupNpmRc(dir: string) {
const npmRcPath = path.join(dir, ".npmrc");
try {
let content = await fs.promises.readFile(npmRcPath, "utf-8");
if (!content.includes(JSR_NPMRC)) {
content += JSR_NPMRC;
await fs.promises.writeFile(npmRcPath, content);
}
} catch (err) {
if (err instanceof Error && (err as any).code === "ENOENT") {
await fs.promises.writeFile(npmRcPath, JSR_NPMRC);
} else {
throw err;
}
}
}
export interface BaseOptions {
pkgManagerName: "npm" | "yarn" | "pnpm" | null;
}
export interface InstallOptions extends BaseOptions {
mode: "dev" | "prod" | "optional";
}
export async function install(packages: JsrPackage[], options: InstallOptions) {
console.log(`Installing ${kl.cyan(packages.join(", "))}...`);
const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName);
await setupNpmRc(pkgManager.cwd);
await pkgManager.install(packages, options);
}
export async function remove(packages: JsrPackage[], options: BaseOptions) {
console.log(`Removing ${kl.cyan(packages.join(", "))}...`);
const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName);
await pkgManager.remove(packages);
}
async function createFiles(cwd: string, files: Record<string, string>) {
await Promise.all(
Array.from(Object.entries(files)).map(([file, content]) => {
const filePath = path.join(cwd, file);
return fs.promises.writeFile(filePath, content, "utf-8");
})
);
}
export async function init(cwd: string) {
await setupNpmRc(cwd);
await createFiles(cwd, {
"package.json": JSON.stringify(
{
name: "@<scope>/<package_name>",
version: "0.0.1",
description: "",
license: "ISC",
type: "module",
},
null,
2
),
// TODO: Rename to jsr.json, once supported in Deno
// deno.json
"deno.json": JSON.stringify(
{
name: "@<scope>/<package_name>",
version: "0.0.1",
description: "",
exports: {
".": "./mod.ts",
},
},
null,
2
),
"mod.ts": [
"export function add(a: number, b: number): number {",
" return a + b;",
"}",
].join("\n"),
"tsconfig.json": JSON.stringify(
{
compilerOptions: {
moduleResolution: "NodeNext",
},
},
null,
2
),
});
}