termkit 1.7.1 → 2.0.1

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.
@@ -1,154 +0,0 @@
1
- const Option = require('./Option'),
2
- cosmetic = require('cosmetic'),
3
- { findCommand, findCommandVariables, findOptions, getVariables } = require('../helpers')
4
-
5
- module.exports = class Command {
6
- constructor(data) {
7
- this.actionFunction = null
8
- this.commandsArray = []
9
- this.commandStrings = ['help']
10
- this.info = null
11
- this.name = null
12
- this.middlewaresArray = []
13
- this.optionsArray = []
14
- this.variables = null
15
- this.versionString = null
16
-
17
- if (!data) return
18
-
19
- if (data.info) this.info = data.info
20
- if (data.name) this.name = data.name
21
- if (data.variables) this.variables = getVariables(data.variables)
22
- if (data.middlewares) this.middlewaresArray = [...data.middlewares]
23
- if (data.options) this.optionsArray = [...data.options]
24
- }
25
- description(info, description) {
26
- this.info = info
27
- this.description = description
28
- return this
29
- }
30
- variable(string) {
31
- let variable = getVariables(string)
32
- if (variable && !this.variables) {
33
- this.variables = variable
34
- } else if (variable) {
35
- this.variables.push(variable)
36
- }
37
- return this
38
- }
39
- action(actionFunction) {
40
- this.actionFunction = actionFunction
41
- return this
42
- }
43
- command(command) {
44
- this.commandsArray.push(command)
45
- return this
46
- }
47
- commands(commands) {
48
- this.commandsArray = commands
49
- commands.map(c => this.commandStrings.unshift(c.name))
50
- for (let command of commands) {
51
- this.commandStrings.unshift(command.name)
52
- }
53
- return this
54
- }
55
- middleware(middleware) {
56
- this.middlewaresArray.push(middleware)
57
- return this
58
- }
59
- middlewares(middlewares) {
60
- this.middlewaresArray.push(...middlewares)
61
- return this
62
- }
63
- option(short, long, variables, info) {
64
- this.optionsArray.push(new Option({short, long, variables, info}))
65
- return this
66
- }
67
- options(options) {
68
- this.optionsArray.push(...options)
69
- return this
70
- }
71
- version(version) {
72
- this.versionString = version
73
- return this
74
- }
75
- help(source) {
76
- const table = []
77
- let program = this.name || 'Program'
78
- if (this.variables) for (const variable of this.variables) program += ` ${variable.raw}`
79
- if (this.optionsArray.length > 0) program += ' [...options]'
80
- table.push({ title: '\nCommand', info: program, data: [] })
81
- if (this.info) table.push({ title: 'Info', info: this.info, data: [] })
82
- if (this.optionsArray.length > 0) {
83
- const section = { title: 'Options', data: []}
84
- for (const option of this.optionsArray) {
85
- let name = ''
86
- if (option.short) name = `-${option.short}`
87
- if (option.short && option.long) name += ', '
88
- if (option.long) name += `--${option.long}`
89
- if (option.variables) for (const variable of option.variables) name += ` ${variable.raw}`
90
- section.data.push([name, option.info || ''])
91
- }
92
- table.push(section)
93
- }
94
- if (this.commandsArray.length > 0) {
95
- const section = { title: 'Subcommands', data: []}
96
- for (let command of this.commandsArray) {
97
- let name = command.name
98
- if (command.variables) for (const variable of command.variables) name += ` ${variable.raw}`
99
- section.data.push([name, command.info || ''])
100
- }
101
- table.push(section)
102
- }
103
- const padding = {}
104
- for (const section of table) for (const array of section.data) for (const [index, string] of array.entries()) if (!padding[index] || string.length > padding[index]) padding[index] = string.length
105
- const lines = []
106
- for (const section of table) {
107
- lines.push(section.title ? cosmetic.cyan.underline(section.title) : '')
108
- if (section.versionString) lines.push(`v${section.versionString}`)
109
- if (section.info) lines.push(section.info)
110
- for (const array of section.data) {
111
- let line = ''
112
- for (let [index, string] of array.entries()) {
113
- if (padding[index] && padding[index] !== string.length) while (string.length < padding[index]) string += ' '
114
- line += line ? ` ${string}` : string
115
- }
116
- lines.push(line)
117
- }
118
- lines.push('')
119
- }
120
- for (const line of lines) console.log(line)
121
- }
122
- async parse (array) {
123
- array.splice(0, 2)
124
- let command = this
125
- const options = { _source: Array.from(array) }
126
- while (array.length) {
127
- if (!array.includes('help')) {
128
- Object.assign(options, findOptions(array, command))
129
- Object.assign(options, findCommandVariables(array, command))
130
- Object.assign(options, findOptions(array, command))
131
- }
132
- if (array.length) {
133
- if (!array.includes('help')) for (const middleware of command.middlewaresArray) await middleware(options)
134
- const newCommand = findCommand(array, command.commandsArray)
135
- if (!newCommand && array[0] === 'help') return command.help(options._source)
136
- if (!newCommand) throw new SyntaxError(`Unknown command: ${array[0]}`)
137
- const name = command.name || '_base'
138
- if (!options._parents) options._parents = {}
139
- options._parents[name] = {}
140
- for (const key of Object.keys(options)) {
141
- if (!key.startsWith('_')) {
142
- options._parents[name][key] = options[key]
143
- delete options[key]
144
- }
145
- }
146
- command = newCommand
147
- }
148
- }
149
- for (const middleware of command.middlewaresArray) await middleware(options)
150
- if (command.actionFunction) return command.actionFunction(options)
151
- if (options._source.length == 2) return command.help(options._source)
152
- throw new Error(`No action for command: ${command.name || '_base'}`)
153
- }
154
- }
@@ -1,5 +0,0 @@
1
- module.exports = class Middleware {
2
- constructor(action) {
3
- this.action = action
4
- }
5
- }
@@ -1,22 +0,0 @@
1
- let helpers = require('../helpers'),
2
- getVariables = helpers.getVariables
3
-
4
- module.exports = class Option {
5
- constructor(data) {
6
- this.short = null
7
- this.long = null
8
- this.info = null
9
- this.variables = null
10
-
11
- if (!data) return
12
-
13
- if (data.short) this.short = data.short
14
- if (data.long) this.long = data.long
15
- if (data.info) this.info = data.info
16
- if (data.variables) this.variables = getVariables(data.variables)
17
- }
18
- description(string) {
19
- this.info = string
20
- return this
21
- }
22
- }
@@ -1,30 +0,0 @@
1
- const Command = require('./Command'),
2
- Middleware = require('./Middleware'),
3
- Option = require('./Option')
4
-
5
- class Termkit {
6
- base = null
7
- command_defaults = {}
8
- static set defaults(obj) {
9
- Termkit.command_defaults = obj
10
- }
11
- static setDefaults(obj) {
12
- Termkit.command_defaults = obj
13
- }
14
- static command(name, variables, info) {
15
- const command = new Command(Object.assign({ name, variables, info }, Termkit.command_defaults))
16
- if (!Termkit.base) Termkit.base = command
17
- return command
18
- }
19
- static middleware(action) {
20
- return new Middleware(action)
21
- }
22
- static option(short, long, variables, info) {
23
- return new Option({ short, long, variables, info })
24
- }
25
- static parse(arr) {
26
- return Termkit.base.parse(arr)
27
- }
28
- }
29
-
30
- module.exports = Termkit
@@ -1,18 +0,0 @@
1
- module.exports = class Variable {
2
- constructor(data) {
3
- this.array = false
4
- this.name = null
5
- this.raw = null
6
- this.required = false
7
- this.value = null
8
-
9
- if (!data) return
10
-
11
- if (data.array) this.array = data.array
12
- if (data.name) this.name = data.name
13
- if (data.raw) this.raw = data.raw
14
- if (data.required) this.required = data.required
15
-
16
- if (this.array) this.value = []
17
- }
18
- }
@@ -1,6 +0,0 @@
1
- let Command = require('./Command'),
2
- Middleware = require('./Middleware'),
3
- Option = require('./Option'),
4
- Variable = require('./Variable')
5
-
6
- module.exports = { Command, Middleware, Option, Variable }
package/test/index.js DELETED
@@ -1,3 +0,0 @@
1
- // require('./_setup')
2
- require('./models')
3
- require('./program')
@@ -1,19 +0,0 @@
1
- describe('Command', () => {
2
- // describe('getToken()', () => {
3
- // it('should get and set token', async() => {
4
- // let result, error
5
- // try {
6
- // result = await getToken({
7
- // email: 'example@example.com',
8
- // password: 'password',
9
- // subdomain: 'example'
10
- // })
11
- // } catch(err) {
12
- // error = err
13
- // }
14
- // should.not.exist(error)
15
- // should.exist(result)
16
- // config.token.should.equal(result)
17
- // })
18
- // })
19
- })
File without changes
File without changes
File without changes
@@ -1,6 +0,0 @@
1
- describe('models', () => {
2
- require('./Command')
3
- require('./Middleware')
4
- require('./Option')
5
- require('./Variable')
6
- })
@@ -1,160 +0,0 @@
1
- const { is, isnt } = require('amprisand'),
2
- { command, middleware, option, setDefaults } = require('../../')
3
-
4
- // setDefaults({
5
- // middlewares: [() => console.log('yooo')]
6
- // })
7
-
8
- let program
9
-
10
- describe('complex', () => {
11
- describe('program = command()', () => {
12
- it('should set up program', () => {
13
- program = command('app', '<req>')
14
- .version(process.env.npm_package_version)
15
- .description('Program description', 'This is some super extensive detail about this command')
16
- .variable('[dir]')
17
- .middlewares([
18
- // middleware((options) => console.log('here', options))
19
- ])
20
- .options([
21
- option('a', 'array', '[arr...]', 'Option with array variable'),
22
- option('r', 'required', '<reqA> <reqB>', 'Option with required variable'),
23
- option('o', 'optional', '[opt]', 'Option with optional variable'),
24
- option('b', 'boolean', null, 'Option with no variable')
25
- ])
26
- .action((options) => ({ command: 'app', options }))
27
- .commands([
28
- command('help', null, 'Help Func')
29
- .action((options) => ({ command: 'help', options })),
30
- command('one', '[reqA] [reqB]', 'Description of one')
31
- .option('a', 'array', '[arr...]', 'Option with array variable')
32
- .option('r', 'required', '<req>', 'Option with required variable')
33
- .option('o', 'optional', '[opt]', 'Option wit h optional variable')
34
- .option('b', 'boolean', null, 'Option with no variable')
35
- .action((options) => ({ command: 'one', options })),
36
- command('two', '<req>', 'Description of two')
37
- .option('a', 'array', '[arr...]', 'Option with array variable')
38
- .option('r', 'required', '<req>', 'Option with required variable')
39
- .option('o', 'optional', '[opt]', 'Option with optional variable')
40
- .option('b', 'boolean', null, 'Option with no variable')
41
- .middleware(() => console.log('two middleware'))
42
- .action((options) => ({ command: 'two', options }))
43
- .commands([
44
- command('three', null, 'Description of three')
45
- .option('a', 'array', '[arr...]', 'Option with array variable')
46
- .option('r', 'required', '<req>', 'Option with required variable')
47
- .option('o', 'optional', '[opt]', 'Option with optional variable')
48
- .option('b', 'boolean', null, 'Option with no variable')
49
- .action((options) => ({ command: 'three', options }))
50
- .commands([
51
- command('help')
52
- .action((options) => {
53
- console.log('custom usage printout')
54
- })
55
- ]),
56
- command('four', '[optA] [optB]', 'Description of four')
57
- .option('a', 'array', '[arr...]', 'Option with array variable')
58
- .option(null, 'required', '<req>', 'Option with required variable')
59
- .option('o', null, '[opt]', 'Option with optional variable')
60
- .option('b', 'boolean', null, 'Option with no variable')
61
- .middleware((options) => console.log('four middleware'))
62
- .action((options) => console.log('four action'))
63
- ])
64
- ])
65
-
66
- })
67
- })
68
- describe('program.parse()', () => {
69
- it('should run program', async () => {
70
- let error, result
71
- try {
72
- result = await program.parse('_ _ req --array arr0 arr1 arr2 --required req1 req2 --optional test --boolean'.split(' '))
73
- } catch(err) {
74
- error = err
75
- }
76
- isnt(error)
77
- is(result)
78
- result.command.is('app')
79
- // program.parse('_ _ -a arr0 arr1 arr2 -r req1 req2 -o -b'.split(' '))
80
- // program.parse('_ _ one testA testB'.split(' '))
81
- // program.parse('_ _ test -r requiredA requiredB one -r requiredC'.split(' '))
82
- // program.parse('_ _ test -a arr0 arr1 one -br required'.split(' '))
83
- // program.parse('_ _ -r reqA reqB'.split(' '))
84
- // program.parse('_ _ dir -r reqA reqB -a arr1 arr2 arr3 -o optional one fail'.split(' '))
85
- // program.parse('_ _ two four -a arr0 arr1 arr2 -r required -ob'.split(' '))
86
- // program.parse('_ _ shortcut'.split(' '))
87
- // program.parse('_ _ help'.split(' '))
88
- // program.parse('_ _ two three help'.split(' '))
89
- // program.parse('_ _ two help'.split(' '))
90
- // program.parse('_ _ two four help'.split(' '))
91
- // program.parse('_ _ two required four optionalA optionalB'.split(' '))
92
- // program.parse('_ _ two required four optionalA'.split(' '))
93
- })
94
- })
95
- describe('program.parse()', () => {
96
- it('should run program', async () => {
97
- let error, result
98
- try {
99
- result = await program.parse('_ _ req two req three -a arr0 arr1 arr2 -r required -ob'.split(' '))
100
- } catch(err) {
101
- error = err
102
- }
103
- isnt(error)
104
- is(result)
105
- result.command.is('three')
106
- })
107
- })
108
- describe('program.parse()', () => {
109
- it('should run program', async () => {
110
- let error, result
111
- try {
112
- result = await program.parse('_ _ req two req three -a arr0 arr1 arr2 --required required -ob'.split(' '))
113
- } catch(err) {
114
- error = err
115
- }
116
- isnt(error)
117
- is(result)
118
- result.command.is('three')
119
- })
120
- })
121
- describe('program.parse(help)', () => {
122
- it('should run program', async () => {
123
- let error, result
124
- try {
125
- result = await program.parse('_ _ help'.split(' '))
126
- } catch(err) {
127
- error = err
128
- }
129
- isnt(error)
130
- is(result)
131
- result.command.is('help')
132
- })
133
- })
134
- describe('program.parse(help)', () => {
135
- it('should run program', async () => {
136
- let error, result
137
- try {
138
- await program.parse('_ _ help'.split(' '))
139
- } catch(err) {
140
- error = err
141
- }
142
- isnt(error)
143
- // is(result)
144
- // result.command.is('help')
145
- })
146
- })
147
- describe('program.parse(-b req)', () => {
148
- it('should run program', async () => {
149
- let error, result
150
- try {
151
- result = await program.parse('_ _ -b req'.split(' '))
152
- } catch(err) {
153
- error = err
154
- }
155
- isnt(error)
156
- is(result)
157
- // result.command.is('help')
158
- })
159
- })
160
- })
@@ -1,4 +0,0 @@
1
- describe('program', () => {
2
- require('./complex')
3
- require('./middleware')
4
- })
@@ -1,73 +0,0 @@
1
- const { is, isnt } = require('amprisand'),
2
- { command, option, setDefaults } = require('../../')
3
-
4
- let program, output
5
-
6
- setDefaults({
7
- middlewares: [
8
- (options) => output ? output.push({ name: 'default', options }) : null
9
- ]
10
- })
11
-
12
- describe('middleware', () => {
13
- describe('program = command()', () => {
14
- it('should create a program with middleware', () => {
15
- program = command('app', '[opt]')
16
- .version('0.0.0')
17
- .options([
18
- option('a', 'array', '[arr...]', 'Option with array variable'),
19
- option('r', 'required', '<reqA>', 'Option with required variable'),
20
- option('o', 'optional', '[opt]', 'Option with optional variable'),
21
- option('b', 'boolean', null, 'Option with no variable')
22
- ])
23
- .middleware(async (options) => await output.push({ name: 'middleware', options }))
24
- .action(async (options) => await output.push({ name: 'action', options }))
25
- .commands([
26
- command('nested')
27
- .options([
28
- option('a', 'array', '[arr...]', 'Option with array variable'),
29
- option('r', 'required', '<reqA>', 'Option with required variable'),
30
- option('o', 'optional', '[opt]', 'Option with optional variable'),
31
- option('b', 'boolean', null, 'Option with no variable')
32
- ])
33
- .middleware(async (options) => await output.push({ name: 'nested middleware', options }))
34
- .action(async(options) => await output.push({ name: 'nested action', options }))
35
- ])
36
- })
37
- })
38
- describe('program.parse()', () => {
39
- it('middleware should be called before action', async () => {
40
- output = []
41
- await program.parse('_ _'.split(' '))
42
- output[0].name.is('default')
43
- output[1].name.is('middleware')
44
- output[2].name.is('action')
45
- })
46
- })
47
- describe('program.parse()', () => {
48
- it('parent middleware should be called first', async () => {
49
- output = []
50
- await program.parse('_ _ nested'.split(' '))
51
- // hits default twice cause every command gets default, nested and top
52
- output[0].name.is('default')
53
- output[1].name.is('middleware')
54
- output[2].name.is('default')
55
- output[3].name.is('nested middleware')
56
- output[4].name.is('nested action')
57
- })
58
- })
59
- describe('program.parse()', () => {
60
- it('should get options and variables', async () => {
61
- output = []
62
- await program.parse('_ _ test -r required nested -b'.split(' '))
63
- // hits default twice cause every command gets default, nested and top
64
- output[0].name.is('default')
65
- output[1].name.is('middleware')
66
- output[2].name.is('default')
67
- output[3].name.is('nested middleware')
68
- output[4].name.is('nested action') // output[0].name.is('middleware')
69
- // output[1].name.is('nested middleware')
70
- // output[2].name.is('nested action')
71
- })
72
- })
73
- })