-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
112 lines (99 loc) · 2.57 KB
/
mod.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
#!/usr/bin/env deno
import { toFileUrl } from "jsr:@std/path/to-file-url";
const args = Array.from(Deno.args);
if (args[0] === "init") {
args.shift();
await run("run", "-A", "https://lume.land/init.ts", ...args);
} else if (args[0] === "upgrade-cli") {
await run(
"install",
"--allow-run",
"--allow-env",
"--allow-read",
"--allow-write=deno.json",
"--name",
"lume",
"--force",
"--reload",
"--global",
"https://deno.land/x/lume_cli/mod.ts",
);
} else if (args[0] === "serve" || args[0] === "build") {
await run("task", ...args);
} else if (args[0] === "local") {
if (args[1] === "--save") {
saveLocal();
} else if (args[1] === "--remove") {
removeLocal();
} else {
applyLocal();
}
} else {
await run("task", "lume", ...args);
}
async function run(...args: string[]) {
if (hasFlag("--drafts", args)) {
Deno.env.set("LUME_DRAFTS", "true");
}
if (hasFlag("--debug", args)) {
Deno.env.set("LUME_LOGS", "DEBUG");
}
if (hasFlag("--info", args)) {
Deno.env.set("LUME_LOGS", "INFO");
}
if (hasFlag("--warning", args)) {
Deno.env.set("LUME_LOGS", "WARN");
}
if (hasFlag("--warn", args)) {
Deno.env.set("LUME_LOGS", "WARN");
}
if (hasFlag("--error", args)) {
Deno.env.set("LUME_LOGS", "ERROR");
}
if (hasFlag("--critical", args)) {
Deno.env.set("LUME_LOGS", "CRITICAL");
}
if (hasFlag("--no-cache", args)) {
Deno.env.set("LUME_NOCACHE", "true");
}
const command = new Deno.Command(Deno.execPath(), {
args: args,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
await command.output();
}
function hasFlag(flag: string, args: string[]): boolean {
if (args.includes(flag)) {
args.splice(args.indexOf(flag), 1);
return true;
}
return false;
}
function saveLocal() {
const local = toFileUrl(Deno.cwd());
if (!local.pathname.endsWith("/")) {
local.pathname += "/";
}
localStorage.setItem("local.lume", local.href);
console.log(`Local path set to ${local.href}`);
}
function removeLocal() {
localStorage.removeItem("local.lume");
console.log(`Local path removed`);
}
function applyLocal() {
const local = localStorage.getItem("local.lume");
if (!local) {
console.error(
"No Lume path saved. Use `lume local --save` inside your Lume repo.",
);
return;
}
const content = Deno.readTextFileSync("deno.json");
const config = JSON.parse(content);
config.imports["lume/"] = local;
Deno.writeTextFileSync("deno.json", JSON.stringify(config, null, 2));
console.log(`Local path applied to deno.json`);
}