archieve-projects/微信机器人/node_modules/cmd-ts/dist/cjs/batteries/fs.js

54 lines
1.8 KiB
JavaScript
Executable File

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.File = exports.Directory = exports.ExistingPath = void 0;
const __1 = require("..");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("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.
*/
exports.ExistingPath = __1.extendType(__1.string, {
displayName: 'path',
description: 'An existing path',
async from(str) {
const resolved = path_1.default.resolve(str);
if (!fs_1.default.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.
*/
exports.Directory = __1.extendType(exports.ExistingPath, {
async from(resolved) {
const stat = fs_1.default.statSync(resolved);
if (stat.isDirectory()) {
return resolved;
}
return path_1.default.dirname(resolved);
},
displayName: 'dir',
description: 'A path to a directory or a file within a directory',
});
/**
* Resolves to a path to an existing file
*/
exports.File = __1.extendType(exports.ExistingPath, {
async from(resolved) {
const stat = fs_1.default.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',
});
//# sourceMappingURL=fs.js.map