Compare commits

..

138 Commits

Author SHA1 Message Date
jkunz b388f56e33 v4.3.0
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-05-13 20:18:55 +00:00
jkunz 1ae31e36bc feat(terminal): add optional live timers and spinners to terminal tasks 2026-05-13 20:18:52 +00:00
jkunz e2eb4eb040 v4.2.0
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-05-13 18:33:08 +00:00
jkunz 502cca375f feat(terminal): enhance terminal task rendering with progress, lifecycle helpers, and configurable output modes 2026-05-13 18:33:06 +00:00
jkunz c07b2969b8 v4.1.0
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-05-13 14:52:58 +00:00
jkunz a5ec2717c5 feat(terminal): add live terminal task rendering with interactive and non-interactive output modes 2026-05-13 14:52:18 +00:00
jkunz 8852bd5c86 v4.0.21
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-04-30 14:29:50 +00:00
jkunz 6279f2cbad fix(smartcli): tighten command parsing and error handling while updating build and package configuration 2026-04-30 14:29:50 +00:00
jkunz e3f5616320 fix(core): Remove flawed safety check in getUserArgs and debug log
- Fixed bug where CLI with no args would return entire argv including node path
- Removed debug 'Wanted command: ...' log from startParse()
2026-01-12 01:22:25 +00:00
jkunz 40c0dfb3df 4.0.19
Default (tags) / security (push) Failing after 22s
Default (tags) / test (push) Failing after 15s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 18:38:18 +00:00
jkunz 4f243289b8 fix(license): Update license files 2025-10-28 18:38:18 +00:00
jkunz 2d28939986 4.0.18
Default (tags) / security (push) Failing after 27s
Default (tags) / test (push) Failing after 26s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 15:42:40 +00:00
jkunz 01623eab2a fix(smartcli): Allow passing argv to startParse and improve getUserArgs Deno/runtime handling; update tests and add license 2025-10-28 15:42:39 +00:00
jkunz 5c65c43589 4.0.17
Default (tags) / security (push) Failing after 21s
Default (tags) / test (push) Failing after 13s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 15:10:45 +00:00
jkunz 72109e478f fix(license): Add MIT license and local Claude settings 2025-10-28 15:10:44 +00:00
jkunz 53d9956735 4.0.16
Default (tags) / security (push) Failing after 21s
Default (tags) / test (push) Failing after 13s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 14:59:46 +00:00
jkunz 913f8556d0 fix(smartcli.helpers): Improve CLI argument parsing and Deno runtime detection; use getUserArgs consistently 2025-10-28 14:59:46 +00:00
jkunz e905af4b21 4.0.15
Default (tags) / security (push) Failing after 22s
Default (tags) / test (push) Failing after 14s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 13:53:34 +00:00
jkunz 2e0b7d5053 fix(smartcli.helpers): Add robust getUserArgs helper and refactor Smartcli to use it; add MIT license and update documentation 2025-10-28 13:53:34 +00:00
jkunz 270f75e8e0 4.0.14
Default (tags) / security (push) Failing after 21s
Default (tags) / test (push) Failing after 12s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 09:32:21 +00:00
jkunz b9ec1e2be6 fix(license): Add MIT license file 2025-10-28 09:32:21 +00:00
jkunz 86d62407e7 4.0.13
Default (tags) / security (push) Failing after 23s
Default (tags) / test (push) Failing after 13s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-10-28 06:27:29 +00:00
jkunz 6efd6232d1 fix(smartcli): Improve CLI argument parsing, update deps and tests 2025-10-28 06:27:29 +00:00
philkunz 44296dc57a 4.0.12 2025-04-01 14:19:35 +00:00
philkunz a08627f058 fix(docs): Update documentation with comprehensive usage examples, improved command alias descriptions, and detailed configuration instructions 2025-04-01 14:19:35 +00:00
philkunz e525e04b07 update description 2024-05-29 14:12:09 +02:00
philkunz 5e2225669d 4.0.11 2024-05-28 13:42:11 +02:00
philkunz a4a3343b1d fix(core): update 2024-05-28 13:42:10 +02:00
philkunz e10e8cf90d 4.0.10 2024-04-14 01:02:06 +02:00
philkunz 19900d088e fix(core): update 2024-04-14 01:02:06 +02:00
philkunz 3e5793f842 4.0.9 2024-04-12 18:24:32 +02:00
philkunz 4fd13e65c4 fix(core): update 2024-04-12 18:24:31 +02:00
philkunz e5a8bbf4a3 update npmextra.json: githost 2024-04-01 21:34:10 +02:00
philkunz f28589603b update npmextra.json: githost 2024-04-01 19:57:50 +02:00
philkunz fed411a30f update npmextra.json: githost 2024-03-30 21:46:48 +01:00
philkunz 2328801f03 4.0.8 2023-08-19 09:47:04 +02:00
philkunz 60883fed6d fix(core): update 2023-08-19 09:47:03 +02:00
philkunz e9fc5b98f2 4.0.7 2023-07-12 15:09:53 +02:00
philkunz fef954c423 fix(core): update 2023-07-12 15:09:53 +02:00
philkunz 39408d9832 switch to new org scheme 2023-07-11 00:25:26 +02:00
philkunz 26d9b3e9cc switch to new org scheme 2023-07-10 02:42:41 +02:00
philkunz 5f027430bf 4.0.6 2022-08-07 11:40:46 +02:00
philkunz d3385782ed fix(core): update 2022-08-07 11:40:45 +02:00
philkunz 7384b54e09 4.0.5 2022-08-04 12:22:49 +02:00
philkunz 0eac72e15d fix(core): update 2022-08-04 12:22:49 +02:00
philkunz b7957b0ab6 4.0.4 2022-08-03 20:21:54 +02:00
philkunz 99a0a9ca81 fix(core): update 2022-08-03 20:21:54 +02:00
philkunz bd66903419 4.0.3 2022-08-03 18:48:40 +02:00
philkunz 740d8dac35 fix(core): update 2022-08-03 18:48:40 +02:00
philkunz 488e7410fe 4.0.2 2022-08-03 18:47:35 +02:00
philkunz 04deb8960c fix(core): update 2022-08-03 18:47:35 +02:00
philkunz 19f0a9563f 4.0.1 2022-08-03 17:07:11 +02:00
philkunz db1e866fe1 fix(core): update 2022-08-03 17:07:11 +02:00
philkunz f7c24a0bd2 4.0.0 2022-08-03 17:00:36 +02:00
philkunz fa59d2da40 BREAKING CHANGE(core): switch to esm 2022-08-03 17:00:36 +02:00
philkunz 311232aeea 3.0.14 2021-04-07 20:28:58 +00:00
philkunz 4cd0844bc3 fix(core): update 2021-04-07 20:28:57 +00:00
philkunz 17c1a687c8 3.0.13 2021-04-07 20:25:18 +00:00
philkunz 1d1264c2b3 fix(core): update 2021-04-07 20:25:17 +00:00
philkunz b036e609ce 3.0.12 2020-05-29 17:48:18 +00:00
philkunz c2ec0df907 fix(core): update 2020-05-29 17:48:17 +00:00
philkunz 167b4d29df 3.0.11 2020-04-13 21:53:18 +00:00
philkunz 02fec216db fix(core): more consistent handling of process.enc.CLI_CALL 2020-04-13 21:53:18 +00:00
philkunz 4e9d2f3e8c 3.0.10 2020-04-13 21:44:27 +00:00
philkunz 65d8a8b6f5 fix(core): now works better with tapbundle tests 2020-04-13 21:44:27 +00:00
philkunz 8e04bd6a62 3.0.9 2020-03-11 22:50:40 +00:00
philkunz 687a5f7c4e fix(core): update 2020-03-11 22:50:40 +00:00
philkunz 17983b1da9 3.0.8 2020-03-11 22:49:44 +00:00
philkunz 5fcdf1ff8f fix(core): update 2020-03-11 22:49:43 +00:00
philkunz ef7ee7fc73 3.0.7 2018-12-11 01:50:59 +01:00
philkunz c48e85897e fix(core): update 2018-12-11 01:50:59 +01:00
philkunz 9466b3e473 3.0.6 2018-09-30 22:45:12 +02:00
philkunz ab3127b8a6 fix(ci): remove obsolete dependencies 2018-09-30 22:45:12 +02:00
philkunz 1e62e27980 3.0.5 2018-09-30 22:36:31 +02:00
philkunz 4b87004478 fix(core): update 2018-09-30 22:36:30 +02:00
philkunz 7750f1fbf5 3.0.4 2018-08-31 00:14:18 +02:00
philkunz c4e5ba6587 fix(structure): remove dist/ dir from git repo 2018-08-31 00:14:18 +02:00
philkunz 9d1f0f22ba 3.0.3 2018-08-31 00:13:05 +02:00
philkunz 1ce9e32116 fix(dependencies): update to latest versions 2018-08-31 00:13:05 +02:00
Phil Kunz adfda70522 3.0.2 2018-06-28 23:57:41 +02:00
Phil Kunz c701e3e04c fix(core): slim down dependencies 2018-06-28 23:57:40 +02:00
philkunz 7b1de5b31d 3.0.1 2018-05-04 00:19:45 +02:00
philkunz 7908fd8cfd update 2018-05-04 00:19:41 +02:00
philkunz 21bd0c9279 3.0.0 2018-05-03 12:10:48 +02:00
philkunz 9d1108e40d change to an all rxjs Subject architecture 2018-05-03 12:10:39 +02:00
philkunz 390e0cb491 system change 2018-03-03 13:57:54 +01:00
philkunz 032fd0c2fd fix(core): cleanup 2018-01-27 18:06:13 +01:00
philkunz 440881c3d8 remove package-lock since using yarn 2018-01-27 18:05:06 +01:00
philkunz f208121e2c 2.0.12 2018-01-27 18:02:05 +01:00
philkunz 7c4ae84871 fix(improve security CI step): 2018-01-27 18:02:01 +01:00
philkunz 668f6c3e16 2.0.11 2018-01-27 17:59:08 +01:00
philkunz b1e08aad1f fix(core): remove vulnerable paths 2018-01-27 17:59:04 +01:00
philkunz f1ab614cdf update ci 2018-01-27 02:27:55 +01:00
philkunz 995c808512 update ci 2018-01-27 02:26:10 +01:00
philkunz 28acb867a0 update ci 2018-01-27 02:23:24 +01:00
philkunz 3148a50d43 update ci 2018-01-27 02:21:53 +01:00
philkunz 41c99de4d8 2.0.10 2018-01-27 02:20:46 +01:00
philkunz a91f56dacf add security step to CI 2018-01-27 02:20:41 +01:00
philkunz f60f17f91e 2.0.9 2017-10-12 23:02:03 +02:00
philkunz d154cf0d0f ensure compatibility with code assertion library 2017-10-12 23:02:00 +02:00
philkunz a6e0fa65e0 2.0.8 2017-10-12 22:44:39 +02:00
philkunz c7e940f597 fix tests and add .triggerOnlyOnProcessEnvCliCall() 2017-10-12 22:44:34 +02:00
philkunz 45d3ce8ffc fix linting issues 2017-10-12 20:38:34 +02:00
philkunz ce121b8b7f 2.0.7 2017-05-07 16:01:14 +02:00
philkunz ce65b8d7c9 fix promise rejection on standard task 2017-05-07 16:01:10 +02:00
philkunz 9acdfca460 2.0.6 2017-04-23 15:21:11 +02:00
philkunz 59bcd8dadf use new tapbundle 2017-04-23 15:21:08 +02:00
philkunz b6375fd8fa 2.0.5 2017-04-22 23:16:52 +02:00
philkunz 8183417c90 comment out one test that makes problems due to tap 2017-04-22 23:16:49 +02:00
philkunz 5e66d35125 update tests 2017-04-22 22:09:51 +02:00
philkunz 3ff4c3ff2f 2.0.4 2017-04-22 21:08:07 +02:00
philkunz 6508b29bfc add npmextra.json 2017-04-22 21:08:01 +02:00
philkunz 66fd7138ab 2.0.3 2017-04-22 21:05:02 +02:00
philkunz f3ce1c1408 update ci 2017-04-22 21:04:59 +02:00
philkunz d2b84acc55 2.0.2 2017-04-22 21:04:15 +02:00
philkunz ce008da9ad update .gitignore 2017-04-22 21:04:11 +02:00
philkunz f0f1f9b86f update to latest standards 2017-04-22 21:03:28 +02:00
philkunz 089787454a 2.0.1 2016-12-18 20:58:39 +01:00
philkunz f8a122b777 fix argvArg for observables 2016-12-18 20:58:37 +01:00
philkunz c6db092062 2.0.0 2016-12-18 20:53:53 +01:00
philkunz 857d31dcb2 introduce triggers 2016-12-18 20:53:50 +01:00
philkunz e257a38688 1.0.16 2016-12-18 01:36:24 +01:00
philkunz 19a5082381 added .triggerCommandByName 2016-12-18 01:36:19 +01:00
philkunz 00f5539e6b improve README 2016-11-19 17:48:56 +01:00
philkunz cacb0221f1 IMprove README 2016-11-19 17:48:07 +01:00
philkunz b98b90163d 1.0.15 2016-11-19 17:41:16 +01:00
philkunz daa6312aea Update Metadata 2016-11-19 17:41:11 +01:00
philkunz 7f2dab091f 1.0.14 2016-11-19 15:02:29 +01:00
philkunz dd293875c4 improve README 2016-11-19 15:02:24 +01:00
philkunz 120eca42ac 1.0.13 2016-11-19 13:43:33 +01:00
philkunz fc289616f6 1.0.12 2016-11-19 13:43:10 +01:00
philkunz e7c1c1c45b cleanup 2016-11-19 13:43:06 +01:00
philkunz f33c759fa8 improve README 2016-10-15 02:12:10 +02:00
philkunz 1185df362b update test file 2016-10-15 01:06:36 +02:00
philkunz 36de8e11f0 1.0.11 2016-10-15 01:01:25 +02:00
philkunz 74ffb3aa87 update deps 2016-10-15 01:01:22 +02:00
philkunz 96a6d01720 1.0.10 2016-10-15 00:56:05 +02:00
philkunz 7833bd0be8 implement standardJS 2016-10-15 00:56:02 +02:00
40 changed files with 16515 additions and 693 deletions
+66
View File
@@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
+124
View File
@@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true
+15 -4
View File
@@ -1,9 +1,20 @@
node_modules/ .nogit/
# artifacts
coverage/ coverage/
public/
pages/ pages/
# installs
node_modules/
ts/*.js # caches
ts/*.js.map .yarn/
ts/typings/ .cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom
-50
View File
@@ -1,50 +0,0 @@
image: hosttoday/ht-docker-node:npmts
stages:
- test
- release
- page
testLEGACY:
stage: test
script:
- npmci test legacy
tags:
- docker
allow_failure: true
testLTS:
stage: test
script:
- npmci test lts
tags:
- docker
testSTABLE:
stage: test
script:
- npmci test stable
tags:
- docker
release:
stage: release
script:
- npmci publish
only:
- tags
tags:
- docker
pages:
image: hosttoday/ht-docker-node:npmpage
stage: page
script:
- npmci test stable
- npmci command npmpage --host gitlab
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
View File
+32
View File
@@ -0,0 +1,32 @@
{
"@git.zone/cli": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartcli",
"description": "A library that simplifies building reactive command-line applications using observables, with robust support for commands, arguments, options, aliases, and asynchronous operation management.",
"npmPackagename": "@push.rocks/smartcli",
"license": "MIT",
"projectDomain": "push.rocks"
},
"release": {
"targets": {
"npm": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
}
}
},
"schemaVersion": 2
},
"@git.zone/tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [LICENSE](LICENSE) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
},
"@ship.zone/szci": {
"npmGlobalTools": []
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}
+1 -1
View File
@@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2015 Push.Rocks Copyright (c) 2015 Task Venture Capital GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
-47
View File
@@ -1,47 +0,0 @@
# smartcli
nodejs wrapper for CLI related tasks. TypeScript ready.
## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartcli)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartcli)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartcli)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartcli/docs)
## Status for master
[![build status](https://gitlab.com/pushrocks/smartcli/badges/master/build.svg)](https://gitlab.com/pushrocks/smartcli/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartcli/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartcli/commits/master)
[![Dependency Status](https://david-dm.org/pushrocks/smartcli.svg)](https://david-dm.org/pushrocks/smartcli)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartcli/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartcli/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartcli/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartcli)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
## Install the package
npm install smartcli --save
## Usage
this plugin tries to establish some logic in which CLI tools work.
take the following commandline input:
```
mytool function argument1 argument2 --option1 -o2 option2Value
```
* 'mytool' obviously is the tool (like git)
* function is the main thing the tool shall do (like commit)
* argument1 and argument2 are arguments
* option1 is a longform option you can add (like --message for message)
* optionValue is the referenced option value (like a commit message)
```typescript
import {Smartcli} from "smartcli"
mySmartcli = new Smartcli();
mySmartcli.standardTask()
.then(argvArg => {
// do something if program is called without an command
});
mySmartcli.question
```
+444
View File
@@ -0,0 +1,444 @@
# Changelog
## Pending
## 2026-05-13 - 4.3.0
### Features
- add optional live timers and spinners to terminal tasks (terminal)
- Adds task options and runtime toggles for live timers and animated spinners in interactive terminal rendering.
- Prefixes every line of multiline updates and failure details with the task name in non-interactive output for clearer logs.
- Uses @push.rocks/smarttime to format timer output as human-readable second-based durations.
## 2026-05-13 - 4.2.0
### Features
- enhance terminal task rendering with progress, lifecycle helpers, and configurable output modes (terminal)
- add task progress reporting and task.run() helpers for automatic completion and failure handling
- support configurable unicode or ascii symbols, cleanup behavior, and throttled non-interactive lifecycle logs
- export new terminal task run and symbol mode types and document the updated terminal API
## 2026-05-13 - 4.1.0
### Features
- add live terminal task rendering with interactive and non-interactive output modes (terminal)
- introduces SmartcliTerminal and SmartcliTerminalTask exports for fixed-row task rendering
- supports task updates, completion, failure handling, and persistent error output
- detects interactive terminals and falls back to append-only logs in CI or non-TTY environments
- adds tests covering interactive rendering, non-interactive logging, and error attachment behavior
## 2026-04-30 - 4.0.21 - fix(smartcli)
tighten command parsing and error handling while updating build and package configuration
- throw an explicit error when triggering an unregistered command instead of failing on an undefined subject
- make the cli version property optional to align with current typing expectations
- update tests to use explicit argv input and export the tap startup call for runtime compatibility
- enable stricter TypeScript configuration and refresh build, dependency, and package metadata files
## 2025-10-28 - 4.0.19 - fix(license)
Update license files and add local tool settings
- Update LICENSE header to reference Task Venture Capital GmbH as copyright holder
- Add a new license file containing the full MIT license text
- Add .claude/settings.local.json to store local tool permission settings
## 2025-10-28 - 4.0.18 - fix(smartcli)
Allow passing argv to startParse and improve getUserArgs Deno/runtime handling; update tests and add license
- Smartcli.startParse now accepts an optional testArgv parameter to bypass automatic runtime detection (makes testing deterministic).
- getUserArgs logic refined: always prefer Deno.args when available (handles Deno run and compiled executables reliably) and improve execPath fallback and slicing behavior for Node/Bun/other launchers.
- Tests updated: test/test.node+deno+bun.ts now passes process.argv explicitly to startParse to avoid Deno.args interference in test environments.
- Added MIT LICENSE file and a local .claude/settings.local.json for environment/permission settings.
## 2025-10-28 - 4.0.17 - fix(license)
Add MIT license and local Claude settings
- Add LICENSE file (MIT) to repository
- Add .claude/settings.local.json with local permissions for tooling
## 2025-10-28 - 4.0.16 - fix(smartcli.helpers)
Improve CLI argument parsing and Deno runtime detection; use getUserArgs consistently
- Enhance getUserArgs() to prefer Deno.args but detect when process.argv was manipulated (e.g. in tests) and fallback to manual parsing
- Add robust handling of process.execPath / execPath basename and compute correct argv offset for known launchers vs. compiled executables
- Call getUserArgs() (no explicit process.argv) from Smartcli.getOption and Smartcli.startParse to ensure consistent cross-runtime behavior
- Expand readme.hints.md with detailed cross-runtime examples and explanation of Deno.args vs process.argv for compiled executables
- Add local claude settings file for tooling configuration
## 2025-10-28 - 4.0.15 - fix(smartcli.helpers)
Add robust getUserArgs helper and refactor Smartcli to use it; add MIT license and update documentation
- Add ts/smartcli.helpers.ts: getUserArgs to normalize user arguments across Node.js, Deno (run/compiled), and Bun, with safety checks for test environments
- Refactor Smartcli (ts/smartcli.classes.smartcli.ts) to use getUserArgs in startParse and getOption for correct argument parsing and improved test compatibility
- Update readme.hints.md with detailed cross-runtime CLI argument parsing guidance
- Add LICENSE (MIT) file
- Add .claude/settings.local.json (local settings)
## 2025-10-28 - 4.0.14 - fix(license)
Add MIT license file
- Add MIT License file to repository to clarify licensing and copyright (Push.Rocks 2015).
## 2025-10-27 - 4.0.13 - fix(smartcli)
Improve CLI argument parsing, update deps and tests
- Enhance startParse() to filter runtime executables and script paths (node, deno, bun, tsx, ts-node) so commands are detected correctly across runtimes.
- Switch path import to node:path in plugins for ESM compatibility.
- Bump various dependencies and devDependencies (including @push.rocks/lik, @push.rocks/smartlog, @push.rocks/smartpromise, @push.rocks/smartrx, yargs-parser, @git.zone tooling) and add packageManager field.
- Replace / reorganize tests: add/modify test/test.node+deno+bun.ts, adjust test script to use --verbose.
- Add deno.lock and include many resolved npm dependencies.
- Add LICENSE file (MIT).
## 2025-04-01 - 4.0.12 - fix(docs)
Update documentation with comprehensive usage examples, improved command alias descriptions, and detailed configuration instructions
- Revised readme.md with in-depth examples covering multiple CLI scenarios and RxJS integration
- Updated package.json and npmextra.json descriptions and keywords to reflect enhanced functionality
- Expanded usage guide with additional commands, error handling strategies, and testing guidelines
## 2024-05-29 - 4.0.11 - general
update description
- Updated the project description
## 2024-05-28 - 4.0.10 - core
fix(core): update
- Fixed core functionality
## 2024-04-13 - 4.0.9 - core
fix(core): update
- Improved core update handling
## 2024-04-12 - 4.0.8 - core / npmextra
- fix(core): update
- update npmextra.json: githost (this change was applied multiple times)
## 2023-08-19 - 4.0.7 - core
fix(core): update
- Fixed core issues
## 2023-07-12 - 4.0.6 - core / org
- fix(core): update
- switch to new org scheme (applied twice)
## 2022-08-07 - 4.0.5 - core
fix(core): update
- Fixed core functionality
## 2022-08-04 - 4.0.4 - core
fix(core): update
- Improved core update
## 2022-08-03 - 4.0.3 - core
fix(core): update
- Fixed core issues
## 2022-08-03 - 4.0.2 - core
fix(core): update
- Updated core handling
## 2022-08-03 - 4.0.1 - core
fix(core): update
- Fixed core functionality
## 2022-08-03 - 4.0.0 - core
fix(core): update
- Improved core update handling
## 2022-08-03 - 3.0.14 - core
BREAKING CHANGE(core): switch to esm
- Switched the project to use ECMAScript modules
## 2021-04-07 - 3.0.13 - core
fix(core): update
- Fixed core update issues
## 2021-04-07 - 3.0.12 - core
fix(core): update
- Updated core functionality
## 2020-05-29 - 3.0.11 - core
fix(core): update
- Fixed core functionality
## 2020-04-13 - 3.0.10 - core
fix(core): more consistent handling of process.enc.CLI_CALL
- Made process.enc.CLI_CALL handling more consistent
## 2020-04-13 - 3.0.9 - core
fix(core): now works better with tapbundle tests
- Improved compatibility with tapbundle tests
## 2020-03-11 - 3.0.8 - core
fix(core): update
- Updated core functionality
## 2020-03-11 - 3.0.7 - core
fix(core): update
- Fixed core update issues
## 2018-12-11 - 3.0.6 - core
fix(core): update
- Updated core functionality
## 2018-09-30 - 3.0.5 - ci
fix(ci): remove obsolete dependencies
- Removed obsolete dependencies from CI configuration
## 2018-09-30 - 3.0.4 - core
fix(core): update
- Updated core functionality
## 2018-08-30 - 3.0.3 - structure
fix(structure): remove dist/ dir from git repo
- Removed the dist/ directory from the repository
## 2018-08-30 - 3.0.2 - dependencies
fix(dependencies): update to latest versions
- Bumped dependency versions to the latest
## 2018-06-28 - 3.0.1 - core
fix(core): slim down dependencies
- Slimmed down core dependencies
## 2018-05-03 - 3.0.0 - general
update
- General update
## 2018-05-03 - 2.0.12 - core / architecture
- change to an all rxjs Subject architecture
- system change
- fix(core): cleanup
- remove package-lock since using yarn
## 2018-01-27 - 2.0.11 - security
fix(improve security CI step):
- Improved security in the CI step
## 2018-01-27 - 2.0.10 - core / ci
- fix(core): remove vulnerable paths
- update ci (applied multiple times)
## 2018-01-27 - 2.0.09 - CI
add security step to CI
- Added an extra security step in the CI process
## 2017-10-12 - 2.0.08 - compatibility
ensure compatibility with code assertion library
- Ensured compatibility with the code assertion library
## 2017-10-12 - 2.0.07 - tests / cli
- fix tests and add .triggerOnlyOnProcessEnvCliCall()
- fix linting issues
## 2017-05-07 - 2.0.06 - tasks
fix promise rejection on standard task
- Fixed a promise rejection issue on standard tasks
## 2017-04-23 - 2.0.05 - tapbundle
use new tapbundle
- Switched to the new tapbundle for testing
## 2017-04-22 - 2.0.04 - tests
- comment out one test that makes problems due to tap
- update tests
## 2017-04-22 - 2.0.03 - npmextra
add npmextra.json
- Added npmextra.json for extra configuration
## 2017-04-22 - 2.0.02 - ci
update ci
- Updated CI configuration
## 2017-04-22 - 2.0.01 - misc
- update .gitignore
- update to latest standards
## 2016-12-18 - 2.0.00 - core
fix argvArg for observables
- Fixed the argvArg handling for observables
## 2016-12-18 - 1.0.16 - triggers
introduce triggers
- Introduced triggers
## 2016-11-19 - 1.0.15 - triggers / docs
- added .triggerCommandByName
- improve README
## 2016-11-19 - 1.0.14 - metadata
Update Metadata
- Updated project metadata
## 2016-11-19 - 1.0.13 - docs
improve README
- Improved the README documentation
## 2016-11-19 - 1.0.11 - core / tests / docs
- cleanup
- improve README
- update test file
## 2016-10-14 - 1.0.10 - deps
update deps
- Updated dependencies
## 2016-10-14 - 1.0.09 - standardJS
implement standardJS
- Implemented standardJS support
## 2016-09-04 - 1.0.08 - typings
improve typings
- Improved TypeScript typings
## 2016-09-04 - 1.0.07 - ci
fix ci
- Fixed CI configuration
## 2016-09-04 - 1.0.06 - base
fix base image
- Fixed the base image used for builds
## 2016-09-04 - 1.0.05 - interaction
- add page stage
- improve typings and docs
- update smartcli
- Add new file
- start interaction module
## 2016-08-26 - 1.0.04 - intellisense
improve intellisense
- Improved editor intellisense
## 2016-06-22 - 1.0.03 - compile
compile fix
- Fixed compilation issues
## 2016-06-22 - 1.0.02 - updates
- fix
- add getCommandPromise
- update deps and transition from npmts to npmts-g
## 2016-06-16 - 1.0.01 - tasks
- standard tasks now returns argv
- some cosmetics
- introduce new classes
## 2016-06-10 - 1.0.00 - version
- fix version return
- return argv to command
## 2016-06-10 - 0.0.13 - smartcli
- first version with basic funtionality
- remove bulk and add some features to Smartcli class
- start restructuring to use a smarter Smartcli class that handles command evaluation for you
- update dependencies
- compile
- add gitlab ci
- start smartcli class
- add class smartcli
- Update README and include commander
- fixed type issue
- fixed test issue
- update deps
## 2016-04-04 - 0.0.12 - deps
updated deps
- Updated dependencies
## 2016-04-04 - 0.0.11 - interface
- updated deps
- work in progress (noted twice)
- small interface fix
## 2015-11-09 - 0.0.10 - travis
improve travis process
- Improved the Travis process
## 2015-11-09 - 0.0.09 - tests / CLI
- add tests and fix some errors
- add Tests and improve TypeScript organization
- start smarter CLI logic
- fix small comment error (applied twice)
## 2015-10-14 - 0.0.08 - readme
- improved readme
- updated readme
- added devStatus badge
## 2015-10-12 - 0.0.07 - various
- improved return objects
- (Minor dependency updates and CI tweaks for beautylog and travis were also applied in this version)
## 2015-10-06 - 0.0.05 - CLI
- small update
- now handling CLI options
## 2015-10-05 - 0.0.4 - tests
modified test
## 2015-10-05 - 0.0.02 - travis / tests
- added travis + tests
- package.json update
## 2015-10-04 - 0.0.01 - initial
added initial structure
## 2015-10-04 - unknown - initial
Initial commit
---
## Summary of Omitted Versions
The following versions contained no additional userfacing changes beyond version bumps and are summarized here: 1.0.12, 0.0.6, and 0.0.3.
Generated
+6246
View File
File diff suppressed because it is too large Load Diff
-2
View File
@@ -1,2 +0,0 @@
import "typings-global";
export { Smartcli } from "./smartcli.classes.smartcli";
-5
View File
@@ -1,5 +0,0 @@
"use strict";
require("typings-global");
var smartcli_classes_smartcli_1 = require("./smartcli.classes.smartcli");
exports.Smartcli = smartcli_classes_smartcli_1.Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFBZ0IsQ0FBQyxDQUFBO0FBR3hCLDBDQUF1Qiw2QkFBNkIsQ0FBQztBQUE3Qyx3REFBNkMifQ==
-32
View File
@@ -1,32 +0,0 @@
import "typings-global";
/**
* allows to specify an user interaction during runtime
*/
export declare type questionType = "input" | "confirm" | "list" | "rawlist" | "expand" | "checkbox" | "password" | "editor";
export interface choiceObject {
name: string;
value: any;
}
export interface validateFunction {
(any: any): boolean;
}
export declare class Interaction {
constructor();
askQuestion(optionsArg: {
type: questionType;
message: string;
default: any;
choices: string[] | choiceObject[];
validate: validateFunction;
}): void;
askQuestionArray: any;
}
export declare class QuestionTree {
constructor(questionString: string, optionsArray: any);
}
export declare class QuestionTreeNode {
constructor();
}
export declare class QuestionStorage {
constructor();
}
-41
View File
@@ -1,41 +0,0 @@
"use strict";
require("typings-global");
const plugins = require("./smartcli.plugins");
class Interaction {
constructor() {
}
;
askQuestion(optionsArg) {
let done = plugins.q.defer();
plugins.inquirer.prompt([{
type: optionsArg.type,
message: optionsArg.message,
default: optionsArg.default,
choices: optionsArg.choices,
validate: optionsArg.validate
}]).then(answers => {
done.resolve(answers);
});
}
;
}
exports.Interaction = Interaction;
class QuestionTree {
constructor(questionString, optionsArray) {
}
;
}
exports.QuestionTree = QuestionTree;
;
class QuestionTreeNode {
constructor() {
}
}
exports.QuestionTreeNode = QuestionTreeNode;
;
class QuestionStorage {
constructor() {
}
}
exports.QuestionStorage = QuestionStorage;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5pbnRlcmFjdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuaW50ZXJhY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUN4QixNQUFZLE9BQU8sV0FBTSxvQkFBb0IsQ0FBQyxDQUFBO0FBZTlDO0lBQ0k7SUFDQSxDQUFDOztJQUVELFdBQVcsQ0FBQyxVQU1YO1FBQ0csSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztRQUM3QixPQUFPLENBQUMsUUFBUSxDQUFDLE1BQU0sQ0FBQyxDQUFDO2dCQUNyQixJQUFJLEVBQUUsVUFBVSxDQUFDLElBQUk7Z0JBQ3JCLE9BQU8sRUFBRSxVQUFVLENBQUMsT0FBTztnQkFDM0IsT0FBTyxFQUFFLFVBQVUsQ0FBQyxPQUFPO2dCQUMzQixPQUFPLEVBQUMsVUFBVSxDQUFDLE9BQU87Z0JBQzFCLFFBQVEsRUFBRSxVQUFVLENBQUMsUUFBUTthQUNoQyxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsT0FBTztZQUNaLElBQUksQ0FBQyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUM7UUFDMUIsQ0FBQyxDQUFDLENBQUM7SUFDUCxDQUFDOztBQUVMLENBQUM7QUF2QlksbUJBQVcsY0F1QnZCLENBQUE7QUFHRDtJQUVJLFlBQVksY0FBc0IsRUFBRSxZQUFZO0lBRWhELENBQUM7O0FBQ0wsQ0FBQztBQUxZLG9CQUFZLGVBS3hCLENBQUE7QUFBQSxDQUFDO0FBRUY7SUFDSTtJQUVBLENBQUM7QUFDTCxDQUFDO0FBSlksd0JBQWdCLG1CQUk1QixDQUFBO0FBQUEsQ0FBQztBQUVGO0lBQ0k7SUFFQSxDQUFDO0FBQ0wsQ0FBQztBQUpZLHVCQUFlLGtCQUkzQixDQUFBIn0=
-52
View File
@@ -1,52 +0,0 @@
/// <reference types="q" />
import "typings-global";
import * as plugins from "./smartcli.plugins";
import { Objectmap } from "lik";
export interface commandPromiseObject {
commandName: string;
promise: plugins.q.Promise<any>;
}
export declare class Smartcli {
argv: any;
questionsDone: any;
parseStarted: any;
commands: any;
questions: any;
version: string;
allCommandPromises: Objectmap<commandPromiseObject>;
constructor();
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
*/
addAlias(keyArg: any, aliasArg: any): void;
/**
* adds a Command by returning a Promise that reacts to the specific commandString given.
*
* Note: in e.g. "npm install something" the "install" is considered the command.
*/
addCommand(definitionArg: {
commandName: string;
}): plugins.q.Promise<any>;
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg: string): plugins.q.Promise<any>;
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg: {
helpText: string;
}): void;
/**
* specify version to be printed for -v --version
*/
addVersion(versionArg: string): void;
/**
* returns promise that is resolved when no commands are specified
*/
standardTask(): plugins.q.Promise<any>;
/**
* start the process of evaluating commands
*/
startParse(): void;
}
-104
View File
@@ -1,104 +0,0 @@
"use strict";
require("typings-global");
const plugins = require("./smartcli.plugins");
// import classes
const lik_1 = require("lik");
;
class Smartcli {
constructor() {
// maps
this.allCommandPromises = new lik_1.Objectmap();
this.argv = plugins.yargs;
this.questionsDone = plugins.q.defer();
this.parseStarted = plugins.q.defer();
}
;
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
*/
addAlias(keyArg, aliasArg) {
this.argv = this.argv.alias(keyArg, aliasArg);
return;
}
;
/**
* adds a Command by returning a Promise that reacts to the specific commandString given.
*
* Note: in e.g. "npm install something" the "install" is considered the command.
*/
addCommand(definitionArg) {
let done = plugins.q.defer();
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(definitionArg.commandName) == 0) {
done.resolve(this.argv);
}
else {
done.reject(this.argv);
}
});
return done.promise;
}
;
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg) {
return this.allCommandPromises.find(commandPromiseObjectArg => {
return commandPromiseObjectArg.commandName === commandNameArg;
}).promise;
}
;
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg) {
this.addCommand({
commandName: "help"
}).then(argvArg => {
plugins.beautylog.log(optionsArg.helpText);
});
}
;
/**
* specify version to be printed for -v --version
*/
addVersion(versionArg) {
this.version = versionArg;
this.addAlias("v", "version");
this.parseStarted.promise
.then(() => {
if (this.argv.v) {
console.log(this.version);
}
});
}
;
/**
* returns promise that is resolved when no commands are specified
*/
standardTask() {
let done = plugins.q.defer();
this.parseStarted.promise
.then(() => {
if (this.argv._.length == 0 && !this.argv.v) {
done.resolve(this.argv);
}
else {
done.reject(this.argv);
}
;
});
return done.promise;
}
/**
* start the process of evaluating commands
*/
startParse() {
this.argv = this.argv.argv;
this.parseStarted.resolve();
return;
}
}
exports.Smartcli = Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUV4QixNQUFZLE9BQU8sV0FBTSxvQkFBb0IsQ0FBQyxDQUFBO0FBRzlDLGlCQUFpQjtBQUNqQixzQkFBd0IsS0FBSyxDQUFDLENBQUE7QUFNN0IsQ0FBQztBQUVGO0lBVUk7UUFGQSxPQUFPO1FBQ1AsdUJBQWtCLEdBQUcsSUFBSSxlQUFTLEVBQXdCLENBQUM7UUFFdkQsSUFBSSxDQUFDLElBQUksR0FBRyxPQUFPLENBQUMsS0FBSyxDQUFDO1FBQzFCLElBQUksQ0FBQyxhQUFhLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztRQUN2QyxJQUFJLENBQUMsWUFBWSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQU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DQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDNUIsQ0FBQztZQUFDLElBQUksQ0FBQyxDQUFDO2dCQUNKLElBQUksQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQzNCLENBQUM7WUFBQSxDQUFDO1FBQ04sQ0FBQyxDQUFDLENBQUM7UUFDUCxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztJQUN4QixDQUFDO0lBRUQ7O09BRUc7SUFDSCxVQUFVO1FBQ04sSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQztRQUMzQixJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQzVCLE1BQU0sQ0FBQztJQUNYLENBQUM7QUFFTCxDQUFDO0FBdkdZLGdCQUFRLFdBdUdwQixDQUFBIn0=
-9
View File
@@ -1,9 +0,0 @@
import "typings-global";
export import yargs = require('yargs');
export import beautylog = require("beautylog");
export import cliff = require("cliff");
export import inquirer = require("inquirer");
export import lik = require("lik");
export import path = require("path");
export import q = require("q");
export import smartparam = require("smartparam");
-11
View File
@@ -1,11 +0,0 @@
"use strict";
require("typings-global");
exports.yargs = require('yargs');
exports.beautylog = require("beautylog");
exports.cliff = require("cliff");
exports.inquirer = require("inquirer");
exports.lik = require("lik");
exports.path = require("path");
exports.q = require("q");
exports.smartparam = require("smartparam");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUVWLGFBQUssV0FBVyxPQUFPLENBQUMsQ0FBQztBQUN6QixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2pDLGFBQUssV0FBVyxPQUFPLENBQUMsQ0FBQztBQUN6QixnQkFBUSxXQUFXLFVBQVUsQ0FBQyxDQUFDO0FBQy9CLFdBQUcsV0FBVyxLQUFLLENBQUMsQ0FBQztBQUNyQixZQUFJLFdBQVcsTUFBTSxDQUFDLENBQUM7QUFDdkIsU0FBQyxXQUFXLEdBQUcsQ0FBQyxDQUFDO0FBQ2pCLGtCQUFVLFdBQVcsWUFBWSxDQUFDLENBQUMifQ==
-15
View File
@@ -1,15 +0,0 @@
{
"structure": {
"readme": "index.md"
},
"plugins": [
"tonic",
"edit-link"
],
"pluginsConfig": {
"edit-link": {
"base": "https://gitlab.com/pushrocks/npmts/edit/master/docs/",
"label": "Edit on GitLab"
}
}
}
+16 -32
View File
@@ -1,47 +1,31 @@
# smartcli # smartcli
nodejs wrapper for CLI related tasks. TypeScript ready.
nodejs wrapper for CLI related tasks
## Availabililty ## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartcli)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartcli) [![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartcli)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartcli) [![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartcli)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartcli/docs) [![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartcli)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartcli/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/smartcli/badges/master/build.svg)](https://gitlab.com/pushrocks/smartcli/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartcli/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartcli/commits/master) [![build status](https://GitLab.com/pushrocks/smartcli/badges/master/build.svg)](https://GitLab.com/pushrocks/smartcli/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartcli/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartcli/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartcli.svg)](https://www.npmjs.com/package/smartcli)
[![Dependency Status](https://david-dm.org/pushrocks/smartcli.svg)](https://david-dm.org/pushrocks/smartcli) [![Dependency Status](https://david-dm.org/pushrocks/smartcli.svg)](https://david-dm.org/pushrocks/smartcli)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartcli/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartcli/master/dependencies/npm) [![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartcli/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartcli/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartcli/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartcli) [![bitHound Code](https://www.bithound.io/github/pushrocks/smartcli/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartcli)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) [![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) [![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Install the package
npm install smartcli --save
## Usage ## Usage
this plugin tries to establish some logic in which CLI tools work. For further information read the linked docs at the top of this README.
take the following commandline input: > MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
``` [![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)
mytool function argument1 argument2 --option1 -o2 option2Value
```
* 'mytool' obviously is the tool (like git)
* function is the main thing the tool shall do (like commit)
* argument1 and argument2 are arguments
* option1 is a longform option you can add (like --message for message)
* optionValue is the referenced option value (like a commit message)
```typescript
import {Smartcli} from "smartcli"
mySmartcli = new Smartcli();
mySmartcli.standardTask()
.then(argvArg => {
// do something if program is called without an command
});
mySmartcli.question
```
-3
View File
@@ -1,3 +0,0 @@
{
"mode":"default"
}
+56 -34
View File
@@ -1,49 +1,71 @@
{ {
"name": "smartcli", "name": "@push.rocks/smartcli",
"version": "1.0.9", "private": false,
"description": "nodejs wrapper for CLI related tasks", "version": "4.3.0",
"main": "dist/index.js", "description": "A library that simplifies building reactive command-line applications using observables, with robust support for commands, arguments, options, aliases, and asynchronous operation management.",
"typings": "dist/index.d.ts", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"scripts": { "scripts": {
"test": "(npmts)", "test": "(tstest test/ --verbose)",
"testm": "(cd ts/compile && gulp) && (node test.js jazz jam --awesome)", "build": "tsbuild --web",
"devTest": "(npm test) && (node test.js --test true)", "buildDocs": "tsdoc"
"reinstall": "(rm -r node_modules && npm install)",
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
"startdev": "(git checkout master && git pull origin master)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitlab.com/pushrocks/smartcli.git" "url": "https://code.foss.global/push.rocks/smartcli.git"
}, },
"keywords": [ "keywords": [
"json", "CLI",
"jade", "command line",
"template" "observable",
"reactive",
"asynchronous",
"commands",
"arguments",
"options",
"alias",
"typescript",
"node.js",
"development tool"
], ],
"author": "Smart Coordination GmbH <office@push.rocks> (https://push.rocks)", "author": "Lossless GmbH <office@lossless.com> (https://lossless.com)",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://gitlab.com/pushrocks/smartcli/issues" "url": "https://code.foss.global/push.rocks/smartcli/issues"
}, },
"homepage": "https://gitlab.com/pushrocks/smartcli", "homepage": "https://code.foss.global/push.rocks/smartcli",
"dependencies": { "dependencies": {
"@types/cliff": "^0.1.3", "@push.rocks/lik": "^6.4.1",
"@types/inquirer": "0.x.x", "@push.rocks/smartlog": "^3.2.2",
"@types/q": "0.x.x", "@push.rocks/smartobject": "^1.0.12",
"@types/yargs": "0.x.x", "@push.rocks/smartpromise": "^4.2.4",
"beautylog": "^5.0.20", "@push.rocks/smartrx": "^3.0.10",
"cliff": "^0.1.10", "@push.rocks/smarttime": "^4.2.3",
"inquirer": "^1.1.2", "yargs-parser": "22.0.0"
"lik": "^1.0.15",
"q": "^1.4.1",
"smartparam": "0.1.1",
"typings-global": "^1.0.6",
"yargs": "^5.0.0"
}, },
"devDependencies": { "devDependencies": {
"npmts-g": "^5.2.8", "@git.zone/tsbuild": "^4.4.1",
"should": "^11.1.0", "@git.zone/tsrun": "^2.0.4",
"typings-test": "^1.0.1" "@git.zone/tstest": "^3.6.6",
} "@types/node": "^25.7.0",
"@types/yargs-parser": "^21.0.3"
},
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
".smartconfig.json",
"LICENSE",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
],
"packageManager": "pnpm@10.28.2"
} }
+7668
View File
File diff suppressed because it is too large Load Diff
+42
View File
@@ -0,0 +1,42 @@
## Cross-Runtime Compatibility
### CLI Argument Parsing
The module uses a robust cross-runtime approach for parsing command-line arguments through the `getUserArgs()` utility in `ts/smartcli.helpers.ts`.
**Runtime-Specific Implementations:**
| Runtime | process.argv Structure | Preferred API | Reason |
|---------|------------------------|---------------|---------|
| **Node.js** | `["/path/to/node", "/path/to/script.js", ...userArgs]` | Manual parsing | No native user-args API |
| **Deno run** | `["deno", "/path/to/script.ts", ...userArgs]` | `Deno.args` ✅ | Pre-filtered by runtime |
| **Deno compiled** | `["/path/to/binary", "/tmp/deno-compile-.../mod.ts", ...userArgs]` | `Deno.args` ✅ | Filters internal bundle path |
| **Bun** | `["/path/to/bun", "/path/to/script.ts", ...userArgs]` | Manual parsing | Bun.argv not pre-filtered |
**Why Deno.args is Critical for Compiled Executables:**
Deno compiled executables insert an internal bundle path at `argv[1]`:
```javascript
process.argv = [
"/usr/local/bin/moxytool", // argv[0] - executable
"/tmp/deno-compile-moxytool/mod.ts", // argv[1] - INTERNAL bundle path
"scripts", // argv[2] - actual user command
"--option" // argv[3+] - user args
]
Deno.args = ["scripts", "--option"] // ✓ Correctly filtered by Deno runtime
```
**getUserArgs() Logic:**
1. **Prefer Deno.args** when available (unless process.argv appears manipulated for testing)
2. **Fallback to manual parsing** for Node.js and Bun:
- Check `process.execPath` basename
- Known launchers (node, deno, bun, tsx, ts-node) → skip 2 args
- Unknown (compiled executables) → skip 1 arg
3. **Test detection**: If `process.argv.length > 2` in Deno, use manual parsing (handles test manipulation)
**Key Benefits:**
- ✅ Works with custom-named compiled executables
- ✅ Handles Deno's internal bundle path automatically
- ✅ Compatible with test environments
- ✅ No heuristics needed for Deno (runtime does the work)
+462
View File
@@ -0,0 +1,462 @@
# @push.rocks/smartcli
Build small, reactive command-line tools in TypeScript without hand-rolling argument dispatch. `@push.rocks/smartcli` parses the current runtime's user arguments, dispatches the first positional command into an RxJS `Subject`, exposes the parsed `yargs-parser` result to your handler, and gives you a clean fallback path when no command is provided.
It is ESM-first, ships TypeScript declarations, and handles user argument slicing across Node.js, Deno, Deno-compiled executables, Bun, `tsx`, and `ts-node`.
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
## Install
```sh
pnpm add @push.rocks/smartcli
```
## Why Use Smartcli
- Reactive command handlers: every command is an RxJS `Subject`, so command execution fits naturally into observable-based code.
- Tiny API surface: register commands, subscribe handlers, and call `startParse()`.
- Runtime-aware argument handling: Node.js, Deno, Deno-compiled binaries, Bun, `tsx`, and `ts-node` get user-only arguments consistently.
- `yargs-parser` integration: flags and positional arguments arrive as a familiar parsed object.
- Built-in standard command, help command, version output, manual triggering, and parse completion hooks.
## Quick Start
Create a CLI entrypoint, for example `cli.ts`:
```ts
#!/usr/bin/env node
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
cli.addVersion('1.0.0');
cli.addHelp({
helpText: `
Usage:
demo greet --name Ada
demo --version
demo help
Commands:
greet Print a greeting
help Print this help text
`,
});
cli.addCommand('greet').subscribe((argv) => {
const name = argv.name || 'World';
console.log(`Hello, ${name}!`);
});
cli.standardCommand().subscribe(() => {
console.log('Usage: demo greet --name Ada');
console.log('Run "demo help" for details.');
});
cli.startParse();
```
When exposed as a package `bin`, the CLI behaves like this:
```sh
demo greet --name Ada
# Hello, Ada!
demo --version
# 1.0.0
demo help
# prints the configured help text
```
## Terminal Task Rendering
Use `SmartcliTerminal` for long-running jobs that should render cleanly in both interactive and non-interactive environments. In a TTY, active tasks render below each other with a fixed row count, colored status symbols, and optional live timers/spinners. In CI, pipes, Docker logs, or `TERM=dumb`, the same calls become throttled append-only lifecycle logs where every message line is prefixed with its task name.
```ts
import { SmartcliTerminal } from '@push.rocks/smartcli';
const terminal = new SmartcliTerminal();
const buildTask = terminal.task('Build package', {
rows: 3,
showTimer: true,
showSpinner: true,
});
buildTask.update('Installing dependencies');
buildTask.setProgress(1, 2, 'Running tsbuild');
buildTask.complete('Build finished');
const publishTask = terminal.createProcess({
job: 'Publish package',
rows: 4,
});
try {
await publishPackage();
publishTask.complete('Published');
} catch (error) {
publishTask.attachError(error);
}
```
Completed tasks collapse into one permanent success line. Failed tasks collapse into one permanent failure line with error details. If an error should remain visible inside the live task area, use `attachError(error, { keepOpen: true })`.
`showTimer` renders a second-precision live counter using `@push.rocks/smarttime`, for example `4s` or `1m 30s`. `showSpinner` animates the running indicator in interactive terminals and is ignored for append-only output. The shorter aliases `timer` and `spinner` are also accepted.
For scoped work, `task.run()` completes or fails automatically:
```ts
await terminal.task('Generate assets').run(async (task) => {
task.setProgress(1, 3, 'Reading source files');
await readSourceFiles();
task.setProgress(2, 3, 'Rendering assets');
await renderAssets();
task.setProgress(3, 3, 'Writing output');
}, { successMessage: 'Assets generated' });
```
## Execution Model
1. Create a `Smartcli` instance.
2. Register all commands before parsing.
3. Subscribe handlers to the returned command subjects.
4. Call `startParse()` once to parse the current runtime arguments.
5. `startParse()` parses user args with `yargs-parser`.
6. The first positional argument, `argv._[0]`, is treated as the command name.
7. A matching command subject receives the parsed `argv` object through `.next(argv)`.
8. If no command is provided, the subject from `standardCommand()` is triggered when it exists.
9. After normal dispatch, `parseCompleted.promise` resolves with the parsed argument object.
`startParse()` dispatches synchronously. If a handler starts asynchronous work, manage that lifecycle in your application code.
## Commands and Options
Options are available directly on the parsed object handed to your subscription:
```ts
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
cli.addCommand('deploy').subscribe((argv) => {
const environment = argv.env || 'development';
const force = Boolean(argv.force);
console.log(`Deploying to ${environment}`);
if (force) {
console.log('Force mode enabled');
}
});
cli.standardCommand().subscribe(() => {
console.log('Usage: deployer deploy --env production [--force]');
});
cli.startParse();
```
Run it with:
```sh
deployer deploy --env production --force
```
The handler receives a `yargs-parser` result similar to:
```ts
{
_: ['deploy'],
env: 'production',
force: true
}
```
## Positional Arguments
The first positional argument selects the command. Additional positional values remain in `argv._`:
```ts
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
cli.addCommand('run').subscribe((argv) => {
const taskName = argv._[1];
if (!taskName) {
console.log('Usage: tool run <taskName>');
return;
}
console.log(`Running task: ${taskName}`);
});
cli.startParse();
```
```sh
tool run build
# Running task: build
```
## Standard Command
Use `standardCommand()` for the no-command case:
```ts
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
cli.addCommand('status').subscribe(() => {
console.log('Everything is operational.');
});
cli.standardCommand().subscribe(() => {
console.log('Available commands: status');
});
cli.startParse();
```
If no standard command is registered and the user runs the CLI without a command, smartcli prints `no smartcli standard task was created or assigned.`.
## Help and Version Output
`addVersion()` enables `-v` and `--version` when no command is provided:
```ts
cli.addVersion('2.3.0');
```
```sh
tool --version
# 2.3.0
```
`addHelp()` registers a `help` command. It does not create a `--help` flag:
```ts
cli.addHelp({
helpText: `
Usage:
tool build --target app
tool status
`,
});
```
```sh
tool help
```
## Programmatic Dispatch
Use `triggerCommand()` when you want to invoke a registered command yourself, for example in orchestration code or focused tests:
```ts
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
cli.addCommand('build').subscribe((argv) => {
console.log(`Building ${argv.target}`);
});
cli.triggerCommand('build', {
_: ['build'],
target: 'docs',
});
```
`triggerCommand()` throws if the command has not been registered.
## Testing CLIs
`startParse()` accepts an optional `argv` override. Pass a full runtime-style argument array for deterministic tests:
```ts
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
const parsedPromise = cli.parseCompleted.promise;
cli.addCommand('publish').subscribe((argv) => {
console.log(`Publishing with tag ${argv.tag}`);
});
cli.startParse(['node', 'test-cli.js', 'publish', '--tag', 'next']);
const parsed = await parsedPromise;
console.log(parsed.tag);
// next
```
Create a fresh `Smartcli` instance per parse. `parseCompleted` is a single deferred value on the instance and resolves once.
## Cross-Runtime Argument Handling
smartcli uses `getUserArgs()` internally to remove runtime-specific executable and script entries before parsing:
| Runtime mode | Argument strategy |
| --- | --- |
| Deno without an explicit test argv | Uses `Deno.args`, which is already user-only. |
| Node.js | Uses `process.argv` and skips executable plus script path for known launchers. |
| Bun | Uses `process.argv` and skips executable plus script path for known launchers. |
| `tsx` and `ts-node` | Treated as known launchers and parsed like Node.js. |
| Unknown compiled executable | Skips only the executable path, keeping the first user argument intact. |
This matters for compiled CLIs where `process.argv[0]` is the binary itself and the first user argument should not be mistaken for a script path.
## Aliases
The class exposes `addCommandAlias(original, alias)` and the public `aliasObject` for keeping alias metadata:
```ts
cli.addCommandAlias('deploy', 'd');
console.log(cli.aliasObject.deploy);
// ['d']
```
Current command dispatch is exact-match based on `argv._[0]`. If you want executable aliases, register the alias as a command and subscribe it to the same handler:
```ts
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
const deploy = (argv: any) => {
console.log(`Deploying ${argv.target || 'default target'}`);
};
cli.addCommand('deploy').subscribe(deploy);
cli.addCommand('d').subscribe(deploy);
cli.addCommandAlias('deploy', 'd');
cli.startParse();
```
## API Reference
| API | Purpose |
| --- | --- |
| `new Smartcli()` | Creates an isolated CLI parser and command registry. |
| `addCommand(commandName)` | Registers a command and returns its RxJS `Subject`. If the command already exists, the existing subject is reused. |
| `standardCommand()` | Registers and returns the fallback subject for runs without a command. |
| `startParse(testArgv?)` | Parses current user args, or the provided full argv array, and dispatches to the matching command. |
| `triggerCommand(commandName, argvObject)` | Manually emits an argv object into a registered command subject. |
| `getCommandSubject(commandName)` | Returns the subject for a registered command or `null`. |
| `getOption(optionName)` | Parses current runtime args and returns one option by name. Inside command handlers, prefer the provided `argv` object. |
| `addHelp({ helpText })` | Registers a `help` command that logs the provided help text. |
| `addVersion(version)` | Stores the version printed by `-v` or `--version` when no command is selected. |
| `addCommandAlias(original, alias)` | Stores alias metadata in `aliasObject`. Register alias command names separately for direct dispatch. |
| `parseCompleted.promise` | Resolves with the parsed argv object after normal `startParse()` dispatch. |
## Practical Example
This example wires a tiny task runner with commands, options, help, version output, and a standard fallback:
```ts
#!/usr/bin/env node
import { Smartcli } from '@push.rocks/smartcli';
const cli = new Smartcli();
const tasks = new Map<string, () => Promise<void> | void>([
['build', () => console.log('Building project...')],
['test', () => console.log('Running tests...')],
['release', () => console.log('Preparing release...')],
]);
cli.addVersion('1.4.0');
cli.addHelp({
helpText: `
Task Runner
Usage:
tasker list
tasker run <taskName> [--dry]
tasker --version
Available tasks:
build
test
release
`,
});
cli.addCommand('list').subscribe(() => {
for (const taskName of tasks.keys()) {
console.log(taskName);
}
});
cli.addCommand('run').subscribe(async (argv) => {
const taskName = argv._[1];
const dryRun = Boolean(argv.dry);
if (!taskName) {
console.log('Usage: tasker run <taskName> [--dry]');
return;
}
const task = tasks.get(taskName);
if (!task) {
console.log(`Unknown task: ${taskName}`);
return;
}
if (dryRun) {
console.log(`Would run task: ${taskName}`);
return;
}
await task();
});
cli.standardCommand().subscribe(() => {
console.log('Usage: tasker <list|run|help>');
});
cli.startParse();
```
## Sharp Edges Worth Knowing
- Register commands before calling `startParse()`.
- `startParse()` dispatches once; create a new `Smartcli` instance for a second parse.
- `addHelp()` registers the `help` command, not a `--help` option.
- `addVersion()` prints only when no positional command is present.
- `getOption()` reads the current runtime args. In command subscriptions, use the `argv` object you receive for the most testable code.
- Alias metadata is stored, but aliases are not automatically dispatched unless you register the alias command name too.
- smartcli does not validate option schemas. Add validation in your own command handlers when inputs matter.
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
### Company Information
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
-1
View File
@@ -1 +0,0 @@
import "typings-test";
-36
View File
@@ -1,36 +0,0 @@
"use strict";
require("typings-test");
const smartcli = require("../dist/index");
let beautylog = require("beautylog");
let should = require("should");
describe("smartcli.Smartcli class", function () {
let smartCliTestObject;
describe("new Smartcli()", function () {
it("should create a new Smartcli", function () {
smartCliTestObject = new smartcli.Smartcli();
smartCliTestObject.should.be.instanceof(smartcli.Smartcli);
});
});
describe(".addCommand", function () {
it("should add an command", function () {
smartCliTestObject.addCommand({
commandName: "awesome"
});
});
});
describe(".standardTask", function () {
it("should start parsing a standardTask", function (done) {
smartCliTestObject.standardTask()
.then(() => {
console.log("this is the standard Task!");
});
done();
});
});
describe(".startParse", function () {
it("should start parsing the CLI input", function () {
smartCliTestObject.startParse();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFFdEIsTUFBTyxRQUFRLFdBQVcsZUFBZSxDQUFDLENBQUM7QUFDM0MsSUFBSSxTQUFTLEdBQUcsT0FBTyxDQUFDLFdBQVcsQ0FBQyxDQUFDO0FBQ3JDLElBQUksTUFBTSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQztBQUUvQixRQUFRLENBQUMseUJBQXlCLEVBQUM7SUFDL0IsSUFBSSxrQkFBb0MsQ0FBQztJQUN6QyxRQUFRLENBQUMsZ0JBQWdCLEVBQUM7UUFDdEIsRUFBRSxDQUFDLDhCQUE4QixFQUFDO1lBQzlCLGtCQUFrQixHQUFHLElBQUksUUFBUSxDQUFDLFFBQVEsRUFBRSxDQUFDO1lBQzdDLGtCQUFrQixDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLFFBQVEsQ0FBQyxRQUFRLENBQUMsQ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GAAG;IACX,SAAS,CAAC,EAAE,CAAC,gBAAgB,CAAC,CAAC;IAC/B,SAAS,CAAC,OAAO,CAAC,+BAA+B,CAAC,CAAC;AACvD,CAAC,CAAC;AAEF,EAAE,CAAA,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA,CAAC;IACjC,wBAAwB,EAAE,CAAC;AAC/B,CAAC;AAAC,IAAI,CAAC,CAAC;IACJ,SAAS,CAAC,IAAI,CAAC,0EAA0E,CAAC,CAAC;IAC3F,QAAQ,EAAE,CAAC;AACf,CAAC;AAAA,CAAC"}
+39
View File
@@ -0,0 +1,39 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartrx from '@push.rocks/smartrx';
import * as smartcli from '../ts/index.js';
tap.test('should create a new Smartcli', async () => {
const smartCliTestObject = new smartcli.Smartcli();
expect(smartCliTestObject).toBeInstanceOf(smartcli.Smartcli);
});
tap.test('should add an command', async (toolsArg) => {
const done = toolsArg.defer();
const smartCliTestObject = new smartcli.Smartcli();
const awesomeCommandSubject = smartCliTestObject.addCommand('awesome');
expect(awesomeCommandSubject).toBeInstanceOf(smartrx.rxjs.Subject);
awesomeCommandSubject.subscribe(() => {
done.resolve();
});
smartCliTestObject.startParse(['node', 'test.js', 'awesome']);
await done.promise;
});
tap.test('should start parsing a standardTask', async () => {
const smartCliTestObject = new smartcli.Smartcli();
expect(smartCliTestObject.standardCommand()).toBeInstanceOf(smartrx.rxjs.Subject);
});
let hasExecuted: boolean = false;
tap.test('should accept a command', async () => {
const smartCliTestObject = new smartcli.Smartcli();
smartCliTestObject.addCommand('triggerme').subscribe(() => {
hasExecuted = true;
});
smartCliTestObject.triggerCommand('triggerme', {});
expect(hasExecuted).toBeTrue();
});
export default tap.start();
+260
View File
@@ -0,0 +1,260 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartcli from '../ts/index.js';
const delay = async (millisecondsArg: number) => {
await new Promise((resolve) => setTimeout(resolve, millisecondsArg));
};
class TestWritable implements smartcli.ISmartcliWritable {
public chunks: string[] = [];
public isTTY: boolean;
public columns = 80;
constructor(isTTYArg: boolean) {
this.isTTY = isTTYArg;
}
public write(chunkArg: string): boolean {
this.chunks.push(chunkArg);
return true;
}
public toString(): string {
return this.chunks.join('');
}
}
tap.test('should render terminal tasks in non-interactive mode', async () => {
const stream = new TestWritable(false);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: false,
colors: false,
symbols: 'ascii',
nonInteractiveThrottleMs: 0,
});
const task = terminal.createTask({ job: 'build package', rows: 2 });
task.update('running tsbuild');
task.complete('done');
const output = stream.toString();
expect(output).toInclude('start build package');
expect(output).toInclude('update build package: running tsbuild');
expect(output).toInclude('done build package in');
expect(output).toInclude(': done');
expect(terminal.getTasks()).toHaveLength(0);
});
tap.test('should render fixed rows in interactive mode', async () => {
const stream = new TestWritable(true);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: true,
colors: false,
symbols: 'ascii',
cleanup: false,
});
const task = terminal.createTask({ job: 'install dependencies', rows: 2 });
task.update('fetching packages');
const renderedRows = task.renderPlainRows(80);
expect(renderedRows).toHaveLength(2);
expect(renderedRows[0]).toInclude('* install dependencies');
expect(stream.toString()).toInclude('\u001B[?25l');
expect(stream.toString()).toInclude('\u001B[2K');
task.complete('installed');
expect(stream.toString()).toInclude('OK install dependencies');
expect(stream.toString()).toInclude('installed');
expect(stream.toString()).toInclude('\u001B[?25h');
expect(terminal.getTasks()).toHaveLength(0);
});
tap.test('should attach persistent terminal task errors', async () => {
const stream = new TestWritable(true);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: true,
colors: false,
symbols: 'ascii',
cleanup: false,
});
const task = terminal.createTask({ job: 'deploy release', rows: 3 });
task.attachError('deployment failed', { keepOpen: true });
expect(task.status).toEqual('failed');
expect(task.getErrorLines()).toContain('deployment failed');
expect(task.renderPlainRows(80)[0]).toInclude('X deploy release');
expect(terminal.getTasks()).toHaveLength(1);
terminal.clear();
});
tap.test('should collapse failed terminal tasks into permanent output', async () => {
const stream = new TestWritable(false);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: false,
colors: false,
symbols: 'ascii',
});
const task = terminal.createTask({ job: 'publish package' });
task.attachError('registry rejected package');
const output = stream.toString();
expect(output).toInclude('fail publish package in');
expect(output).toInclude(': registry rejected package');
expect(terminal.getTasks()).toHaveLength(0);
});
tap.test('should prefix every non-interactive multiline message with the task name', async () => {
const stream = new TestWritable(false);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: false,
colors: false,
nonInteractiveThrottleMs: 0,
});
const updateTask = terminal.task('multiline update');
updateTask.update('line one\nline two');
updateTask.complete('done');
const failedTask = terminal.task('multiline failure');
failedTask.fail('first failure line\nsecond failure line');
const output = stream.toString();
expect(output).toInclude('update multiline update: line one');
expect(output).toInclude('update multiline update: line two');
expect(output).toInclude('fail multiline failure in');
expect(output).toInclude('fail multiline failure: second failure line');
});
tap.test('should set task progress', async () => {
const stream = new TestWritable(true);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: true,
colors: false,
symbols: 'ascii',
cleanup: false,
});
const task = terminal.task('process files', { rows: 2 });
task.setProgress(2, 5, 'processed files');
expect(task.getLastLogLine()).toInclude('40% (2/5)');
expect(task.renderPlainRows(80)[0]).toInclude('40% (2/5)');
terminal.clear();
});
tap.test('should render an optional second-precision timer', async () => {
const stream = new TestWritable(true);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: true,
colors: false,
symbols: 'ascii',
cleanup: false,
});
const task = terminal.task('timed task', { rows: 2, showTimer: true });
expect(task.getTimerText()).toEqual('0s');
expect(task.renderPlainRows(80)[0]).toInclude('0s');
task.complete('timed complete');
expect(stream.toString()).toInclude('timed complete');
});
tap.test('should render an optional spinner', async () => {
const stream = new TestWritable(true);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: true,
colors: false,
symbols: 'ascii',
cleanup: false,
});
const task = terminal.task('spinner task', {
rows: 2,
showSpinner: true,
spinnerFrames: ['a', 'b'],
spinnerIntervalMs: 20,
});
await delay(90);
const output = stream.toString();
expect(output).toInclude('spinner task');
expect(output.includes('a spinner task') || output.includes('b spinner task')).toBeTrue();
expect(task.getLiveRenderIntervalMs()).toEqual(20);
task.complete('spinner complete');
});
tap.test('should auto-complete task.run', async () => {
const stream = new TestWritable(false);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: false,
colors: false,
nonInteractiveThrottleMs: 0,
});
const task = terminal.task('run operation');
const result = await task.run(async (taskArg) => {
taskArg.update('inside operation');
return 'result';
}, { successMessage: 'operation finished' });
expect(result).toEqual('result');
expect(task.status).toEqual('completed');
expect(stream.toString()).toInclude('done run operation in');
expect(stream.toString()).toInclude('operation finished');
});
tap.test('should auto-fail task.run', async () => {
const stream = new TestWritable(false);
const terminal = new smartcli.SmartcliTerminal({ stream, interactive: false, colors: false });
const task = terminal.task('failing operation');
let caughtError: Error | undefined;
try {
await task.run(async () => {
throw new Error('operation failed');
});
} catch (error) {
caughtError = error as Error;
}
expect(caughtError?.message).toEqual('operation failed');
expect(task.status).toEqual('failed');
expect(stream.toString()).toInclude('fail failing operation in');
expect(stream.toString()).toInclude('operation failed');
});
tap.test('should throttle duplicate non-interactive updates', async () => {
const stream = new TestWritable(false);
const terminal = new smartcli.SmartcliTerminal({
stream,
interactive: false,
colors: false,
nonInteractiveThrottleMs: 10000,
});
const task = terminal.task('quiet task');
task.update('same update');
task.update('same update');
task.update('different but throttled');
const output = stream.toString();
expect(output).toInclude('update quiet task: same update');
expect(output).not.toInclude('different but throttled');
});
export default tap.start();
-37
View File
@@ -1,37 +0,0 @@
import "typings-test";
import smartcli = require("../dist/index");
let beautylog = require("beautylog");
let should = require("should");
describe("smartcli.Smartcli class",function(){
let smartCliTestObject:smartcli.Smartcli;
describe("new Smartcli()",function(){
it("should create a new Smartcli",function(){
smartCliTestObject = new smartcli.Smartcli();
smartCliTestObject.should.be.instanceof(smartcli.Smartcli);
});
});
describe(".addCommand",function(){
it("should add an command",function(){
smartCliTestObject.addCommand({
commandName:"awesome"
});
});
});
describe(".standardTask",function(){
it("should start parsing a standardTask",function(done){
smartCliTestObject.standardTask()
.then(() => {
console.log("this is the standard Task!");
});
done();
})
})
describe(".startParse",function(){
it("should start parsing the CLI input",function(){
smartCliTestObject.startParse();
})
})
});
+8
View File
@@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartcli',
version: '4.3.0',
description: 'A library that simplifies building reactive command-line applications using observables, with robust support for commands, arguments, options, aliases, and asynchronous operation management.'
}
+11 -4
View File
@@ -1,4 +1,11 @@
import "typings-global"; export { Smartcli } from './smartcli.classes.smartcli.js';
export { SmartcliTerminal, SmartcliTerminalTask } from './smartcli.classes.terminal.js';
import {Smartcli} from "./smartcli.classes.smartcli"; export type {
export {Smartcli} from "./smartcli.classes.smartcli"; ISmartcliTerminalAttachErrorOptions,
ISmartcliTerminalOptions,
ISmartcliTerminalTaskRunOptions,
ISmartcliTerminalTaskOptions,
ISmartcliWritable,
TSmartcliTerminalSymbolMode,
TSmartcliTerminalTaskStatus,
} from './smartcli.classes.terminal.js';
-60
View File
@@ -1,60 +0,0 @@
import "typings-global";
import * as plugins from "./smartcli.plugins";
/**
* allows to specify an user interaction during runtime
*/
export type questionType = "input" | "confirm" | "list" | "rawlist" | "expand" | "checkbox" | "password" | "editor"
export interface choiceObject {
name: string;
value: any
}
export interface validateFunction {
(any):boolean
}
export class Interaction {
constructor() {
};
askQuestion(optionsArg: {
type: questionType,
message: string
default: any
choices: string[] | choiceObject[];
validate: validateFunction
}) {
let done = plugins.q.defer();
plugins.inquirer.prompt([{
type: optionsArg.type,
message: optionsArg.message,
default: optionsArg.default,
choices:optionsArg.choices,
validate: optionsArg.validate
}]).then(answers => {
done.resolve(answers);
});
};
askQuestionArray
}
export class QuestionTree {
constructor(questionString: string, optionsArray) {
};
};
export class QuestionTreeNode {
constructor() {
}
};
export class QuestionStorage {
constructor() {
}
}
+127 -81
View File
@@ -1,118 +1,164 @@
import "typings-global"; import * as plugins from './smartcli.plugins.js';
import { getUserArgs } from './smartcli.helpers.js';
import * as plugins from "./smartcli.plugins";
import * as interaction from "./smartcli.classes.interaction";
// import classes
import {Objectmap} from "lik";
// interfaces // interfaces
export interface commandPromiseObject { export interface ICommandObservableObject {
commandName: string; commandName: string;
promise: plugins.q.Promise<any>; subject: plugins.smartrx.rxjs.Subject<any>;
}; }
export class Smartcli { const logger = new plugins.smartlog.ConsoleLog();
argv:any;
questionsDone;
parseStarted;
commands;
questions;
version:string;
// maps
allCommandPromises = new Objectmap<commandPromiseObject>();
constructor(){
this.argv = plugins.yargs;
this.questionsDone = plugins.q.defer();
this.parseStarted = plugins.q.defer();
};
/** /**
* adds an alias, meaning one equals the other in terms of triggering associated commands * class to create a new instance of Smartcli. Handles parsing of command line arguments.
*/ */
addAlias(keyArg,aliasArg):void { export class Smartcli {
this.argv = this.argv.alias(keyArg,aliasArg); /**
return; * this Deferred contains the parsed result in the end
}; */
public parseCompleted = plugins.smartpromise.defer<any>();
public version?: string;
/**
* map of all Trigger/Observable objects to keep track
*/
private commandObservableMap = new plugins.lik.ObjectMap<ICommandObservableObject>();
/**
* maps alias
*/
public aliasObject: { [key: string]: string[] } = {};
/**
* The constructor of Smartcli
*/
constructor() {}
/**
* adds an alias, meaning one equals the other in terms of command execution.
*/
public addCommandAlias(originalArg: string, aliasArg: string): void {
this.aliasObject[originalArg] = this.aliasObject[originalArg] || [];
this.aliasObject[originalArg].push(aliasArg);
}
/** /**
* adds a Command by returning a Promise that reacts to the specific commandString given. * adds a Command by returning a Promise that reacts to the specific commandString given.
*
* Note: in e.g. "npm install something" the "install" is considered the command. * Note: in e.g. "npm install something" the "install" is considered the command.
*/ */
addCommand(definitionArg:{commandName:string}):plugins.q.Promise<any>{ public addCommand(commandNameArg: string): plugins.smartrx.rxjs.Subject<any> {
let done = plugins.q.defer<any>(); let commandSubject: plugins.smartrx.rxjs.Subject<any>;
this.parseStarted.promise const existingCommandSubject = this.getCommandSubject(commandNameArg);
.then(() => { commandSubject = existingCommandSubject || new plugins.smartrx.rxjs.Subject<any>();
if (this.argv._.indexOf(definitionArg.commandName) == 0) {
done.resolve(this.argv); this.commandObservableMap.add({
} else { commandName: commandNameArg,
done.reject(this.argv); subject: commandSubject,
}
}); });
return done.promise; return commandSubject;
}; }
/** /**
* gets a Promise for a command word * execute trigger by name
* @param commandNameArg - the name of the command to trigger
*/ */
getCommandPromiseByName(commandNameArg:string){ public triggerCommand(commandNameArg: string, argvObject: any) {
return this.allCommandPromises.find(commandPromiseObjectArg => { const triggerSubject = this.getCommandSubject(commandNameArg);
return commandPromiseObjectArg.commandName === commandNameArg; if (!triggerSubject) {
}).promise; throw new Error(`No smartcli command registered for ${commandNameArg}`);
}; }
triggerSubject.next(argvObject);
return triggerSubject;
}
/**
* gets the command subject for the specified name.
* call this before calling .parse()
* @param commandNameArg
* @returns
*/
public getCommandSubject(commandNameArg: string) {
const triggerObservableObject = this.commandObservableMap.findSync(
(triggerObservableObjectArg) => {
return triggerObservableObjectArg.commandName === commandNameArg;
}
);
if (triggerObservableObject) {
return triggerObservableObject.subject;
} else {
return null;
}
}
/**
* getOption
*/
public getOption(optionNameArg: string) {
const userArgs = getUserArgs();
const parsedYargs = plugins.yargsParser(userArgs);
return parsedYargs[optionNameArg];
}
/** /**
* allows to specify help text to be printed above the rest of the help text * allows to specify help text to be printed above the rest of the help text
*/ */
addHelp(optionsArg:{ public addHelp(optionsArg: { helpText: string }) {
helpText:string this.addCommand('help').subscribe((argvArg) => {
}){ logger.log('info', optionsArg.helpText);
this.addCommand({ });
commandName:"help" }
}).then(argvArg => {
plugins.beautylog.log(optionsArg.helpText);
})
};
/** /**
* specify version to be printed for -v --version * specify version to be printed for -v --version
*/ */
addVersion(versionArg:string){ public addVersion(versionArg: string) {
this.version = versionArg; this.version = versionArg;
this.addAlias("v","version");
this.parseStarted.promise
.then(() => {
if(this.argv.v){
console.log(this.version);
} }
})
};
/** /**
* returns promise that is resolved when no commands are specified * adds a trigger that is called when no command is specified
*/ */
standardTask():plugins.q.Promise<any>{ public standardCommand(): plugins.smartrx.rxjs.Subject<any> {
let done = plugins.q.defer<any>(); const standardSubject = this.addCommand('standardCommand');
this.parseStarted.promise return standardSubject;
.then(() => {
if(this.argv._.length == 0 && !this.argv.v){
done.resolve(this.argv);
} else {
done.reject(this.argv);
};
});
return done.promise;
} }
/** /**
* start the process of evaluating commands * start the process of evaluating commands
* @param testArgv - Optional argv override for testing (bypasses automatic runtime detection)
*/ */
startParse():void{ public startParse(testArgv?: string[]): void {
this.argv = this.argv.argv; // Get user arguments, properly handling Node.js, Deno (run/compiled), and Bun
this.parseStarted.resolve(); const userArgs = testArgv ? getUserArgs(testArgv) : getUserArgs();
const parsedYArgs = plugins.yargsParser(userArgs);
const wantedCommand = parsedYArgs._[0];
// lets handle some standards
if (!wantedCommand && (parsedYArgs.v || parsedYArgs.version)) {
console.log(this.version || 'unknown version');
return;
}
for (const command of this.commandObservableMap.getArray()) {
if (!wantedCommand) {
const standardCommand = this.commandObservableMap.findSync((commandArg) => {
return commandArg.commandName === 'standardCommand';
});
if (standardCommand) {
standardCommand.subject.next(parsedYArgs);
} else {
console.log('no smartcli standard task was created or assigned.');
}
break;
}
if (command.commandName === parsedYArgs._[0]) {
command.subject.next(parsedYArgs);
break;
}
if (this.aliasObject[parsedYArgs[0]]) {
}
}
this.parseCompleted.resolve(parsedYArgs);
return; return;
} }
} }
+761
View File
@@ -0,0 +1,761 @@
import * as plugins from './smartcli.plugins.js';
export type TSmartcliTerminalTaskStatus = 'running' | 'completed' | 'failed';
export type TSmartcliTerminalSymbolMode = 'auto' | 'unicode' | 'ascii';
export interface ISmartcliWritable {
isTTY?: boolean;
columns?: number;
write(chunk: string): void | boolean;
}
export interface ISmartcliTerminalOptions {
stream?: ISmartcliWritable;
interactive?: boolean;
colors?: boolean;
symbols?: TSmartcliTerminalSymbolMode;
cleanup?: boolean;
nonInteractiveThrottleMs?: number;
}
export interface ISmartcliTerminalTaskOptions {
job: string;
rows?: number;
logLimit?: number;
showTimer?: boolean;
showSpinner?: boolean;
timer?: boolean;
spinner?: boolean;
spinnerFrames?: string[];
spinnerIntervalMs?: number;
}
export interface ISmartcliTerminalAttachErrorOptions {
keepOpen?: boolean;
}
export interface ISmartcliTerminalTaskRunOptions {
successMessage?: string;
errorKeepOpen?: boolean;
}
interface INonInteractiveLogState {
message: string;
timestamp: number;
}
const ansiCodes = {
reset: '\u001B[0m',
red: '\u001B[31m',
green: '\u001B[32m',
cyan: '\u001B[36m',
gray: '\u001B[90m',
};
const unicodeSymbols = {
running: '●',
completed: '✓',
failed: '✕',
};
const asciiSymbols = {
running: '*',
completed: 'OK',
failed: 'X',
};
const unicodeSpinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
const asciiSpinnerFrames = ['-', '\\', '|', '/'];
/**
* A live terminal renderer for multiple fixed-row tasks.
* It automatically falls back to append-only logs in non-interactive environments.
*/
export class SmartcliTerminal {
private stream: ISmartcliWritable;
private interactive: boolean;
private colors: boolean;
private useUnicodeSymbols: boolean;
private cleanupEnabled: boolean;
private nonInteractiveThrottleMs: number;
private tasks: SmartcliTerminalTask[] = [];
private renderedLineCount = 0;
private lastRenderedOutput = '';
private cursorHidden = false;
private cleanupRegistered = false;
private liveRenderInterval: ReturnType<typeof setInterval> | null = null;
private liveRenderIntervalMs = 0;
private nonInteractiveLogState = new Map<SmartcliTerminalTask, INonInteractiveLogState>();
private cleanupHandlers: Array<() => void> = [];
constructor(optionsArg: ISmartcliTerminalOptions = {}) {
this.stream = optionsArg.stream || getDefaultStream();
this.interactive = getInteractiveState(this.stream, optionsArg.interactive);
this.colors = optionsArg.colors ?? (this.interactive && !hasEnvFlag('NO_COLOR'));
this.useUnicodeSymbols = getUnicodeSymbolState(this.stream, optionsArg.symbols);
this.cleanupEnabled = optionsArg.cleanup ?? true;
this.nonInteractiveThrottleMs = Math.max(0, optionsArg.nonInteractiveThrottleMs ?? 250);
}
public createTask(optionsArg: ISmartcliTerminalTaskOptions): SmartcliTerminalTask {
const task = new SmartcliTerminalTask(this, optionsArg);
this.tasks.push(task);
if (this.interactive) {
this.ensureInteractiveSession();
this.render();
this.updateLiveRenderLoop();
} else {
this.writePermanentLine(`start ${task.job}`);
}
return task;
}
public createProcess(optionsArg: ISmartcliTerminalTaskOptions): SmartcliTerminalTask {
return this.createTask(optionsArg);
}
public task(jobArg: string, optionsArg: Omit<ISmartcliTerminalTaskOptions, 'job'> = {}) {
return this.createTask({
...optionsArg,
job: jobArg,
});
}
public isInteractive(): boolean {
return this.interactive;
}
public getTasks(): SmartcliTerminalTask[] {
return [...this.tasks];
}
/** @internal */
public updateTask(taskArg: SmartcliTerminalTask, messageArg?: string): void {
if (!this.tasks.includes(taskArg)) {
return;
}
if (this.interactive) {
this.updateLiveRenderLoop();
this.render();
} else if (messageArg && this.shouldWriteNonInteractiveUpdate(taskArg, messageArg)) {
for (const messageLine of normalizeLines(messageArg)) {
this.writePermanentLine(`update ${taskArg.job}: ${messageLine}`);
}
}
}
/** @internal */
public completeTask(taskArg: SmartcliTerminalTask, messageArg?: string): void {
const message = messageArg || taskArg.getLastLogLine();
const summary = this.interactive
? `${this.getStatusSymbol('completed')} ${taskArg.job} ${taskArg.getElapsedSummaryText()}${message ? ` - ${message}` : ''}`
: `done ${taskArg.job} in ${taskArg.getElapsedSummaryText()}${message ? `: ${message}` : ''}`;
this.finalizeTask(taskArg, [summary], 'completed');
}
/** @internal */
public failTask(taskArg: SmartcliTerminalTask, errorLinesArg: string[]): void {
const summary = this.interactive
? `${this.getStatusSymbol('failed')} ${taskArg.job} ${taskArg.getElapsedSummaryText()}${errorLinesArg[0] ? ` - ${errorLinesArg[0]}` : ''}`
: `fail ${taskArg.job} in ${taskArg.getElapsedSummaryText()}${errorLinesArg[0] ? `: ${errorLinesArg[0]}` : ''}`;
const detailLines = errorLinesArg.slice(1).map((lineArg) => {
return this.interactive ? ` ${lineArg}` : `fail ${taskArg.job}: ${lineArg}`;
});
this.finalizeTask(taskArg, [summary, ...detailLines], 'failed');
}
public clear(): void {
if (this.interactive) {
this.clearRenderedBlock();
this.restoreCursor();
}
this.tasks = [];
this.nonInteractiveLogState.clear();
this.stopLiveRenderLoop();
}
/** @internal */
public getStatusSymbol(statusArg: TSmartcliTerminalTaskStatus): string {
const symbols = this.useUnicodeSymbols ? unicodeSymbols : asciiSymbols;
return symbols[statusArg];
}
/** @internal */
public colorizeLine(lineArg: string, statusArg?: TSmartcliTerminalTaskStatus): string {
if (!this.colors) {
return lineArg;
}
if (statusArg === 'completed') {
return `${ansiCodes.green}${lineArg}${ansiCodes.reset}`;
}
if (statusArg === 'failed') {
return `${ansiCodes.red}${lineArg}${ansiCodes.reset}`;
}
if (statusArg === 'running') {
return `${ansiCodes.cyan}${lineArg}${ansiCodes.reset}`;
}
if (lineArg.startsWith(' ')) {
return `${ansiCodes.gray}${lineArg}${ansiCodes.reset}`;
}
return lineArg;
}
private ensureInteractiveSession(): void {
if (!this.cursorHidden) {
this.stream.write('\u001B[?25l');
this.cursorHidden = true;
}
if (this.cleanupEnabled && !this.cleanupRegistered) {
this.registerProcessCleanup();
}
}
private finalizeTask(
taskArg: SmartcliTerminalTask,
linesArg: string[],
statusArg: TSmartcliTerminalTaskStatus
): void {
this.tasks = this.tasks.filter((task) => task !== taskArg);
this.nonInteractiveLogState.delete(taskArg);
if (this.interactive) {
this.clearRenderedBlock();
this.writePermanentLines(linesArg.map((lineArg) => this.colorizeLine(lineArg, statusArg)));
this.updateLiveRenderLoop();
this.render();
if (this.tasks.length === 0) {
this.restoreCursor();
}
} else {
this.writePermanentLines(linesArg);
}
}
private render(): void {
if (!this.interactive) {
return;
}
const width = this.getLineWidth();
const lines = this.tasks.flatMap((taskArg) => taskArg.renderPlainRows(width));
const coloredLines = lines.map((lineArg) => {
const status = lineArg.startsWith(' ') ? undefined : getStatusFromRenderedLine(lineArg, this);
return this.colorizeLine(lineArg, status);
});
const renderedOutput = coloredLines.join('\n');
if (renderedOutput === this.lastRenderedOutput) {
return;
}
if (this.renderedLineCount > 0) {
this.stream.write(`\u001B[${this.renderedLineCount}F`);
}
const lineCount = Math.max(this.renderedLineCount, coloredLines.length);
for (let index = 0; index < lineCount; index++) {
this.stream.write('\u001B[2K\r');
if (index < coloredLines.length) {
this.stream.write(coloredLines[index]);
}
this.stream.write('\n');
}
this.renderedLineCount = coloredLines.length;
this.lastRenderedOutput = renderedOutput;
}
private clearRenderedBlock(): void {
if (this.renderedLineCount === 0) {
return;
}
this.stream.write(`\u001B[${this.renderedLineCount}F`);
this.stream.write(`\u001B[${this.renderedLineCount}M`);
this.renderedLineCount = 0;
this.lastRenderedOutput = '';
}
private writePermanentLines(linesArg: string[]): void {
for (const line of linesArg) {
this.writePermanentLine(line);
}
}
private writePermanentLine(lineArg: string): void {
this.stream.write(`${lineArg}\n`);
}
private shouldWriteNonInteractiveUpdate(
taskArg: SmartcliTerminalTask,
messageArg: string
): boolean {
const now = Date.now();
const previousState = this.nonInteractiveLogState.get(taskArg);
if (previousState?.message === messageArg) {
return false;
}
if (previousState && now - previousState.timestamp < this.nonInteractiveThrottleMs) {
return false;
}
this.nonInteractiveLogState.set(taskArg, {
message: messageArg,
timestamp: now,
});
return true;
}
private restoreCursor(): void {
if (!this.cursorHidden) {
return;
}
this.stream.write('\u001B[?25h');
this.cursorHidden = false;
this.unregisterProcessCleanup();
}
private registerProcessCleanup(): void {
const processObject = getProcessObject();
if (!processObject?.once) {
return;
}
const restoreOnly = () => {
this.stopLiveRenderLoop();
this.clearRenderedBlock();
if (this.cursorHidden) {
this.stream.write('\u001B[?25h');
this.cursorHidden = false;
}
};
const exitWithSignal = (codeArg: number) => {
restoreOnly();
processObject.exit?.(codeArg);
};
const throwAfterRestore = (errorArg: unknown) => {
restoreOnly();
throw errorArg;
};
const sigintHandler = () => exitWithSignal(130);
const sigtermHandler = () => exitWithSignal(143);
processObject.once('exit', restoreOnly);
processObject.once('SIGINT', sigintHandler);
processObject.once('SIGTERM', sigtermHandler);
processObject.once('uncaughtException', throwAfterRestore);
this.cleanupHandlers = [
() => processObject.off?.('exit', restoreOnly),
() => processObject.off?.('SIGINT', sigintHandler),
() => processObject.off?.('SIGTERM', sigtermHandler),
() => processObject.off?.('uncaughtException', throwAfterRestore),
];
this.cleanupRegistered = true;
}
private updateLiveRenderLoop(): void {
if (!this.interactive) {
this.stopLiveRenderLoop();
return;
}
const liveIntervals = this.tasks
.map((taskArg) => taskArg.getLiveRenderIntervalMs())
.filter((intervalArg): intervalArg is number => Boolean(intervalArg));
const nextInterval = liveIntervals.length > 0 ? Math.min(...liveIntervals) : 0;
if (nextInterval === this.liveRenderIntervalMs) {
return;
}
this.stopLiveRenderLoop();
if (nextInterval > 0) {
this.liveRenderInterval = setInterval(() => this.render(), nextInterval);
(this.liveRenderInterval as any).unref?.();
this.liveRenderIntervalMs = nextInterval;
}
}
private stopLiveRenderLoop(): void {
if (this.liveRenderInterval) {
clearInterval(this.liveRenderInterval);
this.liveRenderInterval = null;
}
this.liveRenderIntervalMs = 0;
}
private unregisterProcessCleanup(): void {
for (const cleanupHandler of this.cleanupHandlers) {
cleanupHandler();
}
this.cleanupHandlers = [];
this.cleanupRegistered = false;
}
private getLineWidth(): number {
return Math.max(20, (this.stream.columns || 80) - 1);
}
}
export class SmartcliTerminalTask {
public readonly job: string;
public readonly rows: number;
public readonly startTime = Date.now();
public status: TSmartcliTerminalTaskStatus = 'running';
private terminal: SmartcliTerminal;
private logLimit: number;
private logLines: string[] = [];
private errorLines: string[] = [];
private progressCurrent?: number;
private progressTotal?: number;
private showTimer: boolean;
private showSpinner: boolean;
private spinnerFrames: string[];
private spinnerIntervalMs: number;
constructor(terminalArg: SmartcliTerminal, optionsArg: ISmartcliTerminalTaskOptions) {
this.terminal = terminalArg;
this.job = optionsArg.job;
this.rows = Math.max(1, Math.floor(optionsArg.rows || 3));
this.logLimit = Math.max(this.rows, Math.floor(optionsArg.logLimit || 100));
this.showTimer = Boolean(optionsArg.showTimer ?? optionsArg.timer ?? false);
this.showSpinner = Boolean(optionsArg.showSpinner ?? optionsArg.spinner ?? false);
this.spinnerFrames = optionsArg.spinnerFrames?.length
? optionsArg.spinnerFrames
: this.terminal.getStatusSymbol('running') === '*'
? asciiSpinnerFrames
: unicodeSpinnerFrames;
this.spinnerIntervalMs = Math.max(20, Math.floor(optionsArg.spinnerIntervalMs || 80));
}
public log(messageArg: string): this {
if (this.status !== 'running') {
return this;
}
const newLines = normalizeLines(messageArg);
this.logLines.push(...newLines);
if (this.logLines.length > this.logLimit) {
this.logLines.splice(0, this.logLines.length - this.logLimit);
}
this.terminal.updateTask(this, newLines.join('\n'));
return this;
}
public update(messageArg: string): this {
return this.log(messageArg);
}
public setTimerEnabled(enabledArg = true): this {
if (this.status !== 'running') {
return this;
}
this.showTimer = enabledArg;
this.terminal.updateTask(this);
return this;
}
public setSpinnerEnabled(enabledArg = true): this {
if (this.status !== 'running') {
return this;
}
this.showSpinner = enabledArg;
this.terminal.updateTask(this);
return this;
}
public setProgress(currentArg: number, totalArg: number, messageArg?: string): this {
if (this.status !== 'running') {
return this;
}
this.progressCurrent = Math.max(0, currentArg);
this.progressTotal = Math.max(0, totalArg);
const progressText = this.getProgressText();
this.log(messageArg ? `${messageArg} ${progressText}` : progressText);
return this;
}
public async run<T>(
operationArg: (taskArg: this) => T | Promise<T>,
optionsArg: ISmartcliTerminalTaskRunOptions = {}
): Promise<T> {
try {
const result = await operationArg(this);
this.complete(optionsArg.successMessage);
return result;
} catch (error) {
this.attachError(error, { keepOpen: optionsArg.errorKeepOpen });
throw error;
}
}
public complete(messageArg?: string): this {
if (this.status !== 'running') {
return this;
}
this.status = 'completed';
this.terminal.completeTask(this, messageArg);
return this;
}
public attachError(
errorArg: unknown,
optionsArg: ISmartcliTerminalAttachErrorOptions = {}
): this {
if (this.status !== 'running') {
return this;
}
this.status = 'failed';
this.errorLines = formatError(errorArg);
if (optionsArg.keepOpen) {
this.terminal.updateTask(this, this.errorLines.join('\n'));
} else {
this.terminal.failTask(this, this.errorLines);
}
return this;
}
public fail(errorArg: unknown): this {
return this.attachError(errorArg);
}
public getElapsedText(): string {
return formatSmarttimeSeconds(Date.now() - this.startTime);
}
public getTimerText(): string {
return formatSmarttimeSeconds(Date.now() - this.startTime);
}
/** @internal */
public getElapsedSummaryText(): string {
return this.showTimer ? this.getTimerText() : this.getElapsedText();
}
public getLastLogLine(): string | undefined {
return this.logLines[this.logLines.length - 1];
}
public getLogLines(): string[] {
return [...this.logLines];
}
public getErrorLines(): string[] {
return [...this.errorLines];
}
/** @internal */
public getLiveRenderIntervalMs(): number | undefined {
if (this.status !== 'running') {
return undefined;
}
if (this.showSpinner) {
return this.spinnerIntervalMs;
}
if (this.showTimer) {
return 1000;
}
return undefined;
}
/** @internal */
public renderPlainRows(widthArg: number): string[] {
const lines: string[] = [];
const detailLines = this.errorLines.length > 0 ? this.errorLines : this.logLines;
const header = this.getHeaderLine();
if (this.rows === 1) {
const lastDetail = detailLines[detailLines.length - 1];
lines.push(`${header}${lastDetail ? ` - ${lastDetail}` : ''}`);
return lines.map((lineArg) => truncateLine(lineArg, widthArg));
}
lines.push(header);
const visibleDetailLineCount = this.rows - 1;
const visibleDetailLines = detailLines.slice(-visibleDetailLineCount);
for (const line of visibleDetailLines) {
lines.push(` ${line}`);
}
while (lines.length < this.rows) {
lines.push('');
}
return lines.map((lineArg) => truncateLine(lineArg, widthArg));
}
private getHeaderLine(): string {
const progressText = this.progressTotal ? ` ${this.getProgressText()}` : '';
const timerText = this.showTimer ? ` ${this.getTimerText()}` : '';
return `${this.getRunningIndicator()} ${this.job}${progressText}${timerText}`;
}
private getRunningIndicator(): string {
if (this.status !== 'running' || !this.showSpinner) {
return this.terminal.getStatusSymbol(this.status);
}
const frameIndex = Math.floor((Date.now() - this.startTime) / this.spinnerIntervalMs) % this.spinnerFrames.length;
return this.spinnerFrames[frameIndex];
}
private getProgressText(): string {
if (!this.progressTotal) {
return '';
}
const percent = Math.floor((this.progressCurrent || 0) / this.progressTotal * 100);
return `${Math.min(100, percent)}% (${this.progressCurrent}/${this.progressTotal})`;
}
}
function getDefaultStream(): ISmartcliWritable {
const processObject = getProcessObject();
if (processObject?.stdout?.write) {
return processObject.stdout;
}
return {
isTTY: false,
write: (chunkArg: string) => {
if (typeof console !== 'undefined') {
console.log(chunkArg.replace(/\n$/, ''));
}
},
};
}
function getInteractiveState(streamArg: ISmartcliWritable, overrideArg?: boolean): boolean {
if (typeof overrideArg === 'boolean') {
return overrideArg;
}
if (!streamArg.isTTY) {
return false;
}
return !(
hasEnvFlag('CI') ||
hasEnvFlag('GITHUB_ACTIONS') ||
hasEnvFlag('JENKINS_URL') ||
hasEnvFlag('GITLAB_CI') ||
hasEnvFlag('TRAVIS') ||
hasEnvFlag('CIRCLECI') ||
getEnvValue('TERM') === 'dumb'
);
}
function getUnicodeSymbolState(
streamArg: ISmartcliWritable,
modeArg: TSmartcliTerminalSymbolMode = 'auto'
): boolean {
if (modeArg === 'unicode') {
return true;
}
if (modeArg === 'ascii') {
return false;
}
const processObject = getProcessObject();
if (processObject?.platform === 'win32' && !getEnvValue('WT_SESSION')) {
return false;
}
return streamArg.isTTY !== false && getEnvValue('TERM') !== 'dumb';
}
function getStatusFromRenderedLine(
lineArg: string,
terminalArg: SmartcliTerminal
): TSmartcliTerminalTaskStatus | undefined {
if (lineArg.startsWith(terminalArg.getStatusSymbol('completed'))) {
return 'completed';
}
if (lineArg.startsWith(terminalArg.getStatusSymbol('failed'))) {
return 'failed';
}
if (lineArg.startsWith(terminalArg.getStatusSymbol('running'))) {
return 'running';
}
return undefined;
}
function hasEnvFlag(nameArg: string): boolean {
const value = getEnvValue(nameArg);
return Boolean(value && value !== '0' && value.toLowerCase() !== 'false');
}
function getEnvValue(nameArg: string): string | undefined {
return getProcessObject()?.env?.[nameArg];
}
function getProcessObject(): any {
const globalObject: any = globalThis as any;
return globalObject.process;
}
function normalizeLines(messageArg: string): string[] {
return String(messageArg)
.split(/\r?\n/)
.map((lineArg) => lineArg.trimEnd())
.filter((lineArg) => lineArg.length > 0);
}
function formatError(errorArg: unknown): string[] {
if (errorArg instanceof Error) {
return normalizeLines(errorArg.stack || errorArg.message || errorArg.name);
}
if (typeof errorArg === 'string') {
return normalizeLines(errorArg);
}
try {
const jsonString = JSON.stringify(errorArg);
return normalizeLines(jsonString === undefined ? String(errorArg) : jsonString);
} catch {
return [String(errorArg)];
}
}
function formatSmarttimeSeconds(millisecondsArg: number): string {
const seconds = Math.floor(millisecondsArg / 1000);
if (seconds === 0) {
return '0s';
}
return plugins.smarttime.getMilliSecondsAsHumanReadableString(
plugins.smarttime.units.seconds(seconds)
);
}
function truncateLine(lineArg: string, widthArg: number): string {
if (lineArg.length <= widthArg) {
return lineArg;
}
if (widthArg <= 3) {
return lineArg.slice(0, widthArg);
}
return `${lineArg.slice(0, widthArg - 3)}...`;
}
+76
View File
@@ -0,0 +1,76 @@
/**
* Return only the user arguments (excluding runtime executable and script path),
* across Node.js, Deno (run/compiled), and Bun.
*
* - Deno: uses Deno.args directly (already user-only in both run and compile).
* - Node/Bun: uses process.execPath's basename to decide if there is a script arg.
* If execPath basename is a known launcher (node/nodejs/bun/deno), skip 2; else skip 1.
*/
export function getUserArgs(argv?: string[]): string[] {
// If argv is explicitly provided, use it instead of Deno.args
// This handles test scenarios where process.argv is manually modified
const useProvidedArgv = argv !== undefined;
// Prefer Deno.args when available and no custom argv provided;
// it's the most reliable for Deno run and compiled.
// Deno.args is ALWAYS correct in Deno environments - it handles the internal bundle path automatically.
// deno-lint-ignore no-explicit-any
const g: any = typeof globalThis !== 'undefined' ? globalThis : {};
if (!useProvidedArgv && g.Deno && g.Deno.args && Array.isArray(g.Deno.args)) {
return g.Deno.args.slice();
}
const a = argv ?? (typeof process !== 'undefined' && Array.isArray(process.argv) ? process.argv : []);
if (!Array.isArray(a) || a.length === 0) return [];
// Determine execPath in Node/Bun (or compat shims)
let execPath = '';
if (typeof process !== 'undefined' && typeof process.execPath === 'string') {
execPath = process.execPath;
} else if (g.Deno && typeof g.Deno.execPath === 'function') {
// Fallback for unusual shims: try Deno.execPath() if present.
try {
execPath = g.Deno.execPath();
} catch {
/* ignore */
}
}
const base = basename(execPath).toLowerCase();
const knownLaunchers = new Set([
'node',
'node.exe',
'nodejs',
'nodejs.exe',
'bun',
'bun.exe',
'deno',
'deno.exe',
'tsx',
'tsx.exe',
'ts-node',
'ts-node.exe',
]);
// Always skip the executable (argv[0]).
let offset = Math.min(1, a.length);
// If the executable is a known runtime launcher, there's almost always a script path in argv[1].
// This handles Node, Bun, and "deno run" (but NOT "deno compile" which won't match 'deno').
if (knownLaunchers.has(base)) {
offset = Math.min(2, a.length);
}
// Note: we intentionally avoid path/URL heuristics on argv[1] so we don't
// accidentally drop the first user arg when it's a path-like value in compiled mode.
// When offset >= a.length, this correctly returns an empty array (no user args).
return a.slice(offset);
}
function basename(p: string): string {
if (!p) return '';
const parts = p.split(/[/\\]/);
return parts[parts.length - 1] || '';
}
+14 -9
View File
@@ -1,10 +1,15 @@
import "typings-global"; // @pushrocks scope
import * as smartlog from '@push.rocks/smartlog';
import * as lik from '@push.rocks/lik';
import * as path from 'node:path';
import * as smartparam from '@push.rocks/smartobject';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrx from '@push.rocks/smartrx';
import * as smarttime from '@push.rocks/smarttime';
export import yargs = require('yargs'); export { smartlog, lik, path, smartparam, smartpromise, smartrx, smarttime };
export import beautylog = require("beautylog");
export import cliff = require("cliff"); // thirdparty scope
export import inquirer = require("inquirer"); import yargsParser from 'yargs-parser';
export import lik = require("lik");
export import path = require("path"); export { yargsParser };
export import q = require("q");
export import smartparam = require("smartparam");
+14
View File
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitAny": true,
"esModuleInterop": true,
"verbatimModuleSyntax": true,
"types": ["node"]
},
"exclude": ["dist_*/**/*.d.ts"]
}