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

Add TS Generator which is a lot based on JS Generator.
This commit is contained in:
Nathan Vasse
2023-01-02 12:06:00 +01:00
committed by NathanVss
parent 2876d29025
commit 66e6aad84d
16 changed files with 159 additions and 35 deletions

View File

@@ -1,13 +1,48 @@
import * as fs from "fs";
import * as path from "path";
import * as child_process from "child_process";
import { run } from "ThemeGenerator";
import { cleanup } from "tests/Utils";
// eslint-disable-next-line import/no-extraneous-dependencies
import * as util from "util";
import Config from "../Config";
jest.mock("../Paths", () => ({
workPath: () => __dirname,
}));
jest.mock("../cunningham.dist.js", () => ({
theme: {
colors: {
primary: "#055FD2",
secondary: "#DA0000",
},
},
}));
const runBin = async (args: string) => {
const exec = util.promisify(child_process.exec);
const promise = exec(
path.join(__dirname, "..", "..", "..", "dist", "bin", "Main.js") +
" " +
args
);
promise.child.stdout?.on("data", (data) => {
console.log("stdout: " + data);
});
promise.child.stderr?.on("data", (data) => {
console.log("stderr: " + data);
});
promise.child.on("close", (code) => {
console.log("closing code: " + code);
});
const { stdout, stderr } = await promise;
console.log("stdout", stdout);
console.log("stderr", stderr);
};
/**
* Test written here are supposed to be general ones and not specific to any generator.
*
@@ -25,28 +60,25 @@ describe("Cunningham Bin", () => {
});
it("Runs without existing config file with default values.", async () => {
const cssTokensFile = path.join(__dirname, Config.tokenFilenames.css);
const cssTokensFile = path.join(__dirname, Config.tokenFilename + ".css");
expect(fs.existsSync(cssTokensFile)).toEqual(false);
await run(["", "", "-g", "css"]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`:root {
\t--c--colors--primary: #055FD2;
\t--c--colors--secondary: #DA0000;
\t--c--theme--colors--primary: #055FD2;
\t--c--theme--colors--secondary: #DA0000;
}`);
});
it("Runs with existing config file using local values.", async () => {
const localConfigurationFile = path.join(
__dirname,
Config.configurationFilenames[0]
);
it("Runs with existing JS config file using local values.", async () => {
const localConfigurationFile = path.join(__dirname, "cunningham.js");
expect(fs.existsSync(localConfigurationFile)).toEqual(false);
const cssTokensFile = path.join(__dirname, Config.tokenFilenames.css);
const cssTokensFile = path.join(__dirname, Config.tokenFilename + ".css");
expect(fs.existsSync(cssTokensFile)).toEqual(false);
fs.copyFileSync(
path.join(__dirname, "assets", Config.configurationFilenames[0]),
path.join(__dirname, "assets", "cunningham.js"),
localConfigurationFile
);
expect(fs.existsSync(localConfigurationFile)).toEqual(true);
@@ -54,20 +86,43 @@ describe("Cunningham Bin", () => {
await run(["", "", "-g", "css"]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`:root {
\t--c--colors--primary: AntiqueWhite;
\t--c--colors--secondary: #DA0000;
\t--c--theme--colors--primary: AntiqueWhite;
\t--c--theme--colors--secondary: #DA0000;
}`);
});
it("Runs with existing TS config file using local values.", async () => {
const localConfigurationFile = path.join(__dirname, "cunningham.ts");
expect(fs.existsSync(localConfigurationFile)).toEqual(false);
const cssTokensFile = path.join(__dirname, Config.tokenFilename + ".css");
expect(fs.existsSync(cssTokensFile)).toEqual(false);
fs.copyFileSync(
path.join(__dirname, "assets", "cunningham.ts"),
localConfigurationFile
);
expect(fs.existsSync(localConfigurationFile)).toEqual(true);
// We must run the bin directly to be sure that it compiles the TS file. ( Importing TS from
// TS will always work )
await runBin(`-g css -cwd ${__dirname}`);
// await run(["", "", "-g", "css"]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toContain(`
\t--c--theme--colors--primary: typescript;`);
});
const testOutput = async (opt: string) => {
const outputDir = path.join(__dirname, "output");
const cssTokensFile = path.join(outputDir, Config.tokenFilenames.css);
const cssTokensFile = path.join(outputDir, Config.tokenFilename + ".css");
expect(fs.existsSync(cssTokensFile)).toEqual(false);
await run(["", "", "-g", "css", opt, outputDir]);
expect(fs.existsSync(cssTokensFile)).toEqual(true);
expect(fs.readFileSync(cssTokensFile).toString()).toEqual(`:root {
\t--c--colors--primary: #055FD2;
\t--c--colors--secondary: #DA0000;
\t--c--theme--colors--primary: #055FD2;
\t--c--theme--colors--secondary: #DA0000;
}`);
};

View File

@@ -1,26 +1,30 @@
import path from "path";
import fs from "fs";
import Config from "Config";
import { Generators } from "Generators";
/**
* 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 tokenFilenames = Object.keys(Generators).map(
(extension) => Config.tokenFilename + "." + extension
);
tokenFilenames.forEach((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);
}
Config.configurationFilenames.forEach((filename) => {
const localConfigurationFile = path.join(dir, filename);
if (fs.existsSync(localConfigurationFile)) {
fs.unlinkSync(localConfigurationFile);
}
});
const outputPath = path.join(dir, "output");
if (fs.existsSync(outputPath)) {