Skip to content

ts-command-line -> trpc-cli #703

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

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
needs: [create_tgz]
strategy:
matrix:
node: [20, 18, 16, 14, 12]
node: [23, 22, 20, 18, 16]
steps:
- uses: actions/setup-node@v4
with:
Expand Down
211 changes: 98 additions & 113 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,30 +622,24 @@ if (require.main === module) {
}
```

Note that this uses the [@rushstack/ts-command-line](https://www.npmjs.com/package/@rushstack/ts-command-line) package, which shows only the top-level message of any errors throw by default. See [here](https://github.com/sequelize/umzug/issues/619#issuecomment-1793297576) for how you can see a full stack trace.

#### CLI Usage

A script like the one above is now a runnable CLI program. You can run `node migrator.js --help` to see how to use it. It will print something like:

<!-- codegen:start {preset: custom, source: ./codegen.js, export: cliHelp} -->
```
usage: <script> [-h] <command> ...

Umzug migrator
Usage: [options] [command]

Positional arguments:
<command>
up Applies pending migrations
down Revert migrations
pending Lists pending migrations
executed Lists executed migrations
create Create a migration file
Options:
-h, --help display help for command

Optional arguments:
-h, --help Show this help message and exit.

For detailed help about a specific command, use: <script> <command> -h
Commands:
up [options] Apply pending migrations
down [options] Revert one or more migrations
create [options] Create a new migration file
pending [options] List migrations due to be applied
executed [options] List migrations that have been applie
help [command] display help for command
```
<!-- codegen:end -->

Expand All @@ -658,50 +652,43 @@ Use `node migrator up --help` and `node migrator down --help` for options (runni
Up:
<!-- codegen:start {preset: custom, source: ./codegen.js, export: cliHelp, action: up} -->
```
usage: <script> up [-h] [--to NAME] [--step COUNT] [--name MIGRATION]
[--rerun {THROW,SKIP,ALLOW}]


Performs all migrations. See --help for more options

Optional arguments:
-h, --help Show this help message and exit.
--to NAME All migrations up to and including this one should be
applied
--step COUNT Apply this many migrations. If not specified, all
will be applied.
--name MIGRATION Explicity declare migration name(s) to be applied.
Only these migrations will be applied.
--rerun {THROW,SKIP,ALLOW}
Specify what action should be taken when a migration
that has already been applied is passed to --name.
The default value is "THROW".
Usage: up [options]

Apply pending migrations

Options:
--to [string] All migrations up to and including this one should be
applied
--step [number] Apply this many migrations. If not specified, all will be
applied.; Exclusive minimum: 0
--name [values...] Type: string Explicitly declare migration name(s) to be
applied. Only these migrations will be applied. array
--rerun [string] What to do if a migration that has already been applied is
passed to --name; Enum: ["THROW","SKIP","ALLOW"] (default:
"THROW")
-h, --help display help for command
```
<!-- codegen:end -->

Down:
<!-- codegen:start {preset: custom, source: ./codegen.js, export: cliHelp, action: down} -->
```
usage: <script> down [-h] [--to NAME] [--step COUNT] [--name MIGRATION]
[--rerun {THROW,SKIP,ALLOW}]


Undoes previously-applied migrations. By default, undoes the most recent
migration only. Use --help for more options. Useful in development to start
from a clean slate. Use with care in production!

Optional arguments:
-h, --help Show this help message and exit.
--to NAME All migrations up to and including this one should be
reverted. Pass '0' to revert all.
--step COUNT Revert this many migrations. If not specified, only
the most recent migration will be reverted.
--name MIGRATION Explicity declare migration name(s) to be reverted.
Only these migrations will be reverted.
--rerun {THROW,SKIP,ALLOW}
Specify what action should be taken when a migration
that has already been applied is passed to --name.
The default value is "THROW".
Usage: down [options]

Revert one or more migrations

Options:
--to [string] All migrations up to and including this one should be
reverted. Pass '0' to revert all.
--step [number] Revert this many migrations. If not specified, only the
most recent migration will be reverted.; Exclusive
minimum: 0
--name [values...] Type: string Explicitly declare migration name(s) to be
reverted. Only these migrations will be reverted. array
--rerun [string] What to do if a migration that has not been applied is
passed to --name; Enum: ["THROW","SKIP","ALLOW"] (default:
"THROW")
-h, --help display help for command
```
<!-- codegen:end -->

Expand All @@ -721,31 +708,31 @@ node migrator executed --help # show help/options

<!-- codegen:start {preset: custom, source: ./codegen.js, export: cliHelp, action: pending} -->
```
usage: <script> pending [-h] [--json]
Usage: pending [options]

Prints migrations returned by `umzug.pending()`. By default, prints migration
names one per line.
List migrations due to be applied

Optional arguments:
-h, --help Show this help message and exit.
--json Print pending migrations in a json format including names and
paths. This allows piping output to tools like jq. Without this
flag, the migration names will be printed one per line.
Options:
--json Print pending migrations in a json format including names and
paths. This allows piping output to tools like jq. Without this
flag, the migration names will be printed one per line. (default:
false)
-h, --help display help for command
```
<!-- codegen:end -->

<!-- codegen:start {preset: custom, source: ./codegen.js, export: cliHelp, action: executed} -->
```
usage: <script> executed [-h] [--json]
Usage: executed [options]

Prints migrations returned by `umzug.executed()`. By default, prints
migration names one per line.
List migrations that have been applie

Optional arguments:
-h, --help Show this help message and exit.
--json Print executed migrations in a json format including names and
paths. This allows piping output to tools like jq. Without this
flag, the migration names will be printed one per line.
Options:
--json Print pending migrations in a json format including names and
paths. This allows piping output to tools like jq. Without this
flag, the migration names will be printed one per line. (default:
false)
-h, --help display help for command
```
<!-- codegen:end -->

Expand Down Expand Up @@ -790,49 +777,47 @@ Use `node migrator create --help` for more options:

<!-- codegen:start {preset: custom, source: ./codegen.js, export: cliHelp, action: create} -->
```
usage: <script> create [-h] --name NAME [--prefix {TIMESTAMP,DATE,NONE}]
[--folder PATH] [--allow-extension EXTENSION]
[--skip-verify] [--allow-confusing-ordering]


Generates a placeholder migration file using a timestamp as a prefix. By
default, mimics the last existing migration, or guesses where to generate the
file if no migration exists yet.

Optional arguments:
-h, --help Show this help message and exit.
--name NAME The name of the migration file. e.g. my-migration.js,
my-migration.ts or my-migration.sql. Note - a prefix
will be added to this name, usually based on a
timestamp. See --prefix
--prefix {TIMESTAMP,DATE,NONE}
The prefix format for generated files. TIMESTAMP uses
a second-resolution timestamp, DATE uses a
day-resolution timestamp, and NONE removes the prefix
completely. The default value is "TIMESTAMP".
--folder PATH Path on the filesystem where the file should be
created. The new migration will be created as a
sibling of the last existing one if this is omitted.
--allow-extension EXTENSION
Allowable extension for created files. By default .js,
.ts and .sql files can be created. To create txt
file migrations, for example, you could use '--name
my-migration.txt --allow-extension .txt' This
parameter may alternatively be specified via the
UMZUG_ALLOW_EXTENSION environment variable.
--skip-verify By default, the generated file will be checked after
creation to make sure it is detected as a pending
migration. This catches problems like creation in the
wrong folder, or invalid naming conventions. This
flag bypasses that verification step.
--allow-confusing-ordering
By default, an error will be thrown if you try to
create a migration that will run before a migration
that already exists. This catches errors which can
cause problems if you change file naming conventions.
If you use a custom ordering system, you can disable
this behavior, but it's strongly recommended that you
don't! If you're unsure, just ignore this option.
Usage: create [options]

Create a new migration file

Options:
--content [string] Content of the migration.
--name <string> Name of the migration file.
--prefix [string] The prefix format for generated files.
TIMESTAMP uses a second-resolution
timestamp, DATE uses a day-resolution
timestamp, and NONE removes the prefix
completely; Enum:
["TIMESTAMP","DATE","NONE"]
--folder [string] Path on the filesystem where the file
should be created. The new migration
will be created as a sibling of the last
existing one if this is omitted.
--allow-extension [string] Allowable extension for created files.
By default .js, .ts and .sql files can
be created. To create txt file
migrations, for example, you could use
'--name my-migration.txt
--allow-extension .txt'
--skip-verify [boolean] By default, the generated file will be
checked after creation to make sure it
is detected as a pending migration. This
catches problems like creation in the
wrong folder, or invalid naming
conventions. This flag bypasses that
verification step.
--allow-confusing-ordering [boolean] By default, an error will be thrown if
you try to create a migration that will
run before a migration that already
exists. This catches errors which can
cause problems if you change file naming
conventions. If you use a custom
ordering system, you can disable this
behavior, but it's strongly recommended
that you don't! If you're unsure, just
ignore this option.
-h, --help display help for command
```
<!-- codegen:end -->

Expand Down
12 changes: 9 additions & 3 deletions codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ const {UmzugCLI} = require('./lib/cli')

/** @type import('eslint-plugin-codegen').Preset<{ action?: string }> */
exports.cliHelp = ({options: {action}}) => {
const cli = new UmzugCLI(new Umzug({migrations: [], logger: undefined}))
const helpable = action ? cli.tryGetAction(action) : cli
for (const stream of [process.stdout, process.stderr]) {
// make sure generated output is consistent across machines
stream.columns = 100
stream.isTTY = false
}
const {cli} = new UmzugCLI(new Umzug({migrations: [], logger: undefined}))
const program = cli.buildProgram({argv: []})
const command = action ? program.commands.find(c => c.name() === action) : program

return [
'```',
stripAnsi(helpable.renderHelpText())
stripAnsi(command?.helpInformation() || program.commands.map(c => c.name()).join('\n'))
.trim()
// for some reason the last `-h` is on its own line
.replace(/\n-h$/, '-h'),
Expand Down
2 changes: 1 addition & 1 deletion examples/7-bundling-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "umzug-bundling-example",
"version": "0.0.0",
"scripts": {
"build": "tsup umzug.ts --external pg-hstore",
"build": "tsup umzug.ts --external pg-hstore --out-dir dist",
"lint": "eslint ."
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
],
"packageManager": "pnpm@8.10.2",
"dependencies": {
"@rushstack/ts-command-line": "^4.12.2",
"emittery": "^0.13.0",
"fast-glob": "^3.3.2",
"pony-cause": "^2.1.4",
Expand All @@ -42,7 +41,9 @@
"source-map-support": "0.5.21",
"sqlite3": "5.1.7",
"strip-ansi": "6.0.1",
"trpc-cli": "0.6.0-12",
"ts-node": "10.9.2",
"tsup": "8.4.0",
"typescript": "4.9.5",
"uuid": "9.0.1",
"verror": "1.10.1",
Expand All @@ -53,11 +54,10 @@
"compile": "tsc -p tsconfig.lib.json",
"build": "pnpm clean && pnpm compile",
"eslint": "eslint . --max-warnings 0",
"lint": "pnpm type-check && pnpm eslint",
"lint": "pnpm build && pnpm eslint",
"prepare": "pnpm build",
"pretest": "rm -rf test/generated",
"test": "vitest run",
"type-check": "tsc -p ."
"test": "vitest run"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -88,6 +88,6 @@
},
"homepage": "https://github.com/sequelize/umzug",
"engines": {
"node": ">=12"
"node": ">=22"
}
}
Loading
Loading