UNPKG

zksync-cli

Version:

CLI tool that simplifies the process of developing applications and interacting with the ZKsync network

50 lines 1.91 kB
import fs from "fs"; import { homedir } from "os"; import path from "path"; import { fileURLToPath } from "url"; const getUserDirectory = () => { // From the XDG Base Directory Specification: // `$XDG_STATE_HOME` defines the base directory relative to which user-specific state files should be stored. // If `$XDG_STATE_HOME` is either not set or empty, a default equal to `$HOME/.local/state/` should be used. const xdgStateHome = process.env.XDG_STATE_HOME || path.join(homedir(), ".local/state/"); return path.join(xdgStateHome, "zksync-cli/"); }; export const getLocalPath = (...filePath) => { return path.join(getUserDirectory(), ...filePath); }; export const fileOrDirExists = (...filePath) => { return fs.existsSync(path.join(...filePath)); }; export const getDirPath = (filePath) => { const filename = fileURLToPath(filePath); return path.dirname(filename); }; export const writeFile = (filePath, data) => { // Create directory if it doesn't exist const directory = path.dirname(filePath); if (!fileOrDirExists(directory)) { fs.mkdirSync(directory, { recursive: true }); } // Then write file fs.writeFileSync(filePath, data, "utf-8"); }; export const createSymlink = (targetPath, linkPath, type = "file") => { if (fileOrDirExists(linkPath)) { throw new Error(`${type} already exists at ${linkPath}`); } fs.symlinkSync(targetPath, linkPath, type); }; export const copyRecursiveSync = (src, dest) => { const stats = fs.statSync(src); const isDirectory = stats.isDirectory(); if (isDirectory) { fs.mkdirSync(dest, { recursive: true }); fs.readdirSync(src).forEach((childItemName) => { copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName)); }); } else { fs.copyFileSync(src, dest); } }; //# sourceMappingURL=files.js.map