-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.ts
109 lines (99 loc) · 2.83 KB
/
bootstrap.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
import { ensureDir, move } from "https://deno.land/std@0.146.0/fs/mod.ts";
import { join, resolve } from "https://deno.land/std@0.146.0/path/mod.ts";
import { checkExists } from "./utils.ts";
// this is fugly but eh...
export const mod = `
import { basename } from "https://deno.land/std@0.146.0/path/mod.ts";
import * as Eta from "https://deno.land/x/eta@v1.12.3/mod.ts";
const __dirname = new URL(".", import.meta.url).pathname;
export default (
readmeFilePath: string,
readmeDirectoryPath: string,
readmeTemplate: string,
): string | Promise<string> | void => {
const template = \`<%~ includeFile("HEADER.template.md", {...it}) %>
\${readmeTemplate}\`.replaceAll('includeFile("', \`includeFile("\${__dirname}\`);
try {
return Eta.render(
template,
{
name: basename(readmeDirectoryPath),
},
);
} catch (e) {
console.error(e);
Deno.exit(1);
}
};
`;
export const header = `# <%= it.name %>
hello world
`;
export const footer = `---
made with k by love
`;
export const bootstrap = async (
templateDirectory: string,
error: (message: string, error?: Error) => void,
) => {
if (await checkExists(templateDirectory, error)) {
console.log(
"You ran bootstrap mode with present templates, backing up.",
);
try {
await move(templateDirectory, `${templateDirectory}.old`, {
overwrite: true,
});
} catch (e) {
error("Something went wrong backing up your templates.", e);
}
}
try {
await ensureDir(templateDirectory);
} catch (e) {
error(
`Something went wrong trying to create the templates directory at ${templateDirectory}.`,
e,
);
}
const modPath = resolve(join(templateDirectory, "mod.ts"));
try {
await Deno.writeTextFile(modPath, mod);
} catch (e) {
error(
`Something went wrong trying to boostrap ${modPath}.`,
e,
);
}
const headerPath = resolve(join(templateDirectory, "HEADER.template.md"));
try {
await Deno.writeTextFile(headerPath, header);
} catch (e) {
error(
`Something went wrong trying to boostrap ${headerPath}.`,
e,
);
}
const footerPath = resolve(join(templateDirectory, "FOOTER.template.md"));
try {
await Deno.writeTextFile(footerPath, footer);
} catch (e) {
error(
`Something went wrong trying to boostrap ${footerPath}.`,
e,
);
}
console.log(`Template bootstrapping done in ${templateDirectory}.`);
console.log("Feel free to edit anything to your liking.");
console.log();
console.log(
"Caching modules once. You may need to recache if you add new imports.",
);
console.log(`deno cache ${templateDirectory}/mod.ts`);
const cacheProcess = Deno.run({
cmd: ["deno", "cache", resolve(join(templateDirectory, "mod.ts"))],
});
await cacheProcess.status();
cacheProcess.close();
console.log("All done!");
};