Skip to content

Commit 9b56f99

Browse files
committed
standard.js
1 parent 5a0f5bc commit 9b56f99

File tree

4 files changed

+152
-140
lines changed

4 files changed

+152
-140
lines changed

lib/cli.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs')
2+
const path = require('path')
23

34
let usage = 'node server.js'
45
if (/server$/.test(process.execPath)) {
@@ -17,7 +18,7 @@ function readCommandLine () {
1718
.option('file', {
1819
description: 'Catalog configuration file or file pattern',
1920
type: 'string',
20-
default: require('path').normalize(__dirname + '/../metadata/TestData2.0.json'),
21+
default: path.normalize(path.join(__dirname, '..', 'metadata', 'TestData2.0.json')),
2122
alias: 'f'
2223
})
2324
.option('port', {
@@ -28,7 +29,7 @@ function readCommandLine () {
2829
})
2930
.option('conf', {
3031
description: 'Server configuration file',
31-
default: require('path').normalize(__dirname + '/../conf/server.json'),
32+
default: path.normalize(path.join(__dirname, '..', 'conf', 'server.json')),
3233
type: 'string',
3334
alias: 'c'
3435
})
@@ -50,7 +51,7 @@ function readCommandLine () {
5051
})
5152
.option('logdir', {
5253
description: 'Log directory',
53-
default: require('path').normalize(__dirname + '/../log'),
54+
default: path.normalize(path.join(__dirname, '..', 'log')),
5455
type: 'string',
5556
alias: 'l'
5657
})
@@ -103,7 +104,7 @@ function readCommandLine () {
103104
})
104105
.option('hapiserverpath', {
105106
description: 'Absolute path to use for $HAPISERVERPATH in server metadata files',
106-
default: require('path').normalize(__dirname + '/..'),
107+
default: path.normalize(path.join(__dirname, '..')),
107108
type: 'string'
108109
})
109110
.option('nodejs', {

lib/metadata.js

+39-36
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function prepmetadata (FILES, FORCE, IGNORE, SKIP_CHECKS, cb) {
104104

105105
if (json.data.file) {
106106
json.data.file = conf.replaceConfigVars(json.data.file)
107-
if (/\${/.test(json.data.file) == false && !fs.existsSync(json.data.file)) {
107+
if (/\${/.test(json.data.file) === false && !fs.existsSync(json.data.file)) {
108108
const msg = json.data.file + ' referenced in ' + catalogid + ' not found.'
109109
log.error(msg, true)
110110
}
@@ -121,15 +121,15 @@ function prepmetadata (FILES, FORCE, IGNORE, SKIP_CHECKS, cb) {
121121

122122
jsonWarn(formats, catalogid)
123123

124-
const update_seconds = json.server['catalog-update']
125-
if (update_seconds) {
126-
if (!Number.isInteger(update_seconds)) {
124+
const updateSeconds = json.server['catalog-update']
125+
if (updateSeconds) {
126+
if (!Number.isInteger(updateSeconds)) {
127127
log.error(serverfile + ' has a non-integer value for server.catalog-update.', true)
128128
}
129-
if (update_seconds < 0) {
129+
if (updateSeconds < 0) {
130130
log.error(serverfile + ' has a negative value for server.catalog-update.', true)
131131
}
132-
log.info('Updating ' + catalogid + ' every ' + update_seconds + ' seconds.')
132+
log.info('Updating ' + catalogid + ' every ' + updateSeconds + ' seconds.')
133133
setInterval(() => {
134134
updatemetadata(FORCE, IGNORE, SKIP_CHECKS, json, (e) => {
135135
const catalogid = json.server.id
@@ -141,7 +141,7 @@ function prepmetadata (FILES, FORCE, IGNORE, SKIP_CHECKS, cb) {
141141
log.info('Updated ' + catalogid)
142142
}
143143
})
144-
}, update_seconds * 1000)
144+
}, updateSeconds * 1000)
145145
}
146146

147147
updatemetadata(FORCE, IGNORE, SKIP_CHECKS, json, (err) => {
@@ -205,26 +205,26 @@ function updatemetadata (FORCE, IGNORE, SKIP_CHECKS, json, cb) {
205205
}
206206
})
207207

208-
function createcat (ds_num, xcatalog) {
208+
function createcat (dsNumber, xcatalog) {
209209
// Each element of xcatalog is a dataset
210210
// Resolve info nodes and checks catalog and info.
211211

212-
if (!xcatalog[ds_num]) {
213-
const msg = `Dataset number ${ds_num} does not have an id element.`
212+
if (!xcatalog[dsNumber]) {
213+
const msg = `Dataset number ${dsNumber} does not have an id element.`
214214
log.error(msg, true)
215215
}
216216

217-
const id = xcatalog[ds_num].id
217+
const id = xcatalog[dsNumber].id
218218

219-
if (ds_num == 0) {
219+
if (dsNumber === 0) {
220220
log.info(`Reading, parsing, and resolving metadata for ${catalogid}`)
221221
}
222222

223223
metadata.newcache[catalogid].ids.push(id)
224224

225-
catalog.catalog[ds_num] = xcatalog[ds_num]
225+
catalog.catalog[dsNumber] = xcatalog[dsNumber]
226226

227-
getmetadata(xcatalog[ds_num], 'info', FORCE, (e, info) => {
227+
getmetadata(xcatalog[dsNumber], 'info', FORCE, (e, info) => {
228228
if (e) {
229229
log.error(e, !FORCE)
230230
if (FORCE) cb(e)
@@ -235,33 +235,33 @@ function updatemetadata (FORCE, IGNORE, SKIP_CHECKS, json, cb) {
235235

236236
function checkinfo (info) {
237237
for (const infoType of ['info', 'info_inline', 'info_url', 'info_file', 'info_command']) {
238-
delete catalog.catalog[ds_num][infoType]
238+
delete catalog.catalog[dsNumber][infoType]
239239
}
240240

241241
info = JSON.parse(JSON.stringify(info))
242242
info.HAPI = json.server.HAPI
243243
info.status = json.status
244244

245245
// Resolve JSON refs in info.
246-
const info_resolved = resolveJSONRefs(JSON.parse(JSON.stringify(info)))
247-
if (SKIP_CHECKS == false) {
246+
const infoResolved = resolveJSONRefs(JSON.parse(JSON.stringify(info)))
247+
if (SKIP_CHECKS === false) {
248248
// log.info("Checking schema validity of /info response metadata for " + id);
249-
validHAPIJSON(info_resolved, info.HAPI, 'info', IGNORE, xcatalog[ds_num].id)
249+
validHAPIJSON(infoResolved, info.HAPI, 'info', IGNORE, xcatalog[dsNumber].id)
250250
// log.info("Checked schema validity of /info response metadata for " + id);
251251
}
252-
delete info_resolved.definitions
253-
metadata.newcache[catalogid].info[id] = info_resolved
252+
delete infoResolved.definitions
253+
metadata.newcache[catalogid].info[id] = infoResolved
254254
metadata.newcache[catalogid]['info-raw'][id] = info
255255

256-
if (ds_num < xcatalog.length - 1) {
257-
createcat(++ds_num, xcatalog)
256+
if (dsNumber < xcatalog.length - 1) {
257+
createcat(++dsNumber, xcatalog)
258258
} else {
259259
checkcat()
260260
}
261261
}
262262

263263
function checkcat () {
264-
if (SKIP_CHECKS == false) {
264+
if (SKIP_CHECKS === false) {
265265
log.info('Checking schema validity of /catalog response metadata for ' + catalogid)
266266
validHAPIJSON(catalog, json.server.HAPI, 'catalog', IGNORE)
267267
// log.info("Checked schema validity of /catalog response metadata for " + catalogid);
@@ -296,7 +296,7 @@ function getmetadata (obj, nodeType, FORCE, cb) {
296296
getone(0)
297297

298298
function getone (i) {
299-
if (i == catalog.length) {
299+
if (i === catalog.length) {
300300
cb(null, catalog)
301301
return
302302
}
@@ -358,15 +358,15 @@ function getmetadata (obj, nodeType, FORCE, cb) {
358358
}
359359

360360
const type = metadataType(obj, nodeType)
361-
if (type == 'inline') {
361+
if (type === 'inline') {
362362
// obj is inline JSON.
363363
cb(null, obj[nodeType + '_' + type])
364364
return
365365
}
366366

367367
if (type === undefined) {
368368
const msg = `No '${nodeType}_{file,url,command,inline}', or '${nodeType}' node found.`
369-
log.error(msg, rue)
369+
log.error(msg, true)
370370
}
371371

372372
let nodeStr = ''
@@ -423,7 +423,7 @@ function getmetadata (obj, nodeType, FORCE, cb) {
423423
} else if (type === 'command') {
424424
nodeStr = nodeStr.trim()
425425
log.info('Executing: ' + nodeStr)
426-
const eopts = { cwd: __dirname + '/..', maxBuffer: 100000000 }
426+
const eopts = { cwd: path.join(__dirname, '..'), maxBuffer: 100000000 }
427427
require('child_process').exec(nodeStr, eopts, (e, info) => {
428428
if (e) {
429429
log.error(e.message.replace(/\n$/, '').replace('\n', '\n '), !FORCE)
@@ -444,7 +444,7 @@ function getmetadata (obj, nodeType, FORCE, cb) {
444444
// Otherwise, attempt to read as a file.
445445
log.info(`'${nodeType}' is ambiguious. It could be a file or command. Attempting to evaluate as a command.`)
446446
log.info(`Attempting to execute '${nodeStr}'`)
447-
const eopts = { cwd: __dirname + '/..', maxBuffer: 100000000 }
447+
const eopts = { cwd: path.join(__dirname, '..'), maxBuffer: 100000000 }
448448
require('child_process').exec(nodeStr, eopts, (e, info) => {
449449
if (!e) {
450450
try {
@@ -467,15 +467,17 @@ function readCatalogConfig (file) {
467467
process.exit(1)
468468
}
469469

470+
let fileStr
470471
log.info('Start reading, parsing, and resolving ' + trimPath(file))
471472
try {
472-
var str = fs.readFileSync(file)
473+
fileStr = fs.readFileSync(file)
473474
} catch (ex) {
474475
log.error(file + ' is not readable: ' + ex.message, true)
475476
}
476477

478+
let json
477479
try {
478-
var json = JSON.parse(str)
480+
json = JSON.parse(fileStr)
479481
} catch (ex) {
480482
const msg = file + ' is not JSON parse-able. Try https://jsonlint.com/. Message: ' + ex.message
481483
log.error(msg, true)
@@ -561,8 +563,9 @@ function landing (json, serverfile, catalogid, IGNORE) {
561563
}
562564

563565
if (landingFile === '' && landingPath === '') {
564-
landingFile = path.normalize(__dirname + '/../node_modules/hapi-server-ui/single.htm')
565-
landingPath = path.normalize(__dirname + '/../node_modules/hapi-server-ui/')
566+
const uiDir = path.join(__dirname, '..', 'node_modules', 'hapi-server-ui')
567+
landingFile = path.normalize(path.join(uiDir, 'single.htm'))
568+
landingPath = path.normalize(uiDir)
566569
log.info('No landingFile or landingPath given in')
567570
log.info(` ${trimPath(serverfile)}`)
568571
log.info(' Will use')
@@ -673,15 +676,15 @@ function resolveJSONRefs (obj) {
673676
const collectIds = function (obj) {
674677
if (!obj) { return }
675678
if (typeof (obj) === 'object') {
676-
if (obj.hasOwnProperty('$ref')) {
679+
if (Object.prototype.hasOwnProperty.call(obj, '$ref')) {
677680
// Remove #/definitions/ from $ref
678681
const name = obj.$ref.replace(/.*\//, '')
679682
hashOfObjects[obj.$ref] = definitions[name]
680683
}
681684
for (const prop in obj) {
682685
collectIds(obj[prop])
683686
}
684-
} else if (typeof (obj) === 'array') {
687+
} else if (Array.isArray(obj)) {
685688
obj.forEach(function (element) {
686689
collectIds(element)
687690
})
@@ -692,7 +695,7 @@ function resolveJSONRefs (obj) {
692695
if (!obj) { return }
693696
if (typeof (obj) === 'object') {
694697
for (const prop in obj) {
695-
if (obj[prop] && typeof (obj[prop]) === 'object' && obj[prop].hasOwnProperty('$ref')) {
698+
if (obj[prop] && typeof (obj[prop]) === 'object' && Object.prototype.hasOwnProperty.call(obj[prop], '$ref')) {
696699
obj[prop] = hashOfObjects[obj[prop].$ref]
697700
} else {
698701
setReferences(obj[prop])
@@ -701,7 +704,7 @@ function resolveJSONRefs (obj) {
701704
} else if (Array.isArray(obj)) {
702705
obj.forEach(function (element, index, array) {
703706
if (element && typeof (element) === 'object' &&
704-
element.hasOwnProperty('$ref')) {
707+
Object.prototype.hasOwnProperty.call(element, '$ref')) {
705708
array[index] = hashOfObjects[element.$ref]
706709
} else {
707710
setReferences(element)

0 commit comments

Comments
 (0)