diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d9a732a..bd12629 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,5 +38,4 @@ jobs: - id: build name: Run build run: | - bun install -g uglify-js shx bun run build diff --git a/build.ts b/build.ts index 8abe552..5321279 100644 --- a/build.ts +++ b/build.ts @@ -2,7 +2,7 @@ import type { BuildConfig } from 'bun' import dts from 'bun-plugin-dts' const defaultBuildConfig: BuildConfig = { - entrypoints: ['./src/index.ts','./src/bin.ts'], + entrypoints: ['./src/index.ts','./src/get-dtos.ts'], outdir: './dist' } diff --git a/package.json b/package.json index 9e0069b..e0b04b9 100644 --- a/package.json +++ b/package.json @@ -13,17 +13,17 @@ "type": "git", "url": "git+https://github.com/ServiceStack/get-dtos.git" }, - "bin": "./dist/bin.js", + "bin": "./dist/get-dtos.js", "main": "./dist/index.js", "exports": { "import": "./dist/index.js" }, "scripts": { "build": "bun run build.ts", - "minify": "shx rm -rf ./dist && bun run build && uglifyjs dist/index.js --compress -o dist/index.min.js", - "test": "tsx --test --test-concurrency=1", + "clean": "shx rm -rf ./dist", + "test": "bun test --", "prepublishOnly": "bun run build", - "release": "bun run minify && bump patch --commit --push --tag && npm publish --access public" + "release": "bun run clean && bun run build && bump patch --commit --push --tag && npm publish --access public" }, "keywords": [ "apis", diff --git a/src/bin.ts b/src/get-dtos.ts similarity index 100% rename from src/bin.ts rename to src/get-dtos.ts diff --git a/src/index.ts b/src/index.ts index 6962b94..553019b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,6 +114,14 @@ export function parseArgs(...args: string[]) : Command { return ret } +function logAnyUnknownCommand(command:Command) { + if (command.unknown?.length) { + console.log(`Unknown Command: ${command.script!.name} ${command.unknown!.join(' ')}\n`) + return -1 + } + return 0 +} + var VERBOSE = false export async function cli(args: string[]) { //var nodeExe = args[0] @@ -142,12 +150,15 @@ export async function cli(args: string[]) { let arg1 = cmdArgs[0] || "" console.log(arg1, cmdArgs, ' VERBOSE: ', VERBOSE) } - + + if (command.unknown?.length) { + return execHelp(command) + } + try { switch (command.type) { case "help": - execHelp(command) - return + return execHelp(command) case "version": console.log("Version: " + packageConf.version) return @@ -180,12 +191,6 @@ export async function cli(args: string[]) { } catch (e) { handleError(e) } - - if (command.unknown?.length) { - console.log("Unknown Command: " + command.script!.name + " " + command.unknown!.join(' ') + "\n") - execHelp(command) - return -1 - } } function handleError(e:any, msg?:string) { @@ -325,7 +330,9 @@ function walk(dir:string) { } function execHelp(command:Command) { - const tool = command.script?.name ?? "npx get-dtos" + const exitCode = logAnyUnknownCommand(command) + + const tool = command.script?.name ?? "get-dtos" var USAGE = ` ${tool} Update all ServiceStack References in directory (recursive) ${tool} Update existing ServiceStack Reference (e.g. dtos.cs) @@ -352,6 +359,7 @@ This tool collects anonymous usage to determine the most used languages to impro To disable set SERVICESTACK_TELEMETRY_OPTOUT=1 environment variable to 1 using your favorite shell. ` console.log(USAGE.trim()) + return exitCode } function normalizeSwitches(cmd:string) { return cmd.replace(/^-+/, '/') } diff --git a/test/args.test.ts b/test/args.test.ts index 5fb4bf6..f67a701 100644 --- a/test/args.test.ts +++ b/test/args.test.ts @@ -7,6 +7,12 @@ describe("cli args tests", () => { expect(parseArgs("")).toEqual({ type:'help' }) }) + it ("unknown commands returns help with unknown", () => { + expect(parseArgs("unknown")).toEqual({ type:'help', unknown:['unknown'] }) + expect(parseArgs("unknown commands")).toEqual({ type:'help', unknown:['unknown commands'] }) + expect(parseArgs("unknown","commands")).toEqual({ type:'help', unknown:['unknown','commands'] }) + }) + it ("arg with language returns update", () => { expect(parseArgs("csharp")).toEqual({ type:'update', lang:'csharp' }) expect(parseArgs("typescript")).toEqual({ type:'update', lang:'typescript' }) diff --git a/test/basic.test.ts b/test/basic.test.ts deleted file mode 100644 index cbd1d5c..0000000 --- a/test/basic.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -// import { $ } from 'bun' -import { describe, it, expect } from 'bun:test' - -describe("basic", () => { - it ("should work", () => { - // const out = await $`node ./dist/bin.js typescript https://openai.servicestack.net`.text() - // console.log('out', out) - // expect(1).toEqual(1) - }) -}) diff --git a/test/cli.test.ts b/test/cli.test.ts new file mode 100644 index 0000000..1f90078 --- /dev/null +++ b/test/cli.test.ts @@ -0,0 +1,23 @@ +import { $ } from 'bun' +import { describe, it, expect } from 'bun:test' + +describe("Does run cli commands", () => { + it ("empty commands should print help", async () => { + let out = await $`./dist/get-dtos.js`.text() + expect(out).toStartWith('get-dtos ') + }) + + it ("unknown commands should fail", async () => { + let out = await $`./dist/get-dtos.js unknown`.text() + expect(out).toStartWith('Unknown Command: get-dtos unknown') + }) + + it ("should print version", async () => { + let out = await $`./dist/get-dtos.js --version`.text() + expect(out).toStartWith('Version: ') + out = await $`./dist/get-dtos.js -version`.text() + expect(out).toStartWith('Version: ') + out = await $`./dist/get-dtos.js -v`.text() + expect(out).toStartWith('Version: ') + }) +})