mirror of
https://github.com/CompeyDev/create-guilded-bot.git
synced 2025-03-04 11:01:37 +00:00
feat: initial non-interactive CLI support
This commit is contained in:
parent
93d06fb323
commit
e459089280
4 changed files with 163 additions and 86 deletions
102
packages/cli/lib/interactiveClient.ts
Normal file
102
packages/cli/lib/interactiveClient.ts
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
import fs, { createWriteStream, mkdir, readdir, readdirSync } from "fs";
|
||||||
|
import { copySync, removeSync } from "fs-extra";
|
||||||
|
import * as inquirer from "inquirer";
|
||||||
|
import getPackageManager from "../lib/getPackageManager";
|
||||||
|
import install from "../lib/installDependencies";
|
||||||
|
import * as logger from "../utils/logger";
|
||||||
|
import constants from "../utils/constants";
|
||||||
|
import getConstant from "../utils/constants";
|
||||||
|
import stream from "got";
|
||||||
|
import unzip from "unzip-stream";
|
||||||
|
import validateClient from "../lib/validateClient";
|
||||||
|
|
||||||
|
export default function main(shouldInstall: boolean) {
|
||||||
|
const welcomeASCII = getConstant("welcomeMessage");
|
||||||
|
console.log(welcomeASCII);
|
||||||
|
|
||||||
|
inquirer
|
||||||
|
.prompt([
|
||||||
|
{
|
||||||
|
type: "input",
|
||||||
|
name: "location",
|
||||||
|
message: "Where should the project be initialized?",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "list",
|
||||||
|
name: "flavor",
|
||||||
|
message: "Which flavor?",
|
||||||
|
choices: ["TypeScript", "JavaScript"],
|
||||||
|
filter(val) {
|
||||||
|
return val.toLowerCase();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.then((answers) => {
|
||||||
|
validateClient();
|
||||||
|
mkdir(answers.location, (e) => {
|
||||||
|
if (e && e.code != "EEXIST") {
|
||||||
|
logger.error("Failed to create project directory.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e) {
|
||||||
|
if (e.code == "EEXIST") {
|
||||||
|
readdir(answers.location, (_, files) => {
|
||||||
|
if (files.length) {
|
||||||
|
logger.error("Directory not empty.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let packageManager = getPackageManager();
|
||||||
|
|
||||||
|
const start = async (TEMPLATE_DOWNLOAD_URL: string) => {
|
||||||
|
const download = stream(TEMPLATE_DOWNLOAD_URL, { isStream: true }).pipe(
|
||||||
|
createWriteStream(`${answers.location}/create-guilded-bot_ts.zip`)
|
||||||
|
);
|
||||||
|
download.on("finish", () => {
|
||||||
|
fs.createReadStream(`${answers.location}/create-guilded-bot_ts.zip`)
|
||||||
|
.pipe(unzip.Extract({ path: `${answers.location}` }))
|
||||||
|
.on("finish", () => {
|
||||||
|
removeSync(`${answers.location}/create-guilded-bot_ts.zip`);
|
||||||
|
logger.success("🚀 Let's get started.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (answers.flavor == "typescript") {
|
||||||
|
const TEMPLATE_DOWNLOAD_URL =
|
||||||
|
"https://files.devcomp.xyz/r/create-guilded-bot_ts.zip";
|
||||||
|
|
||||||
|
start(TEMPLATE_DOWNLOAD_URL).then(() => {
|
||||||
|
if (shouldInstall) {
|
||||||
|
install(
|
||||||
|
packageManager as "npm" | "pnpm" | "yarn" | null,
|
||||||
|
answers.location
|
||||||
|
);
|
||||||
|
} else if (!shouldInstall) {
|
||||||
|
logger.custom(packageManager, "Skipping installation step.")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (answers.flavor == "javascript") {
|
||||||
|
const TEMPLATE_DOWNLOAD_URL =
|
||||||
|
"https://files.devcomp.xyz/r/create-guilded-bot_js.zip";
|
||||||
|
|
||||||
|
start(TEMPLATE_DOWNLOAD_URL).then(() => {
|
||||||
|
if (shouldInstall) {
|
||||||
|
install(
|
||||||
|
packageManager as "npm" | "pnpm" | "yarn" | null,
|
||||||
|
answers.location
|
||||||
|
);
|
||||||
|
} else if (!shouldInstall) {
|
||||||
|
logger.custom(packageManager, "Skipping installation step.")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,94 +1,44 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import fs, { createWriteStream, mkdir, readdir, readdirSync } from "fs";
|
import getConstant from "../utils/constants";
|
||||||
import { copySync, removeSync } from "fs-extra";
|
import interactiveClient from "../lib/interactiveClient";
|
||||||
import * as inquirer from "inquirer";
|
|
||||||
import getPackageManager from "../lib/getPackageManager";
|
|
||||||
import install from "../lib/installDependencies";
|
|
||||||
import * as logger from "../utils/logger";
|
import * as logger from "../utils/logger";
|
||||||
import constants from "../lib/constants";
|
import * as globals from "../utils/globals"
|
||||||
import getConstant from "../lib/constants";
|
|
||||||
import stream from "got";
|
|
||||||
import unzip from "unzip-stream";
|
|
||||||
import validateClient from "../lib/validateClient";
|
|
||||||
|
|
||||||
|
|
||||||
|
const args = process.argv.filter((_, i: number) => {
|
||||||
|
return i > 1;
|
||||||
|
});
|
||||||
const weclomeASCII = getConstant("welcomeMessage");
|
const weclomeASCII = getConstant("welcomeMessage");
|
||||||
console.log(weclomeASCII);
|
const helpMenu = getConstant("helpMenu");
|
||||||
|
|
||||||
inquirer
|
|
||||||
.prompt([
|
if (args.length > 0) {
|
||||||
{
|
switch (args[0]) {
|
||||||
type: "input",
|
case "-h":
|
||||||
name: "location",
|
case "--help":
|
||||||
message: "Where should the project be initialized?",
|
case "help":
|
||||||
},
|
console.log(weclomeASCII);
|
||||||
{
|
console.log(helpMenu);
|
||||||
type: "list",
|
break;
|
||||||
name: "flavor",
|
case "-i":
|
||||||
message: "Which flavor?",
|
case "--interactive":
|
||||||
choices: ["TypeScript", "JavaScript"],
|
interactiveClient(true);
|
||||||
filter(val) {
|
break;
|
||||||
return val.toLowerCase();
|
case "--no-install":
|
||||||
},
|
case "-n":
|
||||||
},
|
globals.setGlobal("shouldInstall", false)
|
||||||
])
|
interactiveClient(false)
|
||||||
.then((answers) => {
|
break;
|
||||||
validateClient();
|
default:
|
||||||
mkdir(answers.location, (e) => {
|
console.log(weclomeASCII);
|
||||||
if (e && e.code != "EEXIST") {
|
process.stdout.write(" ");
|
||||||
logger.error("Failed to create project directory.");
|
logger.error("Unknown command.");
|
||||||
process.exit(1);
|
console.log(helpMenu);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (e) {
|
if (args.length == 0) {
|
||||||
if (e.code == "EEXIST") {
|
interactiveClient(true);
|
||||||
readdir(answers.location, (_, files) => {
|
}
|
||||||
if (files.length) {
|
|
||||||
logger.error("Directory not empty.");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let packageManager = getPackageManager();
|
|
||||||
|
|
||||||
const start = async (TEMPLATE_DOWNLOAD_URL: string) => {
|
|
||||||
const download = stream(TEMPLATE_DOWNLOAD_URL, { isStream: true }).pipe(
|
|
||||||
createWriteStream(`${answers.location}/create-guilded-bot_ts.zip`)
|
|
||||||
);
|
|
||||||
download.on("finish", () => {
|
|
||||||
fs.createReadStream(`${answers.location}/create-guilded-bot_ts.zip`)
|
|
||||||
.pipe(unzip.Extract({ path: `${answers.location}` }))
|
|
||||||
.on("finish", () => {
|
|
||||||
removeSync(`${answers.location}/create-guilded-bot_ts.zip`);
|
|
||||||
logger.success("🚀 Let's get started.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (answers.flavor == "typescript") {
|
|
||||||
const TEMPLATE_DOWNLOAD_URL =
|
|
||||||
"https://files.devcomp.xyz/r/create-guilded-bot_ts.zip";
|
|
||||||
|
|
||||||
start(TEMPLATE_DOWNLOAD_URL).then(() => {
|
|
||||||
install(
|
|
||||||
packageManager as "npm" | "pnpm" | "yarn" | null,
|
|
||||||
answers.location
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (answers.flavor == "javascript") {
|
|
||||||
const TEMPLATE_DOWNLOAD_URL =
|
|
||||||
"https://files.devcomp.xyz/r/create-guilded-bot_js.zip";
|
|
||||||
|
|
||||||
start(TEMPLATE_DOWNLOAD_URL).then(() => {
|
|
||||||
install(
|
|
||||||
packageManager as "npm" | "pnpm" | "yarn" | null,
|
|
||||||
answers.location
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
|
@ -20,6 +20,18 @@ export const c = {
|
||||||
welcomeMessage: gradient.rainbow.multiline(
|
welcomeMessage: gradient.rainbow.multiline(
|
||||||
" _ _ _ _ _ _ _ \n | | (_) | | | | | | | | | \n ___ _ __ ___ __ _| |_ ___ ______ __ _ _ _ _| | __| | ___ __| |______| |__ ___ | |_ \n / __| '__/ _ \\/ _` | __/ _ \\______/ _` | | | | | |/ _` |/ _ \\/ _` |______| '_ \\ / _ \\| __|\n | (__| | | __/ (_| | || __/ | (_| | |_| | | | (_| | __/ (_| | | |_) | (_) | |_ \n \\___|_| \\___|\\__,_|\\__\\___| \\__, |\\__,_|_|_|\\__,_|\\___|\\__,_| |_.__/ \\___/ \\__|\n __/ | \n |___/ \n\n"
|
" _ _ _ _ _ _ _ \n | | (_) | | | | | | | | | \n ___ _ __ ___ __ _| |_ ___ ______ __ _ _ _ _| | __| | ___ __| |______| |__ ___ | |_ \n / __| '__/ _ \\/ _` | __/ _ \\______/ _` | | | | | |/ _` |/ _ \\/ _` |______| '_ \\ / _ \\| __|\n | (__| | | __/ (_| | || __/ | (_| | |_| | | | (_| | __/ (_| | | |_) | (_) | |_ \n \\___|_| \\___|\\__,_|\\__\\___| \\__, |\\__,_|_|_|\\__,_|\\___|\\__,_| |_.__/ \\___/ \\__|\n __/ | \n |___/ \n\n"
|
||||||
),
|
),
|
||||||
|
helpMenu: `
|
||||||
|
Usage: create-guilded-bot [command] [flags]
|
||||||
|
create-guilded-bot [ -h | --help | -v | --version ]
|
||||||
|
|
||||||
|
Generate a project:
|
||||||
|
new [directory] Creates a new project in a specified directory.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--no-install, -n Does not install dependencies for the generated project.
|
||||||
|
--interactive, -i Run the interactive CLI interface
|
||||||
|
|
||||||
|
`,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
const hyphen = kleur.yellow(c.word_HyphenAscii);
|
const hyphen = kleur.yellow(c.word_HyphenAscii);
|
13
packages/cli/utils/globals.ts
Normal file
13
packages/cli/utils/globals.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
export let globalsStore = { meta: { created: new Date().getDate() + new Date().getTime() } }
|
||||||
|
export function setGlobal(global: string, value: any): void {
|
||||||
|
globalsStore[global] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getGlobal(global: string): any {
|
||||||
|
return globalsStore[global]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function getGlobals(): {} {
|
||||||
|
return globalsStore
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue