Compare commits

...

24 Commits

Author SHA1 Message Date
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
18 changed files with 14735 additions and 4292 deletions
+20 -20
View File
@@ -1,32 +1,32 @@
{ {
"npmci": { "@git.zone/cli": {
"npmGlobalTools": [],
"npmAccesslevel": "public"
},
"gitzone": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartcli", "gitrepo": "smartcli",
"description": "A library for easily creating observable CLI tasks with support for commands, arguments, and options.", "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", "npmPackagename": "@push.rocks/smartcli",
"license": "MIT", "license": "MIT",
"keywords": [ "projectDomain": "push.rocks"
"CLI", },
"command line", "release": {
"observable", "targets": {
"tasks", "npm": {
"commands", "registries": [
"arguments", "https://verdaccio.lossless.digital",
"options", "https://registry.npmjs.org"
"typescript", ],
"node.js", "accessLevel": "public"
"development tool" }
]
} }
}, },
"tsdoc": { "schemaVersion": 2
"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" },
"@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": []
} }
} }
-26
View File
@@ -1,26 +0,0 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}
+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
+424
View File
@@ -0,0 +1,424 @@
# Changelog
## Pending
## 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
+6244
View File
File diff suppressed because it is too large Load Diff
+25 -21
View File
@@ -1,28 +1,30 @@
{ {
"name": "@push.rocks/smartcli", "name": "@push.rocks/smartcli",
"private": false, "private": false,
"version": "4.0.10", "version": "4.1.0",
"description": "A library for easily creating observable CLI tasks with support for commands, arguments, and options.", "description": "A library that simplifies building reactive command-line applications using observables, with robust support for commands, arguments, options, aliases, and asynchronous operation management.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "(tstest test/ --web)", "test": "(tstest test/ --verbose)",
"build": "(tsbuild --web --allowimplicitany)", "build": "tsbuild --web",
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitlab.com/pushrocks/smartcli.git" "url": "https://code.foss.global/push.rocks/smartcli.git"
}, },
"keywords": [ "keywords": [
"CLI", "CLI",
"command line", "command line",
"observable", "observable",
"tasks", "reactive",
"asynchronous",
"commands", "commands",
"arguments", "arguments",
"options", "options",
"alias",
"typescript", "typescript",
"node.js", "node.js",
"development tool" "development tool"
@@ -30,23 +32,23 @@
"author": "Lossless GmbH <office@lossless.com> (https://lossless.com)", "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": {
"@push.rocks/lik": "^6.0.3", "@push.rocks/lik": "^6.4.1",
"@push.rocks/smartlog": "^3.0.1", "@push.rocks/smartlog": "^3.2.2",
"@push.rocks/smartparam": "^1.1.10", "@push.rocks/smartobject": "^1.0.12",
"@push.rocks/smartpromise": "^4.0.3", "@push.rocks/smartpromise": "^4.2.4",
"@push.rocks/smartrx": "^3.0.3", "@push.rocks/smartrx": "^3.0.10",
"yargs-parser": "21.1.1" "yargs-parser": "22.0.0"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.66", "@git.zone/tsbuild": "^4.4.1",
"@git.zone/tsrun": "^1.2.42", "@git.zone/tsrun": "^2.0.4",
"@git.zone/tstest": "^1.0.74", "@git.zone/tstest": "^3.6.6",
"@push.rocks/tapbundle": "^5.0.4", "@types/node": "^25.7.0",
"@types/node": "^20.4.1" "@types/yargs-parser": "^21.0.3"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",
@@ -57,10 +59,12 @@
"dist_ts_web/**/*", "dist_ts_web/**/*",
"assets/**/*", "assets/**/*",
"cli.js", "cli.js",
"npmextra.json", ".smartconfig.json",
"LICENSE",
"readme.md" "readme.md"
], ],
"browserslist": [ "browserslist": [
"last 1 chrome versions" "last 1 chrome versions"
] ],
"packageManager": "pnpm@10.28.2"
} }
+6980 -4072
View File
File diff suppressed because it is too large Load Diff
+42 -1
View File
@@ -1 +1,42 @@
No specific hints. ## 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)
+381 -67
View File
@@ -1,133 +1,447 @@
# @push.rocks/smartcli # @push.rocks/smartcli
easy observable cli tasks 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 ## Install
To install `@push.rocks/smartcli`, use the following command in your terminal:
```sh ```sh
npm install @push.rocks/smartcli --save pnpm add @push.rocks/smartcli
``` ```
This will add `@push.rocks/smartcli` as a dependency to your project's `package.json` file and download it into the `node_modules` folder. ## Why Use Smartcli
## Usage - 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.
The `@push.rocks/smartcli` module is designed to simplify the creation of command-line interfaces (CLIs) by providing an easy-to-use API for managing CLI commands and options. It combines observables with the parsing power of [yargs-parser](https://www.npmjs.com/package/yargs-parser) to offer a dynamic and flexible way to handle various CLI tasks. ## Quick Start
### Getting Started Create a CLI entrypoint, for example `cli.ts`:
First, ensure you have TypeScript and the necessary types for node installed in your project. If not, you can add them by running: ```ts
#!/usr/bin/env node
```sh
npm install typescript @types/node --save-dev
```
Then, import the `Smartcli` class from the `@push.rocks/smartcli` package.
```typescript
import { Smartcli } from '@push.rocks/smartcli'; 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();
``` ```
### Creating an Instance When exposed as a package `bin`, the CLI behaves like this:
Create an instance of `Smartcli`. This instance will be used to define and manage your CLI commands. ```sh
demo greet --name Ada
# Hello, Ada!
```typescript demo --version
const mySmartcli = new Smartcli(); # 1.0.0
demo help
# prints the configured help text
``` ```
### Defining Commands ## Terminal Task Rendering
With `Smartcli`, you can define commands that your CLI tool can execute. Here's how you can add a new command: 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. In CI, pipes, Docker logs, or `TERM=dumb`, the same calls become append-only log lines.
```typescript ```ts
mySmartcli.addCommand('install').subscribe((argv) => { import { SmartcliTerminal } from '@push.rocks/smartcli';
console.log('Install command was called with arguments:', argv);
const terminal = new SmartcliTerminal();
const buildTask = terminal.createTask({
job: 'Build package',
rows: 3,
});
buildTask.update('Installing dependencies');
buildTask.log('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 a permanent `[ok]` line. Failed tasks collapse into a permanent `[err]` line with error details. If an error should remain visible inside the live task area, use `attachError(error, { keepOpen: true })`.
## 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
`,
}); });
``` ```
In this example, when the user types `install` after your CLI tool's name in the terminal, the provided function will execute, printing the parsed arguments to the console. ```sh
tool help
### Handling Options
Options can be accessed using the `getOption` method. If you have an option named `--config` or `-c`, you can access its value like this:
```typescript
const configValue = mySmartcli.getOption('config');
console.log('Config value:', configValue);
``` ```
### Default Task (Standard Command) ## Programmatic Dispatch
If you want to perform a task when no specific command is provided, you can use the `standardCommand` method: Use `triggerCommand()` when you want to invoke a registered command yourself, for example in orchestration code or focused tests:
```typescript ```ts
mySmartcli.standardCommand().subscribe((argv) => { import { Smartcli } from '@push.rocks/smartcli';
console.log('No specific command provided. Running default task with arguments:', argv);
const cli = new Smartcli();
cli.addCommand('build').subscribe((argv) => {
console.log(`Building ${argv.target}`);
});
cli.triggerCommand('build', {
_: ['build'],
target: 'docs',
}); });
``` ```
### Version and Help `triggerCommand()` throws if the command has not been registered.
To add a version option that prints the version of your CLI tool, use `addVersion`: ## Testing CLIs
```typescript `startParse()` accepts an optional `argv` override. Pass a full runtime-style argument array for deterministic tests:
mySmartcli.addVersion('1.0.0');
```
For a help option that displays helpful information about your CLI commands and options, use `addHelp`: ```ts
import { Smartcli } from '@push.rocks/smartcli';
```typescript const cli = new Smartcli();
mySmartcli.addHelp({ const parsedPromise = cli.parseCompleted.promise;
helpText: 'Here are the commands you can use...'
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
``` ```
### Parsing and Execution Create a fresh `Smartcli` instance per parse. `parseCompleted` is a single deferred value on the instance and resolves once.
After defining all your commands and options, call `startParse` to begin parsing the command line input and execute the corresponding actions: ## Cross-Runtime Argument Handling
```typescript smartcli uses `getUserArgs()` internally to remove runtime-specific executable and script entries before parsing:
mySmartcli.startParse();
| 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']
``` ```
### Advanced Usage: Command Aliases 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:
You can also define aliases for your commands, allowing users to use alternate names for them: ```ts
import { Smartcli } from '@push.rocks/smartcli';
```typescript const cli = new Smartcli();
mySmartcli.addCommandAlias('install', 'i');
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();
``` ```
With this setup, both `install` and `i` will trigger the same command action. ## API Reference
### Observables and Async Operations | 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. |
Since commands in `smartcli` are handled using observables, you can easily integrate asynchronous operations within your command actions. This makes it perfect for CLI tasks that involve file operations, network requests, or any other async tasks. ## Practical Example
### Conclusion This example wires a tiny task runner with commands, options, help, version output, and a standard fallback:
`@push.rocks/smartcli` offers a robust and intuitive way to build CLI tools with TypeScript, leveraging the reactive programming paradigm. By following the examples provided in this guide, you'll be able to create a feature-rich command-line application tailored to your specific needs. ```ts
#!/usr/bin/env node
import { Smartcli } from '@push.rocks/smartcli';
Remember, this is just the start. The true power of `smartcli` lies in its flexibility and the vast ecosystem of RxJS. Dive into the RxJS documentation to explore more ways to handle data streams and asynchronous events in your CLI app. 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 ## License and Legal Information
This 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. 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. **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 ### 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 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. 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 ### Company Information
Task Venture Capital GmbH Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. 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. 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.
+118
View File
@@ -0,0 +1,118 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartrx from '@push.rocks/smartrx';
import * as smartcli from '../ts/index.js';
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 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();
});
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 });
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('[build package] running tsbuild');
expect(output).toInclude('[ok] build package - 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 });
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('[run] install dependencies');
expect(stream.toString()).toInclude('\u001B[2K');
task.complete('installed');
expect(stream.toString()).toInclude('[ok] install dependencies - installed');
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 });
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('[err] 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 });
const task = terminal.createTask({ job: 'publish package' });
task.attachError('registry rejected package');
const output = stream.toString();
expect(output).toInclude('[err] publish package - registry rejected package');
expect(terminal.getTasks()).toHaveLength(0);
});
export default tap.start();
-42
View File
@@ -1,42 +0,0 @@
import { tap, expect } from '@push.rocks/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();
});
console.log(process.argv);
process.argv.splice(2, 0, 'awesome');
console.log(process.argv);
smartCliTestObject.startParse();
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();
});
tap.start();
+3 -3
View File
@@ -1,8 +1,8 @@
/** /**
* autocreated commitinfo by @pushrocks/commitinfo * autocreated commitinfo by @push.rocks/commitinfo
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartcli', name: '@push.rocks/smartcli',
version: '4.0.10', version: '4.1.0',
description: 'A library for easily creating observable CLI tasks with support for commands, arguments, and options.' description: 'A library that simplifies building reactive command-line applications using observables, with robust support for commands, arguments, options, aliases, and asynchronous operation management.'
} }
+8
View File
@@ -1 +1,9 @@
export { Smartcli } from './smartcli.classes.smartcli.js'; export { Smartcli } from './smartcli.classes.smartcli.js';
export { SmartcliTerminal, SmartcliTerminalTask } from './smartcli.classes.terminal.js';
export type {
ISmartcliTerminalAttachErrorOptions,
ISmartcliTerminalOptions,
ISmartcliTerminalTaskOptions,
ISmartcliWritable,
TSmartcliTerminalTaskStatus,
} from './smartcli.classes.terminal.js';
+12 -22
View File
@@ -1,4 +1,5 @@
import * as plugins from './smartcli.plugins.js'; import * as plugins from './smartcli.plugins.js';
import { getUserArgs } from './smartcli.helpers.js';
// interfaces // interfaces
export interface ICommandObservableObject { export interface ICommandObservableObject {
@@ -17,7 +18,7 @@ export class Smartcli {
*/ */
public parseCompleted = plugins.smartpromise.defer<any>(); public parseCompleted = plugins.smartpromise.defer<any>();
public version: string; public version?: string;
/** /**
* map of all Trigger/Observable objects to keep track * map of all Trigger/Observable objects to keep track
@@ -64,6 +65,9 @@ export class Smartcli {
*/ */
public triggerCommand(commandNameArg: string, argvObject: any) { public triggerCommand(commandNameArg: string, argvObject: any) {
const triggerSubject = this.getCommandSubject(commandNameArg); const triggerSubject = this.getCommandSubject(commandNameArg);
if (!triggerSubject) {
throw new Error(`No smartcli command registered for ${commandNameArg}`);
}
triggerSubject.next(argvObject); triggerSubject.next(argvObject);
return triggerSubject; return triggerSubject;
} }
@@ -91,7 +95,8 @@ export class Smartcli {
* getOption * getOption
*/ */
public getOption(optionNameArg: string) { public getOption(optionNameArg: string) {
const parsedYargs = plugins.yargsParser(process.argv); const userArgs = getUserArgs();
const parsedYargs = plugins.yargsParser(userArgs);
return parsedYargs[optionNameArg]; return parsedYargs[optionNameArg];
} }
@@ -121,26 +126,12 @@ export class Smartcli {
/** /**
* start the process of evaluating commands * start the process of evaluating commands
* @param testArgv - Optional argv override for testing (bypasses automatic runtime detection)
*/ */
public startParse(): void { public startParse(testArgv?: string[]): void {
const parsedYArgs = plugins.yargsParser(process.argv); // Get user arguments, properly handling Node.js, Deno (run/compiled), and Bun
const userArgs = testArgv ? getUserArgs(testArgv) : getUserArgs();
// lets handle commands const parsedYArgs = plugins.yargsParser(userArgs);
let counter = 0;
let foundCommand = false;
parsedYArgs._ = parsedYArgs._.filter((commandPartArg) => {
counter++;
if (typeof commandPartArg === 'number') {
return true;
}
if (counter <= 2 && !foundCommand) {
const isPath = commandPartArg.startsWith('/');
foundCommand = !isPath;
return foundCommand;
} else {
return true;
}
});
const wantedCommand = parsedYArgs._[0]; const wantedCommand = parsedYArgs._[0];
// lets handle some standards // lets handle some standards
@@ -148,7 +139,6 @@ export class Smartcli {
console.log(this.version || 'unknown version'); console.log(this.version || 'unknown version');
return; return;
} }
console.log(`Wanted command: ${wantedCommand}`);
for (const command of this.commandObservableMap.getArray()) { for (const command of this.commandObservableMap.getArray()) {
if (!wantedCommand) { if (!wantedCommand) {
const standardCommand = this.commandObservableMap.findSync((commandArg) => { const standardCommand = this.commandObservableMap.findSync((commandArg) => {
+384
View File
@@ -0,0 +1,384 @@
export type TSmartcliTerminalTaskStatus = 'running' | 'completed' | 'failed';
export interface ISmartcliWritable {
isTTY?: boolean;
columns?: number;
write(chunk: string): void | boolean;
}
export interface ISmartcliTerminalOptions {
stream?: ISmartcliWritable;
interactive?: boolean;
colors?: boolean;
}
export interface ISmartcliTerminalTaskOptions {
job: string;
rows?: number;
logLimit?: number;
}
export interface ISmartcliTerminalAttachErrorOptions {
keepOpen?: boolean;
}
const ansiCodes = {
reset: '\u001B[0m',
red: '\u001B[31m',
green: '\u001B[32m',
cyan: '\u001B[36m',
gray: '\u001B[90m',
};
/**
* 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 tasks: SmartcliTerminalTask[] = [];
private renderedLineCount = 0;
constructor(optionsArg: ISmartcliTerminalOptions = {}) {
this.stream = optionsArg.stream || getDefaultStream();
this.interactive = getInteractiveState(this.stream, optionsArg.interactive);
this.colors = optionsArg.colors ?? (this.interactive && !hasEnvFlag('NO_COLOR'));
}
public createTask(optionsArg: ISmartcliTerminalTaskOptions): SmartcliTerminalTask {
const task = new SmartcliTerminalTask(this, optionsArg);
this.tasks.push(task);
if (this.interactive) {
this.render();
} else {
this.writePermanentLine(`[start] ${task.job}`);
}
return task;
}
public createProcess(optionsArg: ISmartcliTerminalTaskOptions): SmartcliTerminalTask {
return this.createTask(optionsArg);
}
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.render();
} else if (messageArg) {
this.writePermanentLine(`[${taskArg.job}] ${messageArg}`);
}
}
/** @internal */
public completeTask(taskArg: SmartcliTerminalTask, messageArg?: string): void {
const message = messageArg || taskArg.getLastLogLine();
const summary = `[ok] ${taskArg.job}${message ? ` - ${message}` : ''}`;
this.finalizeTask(taskArg, [summary]);
}
/** @internal */
public failTask(taskArg: SmartcliTerminalTask, errorLinesArg: string[]): void {
const summary = `[err] ${taskArg.job}${errorLinesArg[0] ? ` - ${errorLinesArg[0]}` : ''}`;
const detailLines = errorLinesArg.slice(1).map((lineArg) => ` ${lineArg}`);
this.finalizeTask(taskArg, [summary, ...detailLines]);
}
public clear(): void {
if (this.interactive) {
this.clearRenderedBlock();
}
this.tasks = [];
}
private finalizeTask(taskArg: SmartcliTerminalTask, linesArg: string[]): void {
this.tasks = this.tasks.filter((task) => task !== taskArg);
if (this.interactive) {
this.clearRenderedBlock();
this.writePermanentLines(linesArg);
this.render();
} else {
this.writePermanentLines(linesArg);
}
}
private render(): void {
if (!this.interactive) {
return;
}
const width = this.getLineWidth();
const lines = this.tasks.flatMap((taskArg) => {
return taskArg.renderPlainRows(width).map((lineArg, indexArg) => {
const truncatedLine = truncateLine(lineArg, width);
return indexArg === 0 ? this.colorizeStatusLabel(truncatedLine) : truncatedLine;
});
});
if (this.renderedLineCount > 0) {
this.stream.write(`\u001B[${this.renderedLineCount}F`);
}
const lineCount = Math.max(this.renderedLineCount, lines.length);
for (let index = 0; index < lineCount; index++) {
this.stream.write('\u001B[2K\r');
if (index < lines.length) {
this.stream.write(lines[index]);
}
this.stream.write('\n');
}
this.renderedLineCount = lines.length;
}
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;
}
private writePermanentLines(linesArg: string[]): void {
for (const line of linesArg) {
this.writePermanentLine(line);
}
}
private writePermanentLine(lineArg: string): void {
const line = this.colors ? this.colorizeStatusLabel(lineArg) : lineArg;
this.stream.write(`${line}\n`);
}
private colorizeStatusLabel(lineArg: string): string {
if (!this.colors) {
return lineArg;
}
if (lineArg.startsWith('[ok]')) {
return `${ansiCodes.green}[ok]${ansiCodes.reset}${lineArg.slice(4)}`;
}
if (lineArg.startsWith('[err]')) {
return `${ansiCodes.red}[err]${ansiCodes.reset}${lineArg.slice(5)}`;
}
if (lineArg.startsWith('[run]')) {
return `${ansiCodes.cyan}[run]${ansiCodes.reset}${lineArg.slice(5)}`;
}
if (lineArg.startsWith('[start]')) {
return `${ansiCodes.gray}[start]${ansiCodes.reset}${lineArg.slice(7)}`;
}
return lineArg;
}
private getLineWidth(): number {
return Math.max(20, (this.stream.columns || 80) - 1);
}
}
export class SmartcliTerminalTask {
public readonly job: string;
public readonly rows: number;
public status: TSmartcliTerminalTaskStatus = 'running';
private terminal: SmartcliTerminal;
private logLimit: number;
private logLines: string[] = [];
private errorLines: string[] = [];
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));
}
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 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 getLastLogLine(): string | undefined {
return this.logLines[this.logLines.length - 1];
}
public getLogLines(): string[] {
return [...this.logLines];
}
public getErrorLines(): string[] {
return [...this.errorLines];
}
/** @internal */
public renderPlainRows(widthArg: number): string[] {
const statusLabel = this.status === 'failed' ? '[err]' : '[run]';
const lines: string[] = [];
const detailLines = this.errorLines.length > 0 ? this.errorLines : this.logLines;
if (this.rows === 1) {
const lastDetail = detailLines[detailLines.length - 1];
lines.push(`${statusLabel} ${this.job}${lastDetail ? ` - ${lastDetail}` : ''}`);
return lines;
}
lines.push(`${statusLabel} ${this.job}`);
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));
}
}
function getDefaultStream(): ISmartcliWritable {
const globalObject: any = globalThis as any;
if (globalObject.process?.stdout?.write) {
return globalObject.process.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 hasEnvFlag(nameArg: string): boolean {
const value = getEnvValue(nameArg);
return Boolean(value && value !== '0' && value.toLowerCase() !== 'false');
}
function getEnvValue(nameArg: string): string | undefined {
const globalObject: any = globalThis as any;
return globalObject.process?.env?.[nameArg];
}
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 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] || '';
}
+2 -2
View File
@@ -1,8 +1,8 @@
// @pushrocks scope // @pushrocks scope
import * as smartlog from '@push.rocks/smartlog'; import * as smartlog from '@push.rocks/smartlog';
import * as lik from '@push.rocks/lik'; import * as lik from '@push.rocks/lik';
import * as path from 'path'; import * as path from 'node:path';
import * as smartparam from '@push.rocks/smartparam'; import * as smartparam from '@push.rocks/smartobject';
import * as smartpromise from '@push.rocks/smartpromise'; import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrx from '@push.rocks/smartrx'; import * as smartrx from '@push.rocks/smartrx';
+4 -4
View File
@@ -5,10 +5,10 @@
"target": "ES2022", "target": "ES2022",
"module": "NodeNext", "module": "NodeNext",
"moduleResolution": "NodeNext", "moduleResolution": "NodeNext",
"noImplicitAny": true,
"esModuleInterop": true, "esModuleInterop": true,
"verbatimModuleSyntax": true "verbatimModuleSyntax": true,
"types": ["node"]
}, },
"exclude": [ "exclude": ["dist_*/**/*.d.ts"]
"dist_*/**/*.d.ts"
]
} }