文件操作 - index.mjs
返回文件管理
返回主菜单
删除本文件
文件: /var/www/OLD/node_modules/unimport/node_modules/local-pkg/dist/index.mjs
编辑文件内容
import fs from 'node:fs'; import fsPromises from 'node:fs/promises'; import { createRequire } from 'node:module'; import path, { dirname, win32, join } from 'node:path'; import process from 'node:process'; import { fileURLToPath } from 'node:url'; import { interopDefault, resolvePathSync } from 'mlly'; const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; async function findUp(name, { cwd = process.cwd(), type = 'file', stopAt, } = {}) { let directory = path.resolve(toPath(cwd) ?? ''); const {root} = path.parse(directory); stopAt = path.resolve(directory, toPath(stopAt ?? root)); while (directory && directory !== stopAt && directory !== root) { const filePath = path.isAbsolute(name) ? name : path.join(directory, name); try { const stats = await fsPromises.stat(filePath); // eslint-disable-line no-await-in-loop if ((type === 'file' && stats.isFile()) || (type === 'directory' && stats.isDirectory())) { return filePath; } } catch {} directory = path.dirname(directory); } } function _resolve(path, options = {}) { if (options.platform === "auto" || !options.platform) options.platform = process.platform === "win32" ? "win32" : "posix"; if (process.versions.pnp) { const paths = options.paths || []; if (paths.length === 0) paths.push(process.cwd()); const targetRequire = createRequire(import.meta.url); try { return targetRequire.resolve(path, { paths }); } catch { } } const modulePath = resolvePathSync(path, { url: options.paths }); if (options.platform === "win32") return win32.normalize(modulePath); return modulePath; } function resolveModule(name, options = {}) { try { return _resolve(name, options); } catch { return undefined; } } async function importModule(path) { const i = await import(path); if (i) return interopDefault(i); return i; } function isPackageExists(name, options = {}) { return !!resolvePackage(name, options); } function getPackageJsonPath(name, options = {}) { const entry = resolvePackage(name, options); if (!entry) return; return searchPackageJSON(entry); } async function getPackageInfo(name, options = {}) { const packageJsonPath = getPackageJsonPath(name, options); if (!packageJsonPath) return; const packageJson = JSON.parse(await fs.promises.readFile(packageJsonPath, "utf8")); return { name, version: packageJson.version, rootPath: dirname(packageJsonPath), packageJsonPath, packageJson }; } function getPackageInfoSync(name, options = {}) { const packageJsonPath = getPackageJsonPath(name, options); if (!packageJsonPath) return; const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); return { name, version: packageJson.version, rootPath: dirname(packageJsonPath), packageJsonPath, packageJson }; } function resolvePackage(name, options = {}) { try { return _resolve(`${name}/package.json`, options); } catch { } try { return _resolve(name, options); } catch (e) { if (e.code !== "MODULE_NOT_FOUND" && e.code !== "ERR_MODULE_NOT_FOUND") console.error(e); return false; } } function searchPackageJSON(dir) { let packageJsonPath; while (true) { if (!dir) return; const newDir = dirname(dir); if (newDir === dir) return; dir = newDir; packageJsonPath = join(dir, "package.json"); if (fs.existsSync(packageJsonPath)) break; } return packageJsonPath; } async function loadPackageJSON(cwd = process.cwd()) { const path = await findUp("package.json", { cwd }); if (!path || !fs.existsSync(path)) return null; return JSON.parse(await fsPromises.readFile(path, "utf-8")); } async function isPackageListed(name, cwd) { const pkg = await loadPackageJSON(cwd) || {}; return name in (pkg.dependencies || {}) || name in (pkg.devDependencies || {}); } export { getPackageInfo, getPackageInfoSync, importModule, isPackageExists, isPackageListed, loadPackageJSON, resolveModule };
修改文件时间
将文件时间修改为当前时间的前一年
删除文件