@terrazzo/cli 0.0.8 → 0.0.10

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.
Files changed (3) hide show
  1. package/bin/cli.js +29 -15
  2. package/package.json +3 -4
  3. package/src/index.ts +0 -5
package/bin/cli.js CHANGED
@@ -23,10 +23,11 @@
23
23
  * SOFTWARE.
24
24
  */
25
25
 
26
- import { parse, build } from '@terrazzo/parser';
26
+ import { parse, build, defineConfig } from '@terrazzo/parser';
27
27
  import chokidar from 'chokidar';
28
28
  import dotenv from 'dotenv';
29
29
  import fs from 'node:fs';
30
+ import path from 'node:path';
30
31
  import { fileURLToPath } from 'node:url';
31
32
  import pc from 'picocolors';
32
33
  import parser from 'yargs-parser';
@@ -84,7 +85,14 @@ export default async function main() {
84
85
  const resolvedConfigPath = resolveConfig(configPath);
85
86
  if (resolvedConfigPath) {
86
87
  try {
87
- config = (await import(resolvedConfigPath)).default;
88
+ const mod = await import(resolvedConfigPath);
89
+ if (!mod.default) {
90
+ printErrors(
91
+ `No default export found in ${path.relative(fileURLToPath(cwd), resolvedConfigPath)}. See https://terrazzo.dev/docs/cli for instructions.`,
92
+ );
93
+ process.exit(1);
94
+ }
95
+ config = defineConfig(mod.default, { cwd });
88
96
  } catch (err) {
89
97
  printErrors(err.message || err);
90
98
  process.exit(1);
@@ -103,10 +111,6 @@ export default async function main() {
103
111
 
104
112
  let rawSchema = await loadTokens(config.tokens);
105
113
 
106
- const dt = new Intl.DateTimeFormat('en-us', {
107
- hour: '2-digit',
108
- minute: '2-digit',
109
- });
110
114
  const watch = args.includes('-w') || args.includes('--watch');
111
115
 
112
116
  let { tokens, ast } = await parse(rawSchema, { config });
@@ -117,6 +121,10 @@ export default async function main() {
117
121
  printWarnings(result.warnings);
118
122
 
119
123
  if (watch) {
124
+ const dt = new Intl.DateTimeFormat('en-us', {
125
+ hour: '2-digit',
126
+ minute: '2-digit',
127
+ });
120
128
  const tokenWatcher = chokidar.watch(config.tokens.map((filepath) => fileURLToPath(filepath)));
121
129
  tokenWatcher.on('change', async (filePath) => {
122
130
  try {
@@ -163,7 +171,7 @@ export default async function main() {
163
171
  const rawSchema = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
164
172
  const filepath = flags._[0] || config.tokens[0];
165
173
  console.log(pc.underline(filepath.protocol === 'file:' ? fileURLToPath(filepath) : filepath));
166
- await parse(rawSchema, { config }); // will throw if errors
174
+ await parse(rawSchema, { config, continueOnError: true }); // will throw if errors
167
175
  printSuccess(`No errors ${time(start)}`);
168
176
  break;
169
177
  }
@@ -174,7 +182,7 @@ export default async function main() {
174
182
  }
175
183
 
176
184
  const rawSchema = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
177
- const parseResult = await parse(rawSchema, { config }); // will throw if errors
185
+ const parseResult = await parse(rawSchema, { config, continueOnError: true }); // will throw if errors
178
186
 
179
187
  // TODO
180
188
 
@@ -230,11 +238,13 @@ function showHelp() {
230
238
 
231
239
  /** load tokens */
232
240
  async function loadTokens(tokenPaths) {
233
- // TODO: allow merging of tokens
241
+ // TODO: merge tokens
242
+
243
+ const allTokens = [];
234
244
 
235
245
  // download/read
236
- for (const filepath of tokenPaths) {
237
- const pathname = filepath.pathname.toLowerCase();
246
+ for (let i = 0; i < tokenPaths.length; i++) {
247
+ const filepath = tokenPaths[i];
238
248
  if (filepath.protocol === 'http:' || filepath.protocol === 'https:') {
239
249
  try {
240
250
  // if Figma URL
@@ -260,7 +270,7 @@ async function loadTokens(tokenPaths) {
260
270
  headers,
261
271
  });
262
272
  if (res.ok) {
263
- return await res.text();
273
+ allTokens.push(await res.text());
264
274
  }
265
275
  const message = res.status !== 404 ? JSON.stringify(await res.json(), undefined, 2) : '';
266
276
  printErrors(`Figma responded with ${res.status}${message ? `:\n${message}` : ''}`);
@@ -273,19 +283,23 @@ async function loadTokens(tokenPaths) {
273
283
  method: 'GET',
274
284
  headers: { Accept: '*/*', 'User-Agent': 'Mozilla/5.0 Gecko/20100101 Firefox/123.0' },
275
285
  });
276
- return await res.text();
286
+ allTokens.push(await res.text());
277
287
  } catch (err) {
278
288
  printErrors(`${filepath.href}: ${err}`);
279
289
  }
280
290
  } else {
281
291
  if (fs.existsSync(filepath)) {
282
- return fs.readFileSync(filepath, 'utf8');
292
+ allTokens.push(fs.readFileSync(filepath, 'utf8'));
283
293
  } else {
284
- printErrors(`Could not locate ${filepath}. To create one, run \`npx cobalt init\`.`);
294
+ printErrors(
295
+ `Could not locate ${path.relative(fileURLToPath(cwd), fileURLToPath(filepath))}. To create one, run \`npx tz init\`.`,
296
+ );
285
297
  process.exit(1);
286
298
  }
287
299
  }
288
300
  }
301
+
302
+ return allTokens[0];
289
303
  }
290
304
 
291
305
  /** resolve config */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrazzo/cli",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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": {
@@ -37,12 +37,11 @@
37
37
  "merge-anything": "^5.1.7",
38
38
  "picocolors": "^1.0.1",
39
39
  "yargs-parser": "^21.1.1",
40
- "@terrazzo/parser": "^0.0.8",
40
+ "@terrazzo/parser": "^0.0.10",
41
41
  "@terrazzo/token-tools": "^0.0.4"
42
42
  },
43
43
  "devDependencies": {
44
- "@types/node": "^20.14.9",
45
- "execa": "^9.3.0"
44
+ "typescript": "^5.5.3"
46
45
  },
47
46
  "scripts": {
48
47
  "build": "pnpm run build:clean && pnpm run build:ts",
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- import { type Config, defineConfig as defineConfigCore, type ConfigInit } from '@terrazzo/parser';
2
-
3
- export function defineConfig(config: Config): ConfigInit {
4
- return defineConfigCore(config, { cwd: new URL(`file://${process.cwd()}/`) });
5
- }