Skip to content

Commit 184c116

Browse files
authored
Merge pull request #31 from codingtools/feature/bundlephobia-package.json
[BUNDLEPHOBIA]: package.json support done
2 parents 23cafb3 + 512c13a commit 184c116

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/commands/bundlephobia.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import axios from 'axios'
33
import chalk from 'chalk'
44

55
import Logger from '../utilities/logger'
6+
import Utilities from '../utilities/utilities'
67

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

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

24-
private static getPackages(flags: any, args: any) {
25-
let packages = []
26-
27-
if (args.package)
28-
packages.push(args.package)
29-
if (flags.packages)
30-
packages = packages.concat(flags.packages) // not inplace operation
31-
return packages
32-
}
33-
3426
private static getErrorMessage(pkg: string, message: string) {
3527
// replacing will be useful when we do not have specific version
3628
// output will be like below
@@ -57,12 +49,29 @@ export default class Bundlephobia extends Command {
5749
async run() {
5850
const {args, flags} = this.parse(Bundlephobia)
5951

60-
args.packages = Bundlephobia.getPackages(flags, args) // get a list
52+
args.packages = this.getPackages(flags, args) // get a list
6153

54+
Logger.info(this, `running bundlephobia for ${args.packages.length} packages`)
6255
this.checkParameters(flags, args)
6356
this.bundlePhobia(flags, args)
6457
}
6558

59+
private getPackages(flags: any, args: any) {
60+
let packages = []
61+
62+
if (args.package)
63+
packages.push(args.package)
64+
if (flags.packages)
65+
packages = packages.concat(flags.packages) // not inplace operation
66+
67+
// package.json file passed
68+
if (flags.file) {
69+
let jsonObject = Utilities.getJsonObjectFromFile(this, flags.file)
70+
packages = packages.concat(this.convertObjectToArray(jsonObject.dependencies))
71+
}
72+
return packages
73+
}
74+
6675
// tslint:disable-next-line:no-unused
6776
private checkParameters(flags: unknown, args: any) {
6877
if (args.packages.length === 0)
@@ -113,11 +122,16 @@ export default class Bundlephobia extends Command {
113122
}
114123

115124
private getFinalMessage(data: any) {
116-
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)`
125+
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)`
117126
}
118127

119128
private getSuccessMessage(data: any) {
120129
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)`
121130
}
122131

132+
private convertObjectToArray(jsobObject: any) {
133+
return Object.keys(jsobObject).map(key => {
134+
return `${key}@${jsobObject[key]}`
135+
})
136+
}
123137
}

src/utilities/utilities.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,17 @@ export default class Utilities {
1818
}
1919
return fileStr
2020
}
21+
public static getJsonObjectFromFile(thisRef: any, filePath: string) {
22+
if (!fs.existsSync(filePath)) {
23+
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
24+
} else {
25+
let jsonString = fs.readFileSync(filePath, 'utf8')
26+
try {
27+
return JSON.parse(jsonString)
28+
} catch (err) {
29+
Logger.error(this, 'Error parsing JSON string:' + err)
30+
}
31+
}
32+
}
33+
2134
}

0 commit comments

Comments
 (0)