136 lines
4.5 KiB
JavaScript
136 lines
4.5 KiB
JavaScript
|
import chalk from "chalk";
|
||
|
import { input, select } from "@inquirer/prompts";
|
||
|
import fs from "node:fs";
|
||
|
import * as utils from './util.js'
|
||
|
import path from "node:path";
|
||
|
|
||
|
async function main() {
|
||
|
const directory = await input({ message: "Project directory: ", default: "./test" });
|
||
|
utils.ensure_directory(directory);
|
||
|
|
||
|
const choice = await select({
|
||
|
message: "What command do you want to execute?",
|
||
|
choices: [
|
||
|
{
|
||
|
name: 'Initialize project',
|
||
|
value: 'init',
|
||
|
description: 'Initialize a new myfactory addon'
|
||
|
},
|
||
|
{
|
||
|
name: 'Component',
|
||
|
value: 'component',
|
||
|
description: 'Choose from a set of templates to automatically insert into your myfactory addon',
|
||
|
},
|
||
|
]
|
||
|
});
|
||
|
|
||
|
try {
|
||
|
|
||
|
switch (choice) {
|
||
|
case "init":
|
||
|
if (fs.readdirSync(directory).length > 0) {
|
||
|
log.error("Directory must be empty.");
|
||
|
return;
|
||
|
}
|
||
|
const type = await select({
|
||
|
message: "What type of project do you want to initialize?",
|
||
|
choices: [
|
||
|
{
|
||
|
name: "Skeleton",
|
||
|
value: "skeleton",
|
||
|
description: "A basic skeleton project with nothing added"
|
||
|
},
|
||
|
{
|
||
|
name: "Hello World",
|
||
|
value: "helloworld",
|
||
|
description: "A simple hello world addon that generates a new page in the menu and displays some text"
|
||
|
}
|
||
|
]
|
||
|
});
|
||
|
init_project(directory, type)
|
||
|
break;
|
||
|
case "component": insert_component(directory);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
log.error(e.message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Initialize a new myfactory addon
|
||
|
* @param {string} dir Directory in which to initialize the project.
|
||
|
*/
|
||
|
async function init_project(dir, type) {
|
||
|
const projectname = await input({ message: "Project name: ", default: "Example Project" });
|
||
|
const partner = await input({ message: "Partner id: ", default: "ES" });
|
||
|
const module = await input({ message: "Module id: ", default: "Seiwert" });
|
||
|
const version = await input({ message: "Version: ", default: "1.0.0.0" });
|
||
|
const description = await input({ message: "Module description", default: "This is a myfactory Addon" });
|
||
|
|
||
|
utils.ensure_directory(path.join(dir, "Install"));
|
||
|
|
||
|
fs.cpSync(path.join("./templates", type, "/"), dir + "/Install", { recursive: true });
|
||
|
|
||
|
// Make xml def file
|
||
|
fs.writeFileSync(path.join(dir, "myfactory.xml"), utils.template("myfactoryXml", {
|
||
|
partnerid: partner,
|
||
|
moduleid: module,
|
||
|
version,
|
||
|
description
|
||
|
}));
|
||
|
|
||
|
replaceVariablesInNames(dir, {
|
||
|
projectname,
|
||
|
partner,
|
||
|
module
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Function to recursively replace variables in file and directory names
|
||
|
function replaceVariablesInNames(dir, variables) {
|
||
|
fs.readdirSync(dir).forEach(name => {
|
||
|
const oldPath = path.join(dir, name);
|
||
|
const newPath = replaceVariablesInName(name, variables);
|
||
|
const newPathFull = path.join(dir, newPath);
|
||
|
fs.renameSync(oldPath, newPathFull); // Rename the file or directory
|
||
|
if (fs.statSync(newPathFull).isDirectory()) {
|
||
|
replaceVariablesInNames(newPathFull, variables); // Recursively explore subdirectories
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Function to replace variables in a single name
|
||
|
function replaceVariablesInName(name, variables) {
|
||
|
// Replace all instances of {variable} with the corresponding value
|
||
|
Object.keys(variables).forEach(variable => {
|
||
|
const regex = new RegExp('{' + variable + '}', 'g');
|
||
|
name = name.replace(regex, variables[variable]);
|
||
|
});
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Insert a component into an existing myfactory addon
|
||
|
* @param {string} dir
|
||
|
*/
|
||
|
async function insert_component(dir) {
|
||
|
if (!utils.isMyFactoryAddon(dir)) throw new Error("Invalid MyFactory Addon directory.")
|
||
|
|
||
|
const component = select({
|
||
|
message: "What component do you want to add?", choices: [
|
||
|
{
|
||
|
name: "ListView",
|
||
|
value: "listview",
|
||
|
description: "Add a simple listview to your project"
|
||
|
}
|
||
|
]
|
||
|
})
|
||
|
}
|
||
|
|
||
|
const log = {
|
||
|
error(string) {
|
||
|
console.error(chalk.red("ERROR"), string)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
main();
|