cli-api 0.1.1 → 0.2.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.
Files changed (43) hide show
  1. package/README.md +22 -25
  2. package/dist/index.d.mts +394 -0
  3. package/dist/index.mjs +3 -0
  4. package/dist/interfaces-COq24bNI.mjs +391 -0
  5. package/dist/run-C903J5ca.mjs +1137 -0
  6. package/package.json +37 -37
  7. package/.hgignore +0 -12
  8. package/.idea/$CACHE_FILE$ +0 -6
  9. package/.idea/clap.iml +0 -8
  10. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  11. package/.idea/deployment.xml +0 -63
  12. package/.idea/inspectionProfiles/Project_Default.xml +0 -10
  13. package/.idea/misc.xml +0 -6
  14. package/.idea/modules.xml +0 -8
  15. package/.idea/vcs.xml +0 -6
  16. package/Makefile +0 -14
  17. package/babel.config.json +0 -33
  18. package/dist/cjs/index.js +0 -588
  19. package/dist/cjs/index.js.map +0 -1
  20. package/dist/es/index.mjs +0 -578
  21. package/dist/es/index.mjs.map +0 -1
  22. package/dist/types/app-help.d.ts +0 -3
  23. package/dist/types/commands/command-help.d.ts +0 -2
  24. package/dist/types/commands/version.d.ts +0 -2
  25. package/dist/types/constants.d.ts +0 -4
  26. package/dist/types/index.d.ts +0 -3
  27. package/dist/types/interfaces.d.ts +0 -79
  28. package/dist/types/options.d.ts +0 -7
  29. package/dist/types/print-command-help.d.ts +0 -2
  30. package/dist/types/run.d.ts +0 -2
  31. package/dist/types/utils.d.ts +0 -17
  32. package/rollup.config.js +0 -44
  33. package/src/app-help.ts +0 -34
  34. package/src/commands/command-help.ts +0 -28
  35. package/src/commands/version.ts +0 -11
  36. package/src/constants.ts +0 -4
  37. package/src/index.ts +0 -3
  38. package/src/interfaces.ts +0 -89
  39. package/src/options.ts +0 -266
  40. package/src/print-command-help.ts +0 -78
  41. package/src/run.ts +0 -45
  42. package/src/utils.ts +0 -86
  43. package/tsconfig.json +0 -32
package/src/run.ts DELETED
@@ -1,45 +0,0 @@
1
- import {App} from './interfaces'
2
- import {helpCommand} from './commands/command-help'
3
- import {versionCommand} from './commands/version'
4
- import {printHelp} from './app-help'
5
- import {getCommand, parseArgs} from './options'
6
- import {abort, sortBy} from './utils'
7
- import {printCommandHelp} from './print-command-help'
8
-
9
- export default function run(app: App) {
10
- app = {
11
- ...app,
12
- commands: [...sortBy(app.commands, c => c.name), versionCommand,helpCommand],
13
- // commands: sortBy([...app.commands,versionCommand,helpCommand], c => c.name),
14
- }
15
- if (process.argv.length <= 2) {
16
- printHelp(app)
17
- process.exit(0)
18
- }
19
-
20
- const cmd = getCommand(process.argv[2], app)
21
- const rawArgs = process.argv.slice(3)
22
- if (rawArgs.includes('--help')) {
23
- printCommandHelp(app, cmd)
24
- process.exit(0)
25
- }
26
- let args, opts
27
- try {
28
- [args, opts] = parseArgs(cmd, rawArgs)
29
- } catch (err) {
30
- abort(String(err.message))
31
- process.exit(2)
32
- }
33
- Promise.resolve(cmd.execute(opts, args, app))
34
- .then(code => {
35
- if (code != null) {
36
- process.exit(code)
37
- }
38
- process.exit(0)
39
- }, err => {
40
- // console.error(err)
41
- abort(String(err.stack))
42
- process.exit(1)
43
- })
44
-
45
- }
package/src/utils.ts DELETED
@@ -1,86 +0,0 @@
1
- import {App} from './interfaces'
2
- import Path from 'path'
3
- import stringWidth from 'string-width'
4
- import Chalk from 'chalk'
5
- import {EMPTY_ARRAY, FALSE_VALUES, TRUE_VALUES} from './constants'
6
- import FileSys from 'fs'
7
-
8
- export const print = process.stdout.write.bind(process.stdout)
9
- export const printLn = console.log.bind(console)
10
-
11
- function blockError(str: string) {
12
- const lines = str.split('\n')
13
- const width = Math.max(...lines.map(l => stringWidth(l))) + 4
14
- printLn(Chalk.bgRed(space(width)))
15
- for (const line of lines) {
16
- const txt = ` ${line}`
17
- printLn(Chalk.bgRed(txt + space(width, txt)))
18
- }
19
- printLn(Chalk.bgRed(space(width)))
20
- }
21
-
22
- export function abort(message: string, code: number = 1): never {
23
- blockError(message)
24
- process.exit(code)
25
- }
26
-
27
- export function toArray<T>(x: T | T[]): readonly T[] {
28
- if (!x) return EMPTY_ARRAY
29
- return Array.isArray(x) ? x : [x]
30
- }
31
-
32
- export function resolve<T>(x: any): T {
33
- return typeof x === 'function' ? x() : x
34
- }
35
-
36
- export function toBool(str: string | boolean): boolean {
37
- if (typeof str === 'boolean') return str
38
- str = String(str).trim().toLowerCase()
39
- if (TRUE_VALUES.has(str)) {
40
- return true
41
- }
42
- if (FALSE_VALUES.has(str)) {
43
- return false
44
- }
45
- throw new Error(`Could not cast "${str}" to boolean`)
46
- }
47
-
48
- export function space(len: number, str?: string) {
49
- if (str) {
50
- len -= stringWidth(str)
51
- }
52
-
53
- return len > 0 ? ' '.repeat(len) : ''
54
- }
55
-
56
- export function getProcName(app: App) {
57
- if (app.argv0 != null) {
58
- return app.argv0
59
- }
60
- const relPath = Path.relative(process.cwd(), process.argv[1])
61
- // console.log(relPath, process.argv[1])
62
- // console.log(process.argv0,process.argv[0])
63
- return `${Path.basename(process.argv[0])} ${relPath.length < process.argv[1].length ? relPath : process.argv[1]}`
64
- }
65
-
66
- export function includes(needle: string, haystack: string | string[] | undefined) {
67
- if (!haystack) return false
68
- if (Array.isArray(haystack)) return haystack.includes(needle)
69
- return needle === haystack
70
- }
71
-
72
- export function statSync(path: string): FileSys.Stats | null {
73
- try {
74
- return FileSys.lstatSync(path)
75
- } catch {
76
- return null
77
- }
78
- }
79
-
80
- export function sortBy<T>(arr: T[], cmp: (x: T) => string): T[] {
81
- const collator = new Intl.Collator() // 'en',{usage: 'sort',sensitivity:'base'}
82
- const values = arr.map(cmp)
83
- const keys = Array.from(arr.keys())
84
- keys.sort((a, b) => collator.compare(values[a], values[b]))
85
- return keys.map(i => arr[i])
86
- }
package/tsconfig.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "strict": true,
4
- "importHelpers": false,
5
- "inlineSources": true,
6
- "noEmitOnError": true,
7
- "pretty": true,
8
- "module": "commonjs",
9
- "noImplicitAny": true,
10
- "suppressImplicitAnyIndexErrors": false,
11
- "removeComments": false,
12
- "preserveConstEnums": false,
13
- "sourceMap": true,
14
- "lib": ["esnext"],
15
- "skipLibCheck": false,
16
- "outDir": "dist/types",
17
- "emitDeclarationOnly": true,
18
- "target": "esnext",
19
- "declaration": true,
20
- "resolveJsonModule": true,
21
- "esModuleInterop": true,
22
- "moduleResolution": "node",
23
- "isolatedModules": true,
24
- "allowSyntheticDefaultImports": true
25
- },
26
- "files": [
27
- "src/index.ts"
28
- ],
29
- "include": [
30
- "src/**/*.d.ts"
31
- ]
32
- }