@gutenye/script.js 1.1.0 → 2.1.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/README.md +58 -44
- package/package.json +27 -17
- package/src/Argument.ts +40 -0
- package/src/Command.ts +302 -0
- package/src/Option.ts +70 -0
- package/src/ake/README.md +51 -0
- package/src/ake/ake.ts +40 -0
- package/src/ake/akectl.ts +60 -0
- package/src/ake/completions/ake.fish +8 -0
- package/src/ake/shared.ts +45 -0
- package/src/completion.ts +152 -0
- package/src/globals.d.ts +7 -0
- package/src/index.ts +2 -0
- package/src/parseArgv.ts +88 -0
- package/src/script.ts +13 -51
- package/src/spawn.ts +175 -17
- package/src/test.ts +25 -0
- package/src/utils/fs.ts +2 -2
- package/src/utils/index.ts +1 -1
- package/src/app.ts +0 -25
- package/src/command.ts +0 -51
- package/src/csv.ts +0 -12
- package/src/exit.ts +0 -10
- package/src/fileSystem.ts +0 -17
- package/src/mixins.ts +0 -19
- package/src/types/global.d.ts +0 -8
- package/src/ui/index.ts +0 -1
- package/src/ui/table.ts +0 -35
- package/src/yaml.ts +0 -34
- /package/src/utils/{path.ts → nodePath.ts} +0 -0
package/src/utils/fs.ts
CHANGED
|
@@ -75,7 +75,7 @@ async function inputJson(input: ReadFileArgs[0], options?: ReadFileArgs[1]) {
|
|
|
75
75
|
return
|
|
76
76
|
}
|
|
77
77
|
try {
|
|
78
|
-
return JSON.parse(text)
|
|
78
|
+
return JSON.parse(text as string)
|
|
79
79
|
} catch (error) {
|
|
80
80
|
if (error instanceof Error) {
|
|
81
81
|
throw new Error(`[inputJson] ${error.message} from '${input}'`)
|
|
@@ -90,7 +90,7 @@ async function inputJson(input: ReadFileArgs[0], options?: ReadFileArgs[1]) {
|
|
|
90
90
|
async function readJson(input: ReadFileArgs[0], options?: ReadFileArgs[1]) {
|
|
91
91
|
const text = await fs.readFile(cleanPath(input), options)
|
|
92
92
|
try {
|
|
93
|
-
return JSON.parse(text)
|
|
93
|
+
return JSON.parse(text as string)
|
|
94
94
|
} catch (error) {
|
|
95
95
|
if (error instanceof Error) {
|
|
96
96
|
throw new Error(`[readJson] ${error.message} from '${input}'`)
|
package/src/utils/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default as fs } from './fs'
|
|
2
|
-
export { default as
|
|
2
|
+
export { default as nodePath } from './nodePath'
|
package/src/app.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Argument, Option, program } from './command'
|
|
2
|
-
|
|
3
|
-
const [bunPath, scriptJsPath, scriptPath, ...args] = process.argv
|
|
4
|
-
|
|
5
|
-
export const app = program
|
|
6
|
-
|
|
7
|
-
app.Argument = Argument
|
|
8
|
-
app.Option = Option
|
|
9
|
-
|
|
10
|
-
export async function start() {
|
|
11
|
-
if (!scriptPath) {
|
|
12
|
-
echo('Error: missing script path, usage: gutenye-script.js <script>')
|
|
13
|
-
process.exit(1)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
await import(scriptPath)
|
|
17
|
-
|
|
18
|
-
const promise = program.installCompletion()
|
|
19
|
-
if (args.length === 0) {
|
|
20
|
-
// wait for file write operation completed before app print hellp and exit in parse()
|
|
21
|
-
await promise
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
program.parse(args, { from: 'user' })
|
|
25
|
-
}
|
package/src/command.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Command as BaseCommand } from '@gutenye/commander-completion-carapace'
|
|
2
|
-
|
|
3
|
-
export * from '@gutenye/commander-completion-carapace'
|
|
4
|
-
|
|
5
|
-
export class Command extends BaseCommand {
|
|
6
|
-
createCommand(name) {
|
|
7
|
-
return new Command(name)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async invoke(text, ...args) {
|
|
11
|
-
if (args.length === 0) {
|
|
12
|
-
const args = text.split(/ +/)
|
|
13
|
-
return program.parseAsync(args, { from: 'user' })
|
|
14
|
-
} else {
|
|
15
|
-
return program._findCommand(text)._actionHandler(...args)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
actionLin(fn) {
|
|
20
|
-
this._actionLinux = fn
|
|
21
|
-
this._actionOS()
|
|
22
|
-
return this
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
actionMac(fn) {
|
|
26
|
-
this._actionMac = fn
|
|
27
|
-
this._actionOS()
|
|
28
|
-
return this
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
actionWin(fn) {
|
|
32
|
-
this._actionWin = fn
|
|
33
|
-
this._actionOS()
|
|
34
|
-
return this
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
_actionOS(fn) {
|
|
38
|
-
this.action((...args) => {
|
|
39
|
-
switch (process.platform) {
|
|
40
|
-
case 'linux':
|
|
41
|
-
return this._actionLinux?.(...args)
|
|
42
|
-
case 'darwin':
|
|
43
|
-
return this._actionMac?.(...args)
|
|
44
|
-
case 'win32':
|
|
45
|
-
return this._actionWin?.(...args)
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const program = new Command()
|
package/src/csv.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as csv from 'csv-parse/sync'
|
|
2
|
-
|
|
3
|
-
// csv.parse(text, { delimiter: ','*, columns: true* })
|
|
4
|
-
// columns: true -> [{column: value}]
|
|
5
|
-
// columns: false -> [[column], [value]]
|
|
6
|
-
export function parse(text, options = {}) {
|
|
7
|
-
const newOptions = {
|
|
8
|
-
columns: true,
|
|
9
|
-
...options,
|
|
10
|
-
}
|
|
11
|
-
return csv.parse(text, newOptions)
|
|
12
|
-
}
|
package/src/exit.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// exitWithError(message, [more])
|
|
2
|
-
export function exitWithError(message, more) {
|
|
3
|
-
// for comletion <Tab> display error message: 1) no prefix: '\n, it uses first line 2) use console.error
|
|
4
|
-
let text = colors.red.bold(`Error: ${message}`)
|
|
5
|
-
if (more) {
|
|
6
|
-
text += `\n${more}`
|
|
7
|
-
}
|
|
8
|
-
console.error(text)
|
|
9
|
-
process.exit(1)
|
|
10
|
-
}
|
package/src/fileSystem.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { fs, path } from '#/utils'
|
|
2
|
-
|
|
3
|
-
export { fs, path }
|
|
4
|
-
|
|
5
|
-
export const cp = fs.copy
|
|
6
|
-
|
|
7
|
-
export const mv = fs.move
|
|
8
|
-
|
|
9
|
-
export const rm = fs.remove
|
|
10
|
-
|
|
11
|
-
export const mkdir = fs.mkdirp
|
|
12
|
-
|
|
13
|
-
export const ls = glob
|
|
14
|
-
|
|
15
|
-
export const ln = fs.ln
|
|
16
|
-
|
|
17
|
-
export const lns = fs.symlink
|
package/src/mixins.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export async function mixins(...names) {
|
|
2
|
-
const errorNames = []
|
|
3
|
-
for (const name of names) {
|
|
4
|
-
try {
|
|
5
|
-
// TODO: use env to config
|
|
6
|
-
await import(`${process.env.HOME}/bin.src/mixins/${name}`)
|
|
7
|
-
} catch (error) {
|
|
8
|
-
if (error.message.match(/Cannot find module/)) {
|
|
9
|
-
errorNames.push(name)
|
|
10
|
-
} else {
|
|
11
|
-
throw error
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
if (errorNames.length > 0) {
|
|
16
|
-
console.error(`Error: [mixins] not found: ${errorNames.join(', ')}`)
|
|
17
|
-
process.exit(1)
|
|
18
|
-
}
|
|
19
|
-
}
|
package/src/types/global.d.ts
DELETED
package/src/ui/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './table'
|
package/src/ui/table.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { getBorderCharacters, table as rawTable } from 'table'
|
|
2
|
-
import invariant from 'tiny-invariant'
|
|
3
|
-
|
|
4
|
-
// Display table
|
|
5
|
-
// data: [{a: '1'}] | [['a'], ['1'], ..]
|
|
6
|
-
export function table(data) {
|
|
7
|
-
invariant(_.isArray(data), 'data should be array')
|
|
8
|
-
let columns = []
|
|
9
|
-
let rows = []
|
|
10
|
-
if (_.isPlainObject(data[0])) {
|
|
11
|
-
columns = Object.keys(data[0])
|
|
12
|
-
rows = data.map((v) => Object.values(v))
|
|
13
|
-
} else if (_.isArray(data[0])) {
|
|
14
|
-
columns = data[0]
|
|
15
|
-
rows = data.slice(1)
|
|
16
|
-
} else {
|
|
17
|
-
throw new Error('data should be array of object or array')
|
|
18
|
-
}
|
|
19
|
-
const newData = [data[0], ...data.slice(1)]
|
|
20
|
-
const tableText = rawTable(
|
|
21
|
-
[columns.map((v) => colors.green.bold(v)), ...rows],
|
|
22
|
-
{
|
|
23
|
-
drawHorizontalLine: (lineIndex, rowCount) =>
|
|
24
|
-
[0, 1, rowCount].includes(lineIndex),
|
|
25
|
-
border: {
|
|
26
|
-
...getBorderCharacters('norc'),
|
|
27
|
-
topLeft: '╭',
|
|
28
|
-
topRight: '╮',
|
|
29
|
-
bottomLeft: '╰',
|
|
30
|
-
bottomRight: '╯',
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
)
|
|
34
|
-
echo(tableText)
|
|
35
|
-
}
|
package/src/yaml.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { merge as mergeObject } from 'lodash-es'
|
|
2
|
-
import Yaml from 'yaml'
|
|
3
|
-
import { fs } from '#/utils'
|
|
4
|
-
|
|
5
|
-
export const parse = Yaml.parse
|
|
6
|
-
|
|
7
|
-
export const stringify = Yaml.stringify
|
|
8
|
-
|
|
9
|
-
export async function readFile(path: string) {
|
|
10
|
-
const text = (await fs.inputFile(path, 'utf8')) as string
|
|
11
|
-
if (!text) {
|
|
12
|
-
throw new Error(`input file not found: ${path}`)
|
|
13
|
-
}
|
|
14
|
-
return Yaml.parse(text)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export async function writeFile(path: string, data: any) {
|
|
18
|
-
const text = Yaml.stringify(data)
|
|
19
|
-
await fs.outputFile(path, text)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export async function mergeFiles(inputPaths: string[], outputPath: string) {
|
|
23
|
-
const outputObject = {}
|
|
24
|
-
for (const inputPath of inputPaths) {
|
|
25
|
-
const text = (await fs.inputFile(inputPath, 'utf8')) as string
|
|
26
|
-
if (!text) {
|
|
27
|
-
throw new Error(`input file not found: ${inputPath}`)
|
|
28
|
-
}
|
|
29
|
-
const object = Yaml.parse(text)
|
|
30
|
-
mergeObject(outputObject, object)
|
|
31
|
-
}
|
|
32
|
-
const outputText = Yaml.stringify(outputObject)
|
|
33
|
-
await fs.outputFile(outputPath, outputText)
|
|
34
|
-
}
|
|
File without changes
|