@lhci/utils
Advanced tools
Comparing version 0.3.5 to 0.3.6
{ | ||
"name": "@lhci/utils", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"license": "Apache-2.0", | ||
@@ -14,5 +14,6 @@ "repository": { | ||
"isomorphic-fetch": "^2.2.1", | ||
"js-yaml": "^3.13.1", | ||
"tree-kill": "^1.2.1" | ||
}, | ||
"gitHead": "b7cbedeb66b2d2170f3000be715974de3041d145" | ||
"gitHead": "adfbd102aebaf954bbf67879f57e5d3fa9ee2eb6" | ||
} |
@@ -10,10 +10,16 @@ /** | ||
const path = require('path'); | ||
const yaml = require('js-yaml'); | ||
const _ = require('./lodash.js'); | ||
// prettier-ignore | ||
const RC_FILE_NAMES = [ | ||
'.lighthouserc.json', | ||
'lighthouserc.json', | ||
'.lighthouserc.yml', | ||
'lighthouserc.yml', | ||
'.lighthouserc.yaml', | ||
'lighthouserc.yaml', | ||
]; | ||
const YAML_FILE_EXTENSION_REGEX = /\.(yml|yaml)$/i; | ||
/** | ||
@@ -46,2 +52,3 @@ * Yargs will treat any key with a `.` in the name as a specifier for an object subpath. | ||
/** | ||
* Load file, parse and convert all `.` in key names to `:` | ||
* @param {string} pathToRcFile | ||
@@ -51,5 +58,6 @@ * @return {LHCI.LighthouseRc} | ||
function loadRcFile(pathToRcFile) { | ||
// Load the JSON and convert all `.` in key names to `:` | ||
// Load file | ||
const contents = fs.readFileSync(pathToRcFile, 'utf8'); | ||
const rc = JSON.parse(contents); | ||
const rc = parseFileContentToJSON(pathToRcFile, contents); | ||
// Convert all `.` in key names to `:` | ||
recursivelyReplaceDotInKeyName(rc); | ||
@@ -60,2 +68,17 @@ return rc; | ||
/** | ||
* Parse file content to JSON. | ||
* @param {string} pathToRcFile | ||
* @param {string} contents | ||
* @return {LHCI.LighthouseRc} | ||
*/ | ||
function parseFileContentToJSON(pathToRcFile, contents) { | ||
// Check if file path ends in yaml or yml | ||
if (YAML_FILE_EXTENSION_REGEX.test(pathToRcFile)) { | ||
// Parse yaml content to JSON | ||
return yaml.safeLoad(contents); | ||
} | ||
return JSON.parse(contents); | ||
} | ||
/** | ||
* @param {string} dir | ||
@@ -62,0 +85,0 @@ * @return {string|undefined} |
@@ -39,7 +39,8 @@ /** | ||
*/ | ||
function saveLHR(lhr) { | ||
const filename = `lhr-${Date.now()}.json`; | ||
const filePath = path.join(LHCI_DIR, filename); | ||
function saveLHR(lhr, baseDir = LHCI_DIR) { | ||
const baseFilename = `lhr-${Date.now()}`; | ||
const basePath = path.join(baseDir, baseFilename); | ||
ensureDirectoryExists(); | ||
fs.writeFileSync(filePath, lhr); | ||
fs.writeFileSync(`${basePath}.json`, lhr); | ||
fs.writeFileSync(`${basePath}.html`, getHTMLReportForLHR(JSON.parse(lhr))); | ||
} | ||
@@ -46,0 +47,0 @@ |
377003
8706
3
+ Addedjs-yaml@^3.13.1
+ Addedargparse@1.0.10(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedsprintf-js@1.0.3(transitive)