UNPKG

rivet

Version:

Suite of utilities for working with the Rivet consumer-driven API contacts testing model.

67 lines (66 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var glob = require("glob"); var path_1 = require("path"); var fs = require("fs-extra"); var fs_1 = require("fs"); var log_1 = require("../../lib/log"); var config_1 = require("../../lib/config"); exports.saveToFile = function (data, filename, dir) { if (dir === void 0) { dir = 'data/'; } var dirpath = path_1.resolve(dir); fs.ensureDirSync(dirpath); var outputPath = path_1.resolve(dirpath, filename); log_1.log("Saving JSON contract: " + dirpath + "/" + filename); return fs.writeFileSync(outputPath, data); }; var attemptClean = function (clean) { if (clean) { try { if (fs_1.existsSync(clean)) { fs.removeSync(clean); log_1.log("Cleaned directory: " + clean); } else { log_1.warn("Directory '" + clean + "' does not exist, nothing to clean."); } } catch (error) { error("Error cleaning directory: " + clean); console.error(error); } } }; var globOptions = function (ignore, out, cwd) { if (ignore === void 0) { ignore = []; } var options = (ignore.length > 0 && !ignore[0]) ? { ignore: [ '**/node_modules/**/*', out + "**/*", ], } : { ignore: ignore }; return Object.assign({}, { cwd: path_1.join(config_1.default.appRoot, cwd) }, options); }; exports.default = function (argv) { log_1.log('Compiling contracts to JSON'); var clean = argv.clean, out = argv.out, argSrc = argv.src, ignore = argv.ignore, cwd = argv.cwd; var pathGlob = argSrc || '**/*.contract.js'; if (clean) { attemptClean(out); } var options = globOptions(ignore, out, cwd); var paths = glob.sync(pathGlob, options); return paths.reduce(function (result, file) { var _a = path_1.parse(file), dir = _a.dir, name = _a.name; // clear cache to rebuild the JSON for watch var contractPath = path_1.resolve(cwd, file); delete require.cache[require.resolve(contractPath)]; var data = require(contractPath); var dirpath = path_1.resolve(config_1.default.compiledContractsRoot, dir); var object = exports.saveToFile(JSON.stringify(data, null, 2), name + ".json", dirpath); result.push(object); return result; }, []); };