@terrazzo/cli 0.0.13 → 0.0.15

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 (2) hide show
  1. package/bin/cli.js +27 -23
  2. package/package.json +3 -3
package/bin/cli.js CHANGED
@@ -23,7 +23,7 @@
23
23
  * SOFTWARE.
24
24
  */
25
25
 
26
- import { parse, build, defineConfig } from '@terrazzo/parser';
26
+ import { parse, build, defineConfig, Logger } from '@terrazzo/parser';
27
27
  import chokidar from 'chokidar';
28
28
  import dotenv from 'dotenv';
29
29
  import fs from 'node:fs';
@@ -37,6 +37,10 @@ dotenv.config();
37
37
 
38
38
  const [, , cmd, ...args] = process.argv;
39
39
  const cwd = new URL(`file://${process.cwd()}/`);
40
+ const PKG_ROOT = new URL('../', import.meta.url);
41
+ const logger = new Logger(
42
+ // TODO: listen for env vars for debugging, log level, etc
43
+ );
40
44
 
41
45
  const GREEN_CHECK = pc.green('✔');
42
46
 
@@ -65,7 +69,7 @@ export default async function main() {
65
69
 
66
70
  // --version
67
71
  if (flags.version) {
68
- const { version } = parseJSON(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
72
+ const { version } = parseJSON(fs.readFileSync(new URL('./package.json', PKG_ROOT), 'utf8'));
69
73
  console.log(version);
70
74
  process.exit(0);
71
75
  }
@@ -89,11 +93,11 @@ export default async function main() {
89
93
  const mod = await import(resolvedConfigPath);
90
94
  if (!mod.default) {
91
95
  printErrors(
92
- `No default export found in ${path.relative(fileURLToPath(cwd), resolvedConfigPath)}. See https://terrazzo.dev/docs/cli for instructions.`,
96
+ `No default export found in ${path.relative(cwd.href, resolvedConfigPath)}. See https://terrazzo.dev/docs/cli for instructions.`,
93
97
  );
94
98
  process.exit(1);
95
99
  }
96
- config = defineConfig(mod.default, { cwd });
100
+ config = defineConfig(mod.default, { cwd, logger });
97
101
  } catch (err) {
98
102
  printErrors(err.message || err);
99
103
  process.exit(1);
@@ -114,8 +118,8 @@ export default async function main() {
114
118
 
115
119
  const watch = args.includes('-w') || args.includes('--watch');
116
120
 
117
- let { tokens, ast } = await parse(rawSchemas, { config, yamlToMomoa });
118
- let result = await build(tokens, { ast, config });
121
+ let { tokens, ast } = await parse(rawSchemas, { config, logger, yamlToMomoa });
122
+ let result = await build(tokens, { ast, config, logger });
119
123
  writeFiles(result, config);
120
124
 
121
125
  printErrors(result.errors);
@@ -130,10 +134,10 @@ export default async function main() {
130
134
  tokenWatcher.on('change', async (filename) => {
131
135
  try {
132
136
  rawSchemas = await loadTokens(config.tokens);
133
- const parseResult = await parse(rawSchemas, { config, yamlToMomoa });
137
+ const parseResult = await parse(rawSchemas, { config, logger, yamlToMomoa });
134
138
  tokens = parseResult.tokens;
135
139
  ast = parseResult.ast;
136
- result = await build(tokens, { ast, config });
140
+ result = await build(tokens, { ast, config, logger });
137
141
  console.log(
138
142
  `${pc.dim(dt.format(new Date()))} ${pc.green('Tz')}} ${pc.yellow(filename)} updated ${GREEN_CHECK}`,
139
143
  );
@@ -149,10 +153,10 @@ export default async function main() {
149
153
  );
150
154
  config = (await import(filename)).default;
151
155
  rawSchema = await loadTokens(config.tokens);
152
- const parseResult = await parse(tokens, { config, yamlToMomoa });
156
+ const parseResult = await parse(tokens, { config, logger, yamlToMomoa });
153
157
  tokens = parseResult.tokens;
154
158
  ast = parseResult.ast;
155
- result = await build(tokens, { ast, config });
159
+ result = await build(tokens, { ast, config, logger });
156
160
  writeFiles(result, config);
157
161
  } catch (err) {
158
162
  printErrors([err.message || err]);
@@ -171,19 +175,18 @@ export default async function main() {
171
175
  case 'check': {
172
176
  const rawSchemas = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
173
177
  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
178
+ console.log(pc.underline(filename.protocol === 'file:' ? filename.href : filename));
179
+ await parse(rawSchemas, { config, continueOnError: true, logger, yamlToMomoa }); // will throw if errors
176
180
  printSuccess(`No errors ${time(start)}`);
177
181
  break;
178
182
  }
179
183
  case 'lint': {
180
184
  if (!Array.isArray(config.plugins) || !config.plugins.length) {
181
185
  printErrors(`No plugins defined! Add some in ${configPath || 'tokens.config.js'}`);
182
- process.exit(1);
183
186
  }
184
187
 
185
188
  const rawSchema = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
186
- const parseResult = await parse(rawSchema, { config, continueOnError: true, yamlToMomoa }); // will throw if errors
189
+ const parseResult = await parse(rawSchema, { config, continueOnError: true, logger, yamlToMomoa }); // will throw if errors
187
190
 
188
191
  // TODO
189
192
 
@@ -195,11 +198,11 @@ export default async function main() {
195
198
  !fs.existsSync(new URL('./terrazzo.config.mjs', cwd)) &&
196
199
  !fs.existsSync(new URL('./terrazzo.config.cjs', cwd))
197
200
  ) {
198
- fs.cpSync(new URL('../terrazzo.config.js', import.meta.url), new URL('./terrazzo.config.js', cwd));
201
+ fs.cpSync(new URL('./terrazzo.config.js', PKG_ROOT), new URL('./terrazzo.config.js', cwd));
199
202
  printSuccess('terrazzo.config.js created');
200
203
  }
201
204
  if (!fs.existsSync(config.tokens[0])) {
202
- fs.cpSync(new URL('../tokens-example.json', import.meta.url), new URL(config?.tokens, cwd));
205
+ fs.cpSync(new URL('./tokens-example.json', PKG_ROOT), new URL(config?.tokens, cwd));
203
206
  printSuccess(`${config.tokens} created ${time(start)}`);
204
207
  }
205
208
  break;
@@ -254,7 +257,7 @@ async function loadTokens(tokenPaths) {
254
257
  tokenPaths[0] = yamlPath;
255
258
  } else {
256
259
  printErrors(
257
- `Could not locate ${path.relative(fileURLToPath(cwd), fileURLToPath(tokenPaths[0]))}. To create one, run \`npx tz init\`.`,
260
+ `Could not locate ${path.relative(cwd.href, tokenPaths[0].href)}. To create one, run \`npx tz init\`.`,
258
261
  );
259
262
  process.exit(1);
260
263
  }
@@ -316,9 +319,7 @@ async function loadTokens(tokenPaths) {
316
319
  if (fs.existsSync(filename)) {
317
320
  allTokens.push({ filename, src: fs.readFileSync(filename, 'utf8') });
318
321
  } else {
319
- printErrors(
320
- `Could not locate ${path.relative(fileURLToPath(cwd), fileURLToPath(filename))}. To create one, run \`npx tz init\`.`,
321
- );
322
+ printErrors(`Could not locate ${path.relative(cwd.href, filename.href)}. To create one, run \`npx tz init\`.`);
322
323
  process.exit(1);
323
324
  }
324
325
  }
@@ -336,16 +337,19 @@ function resolveConfig(filename) {
336
337
  if (filename) {
337
338
  const configPath = new URL(filename, cwd);
338
339
  if (fs.existsSync(configPath)) {
339
- return fileURLToPath(configPath);
340
+ return configPath.href; // ⚠️ ESM wants "file://..." URLs on Windows & Unix.
340
341
  }
341
342
  return undefined;
342
343
  }
343
344
 
344
345
  // default: terrazzo.config.js
345
- for (const defaultFilename of ['./terrazzo.config.js', './terrazzo.config.mjs']) {
346
+ for (const defaultFilename of [
347
+ './terrazzo.config.mjs', // .mjs takes precedence (it suggestes .js is CJS)
348
+ './terrazzo.config.js',
349
+ ]) {
346
350
  const configPath = new URL(defaultFilename, cwd);
347
351
  if (fs.existsSync(configPath)) {
348
- return fileURLToPath(configPath);
352
+ return configPath.href; // ⚠️ ESM wants "file://..." URLs on Windows & Unix.
349
353
  }
350
354
  }
351
355
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrazzo/cli",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
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": {
@@ -38,8 +38,8 @@
38
38
  "picocolors": "^1.0.1",
39
39
  "yaml-to-momoa": "^0.0.1",
40
40
  "yargs-parser": "^21.1.1",
41
- "@terrazzo/parser": "^0.0.13",
42
- "@terrazzo/token-tools": "^0.0.6"
41
+ "@terrazzo/parser": "^0.0.15",
42
+ "@terrazzo/token-tools": "^0.0.8"
43
43
  },
44
44
  "devDependencies": {
45
45
  "typescript": "^5.5.4"