generated from theripper93/FoundryVTT-Module-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-project.js
87 lines (74 loc) · 2.34 KB
/
init-project.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
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
const fs = require('fs');
const path = require('path');
const projectFolderPath = path.basename(__dirname);
const moduleId = 'module-id';
const replacement = projectFolderPath;
function replaceInFile(filePath) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${filePath}`);
return;
}
let updatedContent = data.replace(new RegExp(moduleId, 'g'), replacement);
if (filePath.includes("module.json")) {
updatedContent = updatedContent.replace(new RegExp("module-name", 'g'), convertToTitleCase(replacement));
}
fs.writeFile(filePath, updatedContent, 'utf8', (err) => {
if (err) {
console.error(`Error writing to file: ${filePath}`);
return;
}
console.log(`Replaced "${moduleId}" with "${replacement}" in ${filePath}`);
});
});
}
const filesToReplaceIn = [
'languages/en.json',
'scripts/main.js',
'module.json',
// Add more file paths as needed
];
filesToReplaceIn.forEach((filePath) => {
const fullPath = path.join(__dirname, filePath);
replaceInFile(fullPath);
deleteInitScript();
selfDestruct();
});
function deleteInitScript() {
const packageJsonPath = path.join(__dirname, 'package.json');
fs.readFile(packageJsonPath, 'utf8', (err, packageData) => {
if (err) {
console.error(`Error reading package.json: ${err}`);
return;
}
try {
const packageInfo = JSON.parse(packageData);
if (packageInfo.scripts && packageInfo.scripts.init) {
delete packageInfo.scripts.init;
fs.writeFile(packageJsonPath, JSON.stringify(packageInfo, null, 2), 'utf8', (err) => {
if (err) {
console.error(`Error writing to package.json: ${err}`);
}
});
}
} catch (parseError) {
console.error(`Error parsing package.json: ${parseError}`);
}
});
}
function selfDestruct() {
const scriptPath = path.join(__dirname, 'init-project.js');
fs.unlink(scriptPath, (err) => {
if (err) {
console.error(`Error deleting script: ${err}`);
} else {
console.log('Script deleted successfully.');
}
});
}
function convertToTitleCase(inputString) {
return inputString
.split('-')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}