-
Notifications
You must be signed in to change notification settings - Fork 0
/
createApp.js
57 lines (46 loc) · 2.18 KB
/
createApp.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
const readline = require("readline-sync");
const fs = require("fs-extra");
const types = ["FormApplication", "Application", "Menu"];
const boilerplates = {
FormApplication: "DocumentFormApp",
Application: "BasicApplication",
Menu: "MenuSetting",
};
function createApplication(type, name) {
// Type 1: FormApplication, 2: Application, 3: Menu
const selectedType = types.includes(type) ? type : types[type - 1];
console.log(`Creating ${selectedType} ${name}...`);
const slugifiedName = name.split(/(?=[A-Z])/).join('-').toLowerCase();
const appFolderPath = "scripts/app";
const templatesFolderPath = "templates";
// Check if the app folder already exists
if (fs.existsSync(`${appFolderPath}/${slugifiedName}`)) {
console.error(`Error: The folder ${appFolderPath}/${slugifiedName} already exists.`);
return;
}
// Copy DocumentFormApp.js to scripts/app/{name}.js
fs.copySync(`boilerplate/${boilerplates[selectedType]}.js`, `${appFolderPath}/${name}.js`);
// Read the copied file and replace occurrences of "DocumentFormApp" with the application name
const filePath = `${appFolderPath}/${name}.js`;
let fileContent = fs.readFileSync(filePath, "utf-8");
fileContent = fileContent.replace(boilerplates[type], name);
fs.writeFileSync(filePath, fileContent);
// Create an empty .hbs file in the templates folder
fs.writeFileSync(`${templatesFolderPath}/${slugifiedName}.hbs`, "");
console.log(`${selectedType} ${name} created successfully.`);
}
// Extract the command-line arguments
const args = process.argv.slice(2);
// If no arguments are provided, ask for type and name
if (args.length === 0) {
const type = readline.question("Enter the type of application (1 for FormApplication, 2 for Application, 3 for Menu): ");
const name = readline.question("Enter the application name: ");
createApplication(type, name);
} else {
// Check if the command is "create FormApplication"
if (args.length === 2) {
createApplication(args[0], args[1]);
} else {
console.error("Invalid command. Usage: create FormApplication {name}");
}
}