(tokens) add JS generator and re-organize the repo

Added JsGenerator which to implied to reorganize a bit the
repo in order to extract generator-specific test into specific
standalone files.
This commit is contained in:
Nathan Vasse
2022-12-30 17:45:35 +01:00
committed by NathanVss
parent 3e98429d04
commit 2876d29025
12 changed files with 188 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
import * as fs from "fs";
import * as path from "path";
import { run } from "ThemeGenerator";
import { cleanup } from "tests/Utils";
import Config from "../Config";
jest.mock("../Paths", () => ({
@@ -8,43 +9,25 @@ jest.mock("../Paths", () => ({
}));
/**
* Empty the current directory from generated tokens file and local
* config to start with an predictable environment.
* Test written here are supposed to be general ones and not specific to any generator.
*
* But as we need at least one generator to execute the bin we need to choose one to use by default,
* that's why we use the css generator.
*/
const cleanup = () => {
const filePath = path.join(__dirname, Config.sass.tokenFilenameCss);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
const localConfigurationFile = path.join(
__dirname,
Config.configurationFilenames[0]
);
if (fs.existsSync(localConfigurationFile)) {
fs.unlinkSync(localConfigurationFile);
}
const outputPath = path.join(__dirname, "output");
if (fs.existsSync(outputPath)) {
fs.rmSync(outputPath, { recursive: true });
}
};
describe("Cunningham Bin", () => {
beforeAll(() => {
jest.spyOn(console, "log").mockImplementation(() => {});
cleanup();
cleanup(__dirname);
});
afterEach(() => {
cleanup();
cleanup(__dirname);
});
it("Runs without existing config file with default values.", async () => {
const cssTokensFile = path.join(__dirname, Config.sass.tokenFilenameCss);
const cssTokensFile = path.join(__dirname, Config.tokenFilenames.css);
expect(fs.existsSync(cssTokensFile)).toEqual(false);
await run([]);
await run(["", "", "-g", "css"]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`:root {
\t--c--colors--primary: #055FD2;
@@ -59,7 +42,7 @@ describe("Cunningham Bin", () => {
);
expect(fs.existsSync(localConfigurationFile)).toEqual(false);
const cssTokensFile = path.join(__dirname, Config.sass.tokenFilenameCss);
const cssTokensFile = path.join(__dirname, Config.tokenFilenames.css);
expect(fs.existsSync(cssTokensFile)).toEqual(false);
fs.copyFileSync(
@@ -68,7 +51,7 @@ describe("Cunningham Bin", () => {
);
expect(fs.existsSync(localConfigurationFile)).toEqual(true);
await run([]);
await run(["", "", "-g", "css"]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`:root {
\t--c--colors--primary: AntiqueWhite;
@@ -78,9 +61,9 @@ describe("Cunningham Bin", () => {
const testOutput = async (opt: string) => {
const outputDir = path.join(__dirname, "output");
const cssTokensFile = path.join(outputDir, Config.sass.tokenFilenameCss);
const cssTokensFile = path.join(outputDir, Config.tokenFilenames.css);
expect(fs.existsSync(cssTokensFile)).toEqual(false);
await run(["", "", opt, outputDir]);
await run(["", "", "-g", "css", opt, outputDir]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`:root {
\t--c--colors--primary: #055FD2;
@@ -95,22 +78,4 @@ describe("Cunningham Bin", () => {
it("Runs with --output options.", async () => {
await testOutput("--output");
});
const testSelector = async (opt: string) => {
const cssTokensFile = path.join(__dirname, Config.sass.tokenFilenameCss);
expect(fs.existsSync(cssTokensFile)).toEqual(false);
await run(["", "", opt, "html"]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`html {
\t--c--colors--primary: #055FD2;
\t--c--colors--secondary: #DA0000;
}`);
};
it("Runs with -s options.", async () => {
await testSelector("-s");
});
it("Runs with --selector options.", async () => {
await testSelector("--selector");
});
});

View File

@@ -0,0 +1,29 @@
import path from "path";
import fs from "fs";
import Config from "Config";
/**
* Empty the current directory from generated tokens file and local
* config to start with an predictable environment.
*/
export const cleanup = (dir: string) => {
Object.entries(Config.tokenFilenames).forEach(([key, filename]) => {
const filePath = path.join(dir, filename);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
});
const localConfigurationFile = path.join(
dir,
Config.configurationFilenames[0]
);
if (fs.existsSync(localConfigurationFile)) {
fs.unlinkSync(localConfigurationFile);
}
const outputPath = path.join(dir, "output");
if (fs.existsSync(outputPath)) {
fs.rmSync(outputPath, { recursive: true });
}
};