@autobe/filesystem
Version:
AI backend server code generator
60 lines • 3.16 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSystemIterator = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const tstl_1 = require("tstl");
/** @internal */
var FileSystemIterator;
(function (FileSystemIterator) {
FileSystemIterator.read = (props) => __awaiter(this, void 0, void 0, function* () {
const output = {};
const iterate = (location) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const directory = yield fs_1.default.promises.readdir(location);
for (const file of directory) {
const next = `${location}/${file}`;
const stat = yield fs_1.default.promises.stat(next);
if (stat.isDirectory())
yield iterate(next);
else if (!((_a = props.extension) === null || _a === void 0 ? void 0 : _a.length) ||
file.endsWith(`.${props.extension}`))
output[`${(_b = props.prefix) !== null && _b !== void 0 ? _b : ""}${next.substring(props.root.length + 1)}`] = yield fs_1.default.promises.readFile(next, "utf-8");
}
});
yield iterate(props.root);
return output;
});
FileSystemIterator.save = (props) => __awaiter(this, void 0, void 0, function* () {
if (props.overwrite !== true && fs_1.default.existsSync(props.root))
yield fs_1.default.promises.rm(props.root, {
recursive: true,
});
const directory = new tstl_1.VariadicSingleton((location) => __awaiter(this, void 0, void 0, function* () {
try {
yield fs_1.default.promises.mkdir(location, {
recursive: true,
});
}
catch (_a) { }
}));
for (const [key, value] of Object.entries(props.files)) {
const file = path_1.default.resolve(`${props.root}/${key}`);
yield directory.get(path_1.default.dirname(file));
yield fs_1.default.promises.writeFile(file, value !== null && value !== void 0 ? value : "", "utf8");
}
});
})(FileSystemIterator || (exports.FileSystemIterator = FileSystemIterator = {}));
//# sourceMappingURL=FileSystemIterator.js.map
;