Skip to content

Commit

Permalink
feat: added in cli progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Bugs5382 committed Dec 30, 2023
1 parent 0c64c4c commit ad2dd3b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import childProcess from 'node:child_process'
import { promisify } from 'node:util'
import cliProgress from 'cli-progress'
import {
GeneratePackageJsonInputWithOptions,
GeneratePackageJsonParams
Expand Down Expand Up @@ -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()
}
}
}
}
24 changes: 24 additions & 0 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cliProgress from 'cli-progress'
import fs from 'fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
Expand Down Expand Up @@ -26,6 +27,16 @@ const recurseDir = async (dir: string): Promise<string[]> => {
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.
Expand All @@ -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.
Expand All @@ -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()
}
}
}

Expand Down

0 comments on commit ad2dd3b

Please sign in to comment.