create-onetech-app
Version:
CLI to quickly set up React projects with popular templates and tools.
118 lines (117 loc) • 3.94 kB
JavaScript
import inquirer from "inquirer";
import TechData from "../data/TechStack.js";
class prompt {
// main function of promt taking via inqurirer
async Prompts({ type, name, message, choices, validate, default: defaultValue, }) {
let query = { type, name, message };
if (choices) {
query.choices = choices;
}
if (validate) {
query.validate = validate;
}
if (defaultValue) {
query.default = defaultValue;
}
try {
const answer = await inquirer.prompt(query);
return answer[name];
}
catch (error) {
if (error instanceof Error) {
console.error("Prompt error:", error.message);
}
else {
console.error("An unknown error occurred during prompt.");
}
console.log("🔻Cleaning up before exit...");
process.exit(1);
}
}
// takes input of projectname
async projectname() {
const query = {
type: "input",
name: "projectname",
message: " Give a name of your project",
validate: this.validateProjectName,
};
const answer = await this.Prompts(query);
return answer;
}
// projectname/dir validator
validateProjectName(input, t = false) {
const trimmed = input.trim();
if (!trimmed)
return !t ? " Project name cannot be empty." : false;
// Cross-platform invalid characters
const invalidChars = /[<>:"/\\|?*\x00-\x1F]/g;
if (invalidChars.test(trimmed)) {
return !t
? 'Name contains invalid characters: <>:"/\\|?* or control characters.'
: false;
}
// Unix (Linux/macOS): disallow forward slash
if (trimmed.includes("/")) {
return !t
? "Name cannot contain '/' (used as path separator on Unix)."
: false;
}
// onetech (rule): - should not startwith
if (trimmed.startsWith("-")) {
return !t ? "Name should not start with '-'." : false;
}
// Windows: reserved names
const reserved = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i;
if (reserved.test(trimmed)) {
return !t
? "This name is reserved by the system. Choose a different name."
: false;
}
// macOS (HFS+): colon is not allowed
if (trimmed.includes(":")) {
return !t
? "Colon ':' is not allowed in folder names (macOS restriction)."
: false;
}
// Max length safety
if (trimmed.length > 255) {
return !t ? "Name is too long (max 255 characters)." : false;
}
return true;
}
// takes input of Base (eg:<vite>,<flutter>,<nextjs>)
async projectBase() {
const query = {
type: "list",
name: "Base",
message: " choose your base",
choices: TechData.map((items) => items.base),
};
const answer = this.Prompts(query);
return answer;
}
// takes input of lang (eg:javascript|typescript)
async projectLang(langs) {
const query = {
type: "list",
name: "Lang",
message: " choose lang",
choices: langs,
};
const answer = await this.Prompts(query);
return answer;
}
// takes input of prebuilt templates like app-tw(base+tailwind setup) app-firebase(base+firebas setup)
async projectTemplates(templates) {
const query = {
type: "list",
name: "template",
message: " choose templates",
choices: templates,
};
const answer = await this.Prompts(query);
return answer;
}
}
export default prompt;