Compare commits
29 Commits
7574c250af
...
master
Author | SHA1 | Date | |
---|---|---|---|
d9de76b188 | |||
b81b77b924 | |||
ee6be8317d | |||
390cc10305 | |||
e1507edd5d | |||
7268ed1643 | |||
04aa24a75d | |||
8eb8deb855 | |||
c0eb113036 | |||
6207de8e5b | |||
7521fb310c | |||
125fb7b5e6 | |||
8f040c38eb | |||
7ad999878a | |||
7395241329 | |||
ed2d0ba047 | |||
78c09809e6 | |||
85b02b552f | |||
3e178b28ae | |||
f5ee4665dc | |||
fe4057d0b5 | |||
551b5c89a7 | |||
5dcd54e451 | |||
d477124973 | |||
f7c03f82f1 | |||
089519844f | |||
bc960f632e | |||
88a35265d0 | |||
3fa23c1697 |
14
.drone.yml
Normal file
14
.drone.yml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
kind: pipeline
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
image: registry:5000/node-build
|
||||||
|
commands:
|
||||||
|
- yarn
|
||||||
|
- curl https://git.scode.ovh/Timo/standard/raw/branch/master/ci.js > ci.js
|
||||||
|
|
||||||
|
- name: build
|
||||||
|
image: registry:5000/node-build
|
||||||
|
commands:
|
||||||
|
- node ci.js
|
@ -8,6 +8,7 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
/* eslint-disable id-match */
|
/* eslint-disable id-match */
|
||||||
|
/* eslint-disable node/no-missing-require */
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
@ -16,7 +17,8 @@ const {
|
|||||||
BooleanOption,
|
BooleanOption,
|
||||||
NumberOption,
|
NumberOption,
|
||||||
ArrayOption,
|
ArrayOption,
|
||||||
FolderOption
|
FolderOption,
|
||||||
|
IntegerOption
|
||||||
} = require ('./dist/lib/index.js');
|
} = require ('./dist/lib/index.js');
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
@ -26,12 +28,14 @@ const {
|
|||||||
.parse ();
|
.parse ();
|
||||||
const num = await new NumberOption ({ name: 'num' })
|
const num = await new NumberOption ({ name: 'num' })
|
||||||
.parse ();
|
.parse ();
|
||||||
|
const int = await new IntegerOption ({ name: 'num' })
|
||||||
|
.parse ();
|
||||||
const arr = await new ArrayOption ({ name: 'arr' })
|
const arr = await new ArrayOption ({ name: 'arr' })
|
||||||
.parse ();
|
.parse ();
|
||||||
const fld = await new FolderOption ({ name: 'fld' })
|
const fld = await new FolderOption ({ name: 'fld' })
|
||||||
.parse ();
|
.parse ();
|
||||||
|
|
||||||
const data = { str, bool, num, arr, fld };
|
const data = { str, bool, num, int, arr, fld };
|
||||||
|
|
||||||
console.log (data);
|
console.log (data);
|
||||||
}) ();
|
}) ();
|
||||||
|
17
CHANGELOG.md
17
CHANGELOG.md
@ -1,5 +1,22 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2.1.0
|
||||||
|
|
||||||
|
- Fix for NumberOption: do not cut off float values
|
||||||
|
- New type IntegerOption: only allows integer values
|
||||||
|
|
||||||
|
## 2.0.0
|
||||||
|
|
||||||
|
Restructuring to split different Option types and keep specific parameters separate
|
||||||
|
|
||||||
|
### Breaking Changes
|
||||||
|
|
||||||
|
- new structure
|
||||||
|
- option 'required' has been removed
|
||||||
|
- automatic console parameters have been removed
|
||||||
|
- help page
|
||||||
|
- quiet switch (interactive prompts can be disabled using the sources option)
|
||||||
|
|
||||||
## 1.8.0
|
## 1.8.0
|
||||||
|
|
||||||
callback in case an option could not be assigned instead of silently skipping
|
callback in case an option could not be assigned instead of silently skipping
|
||||||
|
23
Jenkinsfile
vendored
23
Jenkinsfile
vendored
@ -1,23 +0,0 @@
|
|||||||
pipeline {
|
|
||||||
agent any
|
|
||||||
|
|
||||||
environment {
|
|
||||||
VERSION = VersionNumber([
|
|
||||||
versionNumberString:
|
|
||||||
'${BUILDS_ALL_TIME}',
|
|
||||||
versionPrefix: '1.8.',
|
|
||||||
worstResultForIncrement: 'SUCCESS'
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
stages {
|
|
||||||
stage('Building') {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
currentBuild.displayName = env.VERSION
|
|
||||||
}
|
|
||||||
sh 'yarn ci ${VERSION}'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
81
README.md
81
README.md
@ -1,8 +1,8 @@
|
|||||||
# @sapphirecode/console-app
|
# @sapphirecode/console-app
|
||||||
|
|
||||||
version: 1.8.x
|
version: 2.1.x
|
||||||
|
|
||||||
read parameters from env, console args or interactively
|
read parameters from env, config files, console args or interactively
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@ -17,46 +17,37 @@ yarn:
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const {InteractiveOptions} = require('@sapphirecode/console-app');
|
const {
|
||||||
|
ArrayOption, // arrays made out of numbers, strings and booleans
|
||||||
|
BooleanOption,
|
||||||
|
FileOption, // paths that exist and are a file
|
||||||
|
FolderOption, // paths that exist and are a folder
|
||||||
|
NumberOption, // integer and float values
|
||||||
|
IntegerOption, // only integer values
|
||||||
|
PathOption, // paths that exist in the file system
|
||||||
|
StringOption,
|
||||||
|
} = require('@sapphirecode/console-app');
|
||||||
|
|
||||||
const reader = new InteractiveOptions([
|
const input = await new BooleanOption({
|
||||||
{
|
name: 'foo', // option name used in configs and console arguments
|
||||||
name: 'foo', // name of the option
|
|
||||||
type: 'boolean', // data type
|
// optional settings:
|
||||||
required: true, // require option to be specified (optional)
|
default: false, // default value
|
||||||
default: false, // default value (optional)
|
sources: {
|
||||||
alias: 'f', // shorthand alias in the console (optional)
|
configs: [], // config files to read from. none by default
|
||||||
env: 'fooenv', // environment variable to read from (optional)
|
interactive: true, // use interactive prompts
|
||||||
description: 'the switch foo', // description in the help page (optional)
|
console: true // read from console arguments
|
||||||
message: 'should foo be true?', // message when asking interactively (optional)
|
// environment is always on if the 'env' option below is specified
|
||||||
preset: [], // preset choices for string and path types (optional)
|
|
||||||
error: 'wrong input' // message to display when the user gives invalid input
|
|
||||||
},
|
},
|
||||||
]);
|
alias: 'f', // shorthand console argument name
|
||||||
|
env: 'foo_env', // name of the environment variable to read from
|
||||||
const result = await reader.parse();
|
message: 'input foo', // message to display in interactive prompt
|
||||||
console.log(result.foo);
|
error: 'failed to read foo', // message to display when input was invalid
|
||||||
|
error_callback: (opt, val, err)=>{...}, // function to call when an option value could not be read
|
||||||
|
exit_on_interrupt: true, // exit program when user cancels the interactive prompt
|
||||||
|
}).parse();
|
||||||
```
|
```
|
||||||
|
|
||||||
available data types:
|
|
||||||
|
|
||||||
- string
|
|
||||||
- number
|
|
||||||
- boolean
|
|
||||||
- path: expects a path that exists
|
|
||||||
- file: expects a path that exists and is a file
|
|
||||||
- folder: expects a path that exists and is a folder
|
|
||||||
- array: arrays made out of strings, numbers and booleans
|
|
||||||
|
|
||||||
the console reader automatically adds the options --help (-h) and --quiet (-q)
|
|
||||||
|
|
||||||
- help: shows the yargs help screen
|
|
||||||
- quiet: prevents interactive queries and throws an error when not all required
|
|
||||||
parameters are specified
|
|
||||||
|
|
||||||
the reader can also be constructed with additional options that specify which
|
|
||||||
sources should be used. It reads from all, except config files by default
|
|
||||||
|
|
||||||
config files can import other config files with #include. example:
|
config files can import other config files with #include. example:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@ -68,20 +59,6 @@ config files can import other config files with #include. example:
|
|||||||
|
|
||||||
config files are parsed using [hjson](https://github.com/hjson/hjson-js)
|
config files are parsed using [hjson](https://github.com/hjson/hjson-js)
|
||||||
|
|
||||||
the option exit_on_interrupt determines whether an error should be thrown or the
|
|
||||||
process should exit when the user presses control + c in an interactive prompt.
|
|
||||||
|
|
||||||
```js
|
|
||||||
const reader = new InteractiveOptions([], {
|
|
||||||
args: true,
|
|
||||||
env: true,
|
|
||||||
interactive: true,
|
|
||||||
configs: ['json files to search for options'],
|
|
||||||
exit_on_interrupt: true, // exit when user cancels prompt
|
|
||||||
error_callback: (opt, val, err)=>{...} // function to call when an option value could not be read
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT © Timo Hocker <timo@scode.ovh>
|
MIT © Timo Hocker <timo@scode.ovh>
|
||||||
|
14
jasmine.json
Normal file
14
jasmine.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
{
|
||||||
|
"spec_dir": "test",
|
||||||
|
"spec_files": [
|
||||||
|
"spec/*.js",
|
||||||
|
"spec/*.ts"
|
||||||
|
],
|
||||||
|
"helpers": [
|
||||||
|
"helpers/*.js",
|
||||||
|
"helpers/*.ts"
|
||||||
|
],
|
||||||
|
"stopSpecOnExpectationFailure": false,
|
||||||
|
"random": false
|
||||||
|
}
|
29
jenkins.js
29
jenkins.js
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
||||||
* This file is part of console-app which is released under MIT.
|
|
||||||
* See file 'LICENSE' for full license details.
|
|
||||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const https = require ('https');
|
|
||||||
const fs = require ('fs');
|
|
||||||
const { execSync: exec_sync } = require ('child_process');
|
|
||||||
|
|
||||||
const run_file = fs.createWriteStream ('.jenkins.run.js');
|
|
||||||
|
|
||||||
const [
|
|
||||||
,, ...args
|
|
||||||
] = process.argv;
|
|
||||||
|
|
||||||
run_file.on ('close', () => {
|
|
||||||
exec_sync (`node .jenkins.run.js ${args.join (' ')}`, { stdio: 'inherit' });
|
|
||||||
});
|
|
||||||
|
|
||||||
https.get (
|
|
||||||
'https://git.scode.ovh/Timo/standard/raw/branch/master/jenkins.run.js',
|
|
||||||
(msg) => {
|
|
||||||
msg.pipe (run_file);
|
|
||||||
}
|
|
||||||
);
|
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
export type ErrorCallback = (
|
export type ErrorCallback = (
|
||||||
option: string,
|
option: string,
|
||||||
value: unknown,
|
value: unknown,
|
||||||
|
@ -6,23 +6,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { TypeValidation } from './TypeValidation/TypeValidation';
|
import { TypeValidation } from './TypeValidation/TypeValidation';
|
||||||
|
import { ErrorCallback } from './ErrorCallback';
|
||||||
|
|
||||||
interface SourceConfig {
|
interface SourceConfig {
|
||||||
console?: boolean,
|
console?: boolean,
|
||||||
configs?: string[],
|
configs?: string[],
|
||||||
env?: boolean,
|
|
||||||
interactive?: boolean,
|
interactive?: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Option {
|
interface Option {
|
||||||
name: string;
|
name: string;
|
||||||
required?: boolean;
|
|
||||||
default?: unknown;
|
default?: unknown;
|
||||||
sources?: SourceConfig;
|
sources?: SourceConfig;
|
||||||
alias?: string;
|
alias?: string;
|
||||||
env?: string;
|
env?: string;
|
||||||
message?: string;
|
message?: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
|
error_callback?: ErrorCallback;
|
||||||
|
exit_on_interrupt?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
class OptionValue {
|
class OptionValue {
|
||||||
@ -33,6 +34,20 @@ class OptionValue {
|
|||||||
public constructor (type_validation: TypeValidation) {
|
public constructor (type_validation: TypeValidation) {
|
||||||
this.type_validation = type_validation;
|
this.type_validation = type_validation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async assign_arg (
|
||||||
|
opt: Option,
|
||||||
|
value: unknown
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
this.value = await this.type_validation.to_type (value);
|
||||||
|
this.filled = true;
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
if (typeof opt.error_callback !== 'undefined')
|
||||||
|
opt.error_callback (opt.name, value, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Option, OptionValue };
|
export { Option, OptionValue };
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
export type OptionType =
|
export type OptionType =
|
||||||
'string'
|
'string'
|
||||||
| 'number'
|
| 'number'
|
||||||
|
| 'int'
|
||||||
| 'boolean'
|
| 'boolean'
|
||||||
| 'file'
|
| 'file'
|
||||||
| 'folder'
|
| 'folder'
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { BaseOption } from './BaseOption';
|
import { BaseOption } from './BaseOption';
|
||||||
|
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { OptionSource } from '../Sources/OptionSource';
|
import { OptionSource } from '../Sources/OptionSource';
|
||||||
import { Option, OptionValue } from '../Option';
|
import { Option, OptionValue } from '../Option';
|
||||||
import { EnvSource } from '../Sources/EnvSource';
|
import { EnvSource } from '../Sources/EnvSource';
|
||||||
import { ErrorCallback } from '../ErrorCallback';
|
|
||||||
import { ArgSource } from '../Sources/ArgSource';
|
import { ArgSource } from '../Sources/ArgSource';
|
||||||
import { ConfigSource } from '../Sources/ConfigSource';
|
import { ConfigSource } from '../Sources/ConfigSource';
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
@ -12,26 +18,29 @@ export abstract class BaseOption<T> {
|
|||||||
private _config: Option;
|
private _config: Option;
|
||||||
|
|
||||||
public constructor (
|
public constructor (
|
||||||
config: Option,
|
config: Option
|
||||||
error_callback?: ErrorCallback,
|
|
||||||
exit_on_interrupt = true
|
|
||||||
) {
|
) {
|
||||||
this._config = config;
|
this._config = config;
|
||||||
|
|
||||||
const sources = config.sources || {};
|
const sources = config.sources || {};
|
||||||
if (typeof sources.configs !== 'undefined')
|
const exit_on_interrupt = config.exit_on_interrupt !== false;
|
||||||
this.sources.push (new ConfigSource (sources.configs, error_callback));
|
|
||||||
|
|
||||||
if (sources.env !== false)
|
if (typeof sources.configs !== 'undefined') {
|
||||||
this.sources.push (new EnvSource (error_callback));
|
this.sources.push (new ConfigSource (
|
||||||
|
sources.configs,
|
||||||
|
config.error_callback
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sources.push (new EnvSource (config.error_callback));
|
||||||
|
|
||||||
if (sources.console !== false)
|
if (sources.console !== false)
|
||||||
this.sources.push (new ArgSource (error_callback));
|
this.sources.push (new ArgSource (config.error_callback));
|
||||||
|
|
||||||
if (sources.interactive !== false) {
|
if (sources.interactive !== false) {
|
||||||
this.sources.push (new InteractiveSource (
|
this.sources.push (new InteractiveSource (
|
||||||
exit_on_interrupt,
|
exit_on_interrupt,
|
||||||
error_callback
|
config.error_callback
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { BaseOption } from './BaseOption';
|
import { BaseOption } from './BaseOption';
|
||||||
|
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { PathType } from '../TypeValidation/PathType';
|
import { PathType } from '../TypeValidation/PathType';
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { StringOption } from './StringOption';
|
import { StringOption } from './StringOption';
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { PathType } from '../TypeValidation/PathType';
|
import { PathType } from '../TypeValidation/PathType';
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { StringOption } from './StringOption';
|
import { StringOption } from './StringOption';
|
||||||
|
15
lib/Options/IntegerOption.ts
Normal file
15
lib/Options/IntegerOption.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
|
import { BaseOption } from './BaseOption';
|
||||||
|
|
||||||
|
export class IntegerOption extends BaseOption<number> {
|
||||||
|
protected get validation ():TypeValidation {
|
||||||
|
return new TypeValidation ('int');
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { BaseOption } from './BaseOption';
|
import { BaseOption } from './BaseOption';
|
||||||
|
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { PathType } from '../TypeValidation/PathType';
|
import { PathType } from '../TypeValidation/PathType';
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { StringOption } from './StringOption';
|
import { StringOption } from './StringOption';
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
import { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||||
import { StringOptionConfig } from '../SubConfigs';
|
import { StringOptionConfig } from '../SubConfigs';
|
||||||
import { BaseOption } from './BaseOption';
|
import { BaseOption } from './BaseOption';
|
||||||
|
@ -45,6 +45,6 @@ export class ArgSource extends OptionSource {
|
|||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
await this.assign_arg (opt, val, argv[opt.name]);
|
await val.assign_arg (opt, argv[opt.name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,6 +64,6 @@ export class ConfigSource extends OptionSource {
|
|||||||
const keys = Object.keys (data);
|
const keys = Object.keys (data);
|
||||||
|
|
||||||
if (keys.includes (opt.name))
|
if (keys.includes (opt.name))
|
||||||
await this.assign_arg (opt, val, data[opt.name]);
|
await val.assign_arg (opt, data[opt.name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,20 @@ import { OptionSource } from './OptionSource';
|
|||||||
|
|
||||||
export class EnvSource extends OptionSource {
|
export class EnvSource extends OptionSource {
|
||||||
public async parse (opt: Option, val:OptionValue): Promise<void> {
|
public async parse (opt: Option, val:OptionValue): Promise<void> {
|
||||||
if (
|
if (typeof opt.env === 'undefined')
|
||||||
typeof opt.env !== 'undefined'
|
return;
|
||||||
&& typeof process.env[opt.env] !== 'undefined'
|
|
||||||
)
|
if (typeof process.env[opt.env] === 'undefined') {
|
||||||
await this.assign_arg (opt, val, process.env[opt.env]);
|
if (typeof this.error_callback !== 'undefined') {
|
||||||
|
this.error_callback (
|
||||||
|
opt.name,
|
||||||
|
null,
|
||||||
|
new Error ('environment variable does not exist')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await val.assign_arg (opt, process.env[opt.env]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
lib/Sources/Interactive/ArraySubSource.ts
Normal file
26
lib/Sources/Interactive/ArraySubSource.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { List } from 'enquirer';
|
||||||
|
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||||
|
|
||||||
|
export class ArraySubSource extends InteractiveSubSource {
|
||||||
|
protected condition ():boolean {
|
||||||
|
return this.val.type_validation.option_type === 'array';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async run ():Promise<void> {
|
||||||
|
await this.val.assign_arg (
|
||||||
|
this.opt,
|
||||||
|
await new List ({
|
||||||
|
message: this.get_message (),
|
||||||
|
default: this.opt.default
|
||||||
|
})
|
||||||
|
.run ()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
26
lib/Sources/Interactive/BooleanSubSource.ts
Normal file
26
lib/Sources/Interactive/BooleanSubSource.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Confirm } from 'enquirer';
|
||||||
|
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||||
|
|
||||||
|
export class BooleanSubSource extends InteractiveSubSource {
|
||||||
|
protected condition ():boolean {
|
||||||
|
return this.val.type_validation.option_type === 'boolean';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async run ():Promise<void> {
|
||||||
|
await this.val.assign_arg (
|
||||||
|
this.opt,
|
||||||
|
await new Confirm ({
|
||||||
|
message: this.get_message (),
|
||||||
|
default: this.opt.default
|
||||||
|
})
|
||||||
|
.run ()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
38
lib/Sources/Interactive/InteractiveSubSource.ts
Normal file
38
lib/Sources/Interactive/InteractiveSubSource.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { OptionValue, Option } from '../../Option';
|
||||||
|
|
||||||
|
export abstract class InteractiveSubSource {
|
||||||
|
protected val: OptionValue;
|
||||||
|
protected opt: Option;
|
||||||
|
|
||||||
|
protected abstract condition():boolean;
|
||||||
|
protected abstract async run():Promise<void>;
|
||||||
|
|
||||||
|
public constructor (
|
||||||
|
val:OptionValue,
|
||||||
|
opt:Option
|
||||||
|
) {
|
||||||
|
this.val = val;
|
||||||
|
this.opt = opt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async parse ():Promise<boolean> {
|
||||||
|
if (this.condition ()) {
|
||||||
|
await this.run ();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected get_message (): string {
|
||||||
|
return typeof this.opt.message === 'undefined'
|
||||||
|
? `input ${this.opt.name}`
|
||||||
|
: this.opt.message;
|
||||||
|
}
|
||||||
|
}
|
26
lib/Sources/Interactive/NumberSubSource.ts
Normal file
26
lib/Sources/Interactive/NumberSubSource.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { NumberPrompt } from 'enquirer';
|
||||||
|
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||||
|
|
||||||
|
export class NumberSubSource extends InteractiveSubSource {
|
||||||
|
protected condition ():boolean {
|
||||||
|
return this.val.type_validation.option_type === 'number';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async run ():Promise<void> {
|
||||||
|
await this.val.assign_arg (
|
||||||
|
this.opt,
|
||||||
|
await new NumberPrompt ({
|
||||||
|
message: this.get_message (),
|
||||||
|
default: this.opt.default
|
||||||
|
})
|
||||||
|
.run ()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
29
lib/Sources/Interactive/PresetSubSource.ts
Normal file
29
lib/Sources/Interactive/PresetSubSource.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { AutoComplete } from 'enquirer';
|
||||||
|
import { StringOptionConfig } from '../../SubConfigs';
|
||||||
|
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||||
|
|
||||||
|
export class PresetSubSource extends InteractiveSubSource {
|
||||||
|
protected condition ():boolean {
|
||||||
|
return typeof (this.opt as StringOptionConfig).preset !== 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async run ():Promise<void> {
|
||||||
|
await this.val.assign_arg (
|
||||||
|
this.opt,
|
||||||
|
await new AutoComplete ({
|
||||||
|
message: this.get_message (),
|
||||||
|
default: this.opt.default,
|
||||||
|
choices: (this.opt as StringOptionConfig).preset,
|
||||||
|
limit: 10
|
||||||
|
})
|
||||||
|
.run ()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
26
lib/Sources/Interactive/StringSubSource.ts
Normal file
26
lib/Sources/Interactive/StringSubSource.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Input } from 'enquirer';
|
||||||
|
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||||
|
|
||||||
|
export class StringSubSource extends InteractiveSubSource {
|
||||||
|
protected condition ():boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async run ():Promise<void> {
|
||||||
|
await this.val.assign_arg (
|
||||||
|
this.opt,
|
||||||
|
await new Input ({
|
||||||
|
message: this.get_message (),
|
||||||
|
default: this.opt.default
|
||||||
|
})
|
||||||
|
.run ()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
20
lib/Sources/Interactive/index.ts
Normal file
20
lib/Sources/Interactive/index.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ArraySubSource } from './ArraySubSource';
|
||||||
|
import { BooleanSubSource } from './BooleanSubSource';
|
||||||
|
import { PresetSubSource } from './PresetSubSource';
|
||||||
|
import { StringSubSource } from './StringSubSource';
|
||||||
|
import { NumberSubSource } from './NumberSubSource';
|
||||||
|
|
||||||
|
export const sources = [
|
||||||
|
ArraySubSource,
|
||||||
|
BooleanSubSource,
|
||||||
|
PresetSubSource,
|
||||||
|
NumberSubSource,
|
||||||
|
StringSubSource
|
||||||
|
];
|
@ -7,11 +7,10 @@
|
|||||||
|
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
/* eslint-disable no-process-exit */
|
/* eslint-disable no-process-exit */
|
||||||
import { Confirm, Input, List, AutoComplete } from 'enquirer';
|
|
||||||
import { ErrorCallback } from '../ErrorCallback';
|
import { ErrorCallback } from '../ErrorCallback';
|
||||||
import { Option, OptionValue } from '../Option';
|
import { Option, OptionValue } from '../Option';
|
||||||
import { StringOptionConfig } from '../SubConfigs';
|
|
||||||
import { OptionSource } from './OptionSource';
|
import { OptionSource } from './OptionSource';
|
||||||
|
import { sources } from './Interactive';
|
||||||
|
|
||||||
export class InteractiveSource extends OptionSource {
|
export class InteractiveSource extends OptionSource {
|
||||||
private _exit_on_interrupt: boolean;
|
private _exit_on_interrupt: boolean;
|
||||||
@ -24,62 +23,16 @@ export class InteractiveSource extends OptionSource {
|
|||||||
this._exit_on_interrupt = exit_on_interrupt;
|
this._exit_on_interrupt = exit_on_interrupt;
|
||||||
}
|
}
|
||||||
|
|
||||||
private get_message (opt: Option): string {
|
|
||||||
return typeof opt.message === 'undefined'
|
|
||||||
? `input ${opt.name}`
|
|
||||||
: opt.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async prompt (opt: Option, val:OptionValue): Promise<void> {
|
private async prompt (opt: Option, val:OptionValue): Promise<void> {
|
||||||
if (val.filled)
|
if (val.filled)
|
||||||
return;
|
return;
|
||||||
let value = null;
|
|
||||||
const { option_type } = val.type_validation;
|
|
||||||
const { preset } = opt as StringOptionConfig;
|
|
||||||
if (
|
|
||||||
option_type === 'string'
|
|
||||||
|| option_type === 'file'
|
|
||||||
|| option_type === 'folder'
|
|
||||||
|| option_type === 'path'
|
|
||||||
|| option_type === 'number'
|
|
||||||
) {
|
|
||||||
if (typeof preset === 'undefined') {
|
|
||||||
value = await new Input ({
|
|
||||||
message: this.get_message (opt),
|
|
||||||
default: opt.default
|
|
||||||
})
|
|
||||||
.run ();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
value = await new AutoComplete ({
|
|
||||||
message: this.get_message (opt),
|
|
||||||
default: opt.default,
|
|
||||||
choices: preset,
|
|
||||||
limit: 10
|
|
||||||
})
|
|
||||||
.run ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
option_type === 'boolean'
|
|
||||||
) {
|
|
||||||
value = await new Confirm ({
|
|
||||||
message: this.get_message (opt),
|
|
||||||
default: opt.default
|
|
||||||
})
|
|
||||||
.run ();
|
|
||||||
}
|
|
||||||
if (option_type === 'array') {
|
|
||||||
value = await new List ({
|
|
||||||
message: this.get_message (opt),
|
|
||||||
default: opt.default
|
|
||||||
})
|
|
||||||
.run ();
|
|
||||||
}
|
|
||||||
if (value === null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await this.assign_arg (opt, val, value);
|
for (const src of sources) {
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
if (await new src (val, opt)
|
||||||
|
.parse ())
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async parse (opt: Option, val:OptionValue): Promise<void> {
|
public async parse (opt: Option, val:OptionValue): Promise<void> {
|
||||||
|
@ -16,19 +16,4 @@ export abstract class OptionSource {
|
|||||||
public constructor (error_callback?: ErrorCallback) {
|
public constructor (error_callback?: ErrorCallback) {
|
||||||
this.error_callback = error_callback;
|
this.error_callback = error_callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async assign_arg (
|
|
||||||
opt: Option,
|
|
||||||
val: OptionValue,
|
|
||||||
value: unknown
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
val.value = await val.type_validation.to_type (value);
|
|
||||||
val.filled = true;
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
if (typeof this.error_callback !== 'undefined')
|
|
||||||
this.error_callback (opt.name, value, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
import { Option } from './Option';
|
import { Option } from './Option';
|
||||||
|
|
||||||
interface StringOptionConfig extends Option {
|
interface StringOptionConfig extends Option {
|
||||||
|
@ -20,14 +20,17 @@ export class PathType extends TypeValidation {
|
|||||||
public async to_type (value: unknown): Promise<unknown> {
|
public async to_type (value: unknown): Promise<unknown> {
|
||||||
if (typeof value !== 'string')
|
if (typeof value !== 'string')
|
||||||
throw new Error (`invalid type for ${this.option_type}`);
|
throw new Error (`invalid type for ${this.option_type}`);
|
||||||
if (!await fs.pathExists (value))
|
|
||||||
|
const escaped = value.replace (/\\$/u, '')
|
||||||
|
.replace (/"$/u, '');
|
||||||
|
if (!await fs.pathExists (escaped))
|
||||||
throw new Error ('path does not exist');
|
throw new Error ('path does not exist');
|
||||||
if (this.option_type === 'path')
|
if (this.option_type === 'path')
|
||||||
return value;
|
return escaped;
|
||||||
|
|
||||||
const stat = await fs.stat (value);
|
const stat = await fs.stat (escaped);
|
||||||
if (stat.isDirectory () === (this.option_type === 'folder'))
|
if (stat.isDirectory () === (this.option_type === 'folder'))
|
||||||
return value;
|
return escaped;
|
||||||
|
|
||||||
throw new Error ('cannot assign folder to file');
|
throw new Error ('cannot assign folder to file');
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,13 @@ export class TypeValidation {
|
|||||||
return Promise.resolve (String (value));
|
return Promise.resolve (String (value));
|
||||||
|
|
||||||
if (this.option_type === 'number') {
|
if (this.option_type === 'number') {
|
||||||
|
const as_num = parseFloat (String (value));
|
||||||
|
if (isNaN (as_num))
|
||||||
|
throw new Error ('value is not a number');
|
||||||
|
return Promise.resolve (as_num);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.option_type === 'int') {
|
||||||
const as_num = parseInt (String (value));
|
const as_num = parseInt (String (value));
|
||||||
if (isNaN (as_num))
|
if (isNaN (as_num))
|
||||||
throw new Error ('value is not a number');
|
throw new Error ('value is not a number');
|
||||||
|
@ -12,3 +12,4 @@ export { FolderOption } from './Options/FolderOption';
|
|||||||
export { NumberOption } from './Options/NumberOption';
|
export { NumberOption } from './Options/NumberOption';
|
||||||
export { PathOption } from './Options/PathOption';
|
export { PathOption } from './Options/PathOption';
|
||||||
export { StringOption } from './Options/StringOption';
|
export { StringOption } from './Options/StringOption';
|
||||||
|
export { IntegerOption } from './Options/IntegerOption';
|
||||||
|
37
package.json
37
package.json
@ -1,30 +1,35 @@
|
|||||||
{
|
{
|
||||||
"name": "@sapphirecode/console-app",
|
"name": "@sapphirecode/console-app",
|
||||||
"version": "1.0.0",
|
"version": "2.1.7",
|
||||||
"main": "dist/lib/index.js",
|
"main": "dist/lib/index.js",
|
||||||
"author": "Timo Hocker <timo@scode.ovh>",
|
"author": {
|
||||||
|
"name": "Timo Hocker",
|
||||||
|
"email": "timo@scode.ovh"
|
||||||
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git@git.scode.ovh:timo/console-app"
|
"url": "https://git.scode.ovh:timo/console-app.git"
|
||||||
},
|
},
|
||||||
"description": "read parameters from env, console args or interactively",
|
"bugs": "https://redmine.scode.ovh/projects/console-app",
|
||||||
|
"description": "read parameters from env, config files, console args or interactively",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@ava/typescript": "^1.1.1",
|
|
||||||
"@sapphirecode/eslint-config-ts": "^1.1.4",
|
"@sapphirecode/eslint-config-ts": "^1.1.4",
|
||||||
"@types/fs-extra": "^9.0.0",
|
"@types/fs-extra": "^9.0.0",
|
||||||
"@types/hjson": "^2.4.1",
|
"@types/hjson": "^2.4.1",
|
||||||
|
"@types/jasmine": "^3.5.14",
|
||||||
"@types/yargs": "^15.0.5",
|
"@types/yargs": "^15.0.5",
|
||||||
"ava": "^3.8.2",
|
|
||||||
"eslint": "^7.0.0",
|
"eslint": "^7.0.0",
|
||||||
|
"jasmine": "^3.6.1",
|
||||||
|
"jasmine-ts": "^0.3.0",
|
||||||
"nyc": "^15.0.1",
|
"nyc": "^15.0.1",
|
||||||
"typescript": "^3.9.2"
|
"ts-node": "^9.0.0",
|
||||||
|
"typescript": "^4.0.2"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
||||||
"test": "tsc && nyc ava",
|
"test": "nyc jasmine-ts --config=\"jasmine.json\"",
|
||||||
"compile": "tsc",
|
"compile": "tsc"
|
||||||
"ci": "yarn && node jenkins.js"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"LICENSE",
|
"LICENSE",
|
||||||
@ -38,5 +43,13 @@
|
|||||||
"fs-extra": "^9.0.0",
|
"fs-extra": "^9.0.0",
|
||||||
"hjson": "^3.2.1",
|
"hjson": "^3.2.1",
|
||||||
"yargs": "^15.3.1"
|
"yargs": "^15.3.1"
|
||||||
}
|
},
|
||||||
}
|
"keywords": [
|
||||||
|
"interactive",
|
||||||
|
"console input",
|
||||||
|
"config",
|
||||||
|
"command line args",
|
||||||
|
"environment variables",
|
||||||
|
"parsing"
|
||||||
|
]
|
||||||
|
}
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
||||||
* This file is part of console-app which is released under MIT.
|
|
||||||
* See file 'LICENSE' for full license details.
|
|
||||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
|
||||||
*/
|
|
||||||
|
|
||||||
import test from 'ava';
|
|
||||||
import { PathType } from '../lib/TypeValidation/PathType';
|
|
||||||
|
|
||||||
test ('no file', async (t) => {
|
|
||||||
const validator = new PathType ('file');
|
|
||||||
await t.throwsAsync (
|
|
||||||
() => validator.to_type ('test'),
|
|
||||||
{ message: 'cannot assign folder to file' }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
test ('file', async (t) => {
|
|
||||||
const validator = new PathType ('file');
|
|
||||||
const res = await validator.to_type ('package.json');
|
|
||||||
t.is (res, 'package.json');
|
|
||||||
});
|
|
||||||
test ('no folder', async (t) => {
|
|
||||||
const validator = new PathType ('folder');
|
|
||||||
await t.throwsAsync (
|
|
||||||
() => validator.to_type ('package.json'),
|
|
||||||
{ message: 'cannot assign folder to file' }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
test ('folder', async (t) => {
|
|
||||||
const validator = new PathType ('folder');
|
|
||||||
const res = await validator.to_type ('test');
|
|
||||||
t.is (res, 'test');
|
|
||||||
});
|
|
||||||
test ('no path', async (t) => {
|
|
||||||
const validator = new PathType ('path');
|
|
||||||
await t.throwsAsync (
|
|
||||||
() => validator.to_type ('doesnotexist.file'),
|
|
||||||
{ message: 'path does not exist' }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
test ('path', async (t) => {
|
|
||||||
const validator = new PathType ('path');
|
|
||||||
const res = await validator.to_type ('test');
|
|
||||||
t.is (res, 'test');
|
|
||||||
});
|
|
@ -1,85 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
||||||
* This file is part of console-app which is released under MIT.
|
|
||||||
* See file 'LICENSE' for full license details.
|
|
||||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
|
||||||
*/
|
|
||||||
|
|
||||||
import test from 'ava';
|
|
||||||
import { TypeValidation } from '../lib/TypeValidation/TypeValidation';
|
|
||||||
|
|
||||||
test ('string', async (t) => {
|
|
||||||
const validator = new TypeValidation ('string');
|
|
||||||
const res = await validator.to_type ('foo');
|
|
||||||
t.is (res, 'foo');
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('no number', (t) => {
|
|
||||||
const validator = new TypeValidation ('number');
|
|
||||||
t.throws (
|
|
||||||
() => validator.to_type ('foo'),
|
|
||||||
{ message: 'value is not a number' }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('number', async (t) => {
|
|
||||||
const validator = new TypeValidation ('number');
|
|
||||||
const res = await validator.to_type ('123');
|
|
||||||
t.is (res, 123);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('no boolean', (t) => {
|
|
||||||
const validator = new TypeValidation ('boolean');
|
|
||||||
t.throws (
|
|
||||||
() => validator.to_type ('foo'),
|
|
||||||
{ message: 'value is not a boolean' }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('boolean', async (t) => {
|
|
||||||
const validator = new TypeValidation ('boolean');
|
|
||||||
const r1 = await validator.to_type ('false');
|
|
||||||
const r2 = await validator.to_type ('true');
|
|
||||||
t.is (r1, false);
|
|
||||||
t.is (r2, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('boolean number', async (t) => {
|
|
||||||
const validator = new TypeValidation ('boolean');
|
|
||||||
const r1 = await validator.to_type (0);
|
|
||||||
const r2 = await validator.to_type (1);
|
|
||||||
t.is (r1, false);
|
|
||||||
t.is (r2, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('no array', (t) => {
|
|
||||||
const validator = new TypeValidation ('array');
|
|
||||||
t.throws (
|
|
||||||
() => validator.to_type (1),
|
|
||||||
{ message: 'value is not an array' }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('array', async (t) => {
|
|
||||||
const validator = new TypeValidation ('array');
|
|
||||||
const res = await validator.to_type ([
|
|
||||||
'foo',
|
|
||||||
'bar',
|
|
||||||
'baz'
|
|
||||||
]);
|
|
||||||
t.deepEqual (res, [
|
|
||||||
'foo',
|
|
||||||
'bar',
|
|
||||||
'baz'
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test ('string array', async (t) => {
|
|
||||||
const validator = new TypeValidation ('array');
|
|
||||||
const res = await validator.to_type ('f o o,bar , baz');
|
|
||||||
t.deepEqual (res, [
|
|
||||||
'f o o',
|
|
||||||
'bar',
|
|
||||||
'baz'
|
|
||||||
]);
|
|
||||||
});
|
|
46
test/spec/paths.ts
Normal file
46
test/spec/paths.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { PathType } from '../../lib/TypeValidation/PathType';
|
||||||
|
|
||||||
|
describe ('paths', () => {
|
||||||
|
it ('no file', async () => {
|
||||||
|
const validator = new PathType ('file');
|
||||||
|
await expectAsync (
|
||||||
|
validator.to_type ('test')
|
||||||
|
)
|
||||||
|
.toBeRejectedWithError ('cannot assign folder to file');
|
||||||
|
});
|
||||||
|
it ('file', async () => {
|
||||||
|
const validator = new PathType ('file');
|
||||||
|
const res = await validator.to_type ('package.json');
|
||||||
|
expect (res)
|
||||||
|
.toEqual ('package.json');
|
||||||
|
});
|
||||||
|
it ('no folder', async () => {
|
||||||
|
const validator = new PathType ('folder');
|
||||||
|
await expectAsync (validator.to_type ('package.json'))
|
||||||
|
.toBeRejectedWithError ('cannot assign folder to file');
|
||||||
|
});
|
||||||
|
it ('folder', async () => {
|
||||||
|
const validator = new PathType ('folder');
|
||||||
|
const res = await validator.to_type ('test');
|
||||||
|
expect (res)
|
||||||
|
.toEqual ('test');
|
||||||
|
});
|
||||||
|
it ('no path', async () => {
|
||||||
|
const validator = new PathType ('path');
|
||||||
|
await expectAsync (validator.to_type ('doesnotexist.file'))
|
||||||
|
.toBeRejectedWithError ('path does not exist');
|
||||||
|
});
|
||||||
|
it ('path', async () => {
|
||||||
|
const validator = new PathType ('path');
|
||||||
|
const res = await validator.to_type ('test');
|
||||||
|
expect (res)
|
||||||
|
.toEqual ('test');
|
||||||
|
});
|
||||||
|
});
|
102
test/spec/types.ts
Normal file
102
test/spec/types.ts
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
|
* This file is part of console-app which is released under MIT.
|
||||||
|
* See file 'LICENSE' for full license details.
|
||||||
|
* Created by Timo Hocker <timo@scode.ovh>, October 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TypeValidation } from '../../lib/TypeValidation/TypeValidation';
|
||||||
|
|
||||||
|
// eslint-disable-next-line max-lines-per-function
|
||||||
|
describe ('type validation', () => {
|
||||||
|
it ('string', async () => {
|
||||||
|
const validator = new TypeValidation ('string');
|
||||||
|
const res = await validator.to_type ('foo');
|
||||||
|
expect (res)
|
||||||
|
.toEqual ('foo');
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('no number', () => {
|
||||||
|
const validator = new TypeValidation ('number');
|
||||||
|
expect (
|
||||||
|
() => validator.to_type ('foo')
|
||||||
|
)
|
||||||
|
.toThrowError ('value is not a number');
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('number', async () => {
|
||||||
|
const validator = new TypeValidation ('number');
|
||||||
|
const res = await validator.to_type ('123.4');
|
||||||
|
expect (res)
|
||||||
|
.toEqual (123.4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('int', async () => {
|
||||||
|
const validator = new TypeValidation ('int');
|
||||||
|
const res = await validator.to_type ('123.4');
|
||||||
|
expect (res)
|
||||||
|
.toEqual (123);
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('no boolean', () => {
|
||||||
|
const validator = new TypeValidation ('boolean');
|
||||||
|
expect (
|
||||||
|
() => validator.to_type ('foo')
|
||||||
|
)
|
||||||
|
.toThrowError ('value is not a boolean');
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('boolean', async () => {
|
||||||
|
const validator = new TypeValidation ('boolean');
|
||||||
|
const r1 = await validator.to_type ('false');
|
||||||
|
const r2 = await validator.to_type ('true');
|
||||||
|
expect (r1)
|
||||||
|
.toEqual (false);
|
||||||
|
expect (r2)
|
||||||
|
.toEqual (true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('boolean number', async () => {
|
||||||
|
const validator = new TypeValidation ('boolean');
|
||||||
|
const r1 = await validator.to_type (0);
|
||||||
|
const r2 = await validator.to_type (1);
|
||||||
|
expect (r1)
|
||||||
|
.toEqual (false);
|
||||||
|
expect (r2)
|
||||||
|
.toEqual (true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('no array', () => {
|
||||||
|
const validator = new TypeValidation ('array');
|
||||||
|
expect (
|
||||||
|
() => validator.to_type (1)
|
||||||
|
)
|
||||||
|
.toThrowError ('value is not an array');
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('array', async () => {
|
||||||
|
const validator = new TypeValidation ('array');
|
||||||
|
const res = await validator.to_type ([
|
||||||
|
'foo',
|
||||||
|
'bar',
|
||||||
|
'baz'
|
||||||
|
]);
|
||||||
|
expect (res)
|
||||||
|
.toEqual ([
|
||||||
|
'foo',
|
||||||
|
'bar',
|
||||||
|
'baz'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it ('string array', async () => {
|
||||||
|
const validator = new TypeValidation ('array');
|
||||||
|
const res = await validator.to_type ('f o o,bar , baz');
|
||||||
|
expect (res)
|
||||||
|
.toEqual ([
|
||||||
|
'f o o',
|
||||||
|
'bar',
|
||||||
|
'baz'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
3464
yarn-error.log
3464
yarn-error.log
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user