Skip to content

Commit ede4ef7

Browse files
authored
chore(tokens): move tasks to nx workflow (#2362)
1 parent 7df3edd commit ede4ef7

File tree

8 files changed

+181
-114
lines changed

8 files changed

+181
-114
lines changed

.github/actions/file-diff/index.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ async function run() {
5656
// --------------- End evaluation ---------------
5757

5858
/** Split the data by component package */
59-
const COMPONENTS = splitDataByComponent(pathOutput, diffOutput);
60-
const sections = makeTable(COMPONENTS);
59+
const { filePath, PACKAGES } = splitDataByPackage(pathOutput, path, diffOutput);
60+
const sections = makeTable(PACKAGES, filePath, path);
6161

6262
const overallSize = [...pathOutput.values()].reduce(
6363
(acc, size) => acc + size,
@@ -106,17 +106,17 @@ async function run() {
106106

107107
markdown.push(`<details>`, `<summary><b>Details</b></summary>`, "");
108108

109-
sections.map(({ name, totalSize, totalDiffSize, fileMap }) => {
109+
sections.map(({ name, filePath, totalSize, totalDiffSize, hasChange, fileMap }) => {
110110
const md = ["", `#### ${name}`, ""];
111111
const data = [name, bytesToSize(totalDiffSize)];
112112

113-
if (totalDiffSize - totalSize === 0) return;
113+
if (!hasChange) return;
114114

115115
if (hasDiff) {
116116
// If a diff path was provided and the component folder doesn't exist,
117117
// report that the compiled assets were removed
118118
if (
119-
!existsSync(join(diffPath, "components", name)) ||
119+
!existsSync(join(diffPath, filePath, name)) ||
120120
(totalSize === 0 && totalDiffSize > 0)
121121
) {
122122
data.push("🚨 deleted, moved, or renamed");
@@ -183,7 +183,7 @@ async function run() {
183183
markdown.push(
184184
"",
185185
"<small>",
186-
"* <em>Size determined by adding together the size of the main file (index.css) for all packages in the library.</em><br/>",
186+
"* <em>Size determined by adding together the size of the main file for all packages in the library.</em><br/>",
187187
"* <em>Results are not gzipped or minified.</em><br/>",
188188
"* <em>An ASCII character in UTF-8 is 8 bits or 1 byte.</em>",
189189
"</small>"
@@ -265,15 +265,25 @@ const printPercentChange = function (delta) {
265265

266266
/**
267267
*
268-
* @param {Map<string, Map<string, { byteSize: number, diffByteSize: number }>>} COMPONENTS
269-
* @returns {Array<{ name: string, totalSize: number, totalDiffSize: number, hasChange: boolean, fileMap: Map<string, { byteSize: number, diffByteSize: number }>}>}
268+
* @param {Map<string, Map<string, { byteSize: number, diffByteSize: number }>>} PACKAGES
269+
* @param {string} filePath - The path to the component's dist folder from the root of the repo
270+
* @param {string} path - The path from the github workspace to the root of the repo
271+
* @returns {Array<{ name: string, filePath: string, totalSize: number, totalDiffSize: number, hasChange: boolean, fileMap: Map<string, { byteSize: number, diffByteSize: number }>}>}
270272
*/
271-
const makeTable = function (COMPONENTS) {
273+
const makeTable = function (PACKAGES, filePath, path) {
272274
const sections = [];
273275

274-
/** Next convert that component data into a comment */
275-
COMPONENTS.forEach((fileMap, componentName) => {
276-
const mainFileOnly = [...fileMap.keys()].filter((file) => file.endsWith("index.css"));
276+
/** Next convert that component data into a detailed object for reporting */
277+
PACKAGES.forEach((fileMap, packageName) => {
278+
// Read in the main asset file from the package.json
279+
const packagePath = join(path, filePath, packageName, "package.json");
280+
281+
let mainFile = "index.css";
282+
if (existsSync(packagePath)) {
283+
mainFile = require(join(path, filePath, packageName, "package.json"))?.main;
284+
}
285+
286+
const mainFileOnly = [...fileMap.keys()].filter((file) => file.endsWith(mainFile));
277287
const totalSize = mainFileOnly.reduce(
278288
(acc, filename) => {
279289
const { byteSize = 0 } = fileMap.get(filename);
@@ -296,7 +306,14 @@ const makeTable = function (COMPONENTS) {
296306
*/
297307
if (totalSize === totalDiffSize) return;
298308

299-
sections.push({ name: componentName, totalSize, totalDiffSize, hasChange, fileMap });
309+
sections.push({
310+
name: packageName,
311+
filePath,
312+
totalSize,
313+
totalDiffSize,
314+
hasChange,
315+
fileMap
316+
});
300317
});
301318

302319
return sections;
@@ -305,20 +322,28 @@ const makeTable = function (COMPONENTS) {
305322
/**
306323
* Split out the data indexed by filename into groups by component
307324
* @param {Map<string, number>} dataMap
325+
* @param {string} path
308326
* @param {Map<string, number>} diffMap
309-
* @returns {Map<string, Map<string, { byteSize: number, diffByteSize: number }>>}
327+
* @returns {{ filePath: string, PACKAGES: Map<string, Map<string, { byteSize: number, diffByteSize: number }>>}}
310328
*/
311-
const splitDataByComponent = function (dataMap, diffMap = new Map()) {
312-
const COMPONENTS = new Map();
329+
const splitDataByPackage = function (dataMap, path, diffMap = new Map()) {
330+
const PACKAGES = new Map();
331+
332+
let filePath;
313333
[...dataMap.entries()].forEach(([file, byteSize]) => {
314334
// Determine the name of the component
315335
const parts = file.split(sep);
316336
const componentIdx = parts.findIndex((part) => part === "dist") - 1;
317-
const componentName = parts[componentIdx];
337+
const packageName = parts[componentIdx];
338+
339+
if (!filePath) {
340+
filePath = `${file.replace(path, "")}/${parts.slice(componentIdx + 1, -1).join(sep)}`;
341+
}
342+
318343
const readableFilename = file.replace(/^.*\/dist\//, "");
319344

320-
const fileMap = COMPONENTS.has(componentName)
321-
? COMPONENTS.get(componentName)
345+
const fileMap = PACKAGES.has(packageName)
346+
? PACKAGES.get(packageName)
322347
: new Map();
323348

324349
if (!fileMap.has(readableFilename)) {
@@ -331,7 +356,8 @@ const splitDataByComponent = function (dataMap, diffMap = new Map()) {
331356
}
332357

333358
/** Update the component's table data */
334-
COMPONENTS.set(componentName, fileMap);
359+
PACKAGES.set(packageName, fileMap);
335360
});
336-
return COMPONENTS;
361+
362+
return { filePath, PACKAGES };
337363
};

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ jobs:
112112
with:
113113
path: |
114114
components/*/dist/**
115+
tokens/dist/**
116+
ui-icons/dist/**
115117
key: ${{ matrix.system }}-node${{ matrix.node-version }}-compiled-assets-${{ steps.set-SHAs.outputs.head }}
116118

117119
- name: Build components
@@ -127,6 +129,8 @@ jobs:
127129
with:
128130
path: |
129131
${{ github.workspace }}/components/*/dist/**
132+
${{ github.workspace }}/tokens/dist/**
133+
${{ github.workspace }}/ui-icons/dist/**
130134
${{ github.workspace }}/profile.json
131135
name: ${{ matrix.system }}-node${{ matrix.node-version }}-compiled-assets-${{ steps.set-SHAs.outputs.head }}
132136
# this is important, it lets us catch if the build failed silently

.github/workflows/publish-site.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ jobs:
8585
with:
8686
path: |
8787
components/*/dist/**
88+
tokens/dist/**
89+
ui-icons/dist/**
8890
key: ubuntu-latest-node18-compiled-assets-${{ steps.set-SHAs.outputs.head }}
8991

9092
- name: Build storybook site
91-
if: ${{ github.event.inputs.storybook-url == '' }}
93+
if: ${{ inputs.storybook-url == '' }}
9294
shell: bash
9395
run: yarn ci:storybook --output-dir ../dist/preview
9496

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"clean:preview": "nx clean storybook",
3232
"precompare": "yarn build",
3333
"compare": "node ./tasks/compare-compiled-output.js",
34-
"predev": "nx run-many --projects @spectrum-css/ui-icons,@spectrum-css/tokens --target build",
34+
"predev": "nx run-many --projects @spectrum-css/ui-icons,tokens --target build",
3535
"dev": "NODE_ENV=development BROWSERSYNC_OPEN=true gulp dev",
3636
"docs:mod": "node tasks/mod-extractor.js",
3737
"postdocs:mod": "prettier --cache --write components/*/metadata/*.md > /dev/null",

tasks/compare-compiled-output.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ const Diff2Html = require("diff2html");
2020

2121
require("colors");
2222

23-
const util = require("util");
24-
const exec = util.promisify(require("child_process").exec);
25-
2623
const pathing = {
2724
root: join(__dirname, "../"),
2825
components: join(__dirname, "../components"),
@@ -44,10 +41,12 @@ env.addFilter("print", (data) => JSON.stringify(data, null, 2));
4441
const log = {
4542
error: (err) => process.stderr.write(`${err}\n\n`),
4643
write: (msg) => process.stdout.write(msg),
47-
writeTable: (data, padding = 30) => {
44+
writeTable: (data, { min = 20, max = 30 } = {}) => {
4845
// This utility function is used to print a table of data to the console
49-
const table = (data = [], size = padding) =>
50-
data.map((row) => `${row ?? " "}`.padEnd(size)).join("");
46+
const table = (data = []) => {
47+
return data.map((row, idx) => `${row ?? " "}`.padEnd(idx === 0 ? max : min)).join("");
48+
};
49+
5150
process.stdout.write(`${table(data)}\n`);
5251
},
5352
};
@@ -129,7 +128,7 @@ async function generateDiff(
129128
async function processComponent(
130129
component,
131130
{
132-
cwd = process.cwd(),
131+
cwd,
133132
output = pathing.output,
134133
cacheLocation = pathing.cache,
135134
}
@@ -139,11 +138,15 @@ async function processComponent(
139138
cleanAndMkdir(join(output, "diffs", component));
140139
cleanAndMkdir(join(output, "latest"));
141140

142-
const pkgPath = join(cwd, component, "package.json");
143-
const pkg = existsSync(pkgPath)
141+
const pkgPath = require.resolve(`@spectrum-css/${component}/package.json`) ?? join(cwd, component, "package.json");
142+
const pkg = pkgPath && existsSync(pkgPath)
144143
? require(pkgPath)
145144
: { name: `@spectrum-css/${component}` };
146145

146+
if (!cwd && pkgPath) {
147+
cwd = dirname(pkgPath).split("/").slice(0, -1).join("/");
148+
}
149+
147150
let tag;
148151
let found = 0;
149152

@@ -312,9 +315,7 @@ async function main(
312315
output,
313316
{ skipCache = false } = {}
314317
) {
315-
let buildAllComponents = false;
316318
if (!components || components.length === 0) {
317-
buildAllComponents = true;
318319
components = allComponents;
319320
}
320321

@@ -339,7 +340,6 @@ async function main(
339340
const results = await Promise.all(
340341
components.map(async (component) => {
341342
return processComponent(component, {
342-
cwd: pathing.components,
343343
output: pathing.output,
344344
cacheLocation: pathing.cache,
345345
}).catch((err) =>
@@ -399,14 +399,19 @@ async function main(
399399
});
400400

401401
// This is our report header to indicate the start of a new component's data
402-
log.writeTable([`\n${_.pad(` ${component} `, 20, "-").cyan}\n`]);
402+
log.write(`\n${_.pad(` ${component} `, 20, "-").cyan}\n`);
403403

404404
if (warnings.length > 0) {
405405
warnings.forEach((warning) => log.write(`${warning}\n`));
406406
}
407407

408+
const maxColumnWidth = files.reduce((max, file) => {
409+
if (file.length > max) max = file.length;
410+
return max;
411+
}, 30);
412+
408413
// Write a table of the file sizes to the console for easy comparison
409-
log.writeTable(["Filename", "Size", "Size (release)"], 20);
414+
log.writeTable(["Filename", "Size", "Size (release)"], { min: 15, max: maxColumnWidth + 5});
410415

411416
files.forEach(async (file) => {
412417
let hasFileChange = false;
@@ -416,7 +421,7 @@ async function main(
416421
`${file}`.green,
417422
local?.size ? `${bytesToSize(local.size)}`.gray : `** removed **`.red,
418423
npm?.size ? `${bytesToSize(npm.size)}`.gray : `** new **`.yellow,
419-
]);
424+
], { min: 25, max: maxColumnWidth + 15});
420425

421426
if (local?.size && npm?.size && local.size !== npm.size) {
422427
hasFileChange = true;

tokens/package.json

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,9 @@
1414
"url": "https://github.com/adobe/spectrum-css/issues"
1515
},
1616
"main": "dist/index.css",
17-
"scripts": {
18-
"build": "run-s convert:tokens css:prepare format:results clean:empty",
19-
"build:tokens": "style-dictionary build --config style-dictionary.config.js && postcss --replace dist/css/*.css",
20-
"clean": "rimraf dist",
21-
"clean:empty": "find dist -type f -empty -delete",
22-
"concat:dist": "concat-cli -f dist/css/*.css dist/css/**/*.css -o dist/index.css",
23-
"convert:tokens": "run-p build:tokens copy:*",
24-
"copy:express": "postcss --dir dist/css/express/ custom-express/*.css",
25-
"copy:spectrum": "postcss --dir dist/css/spectrum/ custom-spectrum/*.css",
26-
"css:prepare": "yarn concat:dist && postcss --replace dist/index.css",
27-
"format:results": "prettier --no-config --no-error-on-unmatched-pattern --ignore-unknown --loglevel silent --write dist/"
28-
},
2917
"devDependencies": {
3018
"@adobe/spectrum-tokens": "12.23.1",
31-
"concat-cli": "^4.0.0",
32-
"npm-run-all": "^4.1.5",
19+
"@nxkit/style-dictionary": "^3.0.2",
3320
"postcss": "^8.4.21",
3421
"postcss-cli": "^10.1.0",
3522
"postcss-combine-duplicated-selectors": "^10.0.3",
@@ -40,27 +27,10 @@
4027
"postcss-rgb-mapping": "^1.0.1",
4128
"postcss-sorting": "^8.0.2",
4229
"prettier": "^2.8.8",
43-
"rimraf": "^5.0.1",
4430
"style-dictionary": "^3.8.0",
4531
"style-dictionary-sets": "^2.3.0"
4632
},
4733
"publishConfig": {
4834
"access": "public"
49-
},
50-
"nx": {
51-
"targets": {
52-
"build": {
53-
"inputs": [
54-
"{workspaceRoot}/postcss.config.js",
55-
"{workspaceRoot}/style-dictionary.config.js",
56-
"{workspaceRoot}/utilities/style-dictionary.utils.js",
57-
"{workspaceRoot}/custom-express/*.css",
58-
"{workspaceRoot}/custom-spectrum/*.css"
59-
]
60-
}
61-
},
62-
"includedScripts": [
63-
"build"
64-
]
6535
}
6636
}

0 commit comments

Comments
 (0)