Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import { extendType, string } from '..';
import fs from 'fs';
import path from 'path';
/**
* Resolves an existing path. Produces an error when path does not exist.
* When provided a relative path, extends it using the current working directory.
*/
export const ExistingPath = extendType(string, {
displayName: 'path',
description: 'An existing path',
async from(str) {
const resolved = path.resolve(str);
if (!fs.existsSync(resolved)) {
throw new Error("Path doesn't exist");
}
return resolved;
},
});
/**
* Resolves to a directory if given one, and to a file's directory if file was given.
* Fails when the directory or the given file does not exist.
*/
export const Directory = extendType(ExistingPath, {
async from(resolved) {
const stat = fs.statSync(resolved);
if (stat.isDirectory()) {
return resolved;
}
return path.dirname(resolved);
},
displayName: 'dir',
description: 'A path to a directory or a file within a directory',
});
/**
* Resolves to a path to an existing file
*/
export const File = extendType(ExistingPath, {
async from(resolved) {
const stat = fs.statSync(resolved);
if (stat.isFile()) {
return resolved;
}
throw new Error('Provided path is not a file');
},
displayName: 'file',
description: 'A file in the file system',
});