From 65f38f3eb2b4e8e2bd136b9bd8f37f1966be1661 Mon Sep 17 00:00:00 2001 From: EvolutionX Date: Mon, 9 May 2022 20:44:48 +0530 Subject: [PATCH] feat(*): cli is now functional --- src/{commands/test.js => utilities/edits.js} | 14 +++- src/utilities/install.js | 78 ++++++++++++++++++++ src/utilities/npm.js | 21 ++++++ 3 files changed, 109 insertions(+), 4 deletions(-) rename src/{commands/test.js => utilities/edits.js} (50%) create mode 100644 src/utilities/install.js create mode 100644 src/utilities/npm.js diff --git a/src/commands/test.js b/src/utilities/edits.js similarity index 50% rename from src/commands/test.js rename to src/utilities/edits.js index 476c4ad..413d21e 100644 --- a/src/commands/test.js +++ b/src/utilities/edits.js @@ -1,16 +1,22 @@ -//@ts-check import { readFile, writeFile } from 'node:fs/promises'; import { findUp } from 'find-up'; + /** - * @param {string} name + * It takes a string, finds the package.json file in the directory of the string, and changes the name + * of the package.json file to the string. + * @param {string} name - The name of the project. + * @returns A promise. */ export async function editMain(name) { - const pjLocation = await findUp('package.json'); + const pjLocation = await findUp('package.json', { + cwd: process.cwd() + '/' + name, + }); + const output = JSON.parse(await readFile(pjLocation, 'utf8')); if (!output) throw new Error("Can't read your package.json."); - output.main = name; + output.name = name; return writeFile(pjLocation, JSON.stringify(output, null, 2)); } diff --git a/src/utilities/install.js b/src/utilities/install.js new file mode 100644 index 0000000..d25d0bb --- /dev/null +++ b/src/utilities/install.js @@ -0,0 +1,78 @@ +import { execa } from 'execa'; +import { redBright } from 'colorette'; +import fs from 'fs'; +import path from 'path'; +import { readFile } from 'fs/promises'; +import { findUp } from 'find-up'; +import ora from 'ora'; + +/** + * It installs dependencies from a package.json file + * @param choice - The package manager to use. + * @param name - The name of the project + * @returns a promise. + */ +export async function installDeps(choice, name) { + const pkg = await findUp('package.json', { + cwd: process.cwd() + '/' + name, + }); + if (!pkg) throw new Error('No package.json found!'); + const output = JSON.parse(await readFile(pkg, 'utf8')); + if (!output) throw new Error("Can't read file."); + const deps = output.dependencies; + if (!deps) throw new Error("Can't find dependencies."); + const spin = ora({ + text: `Installing dependencies...`, + spinner: 'aesthetic', + }).start(); + const result = await execa(choice, ['install'], { + cwd: process.cwd() + '/' + name, + }).catch(() => null); + if (!result || result?.failed) { + spin.fail(`${redBright('Failed')} to install dependencies!`); + return process.exit(1); + } else spin.succeed(`Dependencies installed!`); +} + +/** + * Clone the repo, copy the files from the repo to the new project directory, and delete the repo + * @param {string} lang - The language of the template + * @param {string} name - The name of the project + */ +export async function cloneRepo(lang, name) { + await execa('git', [ + 'clone', + `https://github.com/sern-handler/templates.git`, // ? See the idea of @Allyedge having templates built in cli + ]); + copyRecursiveSync(`templates/templates/${lang}`, name); + fs.rmSync(`templates/`, { recursive: true, force: true }); +} + +/** + * If the source is a directory, create the destination directory and then recursively copy the + * contents of the source directory to the destination directory. If the source is not a directory, + * copy the source file to the destination file + * @param {string} src - The source path. + * @param {string} dest - The destination folder where the files will be copied to. + */ +function copyRecursiveSync(src, dest) { + var exists = fs.existsSync(src); + var stats = exists && fs.statSync(src); + var isDirectory = exists && stats.isDirectory(); + if (isDirectory) { + fs.mkdirSync(dest); + fs.readdirSync(src).forEach(function (childItemName) { + copyRecursiveSync( + path.join(src, childItemName), + path.join(dest, childItemName) + ); + }); + } else { + fs.copyFileSync(src, dest); + } +} + +// async function wait(ms) { +// const wait = (await import('util')).promisify(setTimeout); +// return wait(ms); +// } diff --git a/src/utilities/npm.js b/src/utilities/npm.js new file mode 100644 index 0000000..2f73a6c --- /dev/null +++ b/src/utilities/npm.js @@ -0,0 +1,21 @@ +import { execa } from 'execa'; + +/** + * It checks if you have npm or yarn installed, and returns a string based on the result + * @returns A promise that resolves to a string. + */ +export async function npm() { + const npm = await execa('npm', ['-v']); + const npm_version = npm?.stdout; + const yarn = await execa('yarn', ['-v']); + const yarn_version = yarn?.stdout; + if (npm_version && !yarn_version) { + return 'npm'; + } + if (!npm_version && yarn_version) { + return 'yarn'; + } + if (npm_version && yarn_version) { + return 'both'; + } +}