@gutenye/script.js 2.1.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.1.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
@@ -3,11 +3,12 @@
3
3
  import { castArray } from 'lodash-es'
4
4
  import fs from '../utils/fs'
5
5
  import {
6
+ AKE_FILENAMES,
7
+ STORAGE_DIR,
8
+ TEMPLATE_NAME,
6
9
  exitWithError,
7
10
  findAkeFiles,
8
11
  getRemoteDir,
9
- STORAGE_DIR,
10
- TEMPLATE_NAME,
11
12
  } from './shared'
12
13
 
13
14
  const NAME = 'akectl'
@@ -17,17 +18,17 @@ app.meta(NAME)
17
18
 
18
19
  app
19
20
  .cmd('init', 'Create ake file')
20
- .a('<place>', 'Place', ['local', 'remote'])
21
- .a(async (place: string) => {
21
+ .add('<place>', 'Place', ['local', 'remote'])
22
+ .add(async (place: string) => {
22
23
  const akeFiles = await findAkeFiles()
23
24
  if (akeFiles.length > 0) {
24
25
  exitWithError('Already have an ake file, cannot create a new one')
25
26
  }
26
- let target = 'ake'
27
+ let target = AKE_FILENAMES[0]
27
28
  if (place === 'remote') {
28
29
  const remoteDir = getRemoteDir()
29
30
  await fs.mkdirp(remoteDir)
30
- target = `${remoteDir}/ake`
31
+ target = `${remoteDir}/${AKE_FILENAMES[0]}`
31
32
  }
32
33
  const templateFile = `${STORAGE_DIR}/${TEMPLATE_NAME}`
33
34
  if (await fs.pathExists(templateFile)) {
@@ -39,7 +40,7 @@ app
39
40
  await openEditor(target)
40
41
  })
41
42
 
42
- app.cmd('edit', 'Edit ake file').a(async () => {
43
+ app.cmd('edit', 'Edit ake file').add(async () => {
43
44
  const akeFiles = await findAkeFiles()
44
45
  const akeFile = akeFiles[0]
45
46
  if (!akeFile) {
package/src/ake/shared.ts CHANGED
@@ -5,6 +5,7 @@ import fs from '../utils/fs'
5
5
  const HOME = os.homedir()
6
6
  const CWD = process.cwd()
7
7
 
8
+ export const AKE_FILENAMES = ['ake', 'ake.ts']
8
9
  export const STORAGE_DIR = `${HOME}/bin.src/ake`
9
10
  export const TEMPLATE_NAME = 'template'
10
11
 
@@ -14,10 +15,12 @@ export async function findAkeFiles(): Promise<string[]> {
14
15
  const dirsToCheck = [localDir, remoteDir]
15
16
 
16
17
  const akeFiles = await Promise.all(
17
- dirsToCheck.map(async (dir) => {
18
- const akeFile = `${dir}/ake`
19
- return (await fs.pathExists(akeFile)) ? akeFile : null
20
- }),
18
+ dirsToCheck.flatMap((dir) =>
19
+ AKE_FILENAMES.map(async (name) => {
20
+ const akeFile = `${dir}/${name}`
21
+ return (await fs.pathExists(akeFile)) ? akeFile : null
22
+ }),
23
+ ),
21
24
  )
22
25
 
23
26
  return akeFiles.filter(Boolean) as string[]
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 { 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
 
@@ -126,7 +126,7 @@ export async function installCompletion(
126
126
  try {
127
127
  if (!command.name && options.scriptPath) {
128
128
  const basename = path.basename(options.scriptPath)
129
- if (basename === 'ake') {
129
+ if (AKE_FILENAMES.includes(basename)) {
130
130
  command.name = getCompletionName()
131
131
  }
132
132
  }
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']