2020-05-09 19:51:43 +02:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import { TypeValidation } from './TypeValidation';
|
|
|
|
|
|
|
|
export class PathType extends TypeValidation {
|
2020-05-09 21:30:37 +02:00
|
|
|
public get string_type (): 'string'|'number'|'boolean'|'object' {
|
2020-05-09 19:51:43 +02:00
|
|
|
return 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
public async to_type (value: unknown): Promise<unknown> {
|
|
|
|
if (typeof value !== 'string')
|
2020-05-09 21:30:37 +02:00
|
|
|
throw new Error (`invalid type for ${this.option_type}`);
|
2020-05-09 19:51:43 +02:00
|
|
|
if (!await fs.pathExists (value))
|
|
|
|
throw new Error ('path does not exist');
|
2020-05-09 21:30:37 +02:00
|
|
|
if (this.option_type === 'path')
|
2020-05-09 19:51:43 +02:00
|
|
|
return value;
|
|
|
|
|
|
|
|
const stat = await fs.stat (value);
|
2020-05-09 21:30:37 +02:00
|
|
|
if (stat.isDirectory () === (this.option_type === 'folder'))
|
2020-05-09 19:51:43 +02:00
|
|
|
return value;
|
|
|
|
|
|
|
|
throw new Error ('cannot assign folder to file');
|
|
|
|
}
|
|
|
|
}
|