Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint API #353

Merged
merged 12 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fluffy-islands-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@terrazzo/token-tools": minor
"@terrazzo/parser": minor
"@terrazzo/cli": minor
---

Add lint API
5 changes: 5 additions & 0 deletions .changeset/giant-geese-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/cli": patch
---

Create terrazzo.config.mjs by default (still likely a person’s package is CJS by default)
7 changes: 7 additions & 0 deletions .changeset/late-hats-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@terrazzo/token-tools": patch
"@terrazzo/parser": patch
"@terrazzo/cli": patch
---

Add support for $deprecated tokens
5 changes: 5 additions & 0 deletions .changeset/purple-dolls-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/token-tools": minor
---

Change tokenToCulori signature to only accept values
5 changes: 5 additions & 0 deletions .changeset/selfish-fans-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/parser": patch
---

Fix bug in alias resolution
5 changes: 5 additions & 0 deletions .changeset/serious-wombats-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/parser": patch
---

Fix type error with parser output
5 changes: 5 additions & 0 deletions .changeset/tidy-laws-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@terrazzo/cli": patch
---

Ditch yargs-parser in favor of native Node parseArgs
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
node-version: 22
- uses: pnpm/action-setup@v4
with:
version: 9.13.2
version: 9.14.2
run_install: true
- run: pnpm run lint
test:
Expand All @@ -35,7 +35,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- uses: pnpm/action-setup@v4
with:
version: 9.13.2
version: 9.14.2
run_install: true
- run: pnpm test
test-macos:
Expand All @@ -47,7 +47,7 @@ jobs:
node-version: 22.x
- uses: pnpm/action-setup@v4
with:
version: 9.13.2
version: 9.14.2
run_install: true
- run: pnpm test
test-windows:
Expand All @@ -59,6 +59,6 @@ jobs:
node-version: 22.x
- uses: pnpm/action-setup@v4
with:
version: 9.13.2
version: 9.14.2
run_install: true
- run: pnpm test
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9.13.2
version: 9.14.2
run_install: true
- run: pnpm run build
- uses: changesets/action@v1
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@terrazzo/monorepo",
"private": true,
"type": "module",
"packageManager": "pnpm@9.13.2",
"packageManager": "pnpm@9.14.2",
"scripts": {
"build": "turbo run build",
"build:apps": "turbo run build:app",
Expand All @@ -19,16 +19,16 @@
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.9",
"@types/node": "^22.9.0",
"@changesets/cli": "^2.27.10",
"@types/node": "^22.9.3",
"execa": "^9.5.1",
"prettier": "^3.3.3",
"strip-ansi": "^7.1.0",
"stylelint": "^16.10.0",
"stylelint-config-standard": "^36.0.1",
"stylelint-order": "^6.0.4",
"turbo": "^2.3.0",
"typescript": "^5.6.3",
"turbo": "^2.3.1",
"typescript": "^5.7.2",
"vitest": "^2.1.5"
}
}
78 changes: 48 additions & 30 deletions packages/cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import { Logger, build, defineConfig, parse } from '@terrazzo/parser';
import chokidar from 'chokidar';
import dotenv from 'dotenv';
import pc from 'picocolors';
import yamlToMomoa from 'yaml-to-momoa';
import parser from 'yargs-parser';

dotenv.config();

const [, , cmd, ...args] = process.argv;
const [, , ...argsRaw] = process.argv;
const cwd = new URL(`file://${process.cwd()}/`);
const PKG_ROOT = new URL('../', import.meta.url);
const logger = new Logger(
Expand All @@ -44,40 +44,55 @@ const logger = new Logger(

const GREEN_CHECK = pc.green('✔');

const flags = parser(args, {
boolean: ['help', 'watch', 'version'],
string: ['config', 'out'],
alias: {
config: ['c'],
out: ['o'],
version: ['v'],
watch: ['w'],
const { values: flags, positionals } = parseArgs({
args: argsRaw,
allowPositionals: true,
options: {
config: {
type: 'string',
short: 'c',
},
out: {
type: 'string',
short: 'o',
},
help: {
type: 'boolean',
},
watch: {
type: 'boolean',
short: 'w',
},
version: {
type: 'boolean',
},
},
});

export default async function main() {
const start = performance.now();
const cmd = positionals[0];

// ---
// half-run commands: --help, --version, init

// --help
if (flags.help || !cmd) {
showHelp();
process.exit(0);
}

// --version
if (flags.version) {
const { version } = parseJSON(fs.readFileSync(new URL('./package.json', PKG_ROOT), 'utf8'));
const { version } = JSON.parse(fs.readFileSync(new URL('./package.json', PKG_ROOT), 'utf8'));
console.log(version);
process.exit(0);
}

// --help
if (flags.help || !cmd) {
showHelp();
process.exit(0);
}

// ---
// full-run commands: build, check

// setup: load tokens.config.js and tokens.config.json
// setup: load terrazzo.config.js and terrazzo.config.json
let configPath;
if (typeof flags.config === 'string') {
if (flags.config === '') {
Expand All @@ -86,7 +101,12 @@ export default async function main() {
}
configPath = resolveConfig(flags.config);
}
let config = { tokens: [new URL('./tokens.json', cwd)], outDir: new URL('./tokens/', cwd), plugins: [] };
let config = {
tokens: [new URL('./tokens.json', cwd)],
outDir: new URL('./tokens/', cwd),
plugins: [],
ignore: { tokens: [], deprecated: false },
};
const resolvedConfigPath = resolveConfig(configPath);
if (resolvedConfigPath) {
try {
Expand Down Expand Up @@ -116,7 +136,7 @@ export default async function main() {

let rawSchemas = await loadTokens(config.tokens);

const watch = args.includes('-w') || args.includes('--watch');
const watch = flags.watch;

let { tokens, ast } = await parse(rawSchemas, { config, logger, yamlToMomoa });
let result = await build(tokens, { ast, config, logger });
Expand Down Expand Up @@ -173,22 +193,20 @@ export default async function main() {
break;
}
case 'check': {
const rawSchemas = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
const filename = flags._[0] || config.tokens[0];
const rawSchemas = await loadTokens(positionals[1] ? [resolveTokenPath(positionals[1])] : config.tokens);
const filename = positionals[1] || config.tokens[0];
console.log(pc.underline(filename.protocol === 'file:' ? filename.href : filename));
await parse(rawSchemas, { config, continueOnError: true, logger, yamlToMomoa }); // will throw if errors
printSuccess(`No errors ${time(start)}`);
break;
}
case 'lint': {
if (!Array.isArray(config.plugins) || !config.plugins.length) {
printErrors(`No plugins defined! Add some in ${configPath || 'tokens.config.js'}`);
printErrors(`No plugins defined! Add some in ${configPath || 'terrazzo.config.js'}`);
}

const rawSchema = await loadTokens(flags._[0] ? [resolveTokenPath(flags._[0])] : config.tokens);
const parseResult = await parse(rawSchema, { config, continueOnError: true, logger, yamlToMomoa }); // will throw if errors

// TODO
const rawSchema = await loadTokens(positionals[1] ? [resolveTokenPath(positionals[1])] : config.tokens);
await parse(rawSchema, { config, continueOnError: true, logger, yamlToMomoa }); // will throw if errors

break;
}
Expand All @@ -198,8 +216,8 @@ export default async function main() {
!fs.existsSync(new URL('./terrazzo.config.mjs', cwd)) &&
!fs.existsSync(new URL('./terrazzo.config.cjs', cwd))
) {
fs.cpSync(new URL('./terrazzo.config.js', PKG_ROOT), new URL('./terrazzo.config.js', cwd));
printSuccess('terrazzo.config.js created');
fs.cpSync(new URL('./terrazzo.config.mjs', PKG_ROOT), new URL('./terrazzo.config.mjs', cwd));
printSuccess('terrazzo.config.mjs created');
}
if (!fs.existsSync(config.tokens[0])) {
fs.cpSync(new URL('./tokens-example.json', PKG_ROOT), new URL(config?.tokens, cwd));
Expand Down Expand Up @@ -236,7 +254,7 @@ function showHelp() {

[options]
--help Show this message
--config, -c Path to config (default: ./tokens.config.js)
--config, -c Path to config (default: ./terrazzo.config.js)
`);
}

Expand Down
13 changes: 10 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
"style system"
],
"main": "./dist/index.js",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./README.md": "./README.md",
"./package.json": "./package.json"
},
"homepage": "https://terrazzoapp.com/docs/cli",
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,10 +55,9 @@
"dotenv": "^16.4.5",
"merge-anything": "^5.1.7",
"picocolors": "^1.1.1",
"yaml-to-momoa": "^0.0.3",
"yargs-parser": "^21.1.1"
"yaml-to-momoa": "^0.0.3"
},
"devDependencies": {
"typescript": "^5.6.3"
"typescript": "^5.7.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/** @type {import('@terrazzo/cli').Config} */
export default {
import { defineConfig } from '@terrazzo/cli';

export default defineConfig({
tokens: ['tokens.json'],
outDir: './tokens/',
plugins: [
/** @see https://terrazzo.app/docs/cli/integrations */
],
lint: {
/** @see https://terrazzo.app/docs/cli/linting */
/** @see https://terrazzo.app/docs/cli/lint */
},
};
});
12 changes: 12 additions & 0 deletions packages/cli/test/fixtures/init/terrazzo.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from '@terrazzo/cli';

export default defineConfig({
tokens: ['tokens.json'],
outDir: './tokens/',
plugins: [
/** @see https://terrazzo.app/docs/cli/integrations */
],
lint: {
/** @see https://terrazzo.app/docs/cli/lint */
},
});
9 changes: 5 additions & 4 deletions packages/cli/test/fixtures/init/want.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/** @type {import('@terrazzo/cli').Config} */
export default {
import { defineConfig } from '@terrazzo/cli';

export default defineConfig({
tokens: ['tokens.json'],
outDir: './tokens/',
plugins: [
/** @see https://terrazzo.app/docs/cli/integrations */
],
lint: {
/** @see https://terrazzo.app/docs/cli/linting */
/** @see https://terrazzo.app/docs/cli/lint */
},
};
});
4 changes: 2 additions & 2 deletions packages/cli/test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('tz init', () => {
const cwd = new URL('./fixtures/init/', import.meta.url);

await execa('node', ['../../../bin/cli.js', 'init'], { cwd });
expect(fs.existsSync(new URL('./terrazzo.config.js', cwd)));
expect(fs.readFileSync(new URL('./terrazzo.config.js', cwd), 'utf8')).toMatchFileSnapshot(
expect(fs.existsSync(new URL('./terrazzo.config.mjs', cwd)));
await expect(fs.readFileSync(new URL('./terrazzo.config.mjs', cwd), 'utf8')).toMatchFileSnapshot(
fileURLToPath(new URL('./want.config.js', cwd)),
);
});
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/test/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { execa } from 'execa';
import { describe, expect, test } from 'vitest';

const cmd = './bin/cli.js';

describe('tz --version', () => {
test('returns version', async () => {
const { stdout } = await execa('node', [cmd, '--version']);
expect(stdout).toMatch(/^\d+\.\d+\.\d+/);
});
});
Loading