forked from GeopJr/SveltePress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepareTheme.js
61 lines (55 loc) · 1.86 KB
/
prepareTheme.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
// Require it all so we use exp functions
// only available in > 16
import fs from 'fs';
import { join, relative } from 'path';
// Combine package jsons
if (fs.existsSync('./src/lib/SveltePress/theme/meta/package.json')) {
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const themePakcageJson = JSON.parse(
fs.readFileSync('./src/lib/SveltePress/theme/meta/package.json', 'utf8')
);
['devDependencies', 'dependencies'].forEach((x) => {
if (themePakcageJson.hasOwnProperty(x))
packageJson[x] = { ...packageJson[x], ...themePakcageJson[x] };
});
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
}
// Write index.svelte
if (fs.existsSync('./src/lib/SveltePress/theme/index.svelte'))
fs.writeFileSync(
'./src/routes/index.svelte',
fs.readFileSync('./src/lib/SveltePress/theme/index.svelte', 'utf8')
);
if (fs.existsSync('./src/lib/SveltePress/theme/meta/root/')) {
// if cpSync is available
if (typeof fs.cpSync === 'function') {
// Don't override anything.
fs.cpSync('./src/lib/SveltePress/theme/meta/root/', './', { force: false, recursive: true });
} else {
// < 16
copyFiles('./src/lib/SveltePress/theme/meta/root/');
// Recursive function
function copyFiles(source) {
const files = fs.readdirSync(source, { withFileTypes: true });
for (const item of files) {
const fullPath = join(source, item.name);
const rootRelative = join(
'./',
relative('./src/lib/SveltePress/theme/meta/root/', fullPath)
);
if (item.isDirectory()) {
if (!fs.existsSync(rootRelative)) fs.mkdirSync(rootRelative);
copyFiles(fullPath);
} else {
try {
fs.copyFileSync(fullPath, rootRelative, fs.constants.COPYFILE_EXCL);
} catch (e) {
if (e.code === 'EEXIST') {
console.log(`File '${rootRelative}' already exists, skipped.`);
}
}
}
}
}
}
}