UNPKG

@tsed/cli-core

Version:
68 lines (67 loc) 2.27 kB
import { __decorate } from "tslib"; import { join } from "node:path"; import { Injectable } from "@tsed/di"; import { normalizePath } from "@tsed/normalize-path"; import Fs, {} from "fs-extra"; let CliFs = class CliFs { constructor() { this.raw = Fs; } exists(path) { return this.raw.existsSync(path); } join(...args) { return normalizePath(join(...args)); } readFile(file, encoding) { return this.raw.readFile(file, encoding); } readFileSync(file, encoding) { return this.raw.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" }); } 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) => this.exists(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); } }; CliFs = __decorate([ Injectable() ], CliFs); export { CliFs };