From 3e98429d042324bb3d9dfebee0690b2d79f0f238 Mon Sep 17 00:00:00 2001 From: Nathan Vasse Date: Fri, 30 Dec 2022 12:12:04 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(typescript)=20move=20tokens?= =?UTF-8?q?=20sources=20to=20absolute=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Relatives import are quite ugly and reduces readability, but hard to say that Typescript handles it very well for compilation time, but nothing for runtime. That's why I had to add tsc-alias to the build script. Please see this issue for more details. https://github.com/Microsoft/TypeScript/issues/15479 Furthermore, some configuration was needed for Jest to work well. --- packages/tokens/jest.config.ts | 7 +++- packages/tokens/package.json | 5 +-- packages/tokens/src/bin/ConfigLoader.ts | 4 +-- .../src/bin/{ => Generators}/CssGenerator.ts | 4 +-- packages/tokens/src/bin/Main.ts | 2 +- packages/tokens/src/bin/ThemeGenerator.ts | 8 ++--- .../tokens/src/bin/tests/Cunningham.spec.ts | 2 +- packages/tokens/tsconfig.json | 4 +++ yarn.lock | 35 +++++++++++++++++-- 9 files changed, 55 insertions(+), 16 deletions(-) rename packages/tokens/src/bin/{ => Generators}/CssGenerator.ts (91%) diff --git a/packages/tokens/jest.config.ts b/packages/tokens/jest.config.ts index 27cf01181..4e47f875f 100644 --- a/packages/tokens/jest.config.ts +++ b/packages/tokens/jest.config.ts @@ -1,4 +1,9 @@ -export default { +import type { JestConfigWithTsJest } from "ts-jest"; + +const jestConfig: JestConfigWithTsJest = { preset: "ts-jest", testEnvironment: "node", + moduleDirectories: ["node_modules", "src/bin"], }; + +export default jestConfig; diff --git a/packages/tokens/package.json b/packages/tokens/package.json index 822bd3a9d..a01d822e7 100644 --- a/packages/tokens/package.json +++ b/packages/tokens/package.json @@ -15,7 +15,7 @@ "scripts": { "lint": "eslint . 'src/**/*.{ts,tsx}'", "dev": "nodemon --watch 'src/bin' --ext '*' --exec 'yarn build'", - "build": "tsc -p tsconfig.json && cp src/bin/cunningham.dist.js dist/bin && chmod +x dist/bin/Main.js && yarn build-default-theme", + "build": "tsc -p tsconfig.json && tsc-alias && cp src/bin/cunningham.dist.js dist/bin && chmod +x dist/bin/Main.js && yarn build-default-theme", "build-default-theme": "./dist/bin/Main.js -o dist -s html", "test": "FORCE_COLOR=1 jest --verbose src/bin/tests" }, @@ -23,7 +23,8 @@ "chalk": "4.1.2", "commander": "9.4.1", "deepmerge": "4.2.2", - "figlet": "1.5.2" + "figlet": "1.5.2", + "tsc-alias": "^1.8.2" }, "devDependencies": { "@types/figlet": "1.5.5", diff --git a/packages/tokens/src/bin/ConfigLoader.ts b/packages/tokens/src/bin/ConfigLoader.ts index 9eaf2f49f..03358b530 100644 --- a/packages/tokens/src/bin/ConfigLoader.ts +++ b/packages/tokens/src/bin/ConfigLoader.ts @@ -1,9 +1,9 @@ import path from "path"; import * as fs from "fs"; import deepmerge from "deepmerge"; +import { ConfigShape } from "TokensGenerator"; +import { workPath } from "Paths"; import Config from "./Config"; -import { ConfigShape } from "./TokensGenerator"; -import { workPath } from "./Paths"; const getLocalConfig = async () => { const filename = Config.configurationFilenames diff --git a/packages/tokens/src/bin/CssGenerator.ts b/packages/tokens/src/bin/Generators/CssGenerator.ts similarity index 91% rename from packages/tokens/src/bin/CssGenerator.ts rename to packages/tokens/src/bin/Generators/CssGenerator.ts index 4561a6ff0..3cb5b7525 100644 --- a/packages/tokens/src/bin/CssGenerator.ts +++ b/packages/tokens/src/bin/Generators/CssGenerator.ts @@ -1,8 +1,8 @@ import * as path from "path"; import * as fs from "fs"; import chalk from "chalk"; -import Config from "./Config"; -import { flatify } from "./Utils/Flatify"; +import { flatify } from "Utils/Flatify"; +import Config from "Config"; export const cssGenerator = async ( tokens: any, diff --git a/packages/tokens/src/bin/Main.ts b/packages/tokens/src/bin/Main.ts index c2e97a77a..1fb888d38 100644 --- a/packages/tokens/src/bin/Main.ts +++ b/packages/tokens/src/bin/Main.ts @@ -1,5 +1,5 @@ #!/usr/bin/env node -import { run } from "./ThemeGenerator"; +import { run } from "ThemeGenerator"; run(process.argv); diff --git a/packages/tokens/src/bin/ThemeGenerator.ts b/packages/tokens/src/bin/ThemeGenerator.ts index 36e11a085..e2b315ada 100644 --- a/packages/tokens/src/bin/ThemeGenerator.ts +++ b/packages/tokens/src/bin/ThemeGenerator.ts @@ -1,10 +1,10 @@ import { program } from "commander"; import chalk from "chalk"; import figlet from "figlet"; -import { getConfig } from "./ConfigLoader"; -import { tokensGenerator } from "./TokensGenerator"; -import { cssGenerator } from "./CssGenerator"; -import { workPath } from "./Paths"; +import { cssGenerator } from "Generators/CssGenerator"; +import { getConfig } from "ConfigLoader"; +import { tokensGenerator } from "TokensGenerator"; +import { workPath } from "Paths"; export const buildTheme = async () => { const options = program.opts(); diff --git a/packages/tokens/src/bin/tests/Cunningham.spec.ts b/packages/tokens/src/bin/tests/Cunningham.spec.ts index faec68b74..e38c0ba1a 100644 --- a/packages/tokens/src/bin/tests/Cunningham.spec.ts +++ b/packages/tokens/src/bin/tests/Cunningham.spec.ts @@ -1,6 +1,6 @@ import * as fs from "fs"; import * as path from "path"; -import { run } from "../ThemeGenerator"; +import { run } from "ThemeGenerator"; import Config from "../Config"; jest.mock("../Paths", () => ({ diff --git a/packages/tokens/tsconfig.json b/packages/tokens/tsconfig.json index 1000b2b7d..b72cc811d 100644 --- a/packages/tokens/tsconfig.json +++ b/packages/tokens/tsconfig.json @@ -5,6 +5,10 @@ ], "compilerOptions": { "baseUrl": "./src/bin", + "paths": { + "*": ["*"] + }, + "resolveJsonModule": true, "outDir": "./dist/bin", } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index f7116ecec..014741d92 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4871,7 +4871,7 @@ check-error@^1.0.2: resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82" integrity sha512-BrgHpW9NURQgzoNyjfq0Wu6VFO6D7IZEmJNdtgNqpzGG8RuNFHt2jQxWlAs4HMe119chBnv+34syEZtc6IhLtA== -"chokidar@>=3.0.0 <4.0.0", chokidar@^3.4.1, chokidar@^3.4.2, chokidar@^3.5.2: +"chokidar@>=3.0.0 <4.0.0", chokidar@^3.4.1, chokidar@^3.4.2, chokidar@^3.5.2, chokidar@^3.5.3: version "3.5.3" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== @@ -5090,7 +5090,7 @@ comma-separated-tokens@^1.0.0: resolved "https://registry.yarnpkg.com/comma-separated-tokens/-/comma-separated-tokens-1.0.8.tgz#632b80b6117867a158f1080ad498b2fbe7e3f5ea" integrity sha512-GHuDRO12Sypu2cV70d1dkA2EUmXHgntrzbpvOB+Qy+49ypNfGgFQIC2fhhXbnyrJRynDCAARsT7Ou0M6hirpfw== -commander@9.4.1, commander@^9.4.1: +commander@9.4.1, commander@^9.0.0, commander@^9.4.1: version "9.4.1" resolved "https://registry.yarnpkg.com/commander/-/commander-9.4.1.tgz#d1dd8f2ce6faf93147295c0df13c7c21141cfbdd" integrity sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw== @@ -7200,7 +7200,7 @@ globalyzer@0.1.0: resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.0.tgz#cb76da79555669a1519d5a8edf093afaa0bf1465" integrity sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q== -globby@^11.0.2, globby@^11.1.0: +globby@^11.0.2, globby@^11.0.4, globby@^11.1.0: version "11.1.0" resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b" integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g== @@ -9472,6 +9472,11 @@ ms@2.1.3, ms@^2.1.1: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== +mylas@^2.1.9: + version "2.1.13" + resolved "https://registry.yarnpkg.com/mylas/-/mylas-2.1.13.tgz#1e23b37d58fdcc76e15d8a5ed23f9ae9fc0cbdf4" + integrity sha512-+MrqnJRtxdF+xngFfUUkIMQrUUL0KsxbADUkn23Z/4ibGg192Q+z+CQyiYwvWTsYjJygmMR8+w3ZDa98Zh6ESg== + nan@^2.12.1: version "2.17.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.17.0.tgz#c0150a2368a182f033e9aa5195ec76ea41a199cb" @@ -10236,6 +10241,13 @@ pkg-types@^1.0.0: mlly "^1.0.0" pathe "^1.0.0" +plimit-lit@^1.2.6: + version "1.5.0" + resolved "https://registry.yarnpkg.com/plimit-lit/-/plimit-lit-1.5.0.tgz#f66df8a7041de1e965c4f1c0697ab486968a92a5" + integrity sha512-Eb/MqCb1Iv/ok4m1FqIXqvUKPISufcjZ605hl3KM/n8GaX8zfhtgdLwZU3vKjuHGh2O9Rjog/bHTq8ofIShdng== + dependencies: + queue-lit "^1.5.0" + pnp-webpack-plugin@1.6.4: version "1.6.4" resolved "https://registry.yarnpkg.com/pnp-webpack-plugin/-/pnp-webpack-plugin-1.6.4.tgz#c9711ac4dc48a685dabafc86f8b6dd9f8df84149" @@ -10584,6 +10596,11 @@ querystringify@^2.1.1: resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6" integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ== +queue-lit@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/queue-lit/-/queue-lit-1.5.0.tgz#8197fdafda1edd615c8a0fc14c48353626e5160a" + integrity sha512-IslToJ4eiCEE9xwMzq3viOO5nH8sUWUCwoElrhNMozzr9IIt2qqvB4I+uHu/zJTQVqc9R5DFwok4ijNK1pU3fA== + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -12222,6 +12239,18 @@ ts-pnp@^1.1.6: resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92" integrity sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw== +tsc-alias@^1.8.2: + version "1.8.2" + resolved "https://registry.yarnpkg.com/tsc-alias/-/tsc-alias-1.8.2.tgz#3cd24bba7333a5e05cb7db3ac206d7bcec079630" + integrity sha512-ukBkcNekOgwtnSWYLD5QsMX3yQWg7JviAs8zg3qJGgu4LGtY3tsV4G6vnqvOXIDkbC+XL9vbhObWSpRA5/6wbg== + dependencies: + chokidar "^3.5.3" + commander "^9.0.0" + globby "^11.0.4" + mylas "^2.1.9" + normalize-path "^3.0.0" + plimit-lit "^1.2.6" + tsconfig-paths@^3.14.1: version "3.14.1" resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a"