Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/commands/bundlephobia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios'
import chalk from 'chalk'

import Logger from '../utilities/logger'
import Utilities from '../utilities/utilities'

// TODO:
// ADD package.json support
Expand All @@ -17,20 +18,11 @@ export default class Bundlephobia extends Command {
description: 'packages for which cost is required, can pass more than one separated by space',
multiple: true // can get multiple package names
}),
file: flags.string({char: 'f', description: 'path for package.json file'}),
}

static args = [{name: 'package'}] // only one can be passed club which one passed through flag and arg

private static getPackages(flags: any, args: any) {
let packages = []

if (args.package)
packages.push(args.package)
if (flags.packages)
packages = packages.concat(flags.packages) // not inplace operation
return packages
}

private static getErrorMessage(pkg: string, message: string) {
// replacing will be useful when we do not have specific version
// output will be like below
Expand All @@ -57,12 +49,29 @@ export default class Bundlephobia extends Command {
async run() {
const {args, flags} = this.parse(Bundlephobia)

args.packages = Bundlephobia.getPackages(flags, args) // get a list
args.packages = this.getPackages(flags, args) // get a list

Logger.info(this, `running bundlephobia for ${args.packages.length} packages`)
this.checkParameters(flags, args)
this.bundlePhobia(flags, args)
}

private getPackages(flags: any, args: any) {
let packages = []

if (args.package)
packages.push(args.package)
if (flags.packages)
packages = packages.concat(flags.packages) // not inplace operation

// package.json file passed
if (flags.file) {
let jsonObject = Utilities.getJsonObjectFromFile(this, flags.file)
packages = packages.concat(this.convertObjectToArray(jsonObject.dependencies))
}
return packages
}

// tslint:disable-next-line:no-unused
private checkParameters(flags: unknown, args: any) {
if (args.packages.length === 0)
Expand Down Expand Up @@ -113,11 +122,16 @@ export default class Bundlephobia extends Command {
}

private getFinalMessage(data: any) {
return `${chalk.magenta('Total')} [${chalk.cyan(data.count + ' packages resolved')}] has ${data.dependencyCount} dependencies with size of ${Bundlephobia.getSize(data.size)}(${Bundlephobia.getSize(data.gzip)} gzipped)`
return `\n${chalk.magenta('Total')} [${chalk.cyan(data.count + ' packages resolved')}] has ${data.dependencyCount} dependencies with size of ${Bundlephobia.getSize(data.size)}(${Bundlephobia.getSize(data.gzip)} gzipped)`
}

private getSuccessMessage(data: any) {
return `${chalk.magenta(data.name)}@${chalk.cyan(data.version)} has ${data.dependencyCount} dependencies with size of ${Bundlephobia.getSize(data.size)}(${Bundlephobia.getSize(data.gzip)} gzipped)`
}

private convertObjectToArray(jsobObject: any) {
return Object.keys(jsobObject).map(key => {
return `${key}@${jsobObject[key]}`
})
}
}
13 changes: 13 additions & 0 deletions src/utilities/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ export default class Utilities {
}
return fileStr
}
public static getJsonObjectFromFile(thisRef: any, filePath: string) {
if (!fs.existsSync(filePath)) {
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
} else {
let jsonString = fs.readFileSync(filePath, 'utf8')
try {
return JSON.parse(jsonString)
} catch (err) {
Logger.error(this, 'Error parsing JSON string:' + err)
}
}
}

}