From 9cc6dae0724807520caae8714f64cfcbeb84780e Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 9 Jul 2022 20:23:44 -0400 Subject: [PATCH] moving yarn-dlx example to a PR --- yarn-dlx/.gitignore | 2 ++ yarn-dlx/README.md | 16 ++++++++++++++++ yarn-dlx/bin.cjs | 2 ++ yarn-dlx/cli.ts | 26 ++++++++++++++++++++++++++ yarn-dlx/package.json | 15 +++++++++++++++ yarn-dlx/tsconfig.json | 7 +++++++ 6 files changed, 68 insertions(+) create mode 100644 yarn-dlx/.gitignore create mode 100644 yarn-dlx/README.md create mode 100755 yarn-dlx/bin.cjs create mode 100755 yarn-dlx/cli.ts create mode 100644 yarn-dlx/package.json create mode 100644 yarn-dlx/tsconfig.json diff --git a/yarn-dlx/.gitignore b/yarn-dlx/.gitignore new file mode 100644 index 0000000..d502512 --- /dev/null +++ b/yarn-dlx/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/package-lock.json diff --git a/yarn-dlx/README.md b/yarn-dlx/README.md new file mode 100644 index 0000000..8f9d776 --- /dev/null +++ b/yarn-dlx/README.md @@ -0,0 +1,16 @@ +# ts-node-npx-example + +A working example of a CLI tool written in TypeScript: + +- Without a build step +- Without a publish step +- Runnable directly from GitHub using either `npx` or `yarn dlx` + +*Note: at the time of writing, `npx` has bugs and cannot run code from git. Modern versions of `yarn` still work.* + +To run it: + +```shell +yarn dlx 'npx@https://github.com/TypeStrong/ts-node-examples#workspace=yarn-dlx' +``` + diff --git a/yarn-dlx/bin.cjs b/yarn-dlx/bin.cjs new file mode 100755 index 0000000..0c35245 --- /dev/null +++ b/yarn-dlx/bin.cjs @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('ts-node/dist/bin').main(['--esm', `${__dirname}/cli.ts`, ...process.argv.slice(2)]); diff --git a/yarn-dlx/cli.ts b/yarn-dlx/cli.ts new file mode 100755 index 0000000..edb5cbf --- /dev/null +++ b/yarn-dlx/cli.ts @@ -0,0 +1,26 @@ +#!/usr/bin/env -S npx ts-node --esm + +import {Cli, Command, Builtins, Option} from 'clipanion'; + +class SayHello extends Command { + static paths = [Command.Default]; + + firstName = Option.String('--first', {required: true}); + lastName = Option.String('--last', {required: true}); + + async execute() { + console.log(`Hello, ${this.firstName} ${this.lastName}!`); + } +} + +const cli = new Cli({ + binaryName: 'ts-node-npx-demo', + binaryLabel: 'ts-node-npx-demo', + binaryVersion: '1.0.0' +}); + +cli.register(SayHello); +cli.register(Builtins.VersionCommand); +cli.register(Builtins.HelpCommand); + +cli.runExit(process.argv.slice(2)); diff --git a/yarn-dlx/package.json b/yarn-dlx/package.json new file mode 100644 index 0000000..4f17759 --- /dev/null +++ b/yarn-dlx/package.json @@ -0,0 +1,15 @@ +{ + "name": "npx", + "version": "1.0.0", + "type": "module", + "bin": "bin.cjs", + "preferUnplugged": true, + "dependencies": { + "@swc/core": "latest", + "@swc/helpers": "latest", + "@tsconfig/node16-strictest-esm": "latest", + "clipanion": "latest", + "ts-node": "latest", + "typescript": "latest" + } +} diff --git a/yarn-dlx/tsconfig.json b/yarn-dlx/tsconfig.json new file mode 100644 index 0000000..fb23b69 --- /dev/null +++ b/yarn-dlx/tsconfig.json @@ -0,0 +1,7 @@ +{ + // If you don't need native ESM, you can switch to @tsconfig/node16/tsconfig.json and remove "type": "module" from package.json + "extends": "@tsconfig/node16-strictest-esm/tsconfig.json", + "ts-node": { + "swc": true + } +}