@best/store
Version:
Best Store
56 lines • 2.97 kB
JavaScript
/*
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.storeBenchmarkResults = storeBenchmarkResults;
const path_1 = __importDefault(require("path"));
const md_formatter_1 = require("./md-formatter");
const pretty_json_1 = require("./pretty-json");
const fs_1 = __importDefault(require("fs"));
const chalk_1 = __importDefault(require("chalk"));
function formatJSON(json) {
return (0, pretty_json_1.stringify)(json, { indent: 2, maxLength: 90 });
}
function getStoredFileMapping(benchmarkFolder, artifactsFolder) {
const WHITELIST = ['.js', '.html', '.css', '.json'];
const currentFiles = fs_1.default.readdirSync(benchmarkFolder);
const artifactFiles = fs_1.default.readdirSync(artifactsFolder).map((p) => path_1.default.join('artifacts', p));
const files = [...currentFiles, ...artifactFiles].filter((p) => WHITELIST.includes(path_1.default.extname(p)));
return files.reduce((map, file) => {
map[file] = path_1.default.join(benchmarkFolder, file);
return map;
}, {});
}
function storeBenchmarkResults(benchmarkResults, globalConfig) {
return Promise.all(benchmarkResults.map(async (benchmarkResult) => {
const { environment, results, stats, benchmarkInfo: { benchmarkFolder }, } = benchmarkResult;
const { externalStorage } = globalConfig;
const artifactsFolder = path_1.default.join(benchmarkFolder, 'artifacts');
// Environment
fs_1.default.writeFileSync(path_1.default.join(benchmarkFolder, 'environment.md'), (0, md_formatter_1.formatEnvironment)(environment), 'utf8');
// Results
fs_1.default.writeFileSync(path_1.default.join(benchmarkFolder, 'stats.json'), formatJSON(stats), 'utf8');
fs_1.default.writeFileSync(path_1.default.join(benchmarkFolder, 'raw_results.json'), formatJSON(results), 'utf8');
if (externalStorage) {
try {
const storageModule = require(externalStorage);
const fileMap = getStoredFileMapping(benchmarkFolder, artifactsFolder);
await storageModule.storeBenchmarkResults(fileMap, benchmarkResult, globalConfig);
}
catch (err) {
const ERR_TEXT = chalk_1.default.reset.inverse.red.bold(' ERROR ') + ' ';
console.log(ERR_TEXT + `Unable to push to external storage ${chalk_1.default.bold(externalStorage)}: `, err.message || err, '\n');
throw new Error('[best-store] Error executing externalStorage: ' + externalStorage);
}
}
return true;
}));
}
//# sourceMappingURL=index.js.map
;