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

✨ Generate subcommand #30

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 17 additions & 12 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ jobs:
matrix:
node:
- 20.x
- 22.x
#- 22.x
platform:
- os: ubuntu
- os: macos
- os: windows
#- os: macos
#- os: windows
fail-fast: false

steps:
Expand All @@ -78,50 +78,55 @@ jobs:

- name: "Test 1: default case"
run: |
scripts/test.sh foo '' '*.css'
cp fixtures/foo.css $RUNNER_TEMP/test.css
cp fixtures/foo.d.css.ts $RUNNER_TEMP/expected.d.css.ts

cd $RUNNER_TEMP
$GITHUB_WORKSPACE/dist/main.js "*.css"
diff --color=always --strip-trailing-cr -uI "//.*" expected.d.css.ts test.d.css.ts

- name: "Test 2: localsConvention, first position"
if: success() || failure()
run: |
scripts/test.sh casing/casing '' '--localsConvention camelCaseOnly *.css' casing/camelCaseOnly
scripts/test-gen.sh casing/casing '' '--localsConvention camelCaseOnly *.css' casing/camelCaseOnly

- name: "Test 3: localsConvention, second position"
if: success() || failure()
run: |
scripts/test.sh casing/casing '' '*.css --localsConvention camelCaseOnly' casing/camelCaseOnly
scripts/test-gen.sh casing/casing '' '*.css --localsConvention camelCaseOnly' casing/camelCaseOnly

- name: "Test 4: relative outdir"
if: success() || failure()
run: |
scripts/test.sh foo '' '--outdir generated *.css' '' generated/
scripts/test-gen.sh foo '' '--outdir generated *.css' '' generated/

- name: "Test 5: absolute outdir"
if: success() || failure()
run: |
scripts/test.sh foo "" "-o $GITHUB_WORKSPACE/generated *.css" "" "$GITHUB_WORKSPACE"/generated/
scripts/test-gen.sh foo "" "-o $GITHUB_WORKSPACE/generated *.css" "" "$GITHUB_WORKSPACE"/generated/
# Note: This test uses double quotes, which expands differently.

- name: "Test 6: json file config"
if: success() || failure()
run: |
scripts/test.sh foo csstypedrc.json
scripts/test-gen.sh foo csstypedrc.json

- name: "Test 7: yaml file config"
if: success() || failure()
run: |
scripts/test.sh foo csstypedrc.yaml '' '' generated/
scripts/test-gen.sh foo csstypedrc.yaml '' '' generated/

- name: "Test 8: custom config path"
if: success() || failure()
run: |
scripts/test.sh foo css-typed-rc.yaml '-c .config/css-typed-rc.yaml'
scripts/test-gen.sh foo css-typed-rc.yaml '-c .config/css-typed-rc.yaml'

- name: "Test 9: mjs file config"
if: matrix.platform.os != 'windows' && (success() || failure())
# Do not run on Windows due to Windows-only ESM import bug.
# This _could_ be an issue with css-typed, but could be a test/deps issue.
run: |
scripts/test.sh foo custom-config-path.config.mjs '-c .config/custom-config-path.config.mjs'
scripts/test-gen.sh foo custom-config-path.config.mjs '-c .config/custom-config-path.config.mjs'

Publish:
if: ${{ github.ref == 'refs/heads/main' }}
Expand Down
7 changes: 3 additions & 4 deletions scripts/test.sh → scripts/test-gen.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -eo pipefail # Removed `-u` which failed on macos for `options`
set -exo pipefail # Removed `-u` which failed on macos for `options`
IFS=$' ' # We want space splitting for this script

# $1 is the input name, relative to `fixtures`. Required.
Expand Down Expand Up @@ -31,9 +31,8 @@ pushd $RUNNER_TEMP > /dev/null || exit

# `./dist/main.js` is executing local `css-typed` as if installed (same as `bin`).
# But it is `$GITHUB_WORKSPACE/dist/main.js` b/c we `cd $RUNNER_TEMP`.
echo "css-typed " "${options[@]}"
# shellcheck disable=SC2068
$GITHUB_WORKSPACE/dist/main.js ${options[@]}
echo "css-typed gen" "${options[@]}"
$GITHUB_WORKSPACE/dist/main.js gen "${options[@]}"

# Use `diff` to compare the files.
# Use `-I '//.*'` to ignore the first line (comment) which has generated path and timestamp.
Expand Down
6 changes: 6 additions & 0 deletions src/cli/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function cssTypedAction(
) {
return this.action(
async (cliPattern, { config: cliConfigPath, ...cliOptions }, program) => {
console.debug(`[debug] CLI input:`, `pattern=${cliPattern}`, {
config: cliConfigPath,
...cliOptions,
});

// Load file configuration first
const configResult = await loadFileConfig(cliConfigPath);
if (configResult?.filepath) {
Expand All @@ -60,6 +65,7 @@ function cssTypedAction(

// Resolve options from file config and CLI. CLI overrides file config.
const options: Options = { ...fileConfig, ...cliOptions };
console.debug(`[debug] Resolved options to`, options);

// Pattern is required. CLI overrides file config.
const pattern = cliPattern ?? filePattern;
Expand Down
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import { version } from "./version.ts";
await createCssTypedCommand()
.name(`css-typed`)
.description(
`TypeScript declaration generator for CSS files (and other stylesheets).`,
`TypeScript declaration generator for CSS files (and other stylesheets).\n\nRuns \`generate\` when no subcommand given.`,
)
.version(version)
.cssTypedAction(generate)
.helpCommand(`help [command]`, `Displays help for the given command.`)
.addCommand(
createCssTypedCommand()
.name(`gen`)
.alias(`generate`)
.description(`Generates TypeScript declaration files.`)
.cssTypedAction(generate),
)
.parseAsync();
Loading