Compare commits
1 Commits
master
...
f372e1ea17
Author | SHA1 | Date | |
---|---|---|---|
f372e1ea17 |
14
.drone.yml
14
.drone.yml
@ -1,14 +0,0 @@
|
||||
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
|
26
AppTest.js
26
AppTest.js
@ -8,7 +8,6 @@
|
||||
// @ts-nocheck
|
||||
/* eslint-disable no-console */
|
||||
/* eslint-disable id-match */
|
||||
/* eslint-disable node/no-missing-require */
|
||||
|
||||
'use strict';
|
||||
|
||||
@ -17,25 +16,24 @@ const {
|
||||
BooleanOption,
|
||||
NumberOption,
|
||||
ArrayOption,
|
||||
FolderOption,
|
||||
IntegerOption
|
||||
FolderOption
|
||||
} = require ('./dist/lib/index.js');
|
||||
|
||||
(async () => {
|
||||
const str = await new StringOption ({ name: 'str' })
|
||||
.parse ();
|
||||
const bool = await new BooleanOption ({ name: 'bool' })
|
||||
.parse ();
|
||||
const num = await new NumberOption ({ name: 'num' })
|
||||
.parse ();
|
||||
const int = await new IntegerOption ({ name: 'num' })
|
||||
.parse ();
|
||||
const arr = await new ArrayOption ({ name: 'arr' })
|
||||
.parse ();
|
||||
/*
|
||||
* const str = await new StringOption ({ name: 'str' })
|
||||
*.parse ();
|
||||
*const bool = await new BooleanOption ({ name: 'bool' })
|
||||
*.parse ();
|
||||
*const num = await new NumberOption ({ name: 'num' })
|
||||
*.parse ();
|
||||
*const arr = await new ArrayOption ({ name: 'arr' })
|
||||
*.parse ();
|
||||
*/
|
||||
const fld = await new FolderOption ({ name: 'fld' })
|
||||
.parse ();
|
||||
|
||||
const data = { str, bool, num, int, arr, fld };
|
||||
const data = { str, bool, num, arr, fld };
|
||||
|
||||
console.log (data);
|
||||
}) ();
|
||||
|
@ -1,10 +1,5 @@
|
||||
# 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
|
||||
|
23
Jenkinsfile
vendored
Normal file
23
Jenkinsfile
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
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}'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
# @sapphirecode/console-app
|
||||
|
||||
version: 2.1.x
|
||||
version: 2.0.x
|
||||
|
||||
read parameters from env, config files, console args or interactively
|
||||
|
||||
@ -22,8 +22,7 @@ const {
|
||||
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
|
||||
NumberOption,
|
||||
PathOption, // paths that exist in the file system
|
||||
StringOption,
|
||||
} = require('@sapphirecode/console-app');
|
||||
|
14
jasmine.json
14
jasmine.json
@ -1,14 +0,0 @@
|
||||
|
||||
{
|
||||
"spec_dir": "test",
|
||||
"spec_files": [
|
||||
"spec/*.js",
|
||||
"spec/*.ts"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/*.js",
|
||||
"helpers/*.ts"
|
||||
],
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": false
|
||||
}
|
29
jenkins.js
Normal file
29
jenkins.js
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>, 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,10 +1,3 @@
|
||||
/*
|
||||
* 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 = (
|
||||
option: string,
|
||||
value: unknown,
|
||||
|
@ -8,7 +8,6 @@
|
||||
export type OptionType =
|
||||
'string'
|
||||
| 'number'
|
||||
| 'int'
|
||||
| 'boolean'
|
||||
| 'file'
|
||||
| 'folder'
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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 { Option, OptionValue } from '../Option';
|
||||
import { EnvSource } from '../Sources/EnvSource';
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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 { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||
import { StringOption } from './StringOption';
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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 { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||
import { StringOption } from './StringOption';
|
||||
|
@ -1,15 +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>, 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,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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 { TypeValidation } from '../TypeValidation/TypeValidation';
|
||||
import { StringOption } from './StringOption';
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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 { StringOptionConfig } from '../SubConfigs';
|
||||
import { BaseOption } from './BaseOption';
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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 {
|
||||
@ -24,7 +17,10 @@ export abstract class InteractiveSubSource {
|
||||
|
||||
public async parse ():Promise<boolean> {
|
||||
if (this.condition ()) {
|
||||
await this.run ();
|
||||
await this.run ()
|
||||
.catch ((e) => {
|
||||
console.log (e);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -1,26 +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>, 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 ()
|
||||
);
|
||||
}
|
||||
}
|
25
lib/Sources/Interactive/PathCustomPrompt.ts
Normal file
25
lib/Sources/Interactive/PathCustomPrompt.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Prompt } from 'enquirer';
|
||||
|
||||
export class PathPrompt extends Prompt {
|
||||
private _index = 0;
|
||||
private _value = '';
|
||||
|
||||
public constructor (options:Record<string, unknown> = {}) {
|
||||
super (options);
|
||||
}
|
||||
|
||||
public up (): void {
|
||||
this._index--;
|
||||
this.render ();
|
||||
}
|
||||
|
||||
public down (): void {
|
||||
this._index++;
|
||||
this.render ();
|
||||
}
|
||||
|
||||
public render (): void {
|
||||
this.clear ();
|
||||
this.write (`${this.state.message}: ${this._value}`);
|
||||
}
|
||||
}
|
23
lib/Sources/Interactive/PathSubSource.ts
Normal file
23
lib/Sources/Interactive/PathSubSource.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||
import { PathPrompt } from './PathCustomPrompt';
|
||||
|
||||
export class PathSubSource extends InteractiveSubSource {
|
||||
protected condition ():boolean {
|
||||
return [
|
||||
'path',
|
||||
'file',
|
||||
'folder'
|
||||
].includes (this.val.type_validation.option_type);
|
||||
}
|
||||
|
||||
protected async run (): Promise<void> {
|
||||
await this.val.assign_arg (
|
||||
this.opt,
|
||||
await new PathPrompt ({
|
||||
message: this.get_message (),
|
||||
default: this.opt.default
|
||||
})
|
||||
.run ()
|
||||
);
|
||||
}
|
||||
}
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
|
@ -1,20 +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 { ArraySubSource } from './ArraySubSource';
|
||||
import { BooleanSubSource } from './BooleanSubSource';
|
||||
import { PresetSubSource } from './PresetSubSource';
|
||||
import { StringSubSource } from './StringSubSource';
|
||||
import { NumberSubSource } from './NumberSubSource';
|
||||
import { PathSubSource } from './PathSubSource';
|
||||
|
||||
export const sources = [
|
||||
ArraySubSource,
|
||||
BooleanSubSource,
|
||||
PresetSubSource,
|
||||
NumberSubSource,
|
||||
PathSubSource,
|
||||
StringSubSource
|
||||
];
|
||||
|
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* 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';
|
||||
|
||||
interface StringOptionConfig extends Option {
|
||||
|
@ -20,17 +20,14 @@ export class PathType extends TypeValidation {
|
||||
public async to_type (value: unknown): Promise<unknown> {
|
||||
if (typeof value !== 'string')
|
||||
throw new Error (`invalid type for ${this.option_type}`);
|
||||
|
||||
const escaped = value.replace (/\\$/u, '')
|
||||
.replace (/"$/u, '');
|
||||
if (!await fs.pathExists (escaped))
|
||||
if (!await fs.pathExists (value))
|
||||
throw new Error ('path does not exist');
|
||||
if (this.option_type === 'path')
|
||||
return escaped;
|
||||
return value;
|
||||
|
||||
const stat = await fs.stat (escaped);
|
||||
const stat = await fs.stat (value);
|
||||
if (stat.isDirectory () === (this.option_type === 'folder'))
|
||||
return escaped;
|
||||
return value;
|
||||
|
||||
throw new Error ('cannot assign folder to file');
|
||||
}
|
||||
|
@ -40,13 +40,6 @@ export class TypeValidation {
|
||||
return Promise.resolve (String (value));
|
||||
|
||||
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));
|
||||
if (isNaN (as_num))
|
||||
throw new Error ('value is not a number');
|
||||
|
@ -12,4 +12,3 @@ export { FolderOption } from './Options/FolderOption';
|
||||
export { NumberOption } from './Options/NumberOption';
|
||||
export { PathOption } from './Options/PathOption';
|
||||
export { StringOption } from './Options/StringOption';
|
||||
export { IntegerOption } from './Options/IntegerOption';
|
||||
|
37
package.json
37
package.json
@ -1,35 +1,30 @@
|
||||
{
|
||||
"name": "@sapphirecode/console-app",
|
||||
"version": "2.1.7",
|
||||
"version": "1.0.0",
|
||||
"main": "dist/lib/index.js",
|
||||
"author": {
|
||||
"name": "Timo Hocker",
|
||||
"email": "timo@scode.ovh"
|
||||
},
|
||||
"author": "Timo Hocker <timo@scode.ovh>",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.scode.ovh:timo/console-app.git"
|
||||
"url": "git@git.scode.ovh:timo/console-app"
|
||||
},
|
||||
"bugs": "https://redmine.scode.ovh/projects/console-app",
|
||||
"description": "read parameters from env, config files, console args or interactively",
|
||||
"description": "read parameters from env, console args or interactively",
|
||||
"devDependencies": {
|
||||
"@ava/typescript": "^1.1.1",
|
||||
"@sapphirecode/eslint-config-ts": "^1.1.4",
|
||||
"@types/fs-extra": "^9.0.0",
|
||||
"@types/hjson": "^2.4.1",
|
||||
"@types/jasmine": "^3.5.14",
|
||||
"@types/yargs": "^15.0.5",
|
||||
"ava": "^3.8.2",
|
||||
"eslint": "^7.0.0",
|
||||
"jasmine": "^3.6.1",
|
||||
"jasmine-ts": "^0.3.0",
|
||||
"nyc": "^15.0.1",
|
||||
"ts-node": "^9.0.0",
|
||||
"typescript": "^4.0.2"
|
||||
"typescript": "^3.9.2"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
||||
"test": "nyc jasmine-ts --config=\"jasmine.json\"",
|
||||
"compile": "tsc"
|
||||
"test": "tsc && nyc ava",
|
||||
"compile": "tsc",
|
||||
"ci": "yarn && node jenkins.js"
|
||||
},
|
||||
"files": [
|
||||
"LICENSE",
|
||||
@ -43,13 +38,5 @@
|
||||
"fs-extra": "^9.0.0",
|
||||
"hjson": "^3.2.1",
|
||||
"yargs": "^15.3.1"
|
||||
},
|
||||
"keywords": [
|
||||
"interactive",
|
||||
"console input",
|
||||
"config",
|
||||
"command line args",
|
||||
"environment variables",
|
||||
"parsing"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
46
test/PathType.ts
Normal file
46
test/PathType.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>, 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');
|
||||
});
|
85
test/TypeValidation.ts
Normal file
85
test/TypeValidation.ts
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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'
|
||||
]);
|
||||
});
|
@ -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>, 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');
|
||||
});
|
||||
});
|
@ -1,102 +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>, 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'
|
||||
]);
|
||||
});
|
||||
});
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./",
|
||||
|
3464
yarn-error.log
Normal file
3464
yarn-error.log
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user