@gutenye/script.js 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gutenye/script.js",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Write shell scripts in JavaScript",
5
5
  "keywords": [
6
6
  "shell",
package/src/Command.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Argument } from './Argument'
2
- import { installCompletion } from './completion'
3
2
  import { Option } from './Option'
3
+ import { installCompletion } from './completion'
4
4
  import { parseArgv } from './parseArgv'
5
5
 
6
6
  export class Command {
@@ -105,8 +105,6 @@ export class Command {
105
105
  await this.#invokeAction(command, positionals, options, context)
106
106
  }
107
107
 
108
- a = this.add.bind(this)
109
-
110
108
  add(...args: any[]) {
111
109
  if (typeof args[0] === 'function') {
112
110
  this.action = args[0]
@@ -116,7 +114,7 @@ export class Command {
116
114
  if (typeof description !== 'string') {
117
115
  if (completionOrDefault) {
118
116
  throw new Error(
119
- 'Invalid third argument, should be a(name, description, completion) format',
117
+ 'Invalid third argument, should be add(name, description, completion) format',
120
118
  )
121
119
  }
122
120
  completionOrDefault = description
package/src/Option.ts CHANGED
@@ -10,6 +10,7 @@ export class Option {
10
10
  attributeName: string
11
11
  completion: string[] | (() => string[])
12
12
  defaultValue: any
13
+ inlineDefaultValue?: string
13
14
 
14
15
  constructor(
15
16
  rawFlags: string,
@@ -38,6 +39,11 @@ export class Option {
38
39
  this.variadic = rawFlags.includes('...')
39
40
  this.negate = this.long?.startsWith('--no-') ?? false
40
41
 
42
+ const inlineDefaultMatch = rawFlags.match(/\[[\w.]+=(.*?)\]/)
43
+ if (inlineDefaultMatch) {
44
+ this.inlineDefaultValue = inlineDefaultMatch[1]
45
+ }
46
+
41
47
  if (this.long) {
42
48
  let key = this.long.replace(/^--/, '')
43
49
  if (this.negate) key = key.replace(/^no-/, '')
@@ -53,10 +59,10 @@ export class Option {
53
59
  typeof defaultValueOrCompletion === 'function'
54
60
  ) {
55
61
  this.completion = defaultValueOrCompletion
56
- this.defaultValue = undefined
62
+ this.defaultValue = this.inlineDefaultValue
57
63
  } else {
58
64
  this.completion = []
59
- this.defaultValue = defaultValueOrCompletion
65
+ this.defaultValue = defaultValueOrCompletion ?? this.inlineDefaultValue
60
66
  }
61
67
  }
62
68
 
package/src/ake/akectl.ts CHANGED
@@ -4,11 +4,11 @@ import { castArray } from 'lodash-es'
4
4
  import fs from '../utils/fs'
5
5
  import {
6
6
  AKE_FILENAMES,
7
+ STORAGE_DIR,
8
+ TEMPLATE_NAME,
7
9
  exitWithError,
8
10
  findAkeFiles,
9
11
  getRemoteDir,
10
- STORAGE_DIR,
11
- TEMPLATE_NAME,
12
12
  } from './shared'
13
13
 
14
14
  const NAME = 'akectl'
@@ -18,8 +18,8 @@ app.meta(NAME)
18
18
 
19
19
  app
20
20
  .cmd('init', 'Create ake file')
21
- .a('<place>', 'Place', ['local', 'remote'])
22
- .a(async (place: string) => {
21
+ .add('<place>', 'Place', ['local', 'remote'])
22
+ .add(async (place: string) => {
23
23
  const akeFiles = await findAkeFiles()
24
24
  if (akeFiles.length > 0) {
25
25
  exitWithError('Already have an ake file, cannot create a new one')
@@ -40,7 +40,7 @@ app
40
40
  await openEditor(target)
41
41
  })
42
42
 
43
- app.cmd('edit', 'Edit ake file').a(async () => {
43
+ app.cmd('edit', 'Edit ake file').add(async () => {
44
44
  const akeFiles = await findAkeFiles()
45
45
  const akeFile = akeFiles[0]
46
46
  if (!akeFile) {
package/src/completion.ts CHANGED
@@ -2,8 +2,8 @@ import nodeFs from 'node:fs'
2
2
  import os from 'node:os'
3
3
  import path from 'node:path'
4
4
  import * as yaml from 'yaml'
5
- import { AKE_FILENAMES, getCompletionName } from './ake/shared'
6
5
  import type { Command } from './Command'
6
+ import { AKE_FILENAMES, getCompletionName } from './ake/shared'
7
7
 
8
8
  export type CompletionValue = string[] | (() => string[])
9
9
 
package/src/script.ts CHANGED
@@ -3,7 +3,6 @@
3
3
  import path from 'node:path'
4
4
  import { app } from './Command'
5
5
  import { $ } from './spawn'
6
-
7
6
  ;(globalThis as any).$ = $
8
7
  ;(globalThis as any).app = app
9
8
 
package/src/test.ts CHANGED
@@ -5,7 +5,7 @@ import { $ } from './spawn'
5
5
 
6
6
  app
7
7
  .cmd('cmd1 | c1', 'Command 1')
8
- .a('<platform>', 'Platform', [
8
+ .add('<platform>', 'Platform', [
9
9
  'ios',
10
10
  'android',
11
11
  'windows',
@@ -13,9 +13,9 @@ app
13
13
  'linux',
14
14
  'unknown',
15
15
  ])
16
- .a('<name>', 'Name')
17
- .a('-l | --long')
18
- .a(async (platform: string, options: any, ctx: any) => {
16
+ .add('<name>', 'Name')
17
+ .add('-l | --long')
18
+ .add(async (platform: string, options: any, ctx: any) => {
19
19
  console.log(platform, options, ctx)
20
20
  const name = 'Mike Smith'
21
21
  const args = ['arg 1', 'arg 2']