Skip to content

Commit

Permalink
convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed May 23, 2024
1 parent 44ed8a8 commit 70a507e
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 999 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [20, 22]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: latest
node-version: ${{matrix.node}}
- run: make lint test
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ lint-fix: node_modules
npx tsc

.PHONY: test
test: node_modules
test: build node_modules
npx vitest

.PHONY: test-update
test-update: node_modules
test-update: build node_modules
npx vitest -u

.PHONY: build
build: node_modules $(DIST_FILES)

$(DIST_FILES): $(SOURCE_FILES) package-lock.json vite.config.ts
npx vite build
chmod +x $(DIST_FILES)

.PHONY: publish
publish: node_modules
Expand Down
7 changes: 4 additions & 3 deletions versions.test.ts → index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {execa} from "execa";
import {readFileSync} from "node:fs";
import {readFile, writeFile, unlink} from "node:fs/promises";
import {parse as parseToml} from "toml";
import type {SemverLevel} from "./versions.ts";
import type {SemverLevel} from "./index.ts";

const distFile = "dist/index.js";
const pkgFile = new URL("package.json", import.meta.url);
const pyFile = new URL("fixtures/pyproject.toml", import.meta.url);
const testFile = new URL("testfile", import.meta.url);
Expand Down Expand Up @@ -31,7 +32,7 @@ afterAll(async () => {

test("version", async () => {
const {version: expected} = JSON.parse(readFileSync(new URL("package.json", import.meta.url), "utf8"));
const {stdout, exitCode} = await execa("node", ["dist/versions.js", "-v"]);
const {stdout, exitCode} = await execa("node", [distFile, "-v"]);
expect(stdout).toEqual(expected);
expect(exitCode).toEqual(0);
});
Expand All @@ -58,7 +59,7 @@ test("semver", () => {
});

async function run(args: string) {
return await execa(`node dist/versions.js ${args}`, {shell: true});
return await execa(`node ${distFile} ${args}`, {shell: true});

Check failure on line 62 in index.test.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

index.test.ts > versions

ExecaError: Command failed with exit code 1: "node dist/index.js --date --base 13.0.0 --gitless major t*stf*le" Error: ENOENT: no such file or directory, stat 'D:\a\versions\versions\t*stf*le' at statSync (node:fs:1658:25) at ap (file:///D:/a/versions/versions/dist/index.js:4384:17) ❯ statSync node:fs:1658:25 ❯ ap dist/index.js:4384:17 ❯ getFinalError node_modules/execa/lib/return/final-error.js:6:9 ❯ makeError node_modules/execa/lib/return/result.js:86:16 ❯ getAsyncResult node_modules/execa/lib/methods/main-async.js:164:4 ❯ handlePromise node_modules/execa/lib/methods/main-async.js:148:17 ❯ run index.test.ts:62:10 ❯ index.test.ts:87:3 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { shortMessage: 'Command failed with exit code 1: "node dist/index.js --date --base 13.0.0 --gitless major t*stf*le"', command: 'node dist/index.js --date --base 13.0.0 --gitless major t*stf*le', escapedCommand: '"node dist/index.js --date --base 13.0.0 --gitless major t*stf*le"', cwd: 'D:\a\versions\versions', durationMs: 114.2007, failed: true, timedOut: false, isCanceled: false, isTerminated: false, isMaxBuffer: false, exitCode: 1, stdout: 'Error: ENOENT: no such file or directory, stat \'D:\a\versions\versions\t*stf*le\'\n at statSync (node:fs:1658:25)\n at ap (file:///D:/a/versions/versions/dist/index.js:4384:17)', stderr: '', stdio: [ undefined, 'Error: ENOENT: no such file or directory, stat \'D:\a\versions\versions\t*stf*le\'\n at statSync (node:fs:1658:25)\n at ap (file:///D:/a/versions/versions/dist/index.js:4384:17)', '' ], pipedFrom: [] }

Check failure on line 62 in index.test.ts

View workflow job for this annotation

GitHub Actions / test (22, windows-latest)

index.test.ts > versions

ExecaError: Command failed with exit code 1: "node dist/index.js --date --base 13.0.0 --gitless major t*stf*le" Error: ENOENT: no such file or directory, stat 'D:\a\versions\versions\t*stf*le' at statSync (node:fs:1660:25) at ap (file:///D:/a/versions/versions/dist/index.js:4384:17) ❯ statSync node:fs:1660:25 ❯ ap dist/index.js:4384:17 ❯ getFinalError node_modules/execa/lib/return/final-error.js:6:9 ❯ makeError node_modules/execa/lib/return/result.js:86:16 ❯ getAsyncResult node_modules/execa/lib/methods/main-async.js:164:4 ❯ handlePromise node_modules/execa/lib/methods/main-async.js:148:17 ❯ run index.test.ts:62:10 ❯ index.test.ts:87:3 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { shortMessage: 'Command failed with exit code 1: "node dist/index.js --date --base 13.0.0 --gitless major t*stf*le"', command: 'node dist/index.js --date --base 13.0.0 --gitless major t*stf*le', escapedCommand: '"node dist/index.js --date --base 13.0.0 --gitless major t*stf*le"', cwd: 'D:\a\versions\versions', durationMs: 98.6714, failed: true, timedOut: false, isCanceled: false, isTerminated: false, isMaxBuffer: false, exitCode: 1, stdout: 'Error: ENOENT: no such file or directory, stat \'D:\a\versions\versions\t*stf*le\'\n at statSync (node:fs:1660:25)\n at ap (file:///D:/a/versions/versions/dist/index.js:4384:17)', stderr: '', stdio: [ undefined, 'Error: ENOENT: no such file or directory, stat \'D:\a\versions\versions\t*stf*le\'\n at statSync (node:fs:1660:25)\n at ap (file:///D:/a/versions/versions/dist/index.js:4384:17)', '' ], pipedFrom: [] }
}

async function verify(version: string) {
Expand Down
6 changes: 3 additions & 3 deletions versions.ts → index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {basename, dirname, join, relative} from "node:path";
import {cwd, exit as doExit} from "node:process";
import {platform} from "node:os";
import {readFileSync, writeFileSync, accessSync, truncateSync, statSync} from "node:fs";
import {version} from "./package.json" with {type: "json"};
import type {Opts as MinimistOpts} from "minimist";

export type SemverLevel = "patch" | "minor" | "major";

// @ts-ignore
const packageVersion = import.meta.VERSION || "0.0.0";
const packageVersion = version || "0.0.0";
const esc = (str: string) => str.replace(/[|\\{}()[\]^$+*?.-]/g, "\\$&");
const semverRe = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
const isSemver = (str: string) => semverRe.test(str.replace(/^v/, ""));
Expand Down Expand Up @@ -229,7 +229,7 @@ function joinStrings(strings: (string | undefined)[], separator: string) {
function exit(err?: Error | string | void) {
if (err instanceof Error) {
console.info(String(err.stack || err.message || err).trim());
} else {
} else if (err) {
console.info(err);
}
doExit(err ? 1 : 0);
Expand Down
Loading

0 comments on commit 70a507e

Please sign in to comment.