Compare commits

...

21 Commits
dev ... master

Author SHA1 Message Date
d9de76b188 use jasmine
All checks were successful
continuous-integration/drone/push Build is passing
2020-10-03 15:14:14 +02:00
b81b77b924 update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
eslint: 7.7.0 ==> 7.8.1 minor
2020-09-07 13:34:32 +02:00
ee6be8317d update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
@sapphirecode/modelling: 1.1.12 ==> 1.1.13 minor
ava: 3.11.1 ==> 3.12.1 minor
2020-08-30 15:57:26 +02:00
390cc10305 upgrade typescript
All checks were successful
continuous-integration/drone/push Build is passing
2020-08-21 13:16:34 +02:00
e1507edd5d update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
eslint: 7.6.0 ==> 7.7.0 minor
2020-08-19 08:26:00 +02:00
7268ed1643 fix drone config
Some checks failed
continuous-integration/drone/push Build is failing
2020-08-07 08:11:31 +02:00
04aa24a75d update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
ava: 3.10.1 ==> 3.11.1 minor
eslint: 7.5.0 ==> 7.6.0 minor
2020-08-04 12:57:31 +02:00
8eb8deb855 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.21 ==> 1.1.22 minor
@sapphirecode/modelling: 1.1.11 ==> 1.1.12 minor
2020-07-25 16:51:48 +02:00
c0eb113036 fix double quote escape in path selector
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-24 08:33:58 +02:00
6207de8e5b fix backslash breaking path selection
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-24 08:18:36 +02:00
7521fb310c update description
Some checks failed
continuous-integration/drone/push Build is failing
2020-07-24 07:45:46 +02:00
125fb7b5e6 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.20 ==> 1.1.21 minor
@sapphirecode/modelling: 1.1.10 ==> 1.1.11 minor
@sapphirecode/utilities: 1.8.4 ==> 1.8.5 minor
2020-07-19 15:08:47 +02:00
8f040c38eb update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
eslint: 7.4.0 ==> 7.5.0 minor
typescript: 3.9.6 ==> 3.9.7 minor
2020-07-19 12:12:54 +02:00
7ad999878a fix for number input, new integer input
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-19 11:41:06 +02:00
7395241329 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.19 ==> 1.1.20 minor
@sapphirecode/modelling: 1.1.9 ==> 1.1.10 minor
yargs: 15.4.0 ==> 15.4.1 minor
2020-07-16 10:52:47 +02:00
ed2d0ba047 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.17 ==> 1.1.19 minor
@sapphirecode/modelling: 1.1.7 ==> 1.1.9 minor
@sapphirecode/utilities: 1.7.2 ==> 1.8.4 minor
ava: 3.9.0 ==> 3.10.1 minor
enquirer: 2.3.5 ==> 2.3.6 minor
eslint: 7.3.1 ==> 7.4.0 minor
typescript: 3.9.5 ==> 3.9.6 minor
yargs: 15.3.1 ==> 15.4.0 minor
2020-07-10 12:33:41 +02:00
78c09809e6 fix
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-10 08:40:46 +02:00
85b02b552f fix
Some checks failed
continuous-integration/drone/push Build is failing
2020-07-10 08:38:58 +02:00
3e178b28ae switch to drone
Some checks failed
continuous-integration/drone/push Build is failing
2020-07-10 08:36:54 +02:00
f5ee4665dc update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.16 ==> 1.1.17 minor
@sapphirecode/modelling: 1.1.5 ==> 1.1.7 minor
@sapphirecode/utilities: 1.4.5 ==> 1.7.2 minor
2020-07-01 09:32:53 +02:00
fe4057d0b5 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.14 ==> 1.1.16 minor
@sapphirecode/modelling: 1.1.4 ==> 1.1.5 minor
@sapphirecode/utilities: 1.4.4 ==> 1.4.5 minor
@types/hjson: 2.4.1 ==> 2.4.2 minor
eslint: 7.3.0 ==> 7.3.1 minor
2020-06-24 12:51:58 +02:00
36 changed files with 1654 additions and 5008 deletions

14
.drone.yml Normal file
View 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

View File

@ -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);
}) (); }) ();

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
## 2.1.0
- Fix for NumberOption: do not cut off float values
- New type IntegerOption: only allows integer values
## 2.0.0 ## 2.0.0
Restructuring to split different Option types and keep specific parameters separate Restructuring to split different Option types and keep specific parameters separate

23
Jenkinsfile vendored
View File

@ -1,23 +0,0 @@
pipeline {
agent any
environment {
VERSION = VersionNumber([
versionNumberString:
'${BUILDS_ALL_TIME}',
versionPrefix: '2.0.',
worstResultForIncrement: 'SUCCESS'
])
}
stages {
stage('Building') {
steps {
script {
currentBuild.displayName = env.VERSION
}
sh 'yarn ci ${VERSION}'
}
}
}
}

View File

@ -1,6 +1,6 @@
# @sapphirecode/console-app # @sapphirecode/console-app
version: 2.0.x version: 2.1.x
read parameters from env, config files, console args or interactively read parameters from env, config files, console args or interactively
@ -22,7 +22,8 @@ const {
BooleanOption, BooleanOption,
FileOption, // paths that exist and are a file FileOption, // paths that exist and are a file
FolderOption, // paths that exist and are a folder FolderOption, // paths that exist and are a folder
NumberOption, NumberOption, // integer and float values
IntegerOption, // only integer values
PathOption, // paths that exist in the file system PathOption, // paths that exist in the file system
StringOption, StringOption,
} = require('@sapphirecode/console-app'); } = require('@sapphirecode/console-app');

14
jasmine.json Normal file
View File

@ -0,0 +1,14 @@
{
"spec_dir": "test",
"spec_files": [
"spec/*.js",
"spec/*.ts"
],
"helpers": [
"helpers/*.js",
"helpers/*.ts"
],
"stopSpecOnExpectationFailure": false,
"random": false
}

View File

@ -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);
}
);

View 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,

View File

@ -8,6 +8,7 @@
export type OptionType = export type OptionType =
'string' 'string'
| 'number' | 'number'
| 'int'
| 'boolean' | 'boolean'
| 'file' | 'file'
| 'folder' | 'folder'

View 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
*/
import { TypeValidation } from '../TypeValidation/TypeValidation'; import { TypeValidation } from '../TypeValidation/TypeValidation';
import { BaseOption } from './BaseOption'; import { BaseOption } from './BaseOption';

View 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
*/
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';

View 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
*/
import { TypeValidation } from '../TypeValidation/TypeValidation'; import { TypeValidation } from '../TypeValidation/TypeValidation';
import { BaseOption } from './BaseOption'; import { BaseOption } from './BaseOption';

View 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
*/
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';

View 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
*/
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';

View 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');
}
}

View 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
*/
import { TypeValidation } from '../TypeValidation/TypeValidation'; import { TypeValidation } from '../TypeValidation/TypeValidation';
import { BaseOption } from './BaseOption'; import { BaseOption } from './BaseOption';

View 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
*/
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';

View 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
*/
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';

View 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
*/
import { List } from 'enquirer'; import { List } from 'enquirer';
import { InteractiveSubSource } from './InteractiveSubSource'; import { InteractiveSubSource } from './InteractiveSubSource';

View 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
*/
import { Confirm } from 'enquirer'; import { Confirm } from 'enquirer';
import { InteractiveSubSource } from './InteractiveSubSource'; import { InteractiveSubSource } from './InteractiveSubSource';

View 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
*/
import { OptionValue, Option } from '../../Option'; import { OptionValue, Option } from '../../Option';
export abstract class InteractiveSubSource { export abstract class InteractiveSubSource {

View 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 ()
);
}
}

View 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
*/
import { AutoComplete } from 'enquirer'; import { AutoComplete } from 'enquirer';
import { StringOptionConfig } from '../../SubConfigs'; import { StringOptionConfig } from '../../SubConfigs';
import { InteractiveSubSource } from './InteractiveSubSource'; import { InteractiveSubSource } from './InteractiveSubSource';

View 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
*/
import { Input } from 'enquirer'; import { Input } from 'enquirer';
import { InteractiveSubSource } from './InteractiveSubSource'; import { InteractiveSubSource } from './InteractiveSubSource';

View File

@ -1,11 +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 { ArraySubSource } from './ArraySubSource';
import { BooleanSubSource } from './BooleanSubSource'; import { BooleanSubSource } from './BooleanSubSource';
import { PresetSubSource } from './PresetSubSource'; import { PresetSubSource } from './PresetSubSource';
import { StringSubSource } from './StringSubSource'; import { StringSubSource } from './StringSubSource';
import { NumberSubSource } from './NumberSubSource';
export const sources = [ export const sources = [
ArraySubSource, ArraySubSource,
BooleanSubSource, BooleanSubSource,
PresetSubSource, PresetSubSource,
NumberSubSource,
StringSubSource StringSubSource
]; ];

View 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
*/
import { Option } from './Option'; import { Option } from './Option';
interface StringOptionConfig extends Option { interface StringOptionConfig extends Option {

View File

@ -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');
} }

View 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');

View File

@ -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';

View File

@ -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"
]
}

View File

@ -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');
});

View File

@ -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
View 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
View 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'
]);
});
});

File diff suppressed because it is too large Load Diff

2609
yarn.lock

File diff suppressed because it is too large Load Diff