-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlaunch.ts
202 lines (173 loc) · 4.98 KB
/
launch.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/// Convenience util to launch a user's dnit.ts
import { fs, log, path, semver } from "./deps.ts";
type UserSource = {
baseDir: string;
dnitDir: string;
mainSrc: string;
importmap: string | null;
};
type FindUserSourceContext = {
stat: Deno.FileInfo;
path: string;
};
function findUserSourceContext(dir: string): FindUserSourceContext {
return {
path: dir,
stat: Deno.lstatSync(dir),
};
}
function findUserSource(
dir: string,
startCtxArg: FindUserSourceContext | null,
): UserSource | null {
const startCtx = (startCtxArg === null)
? findUserSourceContext(dir)
: startCtxArg;
const dirStat = Deno.lstatSync(dir);
/// Do not cross filesystems (this is how git stops looking for a git dir)
if (dirStat.dev !== startCtx.stat.dev) {
return null;
}
/// Abort at root:
if (path.resolve(path.join(dir, "..")) === dir) {
return null;
}
const subdirs = [
"dnit", // subdirectory (preferred so that subdir is a deno only typescript tree)
"deno/dnit", // alternative path
];
const defaultSources = [
"main.ts",
"dnit.ts",
];
const importmaps = [
"import_map.json",
".import_map.json", // optionally hidden file
];
for (const subdir of subdirs) {
for (const sourceName of defaultSources) {
const res = {
baseDir: path.resolve(dir),
dnitDir: path.resolve(path.join(dir, subdir)),
mainSrc: path.resolve(path.join(dir, subdir, sourceName)),
};
if (fs.existsSync(res.mainSrc)) {
for (const importMapFile of importmaps) {
const importmap = path.resolve(path.join(dir, subdir, importMapFile));
if (fs.existsSync(importmap)) {
return {
...res,
importmap,
};
}
}
return {
...res,
importmap: null,
};
}
}
}
// recurse to parent directory to find dnit script
return findUserSource(path.join(dir, ".."), startCtx);
}
export async function parseDotDenoVersionFile(fname: string): Promise<string> {
const contents = await Deno.readTextFile(fname);
const trimmed = contents.split("\n").map((l) => l.trim()).filter((l) =>
l.length > 0
).join("\n");
return trimmed;
}
export async function getDenoVersion(): Promise<string> {
const cmd = new Deno.Command(Deno.execPath(), {
args: [
"--version",
],
stdout: "piped",
});
const { stdout } = await cmd.output();
const denoVersionStr = new TextDecoder().decode(stdout);
const regmatch = denoVersionStr.match(/deno[ ]+([0-9.]+)/);
if (regmatch) {
return regmatch[1];
}
throw new Error("Invalid parse of deno version output");
}
export function checkValidDenoVersion(
denoVersion: string,
denoReqSemverRange: string,
): boolean {
return semver.satisfies(denoVersion, denoReqSemverRange);
}
export async function launch(logger: log.Logger): Promise<Deno.CommandStatus> {
const userSource = findUserSource(Deno.cwd(), null);
if (userSource !== null) {
logger.info("running source:" + userSource.mainSrc);
logger.info("running wd:" + userSource.baseDir);
logger.info("running importmap:" + userSource.importmap);
logger.info("running dnitDir:" + userSource.dnitDir);
const denoVersion = await getDenoVersion();
logger.info("deno version:" + denoVersion);
const dotDenoVersionFile = path.join(userSource.dnitDir, ".denoversion");
if (fs.existsSync(dotDenoVersionFile)) {
const reqDenoVerStr = await parseDotDenoVersionFile(dotDenoVersionFile);
const validDenoVer = checkValidDenoVersion(denoVersion, reqDenoVerStr);
if (!validDenoVer) {
throw new Error(
`Note that ${dotDenoVersionFile} requires version(s) ${reqDenoVerStr}. The current version is ${denoVersion}. Consider editing the .denoversion file and try again`,
);
}
logger.info("deno version ok:" + denoVersion + " for " + reqDenoVerStr);
}
Deno.chdir(userSource.baseDir);
const permissions = [
"--allow-read",
"--allow-write",
"--allow-run",
"--allow-env",
"--allow-net",
];
const flags = [
"--quiet",
];
const importmap = userSource.importmap
? [
"--importmap",
userSource.importmap,
]
: [];
const args = [
"run",
...flags,
...permissions,
...importmap,
userSource.mainSrc,
"--dnitDir",
userSource.dnitDir,
...Deno.args,
];
logger.info("running command: deno " + args.join(" "));
const cmd = new Deno.Command(Deno.execPath(), {
args,
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
});
const { success, code, signal } = await cmd.output();
logger.info(
`command finished success:${success} code:${code} signal:${signal}`,
);
return {
success,
code,
signal,
};
} else {
logger.error("No dnit.ts or dnit directory found");
return {
success: false,
code: 1,
signal: null,
};
}
}