@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
126 lines • 5.89 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeContractStore = exports.readContract = void 0;
const path = __importStar(require("node:path"));
const fs = __importStar(require("node:fs"));
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const readContract = (pathToContract) => {
let content = '';
try {
content = fs.readFileSync(pathToContract, 'utf-8');
}
catch (e) {
throw new case_plugin_base_1.CaseConfigurationError(`Unable to load contract file from disk at '${pathToContract}': ${e.message}`, 'DONT_ADD_LOCATION', 'DISK_IO_PROBLEM');
}
let contract;
try {
contract = JSON.parse(content);
}
catch (e) {
throw new case_plugin_base_1.CaseConfigurationError(`Unable to parse contract file from disk at '${pathToContract}': ${e.message}`, 'DONT_ADD_LOCATION');
}
return contract;
};
exports.readContract = readContract;
const readFile = (filePath) => {
try {
return { contents: fs.readFileSync(filePath), filePath };
}
catch (e) {
throw new case_plugin_base_1.CaseConfigurationError(`Unable to load contract file from disk at '${filePath}': ${e.message}.\n\nThis is almost certainly a race condition where the files were deleted during the directory read.`, 'DONT_ADD_LOCATION', 'DISK_IO_PROBLEM');
}
};
const readDir = (pathToDir) => fs
.readdirSync(pathToDir)
.flatMap((file) => {
const filePath = path.join(pathToDir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
return readDir(filePath);
}
return [readFile(filePath)];
})
.filter((f) => f !== null);
const readContractsFromDir = (pathToDir, context) => {
if (!fs.existsSync(pathToDir)) {
throw new case_plugin_base_1.CaseConfigurationError(`The directory '${pathToDir}' does not seem to exist, so can't read contracts from it`, 'DONT_ADD_LOCATION', 'DISK_IO_PROBLEM');
}
if (!fs.statSync(pathToDir).isDirectory()) {
throw new case_plugin_base_1.CaseConfigurationError(`'${pathToDir}' exists but is a not a directory, so can't read contracts from it`, 'DONT_ADD_LOCATION', 'DISK_IO_PROBLEM');
}
const jsonContracts = readDir(pathToDir)
.filter((l) => {
const keep = l.filePath.endsWith('.json');
if (!keep) {
context.logger.debug(`Skipping ${l.filePath} because it is not a .json file`);
}
return keep;
})
.map((l) => ({ contents: l.contents.toString(), filePath: l.filePath }))
.map((s) => {
try {
return { contents: JSON.parse(s.contents), filePath: s.filePath };
}
catch (e) {
context.logger.warn(`Failed (${e.message}) while parsing '${s.filePath}' as json.\n\nThis may be a configuration error. The best practice is to:\n\n * Keep the contract directory clear of non-contract files\n * Remove all files in this directory before downloading contracts`);
return { contents: null, filePath: s.filePath };
}
})
.filter((u) => u.contents !== null);
jsonContracts
.filter((item) => item.contents.contractType !== 'case::contract')
.forEach((item) => {
context.logger.debug(`Skipping ${item.filePath} because it is not a case contract (incorrect contractType value of '${item.contents.contractType}')`);
});
const caseContracts = jsonContracts.filter((item) => item.contents.contractType === 'case::contract');
caseContracts
.filter((item) => typeof item.contents.description?.providerName !== 'string' ||
typeof item.contents.description?.consumerName !== 'string')
.forEach((item) => {
context.logger.warn(`Skipping ${item.filePath} because it must have a description.consumerName and description.providerName (was '${item.contents.description?.consumerName}' and '${item.contents.description?.providerName}' respectively)`);
});
return caseContracts.filter((item) => typeof item.contents.description?.providerName === 'string' &&
typeof item.contents.description?.consumerName === 'string');
};
const makeContractStore = (context) => ({
readContract: (pathToContract) => ({
contents: (0, exports.readContract)(pathToContract),
filePath: pathToContract,
}),
readContractsFromDir: (pathToDir) => readContractsFromDir(pathToDir, context),
});
exports.makeContractStore = makeContractStore;
//# sourceMappingURL=contractReader.js.map