create-testplane
Version:
A tool for fast and simple Testplane configuration
131 lines (128 loc) • 5.66 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendWithTypescript = exports.asExpression = exports.addModule = exports.defineVariable = exports.writeTestExample = exports.printSuccessMessage = exports.baseGeneralPromptsHandler = exports.askQuestion = exports.packageNameFromPlugin = exports.optsFromArgv = void 0;
const inquirer_1 = __importDefault(require("inquirer"));
const lodash_1 = __importDefault(require("lodash"));
const path_1 = __importDefault(require("path"));
const colors_1 = require("./colors");
const packageManagement_1 = require("../constants/packageManagement");
const fsUtils_1 = __importDefault(require("../fsUtils"));
const defaultTestplaneConfig_1 = require("../constants/defaultTestplaneConfig");
const optsFromArgv = (argv) => {
if (!argv["_"].length) {
console.info(colors_1.Colors.fillYellow(`Initializing project in ${process.cwd()}`));
argv["_"] = ["."];
}
return {
path: path_1.default.resolve(process.cwd(), argv["_"][0]),
language: argv.lang === "js" ? "js" : "ts",
extraQuestions: Boolean(argv.verbose),
};
};
exports.optsFromArgv = optsFromArgv;
const packageNameFromPlugin = (plugin) => {
for (const pluginSuffix of packageManagement_1.pluginSuffixes) {
if (plugin.endsWith(pluginSuffix)) {
return plugin.substring(0, plugin.length - pluginSuffix.length);
}
}
return plugin;
};
exports.packageNameFromPlugin = packageNameFromPlugin;
const askQuestion = async (question) => {
question.name = "key";
const answers = await inquirer_1.default.prompt(question);
return answers[question.name];
};
exports.askQuestion = askQuestion;
const baseGeneralPromptsHandler = async (testplaneConfig, answers) => {
if (lodash_1.default.isString(answers.baseUrl)) {
testplaneConfig.baseUrl = answers.baseUrl;
}
if (lodash_1.default.isString(answers.gridUrl)) {
testplaneConfig.gridUrl = answers.gridUrl;
}
if (answers.addChromePhone) {
const browserId = "chrome-phone";
const version = await (0, exports.askQuestion)({
type: "input",
message: "chrome-phone: Enter android chrome-phone version (ex: phone-67.1):",
});
lodash_1.default.mergeWith(testplaneConfig, {
sets: {
"touch-phone": {
browsers: [browserId],
files: [`${defaultTestplaneConfig_1.defaultTestplaneTestsDir}/**/*.testplane.(t|j)s`],
},
},
browsers: {
[browserId]: {
desiredCapabilities: {
platformName: "Android",
deviceName: "android",
version,
browserName: "chrome",
},
},
},
}, (objValue, srcValue) => (lodash_1.default.isArray(objValue) ? lodash_1.default.uniq(objValue.concat(srcValue)) : undefined));
}
return testplaneConfig;
};
exports.baseGeneralPromptsHandler = baseGeneralPromptsHandler;
const printSuccessMessage = (configNotes) => {
const successMessage = `
Inside that directory, you can run:
${colors_1.Colors.fillTeal("npx testplane")}
Runs testplane tests
${colors_1.Colors.fillTeal("npx testplane gui")}
Launches testplane's gui
`;
console.info(successMessage);
if (configNotes.length) {
console.info("You also need to complete the setup:");
configNotes.forEach(note => console.info(`
${colors_1.Colors.fillYellow((0, exports.packageNameFromPlugin)(note.plugin))}
${note.configNote}
`));
}
};
exports.printSuccessMessage = printSuccessMessage;
const writeTestExample = async (dirPath, ext) => {
const testExample = `
describe("test", () => {
it("example", async ({browser}) => {
await browser.url("https://github.com/gemini-testing/testplane");
await expect(browser.$(".f4.my-3")).toHaveText("Testplane (ex-hermione) browser test runner based on mocha and wdio");
});
});
`;
await fsUtils_1.default.writeTest(dirPath, `example.testplane.${ext}`, testExample);
};
exports.writeTestExample = writeTestExample;
const asString = (str, quote) => {
const escapedString = str.replace(new RegExp(quote, "gi"), `\\${quote}`);
return quote + escapedString + quote;
};
const defineVariable = (config, { name, value, isExpr }) => {
const quote = lodash_1.default.get(config, ["__template", "quote"], "'");
return lodash_1.default.set(config, ["__variables", name], isExpr ? value : asString(value, quote));
};
exports.defineVariable = defineVariable;
const addModule = (config, variableName, moduleName = variableName) => {
return lodash_1.default.set(config, ["__modules", variableName], moduleName);
};
exports.addModule = addModule;
const asExpression = (value) => `__expression: ${value}`;
exports.asExpression = asExpression;
const extendWithTypescript = async (packageNamesToInstall, appPath) => {
packageNamesToInstall.push("ts-node");
const testplaneTsConfigPath = path_1.default.join(appPath, defaultTestplaneConfig_1.defaultTestplaneTestsDir, "tsconfig.json");
const defaultTestplaneTsConfig = lodash_1.default.set({}, ["compilerOptions", "types"], ["testplane"]);
await fsUtils_1.default.writeJson(testplaneTsConfigPath, defaultTestplaneTsConfig);
};
exports.extendWithTypescript = extendWithTypescript;
//# sourceMappingURL=index.js.map