@tsed/cli-core
Version:
Build your CLI with TypeScript and Decorators
75 lines (74 loc) • 2.56 kB
JavaScript
import { join } from "node:path";
import { RealFileSystemHost } from "@ts-morph/common";
import { injectable } from "@tsed/di";
import { normalizePath } from "@tsed/normalize-path";
import Fs, {} from "fs-extra";
export class CliFs extends RealFileSystemHost {
constructor() {
super(...arguments);
this.raw = Fs;
}
/**
* @deprecated
* @param path
*/
exists(path) {
return this.raw.existsSync(path);
}
join(...args) {
return normalizePath(join(...args));
}
readFile(file, encoding) {
return super.readFile(file, encoding);
}
readFileSync(file, encoding) {
return super.readFileSync(file, encoding);
}
async readJson(file, encoding) {
const content = await this.readFile(file, encoding);
return JSON.parse(content);
}
readJsonSync(file, encoding) {
const content = this.readFileSync(file, encoding);
return JSON.parse(content);
}
async writeJson(file, data, options) {
await this.raw.writeFile(file, JSON.stringify(data, null, 2), options || { encoding: "utf8" });
}
writeJsonSync(file, data, options) {
this.raw.writeFileSync(file, JSON.stringify(data, null, 2), options || { encoding: "utf8" });
}
writeFileSync(path, data, options) {
return this.raw.writeFileSync(path, data, options);
}
writeFile(file, data, options) {
return this.raw.writeFile(file, data, options);
}
ensureDir(path, options) {
return this.raw.ensureDir(path, options);
}
ensureDirSync(path, options) {
return this.raw.ensureDirSync(path, options);
}
findUpFile(root, file) {
return [join(root, file), join(root, "..", file), join(root, "..", "..", file), join(root, "..", "..", "..", file)].find((path) => {
return this.fileExistsSync(path) || this.raw.existsSync(path);
});
}
async importModule(mod, root = process.cwd()) {
try {
if (process.env.NODE_ENV === "development") {
return await import(mod);
}
}
catch (er) { }
const path = this.findUpFile(root, join("node_modules", mod));
if (path) {
const pkg = await this.readJson(join(path, "package.json"));
const file = pkg.exports?.["."]?.import || pkg.exports?.["."]?.default || pkg.exports?.["."] || pkg.module || pkg.main;
return import(join(path, file));
}
return import(mod);
}
}
injectable(CliFs);