Skip to content
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
2 changes: 2 additions & 0 deletions yarn-dlx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/package-lock.json
16 changes: 16 additions & 0 deletions yarn-dlx/README.md
Original file line number Diff line number Diff line change
@@ -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'
```

2 changes: 2 additions & 0 deletions yarn-dlx/bin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('ts-node/dist/bin').main(['--esm', `${__dirname}/cli.ts`, ...process.argv.slice(2)]);
26 changes: 26 additions & 0 deletions yarn-dlx/cli.ts
Original file line number Diff line number Diff line change
@@ -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));
15 changes: 15 additions & 0 deletions yarn-dlx/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions yarn-dlx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}