Timo Hocker c0eb113036
All checks were successful
continuous-integration/drone/push Build is passing
fix double quote escape in path selector
2020-07-24 08:33:58 +02:00

38 lines
1.1 KiB
TypeScript

/*
* 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 fs from 'fs-extra';
import { TypeValidation } from './TypeValidation';
export class PathType extends TypeValidation {
public get string_type (): 'string'|'number'|'boolean'|'object' {
return 'string';
}
public get persistent_type (): 'string'|'number'|'boolean'|'array' {
return 'string';
}
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))
throw new Error ('path does not exist');
if (this.option_type === 'path')
return escaped;
const stat = await fs.stat (escaped);
if (stat.isDirectory () === (this.option_type === 'folder'))
return escaped;
throw new Error ('cannot assign folder to file');
}
}