fix: bugfixes, build updates

This commit is contained in:
2023-12-11 17:31:32 +02:00
committed by Chen Asraf
parent 08192b9c27
commit ecd3a20463
10 changed files with 101 additions and 51 deletions

47
.github/workflows/docs.yml vendored Normal file
View File

@@ -0,0 +1,47 @@
name: Release
on:
push:
branches:
- master
permissions:
contents: read # for checkout
jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'
- name: Install PNPM
run: npm i -g pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run Tests
run: pnpm test
# - name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
# run: npm audit signatures
- name: Semantic Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
- name: Docs
run: pnpm doc
- name: Deploy on GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs

View File

@@ -1,2 +1,2 @@
templates/
scaffolds/
gen/
CHANGELOG.md

View File

@@ -1,32 +1,6 @@
# Change Log
# [2.0.0-pre.3](https://github.com/chenasraf/massarg/compare/v2.0.0-pre.2...v2.0.0-pre.3) (2023-12-02)
### Bug Fixes
* help output formatting/text ([fed6f60](https://github.com/chenasraf/massarg/commit/fed6f602c5e682a2b585fed68ed05d95ed2a0a7e))
* support help option with early quit ([742b597](https://github.com/chenasraf/massarg/commit/742b597f1ec9392f09b6af181d9c9cee7f680ba6))
# [2.0.0-pre.2](https://github.com/chenasraf/massarg/compare/v2.0.0-pre.1...v2.0.0-pre.2) (2023-12-01)
### Bug Fixes
* exported types fixes ([a0aa8ec](https://github.com/chenasraf/massarg/commit/a0aa8ecbb7c112033842f080caaea666a75b88b1))
### Features
* different opt output name (default camelCase) ([48b9602](https://github.com/chenasraf/massarg/commit/48b96022e42c6de1776485be1d829247f50cb6ad))
# [2.0.0-pre.1](https://github.com/chenasraf/massarg/compare/v1.0.6...v2.0.0-pre.1) (2023-11-24)
### Continuous Integration
* update release config ([4b111ab](https://github.com/chenasraf/massarg/commit/4b111ab6f69666a1f5540b08fc932bcf2ac03ee4))
# [2.0.0-pre.3](https://github.com/chenasraf/massarg/compare/v1.0.6...v2.0.0-pre.3) (2023-12-02)
### Features
@@ -38,6 +12,17 @@
* pass main instance to run fn ([29d6973](https://github.com/chenasraf/massarg/commit/29d6973eecbaa2d86e496387fe946a53c9662466))
* transform output name for options ([c414365](https://github.com/chenasraf/massarg/commit/c414365fbe602419a19f5522055e5b3c2fed761a))
* v2 poc ([7d5d302](https://github.com/chenasraf/massarg/commit/7d5d3025c6c65ee2d858aaf1beed50d847f423b4))
* different opt output name (default camelCase) ([48b9602](https://github.com/chenasraf/massarg/commit/48b96022e42c6de1776485be1d829247f50cb6ad))
### Bug Fixes
* exported types fixes ([a0aa8ec](https://github.com/chenasraf/massarg/commit/a0aa8ecbb7c112033842f080caaea666a75b88b1))
* help output formatting/text ([fed6f60](https://github.com/chenasraf/massarg/commit/fed6f602c5e682a2b585fed68ed05d95ed2a0a7e))
* support help option with early quit ([742b597](https://github.com/chenasraf/massarg/commit/742b597f1ec9392f09b6af181d9c9cee7f680ba6))
### Continuous Integration
* update release config ([4b111ab](https://github.com/chenasraf/massarg/commit/4b111ab6f69666a1f5540b08fc932bcf2ac03ee4))
### BREAKING CHANGES

View File

@@ -1,6 +1,6 @@
{
"name": "massarg",
"version": "2.0.0-pre.3",
"version": "0.0.0",
"description": "Flexible, powerful, and simple command/argument parser for CLI applications",
"keywords": [
"shell",

View File

@@ -9,8 +9,9 @@ module.exports = {
[
'@semantic-release/npm',
{
// only update the pkg version on root, don't publish
// only update the pkg version on doc, don't publish
npmPublish: false,
pkgRoot: 'doc',
},
],
[
@@ -36,7 +37,7 @@ module.exports = {
[
'@semantic-release/git',
{
assets: ['package.json', 'CHANGELOG.md'],
assets: ['CHANGELOG.md'],
},
],
//

View File

@@ -328,7 +328,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
message: 'Unknown option',
})
}
const res = option._parseDetails([arg, ...argv], { ...this.args })
const res = option._parseDetails([arg, ...argv], { ...this.args }, this.optionPrefixes)
this.args[res.key as keyof Args] = setOrPush<Args[keyof Args]>(
res.value,
this.args[res.key as keyof Args],

View File

@@ -163,7 +163,7 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
): MassargOption<T> {
switch (config.type) {
case 'number':
return new MassargNumber(config as OptionConfig<number>) as MassargOption<T>
return new MassargNumber(config as OptionConfig<number>) as MassargOption<any>
}
return new MassargOption(config as OptionConfig<T>)
}
@@ -172,11 +172,11 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
return this.outputName || toCamelCase(this.name)
}
_parseDetails(argv: string[], options: ArgsObject): ArgvValue<OptionType> {
_parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue<OptionType> {
// TODO: support --option=value
let input = ''
try {
if (!this._match(argv[0])) {
if (!this._match(argv[0], prefixes)) {
throw new ParseError({
path: [this.name],
code: 'invalid_option',
@@ -212,24 +212,26 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
_isOption(arg: string, prefixes: Prefixes): boolean {
return (
arg.startsWith(OPT_FULL_PREFIX) ||
arg.startsWith(OPT_SHORT_PREFIX) ||
arg.startsWith(NEGATE_SHORT_PREFIX)
arg.startsWith(prefixes.optionPrefix) ||
arg.startsWith(prefixes.aliasPrefix) ||
arg.startsWith(prefixes.negateFlagPrefix) ||
arg.startsWith(prefixes.negateAliasPrefix)
)
}
static findNameInArg(
arg: string,
prefixes: Prefixes,
): string {
static findNameInArg(arg: string, prefixes: Prefixes): string {
const { optionPrefix, aliasPrefix, negateFlagPrefix, negateAliasPrefix } = prefixes
if (arg.startsWith(optionPrefix)) {
// negate full prefix
if (arg.startsWith(`--${negateFlagPrefix}`)) {
return arg.slice(`--${negateFlagPrefix}`.length)
if (arg.startsWith(negateFlagPrefix)) {
return arg.slice(negateFlagPrefix.length)
}
return arg.slice(optionPrefix.length)
}
// negate full prefix
if (arg.startsWith(negateFlagPrefix)) {
return arg.slice(negateFlagPrefix.length)
}
// short prefix
if (arg.startsWith(aliasPrefix) || arg.startsWith(negateAliasPrefix)) {
return arg.slice(aliasPrefix.length)
@@ -266,9 +268,9 @@ export class MassargNumber extends MassargOption<number> {
})
}
_parseDetails(argv: string[], options: ArgsObject): ArgvValue<number> {
_parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue<number> {
try {
const { argv: _argv, value } = super._parseDetails(argv, options)
const { argv: _argv, value } = super._parseDetails(argv, options, prefixes)
if (isNaN(value)) {
throw new ParseError({
path: [this.name],
@@ -324,11 +326,20 @@ export class MassargFlag extends MassargOption<boolean> {
this.negatable = options.negatable ?? false
}
_parseDetails(argv: string[]): ArgvValue<boolean> {
_parseDetails(argv: string[], prefixes: Prefixes): ArgvValue<boolean> {
try {
const isNegation =
argv[0]?.startsWith(NEGATE_SHORT_PREFIX) || argv[0]?.startsWith(NEGATE_FULL_PREFIX)
if (!this._match(argv[0])) {
argv[0]?.startsWith(prefixes.negateAliasPrefix) ||
argv[0]?.startsWith(prefixes.negateFlagPrefix)
if (!this.negatable && isNegation) {
throw new ParseError({
path: [this.name],
code: 'invalid_option',
message: `Option ${this.name} cannot be negated`,
received: JSON.stringify(argv[0]),
})
}
if (!this._match(argv[0], prefixes)) {
throw new ParseError({
path: [this.name],
code: 'invalid_option',

View File

@@ -103,4 +103,10 @@ describe('flag', () => {
}),
).toThrow('Expected string, received number')
})
describe('negation', () => {
test('default', () => {
const command = massarg(opts).flag({ name: 'test2', description: 'test2', aliases: [] })
expect(command.getArgs(['--no-test2'])).toThrow('is not negatable')
})
})
})

View File

@@ -2,7 +2,7 @@
"$schema": "https://typedoc.org/schema.json",
"out": "docs",
"entryPoints": [
"src"
"src/**/*.ts"
],
"theme": "default",
"plugin": [