create-testplane
Version:
A tool for fast and simple Testplane configuration
105 lines • 5.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeJson = exports.writeTest = exports.writeTestplaneConfig = exports.ensureDirectory = exports.exists = void 0;
const lodash_1 = __importDefault(require("lodash"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const defaultTestplaneConfig_1 = require("./constants/defaultTestplaneConfig");
const createDirectory = (path) => fs_1.default.promises.mkdir(path, { recursive: true });
const exists = (path) => new Promise(resolve => {
fs_1.default.promises
.access(path, fs_1.default.constants.F_OK)
.then(() => resolve(true))
.catch(() => resolve(false));
});
exports.exists = exists;
const ensureDirectory = async (path) => {
const stat = await new Promise(resolve => {
fs_1.default.promises
.stat(path)
.then(resolve)
.catch(() => resolve(null));
});
if (stat === null) {
return createDirectory(path);
}
if (!stat.isDirectory()) {
throw Error(`${path} is not a directory!`);
}
};
exports.ensureDirectory = ensureDirectory;
const writeTestplaneConfig = async (dirPath, testplaneConfig) => {
const modules = testplaneConfig.__modules || {};
const variables = testplaneConfig.__variables || {};
const template = testplaneConfig.__template;
const omittedConfig = lodash_1.default.omit(testplaneConfig, ["__modules", "__variables", "__language", "__template"]);
const toIndentedJson = (config) => JSON.stringify(config, null, 4);
const withComments = (configStr) => {
// obj's records like "__comment": "Comment text" are turned into "// Comment text"
// obj's records like "__comment123": "Comment text" are turned into "// Comment text"
// array's strings like "__comment: Comment text" are turned into "// Comment text"
return configStr.replace(/([\t ]*)"__comment\d*(?:(?:: )|(?:": "))(.*)",?/g, "$1// $2");
};
const withReplacedQuotes = (configStr) => {
// removes double quotes from obj's keys where they are not needed
const withoutExtraQuotesConfigStr = configStr.replace(/^[\t ]*"[^:\n\r/-]+(?<!\\)":/gm, match => match.replace(/"/g, ""));
if (template.language === "ts") {
return withoutExtraQuotesConfigStr;
}
// replaces double quotes with single quotes
// unescapes and restores double quotes in comments
return withoutExtraQuotesConfigStr
.replace(/"/g, "'")
.replace(/[\t ]*\/\/ (.*)/g, match => match.replace(/\\'/g, '"'));
};
const withExpressions = (configStr) => {
const quote = template.quote;
const expressionRegExp = new RegExp(`${quote}__expression: (.*)${quote}(,?)$`, "gm");
const repairQuotes = (match) => match.replace(/\\"/g, '"');
const repairSlash = (match) => match.replace(/\\\\/g, "\\");
const repairedConfig = configStr
.replace(expressionRegExp, repairQuotes) // unescapes and restores double quotes in expressions
.replace(expressionRegExp, repairSlash); // restores '\\' in expressions
// strings like '__expression: <expression>' are turned into <expression>
return repairedConfig.replace(expressionRegExp, "$1$2");
};
const getObjectRepr = lodash_1.default.flow([toIndentedJson, withComments, withReplacedQuotes, withExpressions]);
const configImports = Object.keys(modules)
.map(importName => template.getImportModule(importName, modules[importName]))
.join("\n");
const configVariables = Object.keys(variables)
.map(variable => `const ${variable} = ${variables[variable]};`)
.join("\n");
const configBody = template.getExportConfig(getObjectRepr(omittedConfig));
const configContents = [configImports, configVariables, configBody].filter(Boolean).join("\n\n");
const configFileName = template.fileName;
const configPath = path_1.default.resolve(dirPath, configFileName);
await (0, exports.ensureDirectory)(dirPath);
return fs_1.default.promises.writeFile(configPath, configContents);
};
exports.writeTestplaneConfig = writeTestplaneConfig;
const writeTest = async (dirPath, testName, testContent) => {
const testDirPath = path_1.default.resolve(dirPath, defaultTestplaneConfig_1.defaultTestplaneTestsDir);
const testPath = path_1.default.resolve(testDirPath, testName);
try {
await (0, exports.ensureDirectory)(testDirPath);
const isTestfileExist = await (0, exports.exists)(testPath);
if (!isTestfileExist) {
await fs_1.default.promises.writeFile(testPath, testContent);
}
}
catch (_a) {
return;
}
};
exports.writeTest = writeTest;
const writeJson = async (filePath, obj) => {
await (0, exports.ensureDirectory)(path_1.default.dirname(filePath));
return fs_1.default.promises.writeFile(filePath, JSON.stringify(obj, null, 4));
};
exports.writeJson = writeJson;
exports.default = { exists: exports.exists, ensureDirectory: exports.ensureDirectory, writeTestplaneConfig: exports.writeTestplaneConfig, writeTest: exports.writeTest, writeJson: exports.writeJson };
//# sourceMappingURL=fsUtils.js.map