-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
39 lines (32 loc) · 1.31 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
import fs from 'node:fs';
import path from 'node:path';
import { mkdirp, copy, dist } from './utils.js';
export async function create(cwd, options) {
mkdirp(cwd);
writeTemplateFiles(options, cwd);
fs.unlink(path.join(cwd, "meta.json"), (err) => {
if (err) throw err;
})
}
function replaceInFiles(dirPath, searchValue, replaceValue) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
replaceInFiles(filePath, searchValue, replaceValue);
} else {
let content = fs.readFileSync(filePath, 'utf8');
content = content.replace(searchValue, replaceValue);
fs.writeFileSync(filePath, content);
}
});
}
function writeTemplateFiles(options, cwd) {
const dir = dist(`templates/${options.template}`);
copy(dir, cwd, (name) => name.replace('$addon-name-underscore$', options.underscore_name));
replaceInFiles(cwd, /\$addon-name-underscore\$/g, options.underscore_name);
replaceInFiles(cwd, /\$addon-acronym\$/g, options.acronym);
replaceInFiles(cwd, /\$addon-global\$/g, options.global);
replaceInFiles(cwd, /\$addon-name\$/g, options.fancy_name);
}