forked from ankurrsinghal/svelte-legos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-docs.js
61 lines (50 loc) · 1.41 KB
/
gen-docs.js
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
import fs from "fs";
function createDirectoriesFn(base, dirName) {
return function(hookName) {
const libDir = `${base}/${hookName}`;
const dest = `./src/routes/guides/${dirName}/${hookName}`;
try {
fs.mkdirSync(dest);
} catch (err) {
if (err.code !== "EEXIST")
console.error(err.message);
}
const stat = fs.statSync(libDir);
if (stat.isDirectory()) {
const createFiles = createFilesFn(dirName, libDir, hookName);
fs.readdirSync(libDir).forEach(createFiles);
}
}
}
function createFilesFn(dirName, libDir, hookName) {
return function(fp) {
const fullPath = `${libDir}/${fp}`;
const dest = `./src/routes/guides/${dirName}/${hookName}/+page.svelte`;
if (fp.endsWith(".svelte")) {
fs.copyFileSync(fullPath, dest);
}
}
}
function generate(dirName) {
const base = `./src/lib/${dirName}`;
try {
fs.mkdirSync(`./src/routes/guides/${dirName}`);
} catch (err) {
if (err.code !== "EEXIST")
console.error(err.message);
}
const createDirectories = createDirectoriesFn(base, dirName);
fs.readdirSync(base).forEach(createDirectories);
fs.copyFileSync("./src/routes/guides/layout-hook.svelte", `./src/routes/guides/${dirName}/+layout.svelte`);
fs.copyFileSync("./src/routes/guides/layout-hook.server.ts", `./src/routes/guides/${dirName}/+layout.server.ts`);
}
[
"actions",
"stores",
"derivatives",
"middlewares",
"stores",
"transitions",
"utilities",
"watchers"
].forEach(generate)