This repository was archived by the owner on Jul 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 1.45 KB
/
index.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
import fs, { readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
export const createPurplet = async (dir, options) => {
fs.mkdirSync(dir, { recursive: true });
try {
const templateDir = fileURLToPath(
new URL(`./templates/blank`, import.meta.url).href
);
copyFiles(dir, templateDir, options.deps);
} catch (e) {
throw new Error(e);
}
};
const copyFiles = (dir, templateDir, deps) => {
const files = fs.readdirSync(templateDir);
const ignored = fs
.readFileSync(
fileURLToPath(new URL(`./templates/blank/.ignore`, import.meta.url)),
'utf-8'
)
.split('\r\n');
files.forEach((file) => {
const filePath = path.join(templateDir, file);
const fileName = path.basename(filePath);
if (ignored.includes(fileName)) {
return;
}
if (fs.lstatSync(filePath).isDirectory()) {
fs.mkdirSync(path.join(dir, fileName), { recursive: true });
copyFiles(path.join(dir, fileName), filePath);
return;
}
if (fileName === 'package.template.json') {
const pjson = JSON.parse(readFileSync(filePath, 'utf-8'));
pjson.name = dir;
pjson.dependencies = {
['purplet']: deps.purplet,
['discord.js']: deps['discord.js'],
};
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify(pjson, null, 2)
);
return;
}
fs.copyFileSync(filePath, path.join(dir, fileName));
});
};