@khaledeltohamy14/token-pipeline-engine 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khaledeltohamy14/token-pipeline-engine",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Reusable Galaxy design token pipeline engine for splitting design tokens and generating token/theme files.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -1,58 +1,11 @@
1
- const CATEGORY_NAMES = [
2
- 'global',
3
- 'alias',
4
- 'components',
5
- 'spacing',
6
- 'typography',
7
- 'radius',
8
- 'shadow',
9
- 'dimensions',
10
- ];
11
-
12
- function asRecord(value) {
13
- return value && typeof value === 'object' && !Array.isArray(value)
14
- ? value
15
- : {};
16
- }
17
-
18
- function splitTokenDocument(document) {
19
- const source = asRecord(document);
20
- const tokens = asRecord(source.tokens);
21
- const result = {};
22
-
23
- for (const category of CATEGORY_NAMES) {
24
- result[category] = asRecord(tokens[category]);
25
- }
26
-
27
- result.brands = asRecord(tokens.brands);
28
-
29
- return result;
30
- }
31
-
32
- function splitTokenFiles(document, root = 'token_pipeline/tokens') {
33
- const split = splitTokenDocument(document);
34
- const files = [];
35
-
36
- for (const category of CATEGORY_NAMES) {
37
- files.push({
38
- path: `${root}/${category}.json`,
39
- content: `${JSON.stringify(split[category], null, 2)}\n`,
40
- mode: '100644',
41
- });
42
- }
43
-
44
- for (const [brandName, brandTokens] of Object.entries(split.brands)) {
45
- files.push({
46
- path: `token_pipeline/brands/${brandName}.json`,
47
- content: `${JSON.stringify(asRecord(brandTokens), null, 2)}\n`,
48
- mode: '100644',
49
- });
50
- }
51
-
52
- return files;
53
- }
54
-
55
- module.exports = {
56
- splitTokenDocument,
57
- splitTokenFiles,
58
- };
1
+ const CATEGORY_NAMES = ['global', 'alias', 'components', 'spacing', 'typography', 'radius', 'shadow', 'dimensions'];
2
+ const ALIASES = { global: 'global', globals: 'global', alias: 'alias', aliases: 'alias', component: 'components', components: 'components', spacing: 'spacing', typography: 'typography', radius: 'radius', shadow: 'shadow', dimension: 'dimensions', dimensions: 'dimensions' };
3
+ const asRecord = v => v && typeof v === 'object' && !Array.isArray(v) ? v : {};
4
+ const norm = v => String(v || '').trim().replace(/[_-]+/g, ' ').replace(/\s+/g, ' ').toLowerCase();
5
+ const clone = v => v && typeof v === 'object' ? (Array.isArray(v) ? v.map(clone) : Object.fromEntries(Object.entries(v).map(([k, x]) => [k, clone(x)]))) : v;
6
+ function setNested(root, path, value) { let cur = root; const parts = path.filter(Boolean); if (!parts.length) return; for (let i = 0; i < parts.length - 1; i++) { if (!cur[parts[i]] || typeof cur[parts[i]] !== 'object' || Array.isArray(cur[parts[i]])) cur[parts[i]] = {}; cur = cur[parts[i]]; } cur[parts.at(-1)] = clone(value); }
7
+ function leaf(v) { return !!(v && typeof v === 'object' && !Array.isArray(v) && ('$value' in v || '$type' in v || v.$extensions?.figma)); }
8
+ function collect(v, path, out) { if (leaf(v)) { const c = norm(v.$extensions?.figma?.collectionName), cat = ALIASES[c]; if (cat) { setNested(out[cat], path, v); } else if (c) { if (!out.brands[c]) out.brands[c] = {}; setNested(out.brands[c], path, v); } return; } if (!v || typeof v !== 'object' || Array.isArray(v)) return; for (const [k, x] of Object.entries(v)) collect(x, path.concat(k), out); }
9
+ function splitTokenDocument(document) { const tokens = asRecord(asRecord(document).tokens); if (CATEGORY_NAMES.some(k => Object.hasOwn(tokens, k))) { const r = {}; for (const k of CATEGORY_NAMES) r[k] = asRecord(tokens[k]); r.brands = asRecord(tokens.brands); return r; } const r = Object.fromEntries(CATEGORY_NAMES.map(k => [k, {}])); r.brands = {}; collect(tokens, [], r); return r; }
10
+ function splitTokenFiles(document, root = 'token_pipeline/tokens') { const s = splitTokenDocument(document), files = CATEGORY_NAMES.map(k => ({ path: `${root}/${k}.json`, content: `${JSON.stringify(s[k], null, 2)}\n`, mode: '100644' })); for (const [name, tokens] of Object.entries(s.brands)) files.push({ path: `token_pipeline/brands/${name}.json`, content: `${JSON.stringify(asRecord(tokens), null, 2)}\n`, mode: '100644' }); return files; }
11
+ module.exports = { splitTokenDocument, splitTokenFiles };