feat: stable(r) dependency installation and colorful logging

This commit is contained in:
Erica Marigold 2022-11-19 18:39:25 +05:30
parent df5ec67c3b
commit c06fa1c3a3
No known key found for this signature in database
GPG key ID: 23CD97ABBBCC5ED2
8 changed files with 182 additions and 30 deletions

View file

@ -1,44 +1,87 @@
import { spawn } from "child_process"; import { exec, spawn } from "child_process";
import { userInfo } from "os"; import { platform, userInfo } from "os";
import * as logger from "../utils/logger"
export default function install(packageManager: "npm" | "pnpm" | "yarn" | null, workingDirectory: string) { export default function install(packageManager: "npm" | "pnpm" | "yarn" | null, workingDirectory: string) {
const cmds = { const cmds = {
npm: { npm: {
win32: {
command: "npm.cmd",
args: ["install"],
full: "npm.cmd install"
},
linux: {
command: "npm.cmd",
args: ["install"],
full: "npm install"
},
darwin: {
command: "npm", command: "npm",
args: ["install"] args: ["install"],
full: "npm install"
}
}, },
pnpm: { pnpm: {
win32: {
command: "pnpm.cmd",
args: ["i"],
full: "pnpm.cmd install"
},
linux: {
command: "pnpm", command: "pnpm",
args: ["install"] args: ["i"],
full: "pnpm install"
},
darwin: {
command: "pnpm",
args: ["i"],
full: "pnpm install"
}
}, },
yarn: { yarn: {
command: "yarn", win32: {
args: [] command: "yarn.cmd",
} args: [],
} full: "yarn.cmd"
},
console.log(userInfo().shell) linux: {
command: "yarn",
args: [],
full: "yarn"
},
darwin: {
command: "yarn",
args: [],
full: "yarn"
}
}
}
switch (packageManager) { switch (packageManager) {
case null: case null:
const spawnedNPM = spawn(`${cmds.npm.command}`, cmds.npm.args, { cwd: workingDirectory, stdio: "inherit", shell: userInfo().shell }) exec("npm config set color always")
logger.info("Installing dependencies with npm.")
const spawnedNPM = spawn(`${cmds.npm[platform()].command}`, cmds.npm[platform()].args, { cwd: workingDirectory })
spawnedNPM.stdout.on("data", (out) => { spawnedNPM.stdout.on("data", (out) => {
console.log(`Installing dependencies using npm`) logger.custom("npm", out.toString().trim())
console.log(`\rnpm :: ${out}`)
}) })
case "npm" || "pnpm" || "yarn": default:
const spawnedAny = spawn(`${cmds[packageManager].command}`, cmds[packageManager].args, { cwd: workingDirectory, stdio: "inherit", shell: userInfo().shell }) logger.info(`Installing dependencies with ${packageManager}`)
const spawnedAny = spawn(`${cmds[packageManager][platform()].command}`, cmds[packageManager][platform()].args, { cwd: workingDirectory })
spawnedAny.stdout.on("data", (out) => { spawnedAny.stdout.on("data", (out) => {
console.log(`Installing dependencies using ${packageManager}`) logger.custom(packageManager, out.toString().trim())
console.log(`\r${packageManager} :: ${out}`)
}) })
spawnedAny.stderr.on("data", (err) => { spawnedAny.stderr.on("data", (err) => {
console.error(`\r${packageManager} error: ${err}`) logger.customError(packageManager, err.toString().trim())
}) })
} }
} }

View file

@ -12,6 +12,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"fs-extra": "^10.1.0",
"inquirer": "8.2.3", "inquirer": "8.2.3",
"kleur": "^4.1.5" "kleur": "^4.1.5"
}, },

View file

@ -1,9 +1,16 @@
import { mkdir, readdir, readdirSync } from "fs";
import * as inquirer from "inquirer"; import * as inquirer from "inquirer";
import getPackageManager from "../lib/getPackageManager"; import getPackageManager from "../lib/getPackageManager";
import install from "../lib/installDependencies"; import install from "../lib/installDependencies";
import * as logger from "../utils/logger"
inquirer inquirer
.prompt([ .prompt([
{
type: "input",
name: "location",
message: "Where should the project be initialized?"
},
{ {
type: 'list', type: 'list',
name: 'flavor', name: 'flavor',
@ -13,16 +20,30 @@ inquirer
return val.toLowerCase(); return val.toLowerCase();
}, },
}, },
]) ])
.then((answers) => { .then((answers) => {
console.log(JSON.stringify(answers, null, ' ')); mkdir(answers.location, (e) => {
let packageManager = getPackageManager() if (e && e.code != "EEXIST") {
logger.error("Failed to create project directory.")
if (packageManager !== "npm"||"pnpm"||"yarn") { process.exit(1)
packageManager = "npm"
} }
install(packageManager as "npm"|"pnpm"|"yarn"|null, ".") if (e) {
if (e.code == "EEXIST") {
readdir(answers.location, (_, files) => {
if (files.length) {
logger.error("Directory not empty.")
process.exit(1)
}
})
}
}
})
let packageManager = getPackageManager()
install(packageManager as "npm" | "pnpm" | "yarn" | null, answers.location)
}); });

View file

@ -0,0 +1,30 @@
import * as kleur from 'kleur';
const progressStyle = kleur.bold().gray
const successStyle = kleur.bold().green
const errorStyle = kleur.black().bold().red
const warningStyle = kleur.black().bold().yellow
export function warn(log: string) {
console.log(warningStyle(`warning`), " ", log )
}
export function error(log: string) {
console.log(errorStyle(`error`), " ", log )
}
export function info(log: string) {
console.log(successStyle(`info`), " ", log )
}
export function success(log: string) {
console.log(successStyle(`success`), " ", log )
}
export function custom(customType: string, log: string) {
console.log(progressStyle(`[${customType}] `), log)
}
export function customError(customType: string, log: string) {
console.log(errorStyle(`[${customType}] error `), log)
}

24
packages/package-lock.json generated Normal file
View file

@ -0,0 +1,24 @@
{
"name": "packages",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"dependencies": {
"async": "latest"
}
},
"node_modules/async": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz",
"integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ=="
}
},
"dependencies": {
"async": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz",
"integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ=="
}
}
}

5
packages/package.json Normal file
View file

@ -0,0 +1,5 @@
{
"dependencies": {
"async": "latest"
}
}

View file

@ -43,6 +43,7 @@ importers:
'@types/inquirer': 8.2.3 '@types/inquirer': 8.2.3
'@types/node': ^18.11.9 '@types/node': ^18.11.9
'@types/prompt': ^1.1.4 '@types/prompt': ^1.1.4
fs-extra: ^10.1.0
inquirer: 8.2.3 inquirer: 8.2.3
kleur: ^4.1.5 kleur: ^4.1.5
ts-node: ^10.9.1 ts-node: ^10.9.1
@ -51,6 +52,7 @@ importers:
tslib: ^2.4.1 tslib: ^2.4.1
typescript: ^4.9.3 typescript: ^4.9.3
dependencies: dependencies:
fs-extra: 10.1.0
inquirer: 8.2.3 inquirer: 8.2.3
kleur: 4.1.5 kleur: 4.1.5
devDependencies: devDependencies:
@ -1156,6 +1158,15 @@ packages:
resolution: {integrity: sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==} resolution: {integrity: sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==}
dev: false dev: false
/fs-extra/10.1.0:
resolution: {integrity: sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==}
engines: {node: '>=12'}
dependencies:
graceful-fs: 4.2.10
jsonfile: 6.1.0
universalify: 2.0.0
dev: false
/fs.realpath/1.0.0: /fs.realpath/1.0.0:
resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==} resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==}
@ -1250,6 +1261,10 @@ packages:
slash: 3.0.0 slash: 3.0.0
dev: false dev: false
/graceful-fs/4.2.10:
resolution: {integrity: sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==}
dev: false
/has-bigints/1.0.2: /has-bigints/1.0.2:
resolution: {integrity: sha512-tSvCKtBr9lkF0Ex0aQiP9N+OpV4zi2r/Nee5VkRDbaqv35RLYMzbwQfFSZZH0kR+Rd6302UJZ2p/bJCEoR3VoQ==} resolution: {integrity: sha512-tSvCKtBr9lkF0Ex0aQiP9N+OpV4zi2r/Nee5VkRDbaqv35RLYMzbwQfFSZZH0kR+Rd6302UJZ2p/bJCEoR3VoQ==}
dev: false dev: false
@ -1509,6 +1524,14 @@ packages:
minimist: 1.2.7 minimist: 1.2.7
dev: false dev: false
/jsonfile/6.1.0:
resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==}
dependencies:
universalify: 2.0.0
optionalDependencies:
graceful-fs: 4.2.10
dev: false
/jsx-ast-utils/3.3.3: /jsx-ast-utils/3.3.3:
resolution: {integrity: sha512-fYQHZTZ8jSfmWZ0iyzfwiU4WDX4HpHbMCZ3gPlWYiCl3BoeOTsqKBqnTVfH2rYT7eP5c3sVbeSPHnnJOaTrWiw==} resolution: {integrity: sha512-fYQHZTZ8jSfmWZ0iyzfwiU4WDX4HpHbMCZ3gPlWYiCl3BoeOTsqKBqnTVfH2rYT7eP5c3sVbeSPHnnJOaTrWiw==}
engines: {node: '>=4.0'} engines: {node: '>=4.0'}
@ -2297,6 +2320,11 @@ packages:
which-boxed-primitive: 1.0.2 which-boxed-primitive: 1.0.2
dev: false dev: false
/universalify/2.0.0:
resolution: {integrity: sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==}
engines: {node: '>= 10.0.0'}
dev: false
/uri-js/4.4.1: /uri-js/4.4.1:
resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
dependencies: dependencies: