From ad2dd3b422dcc1699213191b36545d5baaa95372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CBugs5382=E2=80=9D?= Date: Fri, 29 Dec 2023 23:30:07 -0500 Subject: [PATCH] feat: added in cli progress bar --- src/npm.ts | 19 +++++++++++++++++-- src/template.ts | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/npm.ts b/src/npm.ts index 8219ba2..43a614a 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -1,5 +1,6 @@ import childProcess from 'node:child_process' import { promisify } from 'node:util' +import cliProgress from 'cli-progress' import { GeneratePackageJsonInputWithOptions, GeneratePackageJsonParams @@ -145,7 +146,21 @@ export const installDeps = async (dependencies: string[], options: { dev?: boole args.push('--save-dev') } - if (typeof process.env.NODE_ENV === 'undefined') { - await execFile('npm', [...args, ...dependencies]) + if (dependencies.length > 0) { + const bar = new cliProgress.SingleBar({}, cliProgress.Presets.rect) + bar.start(dependencies.length, 0) + + let value = 0 + + for (const depend of dependencies) { + value++ + if (typeof process.env.NODE_ENV === 'undefined') { + await execFile('npm', [...args, depend]) + } + bar.update(value) + if (value >= bar.getTotal()) { + bar.stop() + } + } } } diff --git a/src/template.ts b/src/template.ts index f5861b6..dc11954 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,3 +1,4 @@ +import cliProgress from 'cli-progress' import fs from 'fs' import path from 'node:path' import { fileURLToPath } from 'node:url' @@ -26,6 +27,16 @@ const recurseDir = async (dir: string): Promise => { return result } +/** + * Count Occurrences + * @since 1.2.1 + * @param array + * @param value + */ +const getOccurrence = (array: any, value: any): number => { + return array.filter((v: any) => (v.includes(value) === true)).length +} + /** * Copy all files from a given source folder into the target folder. * @description This will recurse into subfolders. @@ -45,9 +56,18 @@ const copyTemplateFiles = async ( const resolvedSource = path.resolve(source) const templateFileNames = await recurseDir(resolvedSource) + const totalOfGitKeep = getOccurrence(templateFileNames, '.keepfolder') + + const bar = new cliProgress.SingleBar({}, cliProgress.Presets.rect) + bar.start(templateFileNames.length - totalOfGitKeep, 0) + + let value = 0 + const filesAdded: string[] = [] for (const file of templateFileNames) { + value++ + let contents = fs.readFileSync(file, { encoding: 'utf8' }) // Figure out where we're writing this file. @@ -68,6 +88,10 @@ const copyTemplateFiles = async ( if (!destFile.includes('.keepfolder')) { fs.writeFileSync(destFile, contents) filesAdded.push(baseFile) + bar.update(value) + if (value >= bar.getTotal()) { + bar.stop() + } } }