-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.ts
78 lines (63 loc) · 1.72 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { porcelain } from "https://deno.land/x/libpkgx@v0.18.1/mod.ts";
const { run } = porcelain;
const mdopsContent = (file: string, script: string) =>
`// After updating run '${script} recompile'
import { allInOne } from "https://raw.githubusercontent.com/mdops-org/mdops-cli/main/mod.ts";
if (import.meta.main) {
allInOne({
opsFile: "${file}",
dependenciesSelector: 'heading[value="Dependencies"] > table',
tasksSelector: 'heading[value="Tasks"] > heading',
});
}
`;
const depsContent = `
## Dependencies
| dependency | version |
|------------|---------|
| | |
`;
const tasksContent = `
## Tasks
### recompile
\`\`\`shell
scripts/mdops recompile
\`\`\`
`;
type Options = {
mdFile: string;
script: string;
};
export const initScript = async ({ mdFile, script }: Options) => {
try {
using _ = await Deno.open(mdFile, { create: true, append: true });
const mdContent = await Deno.readTextFile(mdFile);
if (!mdContent.includes("# Dependencies")) {
Deno.writeTextFile(mdFile, depsContent, { append: true });
}
if (!mdContent.includes("# Tasks")) {
Deno.writeTextFile(mdFile, tasksContent, { append: true });
}
using __ = await Deno.open(`${script}.ts`);
console.error(`The ${script}.ts file already exists`);
Deno.exit();
} catch {
try {
if (script.includes("/")) {
await Deno.mkdir(script.substring(0, script.lastIndexOf("/")), {
recursive: true,
});
}
} catch {
// empty
} finally {
await Deno.writeTextFile(
`${script}.ts`,
mdopsContent(mdFile, script),
);
await run(
`deno@1.41.2 compile -A --unstable -o ${script} ${script}.ts`,
);
}
}
};