@uuv/runner-commons
Version:
A common lib for uuv
243 lines (242 loc) • 12.1 kB
JavaScript
;
/**
* Software Name : UUV
*
* SPDX-License-Identifier: MIT
*
* This software is distributed under the MIT License,
* see the "LICENSE" file for more details
*
* Authors: NJAKO MOLOM Louis Fredice & SERVICAL Stanley
* Software description: Make test writing fast, understandable by any human
* understanding English or French.
*/
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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutocompletionSuggestion = void 0;
exports.runGenerateDoc = runGenerateDoc;
const fs_1 = __importDefault(require("fs"));
const common_1 = require("./common");
const path = __importStar(require("path"));
const dictionary_1 = require("@uuv/dictionary");
const lodash_1 = __importDefault(require("lodash"));
class AutocompletionSuggestion {
suggestion;
link;
}
exports.AutocompletionSuggestion = AutocompletionSuggestion;
function runGenerateDoc(destDir) {
const GENERATED_DIR_DOC = `${destDir}/docs/04-wordings/01-generated-wording-description`;
const GENERATED_DIR_DOC_FR = `${destDir}/i18n/fr/docusaurus-plugin-content-docs/current/04-wordings/01-generated-wording-description`;
Object.values(dictionary_1.LANG).forEach((lang, index) => {
const indexOfFile = (index + 1).toLocaleString("fr-FR", {
minimumIntegerDigits: 2,
useGrouping: false,
});
const generatedFile = `${GENERATED_DIR_DOC}/${indexOfFile}-${lang}-generated-wording-description.md`;
const dictionary = (0, dictionary_1.getDefinedDictionary)(lang);
const autocompletionSuggestionFile = path.join(GENERATED_DIR_DOC, `${lang}-autocompletion-suggestion.json`);
cleanGeneratedFilesIfExists(generatedFile, lang, indexOfFile);
common_1.Common.cleanGeneratedFilesIfExists(autocompletionSuggestionFile);
generateWordingFiles(dictionary, generatedFile, lang, indexOfFile, autocompletionSuggestionFile);
fs_1.default.copyFileSync(generatedFile, `${GENERATED_DIR_DOC_FR}/${indexOfFile}-${lang}-generated-wording-description.md`);
fs_1.default.copyFileSync(autocompletionSuggestionFile, path.join(GENERATED_DIR_DOC_FR, `${lang}-autocompletion-suggestion.json`));
});
function cleanGeneratedFilesIfExists(generatedFile, lang, indexOfFile) {
if (fs_1.default.existsSync(generatedFile)) {
fs_1.default.rmSync(generatedFile);
console.log(`[DEL] ${indexOfFile}-${lang}-generated-wording-description.md deleted successfully`);
}
}
function generateWordingFiles(dictionary, generatedFile, lang, indexOfFile, autocompletionSuggestionFile) {
const { wordingFileContent, autocompletionSuggestionContent } = computeWordingFile(dictionary, lang);
writeWordingFile(generatedFile, wordingFileContent, lang, indexOfFile);
common_1.Common.writeWordingFile(autocompletionSuggestionFile, autocompletionSuggestionContent);
}
function computeWordingFile(dictionary, lang) {
const wordingEnrichedNormalizedOrigin = normalizedForMdx(dictionary.getRoleBasedSentencesTemplate());
const wordingsBaseNormalized = normalizedForMdx(dictionary.getBaseSentences());
const title = (function () {
switch (lang) {
case dictionary_1.LANG.FR.toString():
return ("# Français \n" +
":::caution\n" +
"Pensez bien à rajouter `#language: fr` en entête de votre fichier feature.\n" +
":::\n\n");
default:
return "# English";
}
})();
const autocompletionComponent = `\nimport {UuvWordingAutocomplete} from '@site/src/components/WordingAutocomplete/uuv-wording-autocomplete.js';\n\n<UuvWordingAutocomplete lang={'${lang}'}/>\n`;
const rows = [title, autocompletionComponent];
const autocompletionSuggestions = [];
const stepTitle = (function () {
switch (lang) {
case dictionary_1.LANG.FR.toString():
return ["## Etant donné que", "## Quand", "## Alors"];
case dictionary_1.LANG.EN.toString():
return ["## Given", "## When", "## Then"];
default:
return ["", "", ""];
}
})();
const { step: given, autocompletionSuggestions: givenAutocompletionSuggestions } = computeStepDefinition(wordingsBaseNormalized, "key.given", stepTitle[0], undefined);
const { step: when, autocompletionSuggestions: whenAutocompletionSuggestions } = computeStepDefinition(wordingsBaseNormalized, "key.when", stepTitle[1], undefined);
const { step: then, autocompletionSuggestions: thenAutocompletionSuggestions } = computeStepDefinition(wordingsBaseNormalized, "key.then", stepTitle[2], undefined);
rows.push(...given, ...when, ...then);
autocompletionSuggestions.push(...givenAutocompletionSuggestions, ...whenAutocompletionSuggestions, ...thenAutocompletionSuggestions);
rows.push("## Par rôle");
// console.debug("roles", wordingsEnrichedJson.role)
const definedRoles = dictionary.getDefinedRoles();
definedRoles.forEach((role) => {
rows.push(`### ${role.id}`);
const wordingEnrichedNormalized = lodash_1.default.cloneDeep(wordingEnrichedNormalizedOrigin);
wordingEnrichedNormalized.forEach(sentence => {
sentence.wording = sentence.wording.replaceAll("$roleName", role.name)
.replaceAll("$roleId", role.id)
.replaceAll("$definiteArticle", role.getDefiniteArticle())
.replaceAll("$indefiniteArticle", role.getIndefiniteArticle())
.replaceAll("$namedAdjective", role.namedAdjective())
.replaceAll("$ofDefiniteArticle", role.getOfDefiniteArticle());
});
const { step: enrichedGiven, autocompletionSuggestions: enrichedGivenAutocompletionSuggestions } = computeStepDefinition(wordingEnrichedNormalized, "key.given", undefined, "####", role);
if (enrichedGiven.length > 1) {
rows.push(...enrichedGiven);
autocompletionSuggestions.push(...enrichedGivenAutocompletionSuggestions);
}
const { step: enrichedWhen, autocompletionSuggestions: enrichedWhenAutocompletionSuggestions } = computeStepDefinition(wordingEnrichedNormalized, "key.when", undefined, "####", role);
if (enrichedWhen.length > 1) {
rows.push(...enrichedWhen);
autocompletionSuggestions.push(...enrichedWhenAutocompletionSuggestions);
}
const { step: enrichedThen, autocompletionSuggestions: enrichedThenAutocompletionSuggestions } = computeStepDefinition(wordingEnrichedNormalized, "key.then", undefined, "####", role);
if (enrichedThen.length > 1) {
rows.push(...enrichedThen);
autocompletionSuggestions.push(...enrichedThenAutocompletionSuggestions);
}
});
return {
wordingFileContent: rows.join("\n"),
autocompletionSuggestionContent: JSON.stringify(autocompletionSuggestions)
};
}
/* eslint-disable @typescript-eslint/no-explicit-any */
function computeStepDefinition(wordingsJson, stepKey, stepTitle, level = "###", role) {
const step = [];
const autocompletionSuggestion = [];
if (stepTitle) {
step.push(stepTitle);
}
wordingsJson.forEach((conf) => {
if (conf.key.startsWith(stepKey) && shouldGenerate(conf, role)) {
const wording = `${level} ${conf.wording}`;
step.push(wording);
conf.description?.split("\\n").forEach((descriptionLine, index, array) => {
if (index === 0) {
step.push(`> ${descriptionLine ?? ""}`);
}
else if (index === array.length - 1) {
step.push(` ${descriptionLine ?? ""}\n`);
}
else {
step.push(` ${descriptionLine ?? ""}`);
}
});
autocompletionSuggestion.push({
suggestion: conf.wording
.replaceAll("\\", ""),
link: conf.wording
.replaceAll("{", "")
.replaceAll("}", "")
.replaceAll("(", "")
.replaceAll(")", "")
.replaceAll("'", "")
.replaceAll("\\", "")
.replaceAll(/\s/g, "-").toLowerCase()
});
}
});
return {
step,
autocompletionSuggestions: autocompletionSuggestion
};
}
function shouldGenerate(wordingsConf, role) {
if (role === undefined) {
return true;
}
switch (wordingsConf.section) {
case "contains":
return role.shouldGenerateContainsSentence;
case "click":
return role.shouldGenerateClickSentence;
case "type":
return role.shouldGenerateTypeSentence;
case "keyboard":
return role.shouldGenerateKeyboardSentence;
case "checkable":
return role.shouldGenerateCheckedSentence;
default:
return true;
}
}
function writeWordingFile(generatedFile, data, lang, indexOfFile) {
fs_1.default.writeFileSync(generatedFile, data);
console.log(`[WRITE] ${indexOfFile}-${lang}-generated-wording-description.md written successfully`);
}
function normalizedForMdx(data) {
data.forEach(sentence => {
sentence.wording = normalizeFieldForMdx(sentence.wording);
sentence.description = normalizeFieldForMdx(sentence.description);
});
return data;
}
function normalizeFieldForMdx(field) {
return field
.replaceAll("{string}", "\\{string\\}")
.replaceAll("{}", "\\{\\}")
.replaceAll("{int}", "\\{int\\}")
.replaceAll("{key}", "\\{key\\}")
.replaceAll("{reverseTab}", "\\{reverseTab\\}")
.replaceAll("{tab}", "\\{tab\\}")
.replaceAll("{down}", "\\{down\\}")
.replaceAll("{right}", "\\{right\\}")
.replaceAll("{left}", "\\{left\\}")
.replaceAll("{up}", "\\{up\\}");
}
}