|
1 |
| -'use strict' |
2 |
| -const path = require('path') |
3 |
| -const fs = require('fs') |
4 |
| -const normalizeUrl = require('normalize-url') |
5 |
| -const Ajv = require('ajv') |
6 |
| -const got = require('got') |
7 |
| -const betterAjvErrors = require('better-ajv-errors') |
| 1 | +'use strict'; |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const normalizeUrl = require('normalize-url'); |
| 5 | +const Ajv = require('ajv'); |
| 6 | +const got = require('got'); |
| 7 | +const betterAjvErrors = require('better-ajv-errors'); |
8 | 8 |
|
9 |
| -let ajv |
10 |
| -let compiledSchema |
11 |
| -let ajvElement |
12 |
| -let compiledSchemaElement |
| 9 | +let ajv; |
| 10 | +let compiledSchema; |
| 11 | +let ajvElement; |
| 12 | +let compiledSchemaElement; |
13 | 13 |
|
14 |
| -const schema = module.exports.schema = require('./schema.json') |
15 |
| -const supportElementSchema = module.exports.schema = require('./support-element-schema.json') |
| 14 | +const schema = module.exports.schema = require('./schema.json'); |
| 15 | +const supportElementSchema = module.exports.schema = require('./support-element-schema.json'); |
16 | 16 |
|
17 |
| -module.exports.defaultPackageSupport = 'package-support.json' |
| 17 | +module.exports.defaultPackageSupport = 'package-support.json'; |
18 | 18 |
|
19 | 19 | function getBetterAjvErrorsOptions (cli) {
|
20 |
| - const options = { format: 'js' } |
| 20 | + const options = { format: 'js' }; |
21 | 21 | if (cli === true) {
|
22 |
| - options.format = 'cli' |
| 22 | + options.format = 'cli'; |
23 | 23 | }
|
24 |
| - return options |
| 24 | + return options; |
25 | 25 | }
|
26 | 26 |
|
27 | 27 | module.exports.validateSupportElement = (obj, cli = false) => {
|
28 | 28 | if (!ajvElement) {
|
29 |
| - ajvElement = new Ajv({ jsonPointers: true }) |
30 |
| - compiledSchemaElement = ajvElement.compile(supportElementSchema) |
| 29 | + ajvElement = new Ajv({ jsonPointers: true }); |
| 30 | + compiledSchemaElement = ajvElement.compile(supportElementSchema); |
31 | 31 | }
|
32 | 32 |
|
33 |
| - const validates = compiledSchemaElement(obj) |
| 33 | + const validates = compiledSchemaElement(obj); |
34 | 34 | if (!validates) {
|
35 |
| - const err = new Error('Validation Failed') |
36 |
| - err.validationErrors = compiledSchemaElement.errors |
37 |
| - err.prettyValidationErrors = betterAjvErrors(supportElementSchema, obj, compiledSchemaElement.errors, getBetterAjvErrorsOptions(cli)) |
38 |
| - err.validationSchema = compiledSchemaElement.schema |
39 |
| - throw err |
| 35 | + const err = new Error('Validation Failed'); |
| 36 | + err.validationErrors = compiledSchemaElement.errors; |
| 37 | + err.prettyValidationErrors = betterAjvErrors(supportElementSchema, obj, compiledSchemaElement.errors, getBetterAjvErrorsOptions(cli)); |
| 38 | + err.validationSchema = compiledSchemaElement.schema; |
| 39 | + throw err; |
40 | 40 | }
|
41 |
| - return true |
42 |
| -} |
| 41 | + return true; |
| 42 | +}; |
43 | 43 |
|
44 | 44 | module.exports.validate = (obj, cli = false) => {
|
45 | 45 | if (!ajv) {
|
46 |
| - ajv = new Ajv({ jsonPointers: true }) |
47 |
| - compiledSchema = ajv.compile(schema) |
| 46 | + ajv = new Ajv({ jsonPointers: true }); |
| 47 | + compiledSchema = ajv.compile(schema); |
48 | 48 | }
|
49 | 49 |
|
50 |
| - const validates = compiledSchema(obj) |
| 50 | + const validates = compiledSchema(obj); |
51 | 51 | if (!validates) {
|
52 |
| - const err = new Error('Validation Failed') |
53 |
| - err.validationErrors = compiledSchema.errors |
54 |
| - err.prettyValidationErrors = betterAjvErrors(schema, obj, compiledSchema.errors, getBetterAjvErrorsOptions(cli)) |
55 |
| - err.validationSchema = compiledSchema.schema |
56 |
| - throw err |
| 52 | + const err = new Error('Validation Failed'); |
| 53 | + err.validationErrors = compiledSchema.errors; |
| 54 | + err.prettyValidationErrors = betterAjvErrors(schema, obj, compiledSchema.errors, getBetterAjvErrorsOptions(cli)); |
| 55 | + err.validationSchema = compiledSchema.schema; |
| 56 | + throw err; |
57 | 57 | }
|
58 |
| - return true |
59 |
| -} |
| 58 | + return true; |
| 59 | +}; |
60 | 60 |
|
61 | 61 | // extract the URL for the support info from the pkg info
|
62 | 62 | module.exports.getRemoteSupportInfoUrl = (repository, supportPath) => {
|
63 |
| - const gitHubInitialPath = '/blob/master/' |
| 63 | + const gitHubInitialPath = '/blob/master/'; |
64 | 64 |
|
65 | 65 | if (repository && (repository.type === 'git')) {
|
66 |
| - const directory = repository.directory || '' |
67 |
| - let remoteURL = repository.url |
68 |
| - const trailerLocation = remoteURL.indexOf('.git') |
| 66 | + const directory = repository.directory || ''; |
| 67 | + let remoteURL = repository.url; |
| 68 | + const trailerLocation = remoteURL.indexOf('.git'); |
69 | 69 | if (trailerLocation >= 0) {
|
70 | 70 | remoteURL = remoteURL.substring(0, trailerLocation) +
|
71 | 71 | gitHubInitialPath + '/' + directory +
|
72 |
| - '/' + supportPath |
| 72 | + '/' + supportPath; |
73 | 73 | }
|
74 |
| - return normalizeUrl(remoteURL) |
| 74 | + return normalizeUrl(remoteURL); |
75 | 75 | } else {
|
76 |
| - return 'unknown' |
| 76 | + return 'unknown'; |
77 | 77 | }
|
78 |
| -} |
| 78 | +}; |
79 | 79 |
|
80 | 80 | // the path should not try to escape the root as it
|
81 | 81 | // is specified to be a relative path
|
82 | 82 | module.exports.sanitizePath = (basePath, inputPath, basePathOverride) => {
|
83 |
| - const effectiveBasePath = path.normalize(basePathOverride || basePath) |
84 |
| - const candidatePath = path.join(basePath, inputPath) |
85 |
| - let ret = path.basename(inputPath) |
| 83 | + const effectiveBasePath = path.normalize(basePathOverride || basePath); |
| 84 | + const candidatePath = path.join(basePath, inputPath); |
| 85 | + let ret = path.basename(inputPath); |
86 | 86 | if (candidatePath.indexOf(effectiveBasePath) === 0) {
|
87 | 87 | // inputPath did not try to escape
|
88 |
| - ret = inputPath |
| 88 | + ret = inputPath; |
89 | 89 | }
|
90 |
| - return ret |
91 |
| -} |
| 90 | + return ret; |
| 91 | +}; |
92 | 92 |
|
93 | 93 | async function resolveSupportInfo (supportInfo, fetchCanonical) {
|
94 |
| - supportInfo.resolved = false |
| 94 | + supportInfo.resolved = false; |
95 | 95 | if (fetchCanonical) {
|
96 | 96 | const url = normalizeUrl(supportInfo.url
|
97 | 97 | .replace('blob', '')
|
98 |
| - .replace('github.com', 'raw.githubusercontent.com')) |
| 98 | + .replace('github.com', 'raw.githubusercontent.com')); |
99 | 99 | try {
|
100 |
| - const result = await got(url) |
101 |
| - supportInfo.contents = result.body |
102 |
| - supportInfo.resolved = true |
| 100 | + const result = await got(url); |
| 101 | + supportInfo.contents = result.body; |
| 102 | + supportInfo.resolved = true; |
103 | 103 | } catch (e) {
|
104 |
| - supportInfo.error = e |
| 104 | + supportInfo.error = e; |
105 | 105 | }
|
106 | 106 | }
|
107 | 107 | }
|
108 | 108 |
|
109 | 109 | module.exports.getSupportData = async (pkg, pkgPath, preferCanonical, basePathOverride, fetchCanonical) => {
|
110 |
| - const supportInfo = { contents: 'unknown', url: '', resolved: true } |
| 110 | + const supportInfo = { contents: 'unknown', url: '', resolved: true }; |
111 | 111 | if (pkg.support) {
|
112 | 112 | if ((pkg.support === true) || (typeof pkg.support === 'string')) {
|
113 | 113 | const supportInfoName = (pkg.support === true)
|
114 |
| - ? module.exports.defaultPackageSupport : module.exports.sanitizePath(pkgPath, pkg.support, basePathOverride) |
115 |
| - const localPath = path.join(pkgPath, supportInfoName) |
| 114 | + ? module.exports.defaultPackageSupport : module.exports.sanitizePath(pkgPath, pkg.support, basePathOverride); |
| 115 | + const localPath = path.join(pkgPath, supportInfoName); |
116 | 116 | if (!preferCanonical && fs.existsSync(localPath)) {
|
117 |
| - supportInfo.contents = fs.readFileSync(localPath) |
118 |
| - supportInfo.resolved = true |
| 117 | + supportInfo.contents = fs.readFileSync(localPath); |
| 118 | + supportInfo.resolved = true; |
119 | 119 | } else {
|
120 |
| - supportInfo.url = module.exports.getRemoteSupportInfoUrl(pkg.repository, supportInfoName) |
121 |
| - await resolveSupportInfo(supportInfo, fetchCanonical) |
| 120 | + supportInfo.url = module.exports.getRemoteSupportInfoUrl(pkg.repository, supportInfoName); |
| 121 | + await resolveSupportInfo(supportInfo, fetchCanonical); |
122 | 122 | }
|
123 | 123 | } else if (typeof pkg.support === 'object') {
|
124 |
| - const supportInfoName = pkg.support.path ? pkg.support.path : module.exports.defaultPackageSupport |
125 |
| - supportInfo.url = module.exports.getRemoteSupportInfoUrl(pkg.support.repository, supportInfoName) |
126 |
| - await resolveSupportInfo(supportInfo, fetchCanonical) |
| 124 | + const supportInfoName = pkg.support.path ? pkg.support.path : module.exports.defaultPackageSupport; |
| 125 | + supportInfo.url = module.exports.getRemoteSupportInfoUrl(pkg.support.repository, supportInfoName); |
| 126 | + await resolveSupportInfo(supportInfo, fetchCanonical); |
127 | 127 | }
|
128 | 128 | }
|
129 |
| - return supportInfo |
130 |
| -} |
| 129 | + return supportInfo; |
| 130 | +}; |
0 commit comments