@terrazzo/cli 0.0.11 → 0.0.13

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/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # @terrazzo/cli
2
+
3
+ ## 0.0.13
4
+
5
+ ### Patch Changes
6
+
7
+ - [#289](https://github.com/terrazzoapp/terrazzo/pull/289) [`0fc9738`](https://github.com/terrazzoapp/terrazzo/commit/0fc9738bb3dfecb680d225e4bd3970f21cfe8079) Thanks [@drwpow](https://github.com/drwpow)! - Add YAML support
8
+
9
+ - Updated dependencies [[`0fc9738`](https://github.com/terrazzoapp/terrazzo/commit/0fc9738bb3dfecb680d225e4bd3970f21cfe8079), [`6a875b1`](https://github.com/terrazzoapp/terrazzo/commit/6a875b163539dba8111911851a7819732056b3aa)]:
10
+ - @terrazzo/parser@0.0.13
11
+
12
+ ## 0.0.12
13
+
14
+ ### Patch Changes
15
+
16
+ - [#285](https://github.com/terrazzoapp/terrazzo/pull/285) [`e8a0df1`](https://github.com/terrazzoapp/terrazzo/commit/e8a0df1f3b50cf7cb292bcc475aae271feae4569) Thanks [@drwpow](https://github.com/drwpow)! - Add support for multiple token files
17
+
18
+ - Updated dependencies [[`e8a0df1`](https://github.com/terrazzoapp/terrazzo/commit/e8a0df1f3b50cf7cb292bcc475aae271feae4569)]:
19
+ - @terrazzo/token-tools@0.0.6
20
+ - @terrazzo/parser@0.0.12
package/bin/cli.js CHANGED
@@ -30,6 +30,7 @@ import fs from 'node:fs';
30
30
  import path from 'node:path';
31
31
  import { fileURLToPath } from 'node:url';
32
32
  import pc from 'picocolors';
33
+ import yamlToMomoa from 'yaml-to-momoa';
33
34
  import parser from 'yargs-parser';
34
35
 
35
36
  dotenv.config();
@@ -109,11 +110,11 @@ export default async function main() {
109
110
  process.exit(1);
110
111
  }
111
112
 
112
- let rawSchema = await loadTokens(config.tokens);
113
+ let rawSchemas = await loadTokens(config.tokens);
113
114
 
114
115
  const watch = args.includes('-w') || args.includes('--watch');
115
116
 
116
- let { tokens, ast } = await parse(rawSchema, { config });
117
+ let { tokens, ast } = await parse(rawSchemas, { config, yamlToMomoa });
117
118
  let result = await build(tokens, { ast, config });
118
119
  writeFiles(result, config);
119
120
 
@@ -125,30 +126,30 @@ export default async function main() {
125
126
  hour: '2-digit',
126
127
  minute: '2-digit',
127
128
  });
128
- const tokenWatcher = chokidar.watch(config.tokens.map((filepath) => fileURLToPath(filepath)));
129
- tokenWatcher.on('change', async (filePath) => {
129
+ const tokenWatcher = chokidar.watch(config.tokens.map((filename) => fileURLToPath(filename)));
130
+ tokenWatcher.on('change', async (filename) => {
130
131
  try {
131
- rawSchema = await loadTokens(config.tokens);
132
- const parseResult = await parse(tokens, { config });
132
+ rawSchemas = await loadTokens(config.tokens);
133
+ const parseResult = await parse(rawSchemas, { config, yamlToMomoa });
133
134
  tokens = parseResult.tokens;
134
135
  ast = parseResult.ast;
135
136
  result = await build(tokens, { ast, config });
136
137
  console.log(
137
- `${pc.dim(dt.format(new Date()))} ${pc.green('Tz')}} ${pc.yellow(filePath)} updated ${GREEN_CHECK}`,
138
+ `${pc.dim(dt.format(new Date()))} ${pc.green('Tz')}} ${pc.yellow(filename)} updated ${GREEN_CHECK}`,
138
139
  );
139
140
  } catch (err) {
140
141
  printErrors([err.message || err]);
141
142
  }
142
143
  });
143
144
  const configWatcher = chokidar.watch(resolveConfig(configPath));
144
- configWatcher.on('change', async (filePath) => {
145
+ configWatcher.on('change', async (filename) => {
145
146
  try {
146
147
  console.log(
147
148
  `${pc.dim(dt.format(new Date()))} ${pc.green('Tz')} ${pc.yellow('Config updated. Reloading…')}`,
148
149
  );
149
- config = (await import(filePath)).default;
150
+ config = (await import(filename)).default;
150
151
  rawSchema = await loadTokens(config.tokens);
151
- const parseResult = await parse(tokens, { config });
152
+ const parseResult = await parse(tokens, { config, yamlToMomoa });
152
153
  tokens = parseResult.tokens;
153
154
  ast = parseResult.ast;
154
155
  result = await build(tokens, { ast, config });
@@ -168,10 +169,10 @@ export default async function main() {
168
169
  break;
169
170
  }
170
171
  case 'check': {
171
- const rawSchema = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
172
- const filepath = flags._[0] || config.tokens[0];
173
- console.log(pc.underline(filepath.protocol === 'file:' ? fileURLToPath(filepath) : filepath));
174
- await parse(rawSchema, { config, continueOnError: true }); // will throw if errors
172
+ const rawSchemas = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
173
+ const filename = flags._[0] || config.tokens[0];
174
+ console.log(pc.underline(filename.protocol === 'file:' ? fileURLToPath(filename) : filename));
175
+ await parse(rawSchemas, { config, continueOnError: true, yamlToMomoa }); // will throw if errors
175
176
  printSuccess(`No errors ${time(start)}`);
176
177
  break;
177
178
  }
@@ -182,7 +183,7 @@ export default async function main() {
182
183
  }
183
184
 
184
185
  const rawSchema = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
185
- const parseResult = await parse(rawSchema, { config, continueOnError: true }); // will throw if errors
186
+ const parseResult = await parse(rawSchema, { config, continueOnError: true, yamlToMomoa }); // will throw if errors
186
187
 
187
188
  // TODO
188
189
 
@@ -238,21 +239,45 @@ function showHelp() {
238
239
 
239
240
  /** load tokens */
240
241
  async function loadTokens(tokenPaths) {
241
- // TODO: merge tokens
242
-
243
242
  const allTokens = [];
244
243
 
244
+ if (!Array.isArray(tokenPaths)) {
245
+ printErrors(`loadTokens: Expected array, received ${typeof tokenPaths}`);
246
+ process.exit(1);
247
+ }
248
+
249
+ // if this is the default value, also check for tokens.yaml
250
+ if (tokenPaths.length === 1 && tokenPaths[0].href === new URL('./tokens.json', cwd).href) {
251
+ if (!fs.existsSync(tokenPaths[0])) {
252
+ const yamlPath = new URL('./tokens.yaml', cwd);
253
+ if (fs.existsSync(yamlPath)) {
254
+ tokenPaths[0] = yamlPath;
255
+ } else {
256
+ printErrors(
257
+ `Could not locate ${path.relative(fileURLToPath(cwd), fileURLToPath(tokenPaths[0]))}. To create one, run \`npx tz init\`.`,
258
+ );
259
+ process.exit(1);
260
+ }
261
+ }
262
+ }
263
+
245
264
  // download/read
246
265
  for (let i = 0; i < tokenPaths.length; i++) {
247
- const filepath = tokenPaths[i];
248
- if (filepath.protocol === 'http:' || filepath.protocol === 'https:') {
266
+ const filename = tokenPaths[i];
267
+
268
+ if (!(filename instanceof URL)) {
269
+ printErrors(`loadTokens[${i}]: Expected URL, received ${filename}`);
270
+ process.exit(1);
271
+ }
272
+
273
+ if (filename.protocol === 'http:' || filename.protocol === 'https:') {
249
274
  try {
250
275
  // if Figma URL
251
- if (filepath.host === 'figma.com' || filepath.host === 'www.figma.com') {
252
- const [_, fileKeyword, fileKey] = filepath.pathname.split('/');
276
+ if (filename.host === 'figma.com' || filename.host === 'www.figma.com') {
277
+ const [_, fileKeyword, fileKey] = filename.pathname.split('/');
253
278
  if (fileKeyword !== 'file' || !fileKey) {
254
279
  printErrors(
255
- `Unexpected Figma URL. Expected "https://www.figma.com/file/:file_key/:file_name?…", received "${filepath.href}"`,
280
+ `Unexpected Figma URL. Expected "https://www.figma.com/file/:file_key/:file_name?…", received "${filename.href}"`,
256
281
  );
257
282
  process.exit(1);
258
283
  }
@@ -270,7 +295,7 @@ async function loadTokens(tokenPaths) {
270
295
  headers,
271
296
  });
272
297
  if (res.ok) {
273
- allTokens.push(await res.text());
298
+ allTokens.push({ filename, src: await res.text() });
274
299
  }
275
300
  const message = res.status !== 404 ? JSON.stringify(await res.json(), undefined, 2) : '';
276
301
  printErrors(`Figma responded with ${res.status}${message ? `:\n${message}` : ''}`);
@@ -279,27 +304,27 @@ async function loadTokens(tokenPaths) {
279
304
  }
280
305
 
281
306
  // otherwise, expect YAML/JSON
282
- const res = await fetch(filepath, {
307
+ const res = await fetch(filename, {
283
308
  method: 'GET',
284
309
  headers: { Accept: '*/*', 'User-Agent': 'Mozilla/5.0 Gecko/20100101 Firefox/123.0' },
285
310
  });
286
- allTokens.push(await res.text());
311
+ allTokens.push({ filename, src: await res.text() });
287
312
  } catch (err) {
288
- printErrors(`${filepath.href}: ${err}`);
313
+ printErrors(`${filename.href}: ${err}`);
289
314
  }
290
315
  } else {
291
- if (fs.existsSync(filepath)) {
292
- allTokens.push(fs.readFileSync(filepath, 'utf8'));
316
+ if (fs.existsSync(filename)) {
317
+ allTokens.push({ filename, src: fs.readFileSync(filename, 'utf8') });
293
318
  } else {
294
319
  printErrors(
295
- `Could not locate ${path.relative(fileURLToPath(cwd), fileURLToPath(filepath))}. To create one, run \`npx tz init\`.`,
320
+ `Could not locate ${path.relative(fileURLToPath(cwd), fileURLToPath(filename))}. To create one, run \`npx tz init\`.`,
296
321
  );
297
322
  process.exit(1);
298
323
  }
299
324
  }
300
325
  }
301
326
 
302
- return allTokens[0];
327
+ return allTokens;
303
328
  }
304
329
 
305
330
  /**
@@ -326,14 +351,14 @@ function resolveConfig(filename) {
326
351
  }
327
352
 
328
353
  /** Resolve tokens.json path (for lint command) */
329
- function resolveTokenPath(filepath) {
330
- const tokensPath = new URL(filepath, cwd);
354
+ function resolveTokenPath(filename) {
355
+ const tokensPath = new URL(filename, cwd);
331
356
  if (!fs.existsSync(tokensPath)) {
332
- printErrors(`Could not locate ${filepath}. Does the file exist?`);
357
+ printErrors(`Could not locate ${filename}. Does the file exist?`);
333
358
  process.exit(1);
334
359
  }
335
360
  if (!fs.statSync(tokensPath).isFile()) {
336
- printErrors(`Expected JSON or YAML file, received ${filepath}.`);
361
+ printErrors(`Expected JSON or YAML file, received ${filename}.`);
337
362
  process.exit(1);
338
363
  }
339
364
  return tokensPath;
@@ -373,8 +398,8 @@ export function printWarnings(warnings) {
373
398
  /** Write files */
374
399
  export function writeFiles(result, config) {
375
400
  for (const { filename, contents } of result.outputFiles) {
376
- const filepath = new URL(filename, config.outDir);
377
- fs.mkdirSync(new URL('.', filepath), { recursive: true });
378
- fs.writeFileSync(filepath, contents);
401
+ const output = new URL(filename, config.outDir);
402
+ fs.mkdirSync(new URL('.', output), { recursive: true });
403
+ fs.writeFileSync(output, contents);
379
404
  }
380
405
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrazzo/cli",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "CLI for managing design tokens using the Design Tokens Community Group (DTCG) standard and generating code for any platform via plugins.",
5
5
  "type": "module",
6
6
  "author": {
@@ -36,16 +36,17 @@
36
36
  "dotenv": "^16.4.5",
37
37
  "merge-anything": "^5.1.7",
38
38
  "picocolors": "^1.0.1",
39
+ "yaml-to-momoa": "^0.0.1",
39
40
  "yargs-parser": "^21.1.1",
40
- "@terrazzo/parser": "^0.0.11",
41
- "@terrazzo/token-tools": "^0.0.4"
41
+ "@terrazzo/parser": "^0.0.13",
42
+ "@terrazzo/token-tools": "^0.0.6"
42
43
  },
43
44
  "devDependencies": {
44
- "typescript": "^5.5.3"
45
+ "typescript": "^5.5.4"
45
46
  },
46
47
  "scripts": {
47
48
  "build": "pnpm run build:clean && pnpm run build:ts",
48
- "build:clean": "del dist",
49
+ "build:clean": "del-cli dist",
49
50
  "build:ts": "tsc -p tsconfig.build.json",
50
51
  "dev": "tsc -p tsconfig.build.json -w",
51
52
  "lint": "biome check .",
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { type Config, type ConfigInit } from '@terrazzo/parser';
2
- export declare function defineConfig(config: Config): ConfigInit;
package/dist/index.js DELETED
@@ -1,5 +0,0 @@
1
- import { defineConfig as defineConfigCore } from '@terrazzo/parser';
2
- export function defineConfig(config) {
3
- return defineConfigCore(config, { cwd: new URL(`file://${process.cwd()}/`) });
4
- }
5
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,YAAY,IAAI,gBAAgB,EAAmB,MAAM,kBAAkB,CAAC;AAElG,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,OAAO,gBAAgB,CAAC,MAAM,EAAE,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,UAAU,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;AAChF,CAAC"}