Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
testMatch: [
"<rootDir>/tests/**/*.[jt]s?(x)",
"<rootDir>/tests/**/?(*.)+(spec|test).[jt]s?(x)",
"<rootDir>/server/__tests__/**/*.test.cjs",
],
};
44 changes: 44 additions & 0 deletions server/__tests__/prepare-glossary.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";

const fs = require("fs");
const os = require("os");
const path = require("path");

let writeGlossaryFiles;
beforeAll(() => {
({ writeGlossaryFiles } = require("../prepare-glossary.cjs"));
});

describe("writeGlossaryFiles", () => {
let tmpDir;

beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "glossary-test-"));
fs.mkdirSync(path.join(tmpDir, "parameters"));
});

afterEach(() => {
fs.rmSync(tmpDir, { recursive: true });
});

test("glossary.ts exists on disk when the promise resolves", async () => {
await writeGlossaryFiles(tmpDir, {}, [], []);
expect(fs.existsSync(path.join(tmpDir, "parameters", "glossary.ts"))).toBe(true);
});

test("glossary-full.ts exists on disk when the promise resolves", async () => {
await writeGlossaryFiles(tmpDir, {}, [], []);
expect(fs.existsSync(path.join(tmpDir, "parameters", "glossary-full.ts"))).toBe(true);
});

test("resolves (does not reject) when a write fails, and logs the error", async () => {
const badDir = path.join(tmpDir, "nonexistent");
const consoleSpy = jest.spyOn(console, "log").mockImplementation(() => {});
await expect(writeGlossaryFiles(badDir, {}, [], [])).resolves.toBeUndefined();
expect(consoleSpy).toHaveBeenCalledWith(
"Error! Couldn't write to the file.",
expect.objectContaining({ code: "ENOENT" })
);
consoleSpy.mockRestore();
});
});
76 changes: 38 additions & 38 deletions server/prepare-glossary.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function processLanguageSheet() {
XLSX.utils.aoa_to_sheet(rows.data.values),
{
defval: "",
}
},
);

/* -------------------------------------------------------------------------- */
Expand All @@ -51,7 +51,7 @@ async function processLanguageSheet() {
parameterInfo.type === "multicategorical"
)
parameterInfo.categories = getCategoriesFromString(
parameter["CATEGORIES"]
parameter["CATEGORIES"],
);
// Exclude rows that Denis used for other notes
// if (parameterInfo.name && parameterInfo.type)
Expand Down Expand Up @@ -80,55 +80,55 @@ async function processLanguageSheet() {
/* -------------------------------------------------------------------------- */

const superMatchingParams = Object.keys(data).filter((key) =>
key.includes("@")
key.includes("@"),
);

await writeGlossaryFiles(process.cwd(), data, dataFull, superMatchingParams);
}

async function writeGlossaryFiles(cwd, data, dataFull, superMatchingParams) {
const exportWarning = `/*
Do not modify this file! Run npm \`npm run glossary\` at ROOT of this project to fetch from the Google Sheets.
https://docs.google.com/spreadsheets/d/1x65NjykMm-XUOz98Eu_oo6ON2xspm_h0Q0M2u6UGtug/edit#gid=1287694458
*/\n\n`;
const exportHandle = `interface Glossary {[parameter: string]: { [field: string]: string | string[] };}\n\nexport const GLOSSARY: Glossary =`;

const exportSuperMatchingParamArray = `\n\nexport const SUPER_MATCHING_PARAMS: string[] = ${JSON.stringify(
superMatchingParams
superMatchingParams,
)};\n`;

fs.writeFile(
`${process.cwd()}/parameters/glossary.ts`,
exportWarning +
exportHandle +
JSON.stringify(data) +
exportSuperMatchingParamArray,
(error) => {
if (error) {
console.log("Error! Couldn't write to the file.", error);
} else {
console.log(
"EasyEyes glossary of inputs fetched and written into files successfully."
);
}
}
);
try {
await fs.promises.writeFile(
`${cwd}/parameters/glossary.ts`,
exportWarning +
exportHandle +
JSON.stringify(data) +
exportSuperMatchingParamArray,
);
console.log(
"EasyEyes glossary of inputs fetched and written into files successfully.",
);
} catch (error) {
console.log("Error! Couldn't write to the file.", error);
}

const exportHandleFull1 = `interface GlossaryFullItem { [field: string]: string | string[] };\n\n`;
const exportHandleFull2 = `export const GLOSSARY: GlossaryFullItem[] =`;
fs.writeFile(
`${process.cwd()}/parameters/glossary-full.ts`,
exportWarning +
exportHandleFull1 +
exportHandleFull2 +
JSON.stringify(dataFull) +
exportSuperMatchingParamArray,
(error) => {
if (error) {
console.log("Error! Couldn't write to the file.", error);
} else {
console.log(
"EasyEyes glossary (FULL VERSION) of inputs fetched and written into files successfully."
);
}
}
);
try {
await fs.promises.writeFile(
`${cwd}/parameters/glossary-full.ts`,
exportWarning +
exportHandleFull1 +
exportHandleFull2 +
JSON.stringify(dataFull) +
exportSuperMatchingParamArray,
);
console.log(
"EasyEyes glossary (FULL VERSION) of inputs fetched and written into files successfully.",
);
} catch (error) {
console.log("Error! Couldn't write to the file.", error);
}
}

require("dns").resolve("www.google.com", function (err) {
Expand All @@ -138,7 +138,7 @@ require("dns").resolve("www.google.com", function (err) {
try {
if (fs.existsSync(credentialPath)) {
console.log("Fetching up-to-date glossary...");
processLanguageSheet();
processLanguageSheet().catch(console.error);
} else {
console.log(":( Failed to fetch GLOSSARY. No credentials.json found.");
}
Expand Down