Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 027f7d4

Browse files
author
Roman Labunsky
committed
feat: changed structure
1 parent 620a405 commit 027f7d4

4 files changed

Lines changed: 27 additions & 70 deletions

File tree

lib/cli-renderer.js

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@ const score = require('./score')
44

55
const SOURCE_PACKAGE_TYPE = 'sourcePackage'
66
const ALTERNATIVE_PACKAGE_TYPE = 'alternativePackage'
7-
const PACKAGE_DISPLAY_HEADERS = [
8-
'Package Name',
9-
'Smart Version',
10-
'License',
11-
'Description',
12-
'My Usage',
13-
'Score'
14-
]
7+
const PACKAGE_DISPLAY_HEADERS = ['Package Name', 'Smart Version', 'License', 'Description', 'My Usage', 'Score']
158
const VERSION_DISPLAY_HEADERS = [
169
'Package Name',
1710
'package.json Version',
@@ -73,7 +66,7 @@ class Renderer {
7366
`${style(data.recommendedVersion || 'No Insight')}`,
7467
`${style(this.trimStringToLength(data.license, 4) || '')}`,
7568
`${style(this.trimStringToLength(data.description, 20) || '')}`,
76-
`${style(this.fractionToPercentageString(data.usage) || 'No Insight')}`
69+
`${style(this.fractionToPercentageString(data.internalUsage.usage) || 'No Insight')}`
7770
]
7871
if (hasHigherScore) style = style.bold.green
7972
dataSet.push(`${style(Number(data.calculatedScore) || '')}`)
@@ -94,8 +87,7 @@ class Renderer {
9487

9588
getScore(pkgData, weights) {
9689
let calculatedScore
97-
if (Object.keys(weights).length > 0)
98-
calculatedScore = score.calculatePackageScore(pkgData, weights, true)
90+
if (Object.keys(weights).length > 0) calculatedScore = score.calculatePackageScore(pkgData, weights, true)
9991
else calculatedScore = pkgData.score.final
10092
return (Number(calculatedScore) * 100).toFixed()
10193
}
@@ -118,35 +110,20 @@ class Renderer {
118110
alternative.calculatedScore = scores[alternative.name]
119111
}
120112

121-
const highestScorePackageName = Object.keys(scores).sort(
122-
(a, b) => scores[b] - scores[a]
123-
)[0]
113+
const highestScorePackageName = Object.keys(scores).sort((a, b) => scores[b] - scores[a])[0]
124114
this.logger.info(highestScorePackageName)
125-
this.queue.push(
126-
`Alternative Packages for ${chalk.bold.cyan(pkgSet.source.name)}`
127-
)
115+
this.queue.push(`Alternative Packages for ${chalk.bold.cyan(pkgSet.source.name)}`)
128116
const table = this.createTableWithHeders(PACKAGE_DISPLAY_HEADERS)
129117

130-
if (idx < data.length)
131-
table.push(this.createSeparatorRow(PACKAGE_DISPLAY_HEADERS.length))
118+
if (idx < data.length) table.push(this.createSeparatorRow(PACKAGE_DISPLAY_HEADERS.length))
132119
table.push(
133-
this.createPackageRow(
134-
pkgSet.source,
135-
SOURCE_PACKAGE_TYPE,
136-
pkgSet.source.name === highestScorePackageName
137-
)
138-
)
139-
pkgSet.alternatives = pkgSet.alternatives.sort(
140-
(a, b) => b.calculatedScore - a.calculatedScore
120+
this.createPackageRow(pkgSet.source, SOURCE_PACKAGE_TYPE, pkgSet.source.name === highestScorePackageName)
141121
)
122+
pkgSet.alternatives = pkgSet.alternatives.sort((a, b) => b.calculatedScore - a.calculatedScore)
142123

143124
for (const alternative of pkgSet.alternatives) {
144125
table.push(
145-
this.createPackageRow(
146-
alternative,
147-
ALTERNATIVE_PACKAGE_TYPE,
148-
alternative.name === highestScorePackageName
149-
)
126+
this.createPackageRow(alternative, ALTERNATIVE_PACKAGE_TYPE, alternative.name === highestScorePackageName)
150127
)
151128
}
152129

@@ -187,9 +164,7 @@ class Renderer {
187164

188165
renderLegend(packageList) {
189166
if (packageList.some(pkg => pkg.source.deprecated))
190-
this.queue.push(
191-
`${chalk.bgRed.bold(' ')} - ${chalk('Package is deprecated')}`
192-
)
167+
this.queue.push(`${chalk.bgRed.bold(' ')} - ${chalk('Package is deprecated')}`)
193168
}
194169

195170
renderLink(linkType, data) {
@@ -202,22 +177,14 @@ class Renderer {
202177
let packages = [pkgSet.source.name]
203178
if (pkgSet.alternatives && pkgSet.alternatives.length > 0) {
204179
packages = packages.concat(pkgSet.alternatives.map(alt => alt.name))
205-
this.queue.push(
206-
chalk`{blue.bold https://platform.datree.io/pkg/alternative-packages/${packages.join(
207-
','
208-
)}}`
209-
)
180+
this.queue.push(chalk`{blue.bold https://platform.datree.io/pkg/alternative-packages/${packages.join(',')}}`)
210181
}
211182
}
212183

213184
_renderSinglePackageLinks(packageList) {
214185
for (const pkg of packageList)
215186
if (pkg.source.deprecated)
216-
this.queue.push(
217-
chalk`{blue.bold https://platform.datree.io/pkg/single-package/${
218-
pkg.source.name
219-
}}`
220-
)
187+
this.queue.push(chalk`{blue.bold https://platform.datree.io/pkg/single-package/${pkg.source.name}}`)
221188
}
222189
}
223190

lib/insight.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const loggerFactory = require('./loggerFactory')
88
const utils = require('./utils')
99

1010
const logger = loggerFactory.get('insight')
11-
const BASE_URL = 'https://gateway.datree.io'
11+
const BASE_URL = 'http://localhost:8000'
1212

1313
program
1414
.option('-e, --event <event>', 'Event type')
@@ -73,7 +73,8 @@ const main = async function() {
7373
case 'insight':
7474
const insight = await getInsights(data, token)
7575
const userWeights = await weights.getWeights(token)
76-
console.log(JSON.stringify({ insight, userWeights }))
76+
const res = JSON.stringify({ insight, userWeights })
77+
process.stdout.write(res)
7778
break
7879
default:
7980
logger.error(`Unknown event ${program.event}`)
@@ -83,11 +84,11 @@ const main = async function() {
8384
if (require.main === module) {
8485
main()
8586
.then(_ => {
86-
process.exit(0)
87+
process.exitCode = 0
8788
})
8889
.catch(err => {
8990
logger.error(err)
90-
process.exit(1)
91+
process.exitCode = 1
9192
})
9293
} else {
9394
module.exports = {

lib/main.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ async function main() {
2222
// Assign argv
2323
info.argv = process.argv
2424
// Get full action
25-
info.full_action = [info.action].concat(
26-
argumentsExtractor.extractActionArgs()
27-
)
25+
info.full_action = [info.action].concat(argumentsExtractor.extractActionArgs())
2826

2927
// If in npm install - parse installed packages
3028
if (info.action === 'install' || info.action === 'i') {
@@ -40,9 +38,7 @@ async function main() {
4038
`${data}`
4139
])
4240

43-
const insightsResponse = JSON.parse(
44-
insightsProcess.stdout.toString('utf8')
45-
)
41+
const insightsResponse = JSON.parse(insightsProcess.stdout.toString('utf8'))
4642
const insight = insightsResponse.insight
4743

4844
if (insight && insight.length > 0) {
@@ -61,10 +57,7 @@ async function main() {
6157
} else {
6258
installType = 'single'
6359
insight.forEach(pkgSet => {
64-
const pkgJsonVersion = node.getVersionFromDependencies(
65-
info.packages,
66-
pkgSet.source.name
67-
)
60+
const pkgJsonVersion = node.getVersionFromDependencies(info.packages, pkgSet.source.name)
6861
pkgSet.source.pkgJsonVersion = pkgJsonVersion
6962
})
7063
renderer.renderVersionsTable({

package.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
"description": "datree.io Node.js module",
55
"main": "dist/index.js",
66
"scripts": {
7-
"test":
8-
"nyc ./node_modules/mocha/bin/mocha --require babel-core/register test/unit && nyc report --reporter=text-lcov | coveralls",
9-
"test-html":
10-
"nyc --reporter=html --reporter=text ./node_modules/mocha/bin/mocha --require babel-core/register test/unit && open coverage/index.html",
7+
"test": "nyc ./node_modules/mocha/bin/mocha --require babel-core/register test/unit && nyc report --reporter=text-lcov | coveralls",
8+
"test-html": "nyc --reporter=html --reporter=text ./node_modules/mocha/bin/mocha --require babel-core/register test/unit && open coverage/index.html",
119
"test-integration": "./test/integration/test.sh",
1210
"clean": "rm -rf dist",
13-
"build":
14-
"npm run clean && mkdir dist && babel index.js lib/* -s -d dist && cp package.json dist/",
15-
"semantic-release":
16-
"semantic-release pre && npm publish && semantic-release post",
17-
"semantic-release-local":
18-
"semantic-release pre --debug=false && npm publish && semantic-release post --debug=false",
11+
"build": "npm run clean && mkdir dist && babel index.js lib/* -s -d dist && cp package.json dist/",
12+
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
13+
"semantic-release-local": "semantic-release pre --debug=false && npm publish && semantic-release post --debug=false",
1914
"postinstall": "node dist/lib/install.js",
2015
"preuninstall": "node dist/lib/uninstall.js"
2116
},
@@ -61,6 +56,7 @@
6156
},
6257
"prettier": {
6358
"semi": false,
64-
"singleQuote": true
59+
"singleQuote": true,
60+
"printWidth": 120
6561
}
6662
}

0 commit comments

Comments
 (0)