Skip to content

Commit b291e92

Browse files
committed
feat: add option to fetch support information
- add option to fetch support information from github repository when requested. Signed-off-by: Michael Dawson <[email protected]>
1 parent 2a14d23 commit b291e92

File tree

11 files changed

+91
-8
lines changed

11 files changed

+91
-8
lines changed

index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const fs = require('fs')
44
const normalizeUrl = require('normalize-url')
55
const Ajv = require('ajv')
6+
const got = require('got')
67

78
let ajv
89
let compiledSchema
@@ -59,7 +60,23 @@ module.exports.sanitizePath = (basePath, inputPath, basePathOverride) => {
5960
return ret
6061
}
6162

62-
module.exports.getSupportData = (pkg, pkgPath, preferCanonical, basePathOverride) => {
63+
async function resolveSupportInfo (supportInfo, fetchCanonical) {
64+
supportInfo.resolved = false
65+
if (fetchCanonical) {
66+
const url = normalizeUrl(supportInfo.url
67+
.replace('blob', '')
68+
.replace('github.com', 'raw.githubusercontent.com'))
69+
try {
70+
const result = await got(url)
71+
supportInfo.contents = result.body
72+
supportInfo.resolved = true
73+
} catch (e) {
74+
supportInfo.error = e
75+
}
76+
}
77+
}
78+
79+
module.exports.getSupportData = async (pkg, pkgPath, preferCanonical, basePathOverride, fetchCanonical) => {
6380
const supportInfo = { contents: 'unknown', url: '', resolved: true }
6481
if (pkg.support) {
6582
if ((pkg.support === true) || (typeof pkg.support === 'string')) {
@@ -68,14 +85,15 @@ module.exports.getSupportData = (pkg, pkgPath, preferCanonical, basePathOverride
6885
const localPath = path.join(pkgPath, supportInfoName)
6986
if (!preferCanonical && fs.existsSync(localPath)) {
7087
supportInfo.contents = fs.readFileSync(localPath)
88+
supportInfo.resolved = true
7189
} else {
7290
supportInfo.url = module.exports.getRemoteSupportInfoUrl(pkg.repository, supportInfoName)
73-
supportInfo.resolved = false
91+
await resolveSupportInfo(supportInfo, fetchCanonical)
7492
}
7593
} else if (typeof pkg.support === 'object') {
7694
const supportInfoName = pkg.support.path ? pkg.support.path : module.exports.defaultPackageSupport
7795
supportInfo.url = module.exports.getRemoteSupportInfoUrl(pkg.support.repository, supportInfoName)
78-
supportInfo.resolved = false
96+
await resolveSupportInfo(supportInfo, fetchCanonical)
7997
}
8098
}
8199
return supportInfo

lib/cli/commands/show.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ const support = require('../../../index.js')
33

44
// print out the information for a module and its
55
// children
6-
function printModule (module, depth, argv) {
6+
async function printModule (module, depth, argv) {
77
const pkg = module.package
88
const moduleInfo = `${pkg.name}(${pkg.version})`
99

10-
const supportInfo = support.getSupportData(pkg, module.path, argv.canonical, argv['base-path'])
10+
const supportInfo = await support.getSupportData(pkg,
11+
module.path,
12+
argv.canonical,
13+
argv['base-path'],
14+
argv.fetch)
1115
if (supportInfo.resolved) {
1216
console.log(`${' '.repeat(2 * (depth + 1))}${moduleInfo} - ${supportInfo.contents}`)
17+
} else if (supportInfo.error) {
18+
console.log(`${' '.repeat(2 * (depth + 1))}${moduleInfo} - failed to fetch - ${supportInfo.url}`)
1319
} else {
1420
console.log(`${' '.repeat(2 * (depth + 1))}${moduleInfo} - ${supportInfo.url}`)
1521
}
@@ -31,7 +37,9 @@ module.exports = function (opts) {
3137
const Arborist = require('@npmcli/arborist')
3238
const arb = new Arborist()
3339
arb.loadActual().then(root => {
34-
printModule(root, 0, argv)
40+
printModule(root, 0, argv).then(() => {}, (e) => {
41+
argv.log.error('show failed: ', e)
42+
})
3543
})
3644
}
3745
}

lib/cli/commands/validate.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ module.exports = function (opts) {
2525

2626
let supportData = userPackageJson.support
2727
if (supportData === true) {
28-
supportData = support.getSupportData(userPackageJson, path.join(process.cwd()), argv.canonical, argv['base-path'])
28+
supportData = await support.getSupportData(userPackageJson,
29+
path.join(process.cwd()),
30+
argv.canonical,
31+
argv['base-path'],
32+
false)
2933
if (supportData.resolved) {
3034
const supportDataJSON = JSON.parse(supportData.contents)
3135
try {

lib/cli/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ module.exports = (options = {}) => {
6161
type: 'boolean',
6262
group: 'preferences'
6363
})
64+
.option('fetch', {
65+
describe: 'Fetch canonical support info from remote repos when necessary',
66+
type: 'boolean',
67+
group: 'preferences'
68+
})
6469
.option('base-path', {
6570
describe: 'Allow use of support info files up to base-path',
6671
type: 'string',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"dependencies": {
2727
"@npmcli/arborist": "0.0.0-pre.19",
2828
"ajv": "^6.11.0",
29-
"contains-path": "^1.0.0",
29+
"got": "^11.1.3",
3030
"loggerr": "^3.0.0-0",
3131
"normalize-url": "^5.0.0",
3232
"yargs": "^15.0.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
show --fetch
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@pkgjs/support-separate-repo(0.0.1) - failed to fetch - https://github.com/pkgjs/support/blob/master/examples/does-not-exist.json
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@pkgjs/support-separate-repo",
3+
"version": "0.0.1",
4+
"support": {
5+
"path": "./examples/does-not-exist.json",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/pkgjs/support.git"
9+
}
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/pkgjs/support-separate-repo.git"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
show --fetch
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@pkgjs/support-separate-repo(0.0.1) - {
2+
"versions": [{
3+
"version": "*",
4+
"target": {
5+
"node": "lts"
6+
},
7+
"response": {
8+
"type": "best-effort"
9+
},
10+
"backing": {
11+
"hobby": "https://github.com/pkgjs/support"
12+
}
13+
}]
14+
}
15+

0 commit comments

Comments
 (0)