@tomjs/node
Version:
A collection of functions for `node.js` projects
149 lines (148 loc) • 3.73 kB
JavaScript
import fs3 from 'node:fs';
import fsp3 from 'node:fs/promises';
import path2 from 'node:path';
import fs2 from 'node:fs';
import fsp2 from 'node:fs/promises';
import path from 'node:path';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
function removeFile(path3) {
return fsp.rm(path3, { force: true, recursive: true });
}
function removeFileSync(path3) {
fs.rmSync(path3, { force: true, recursive: true });
}
function rm(path3) {
return fsp.rm(path3, { force: true, recursive: true });
}
function rmSync(path3) {
fs.rmSync(path3, { force: true, recursive: true });
}
var checkPath = pth => {
if (process.platform === 'win32') {
const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''));
if (pathHasInvalidWinCharacters) {
const error = new Error(`Path contains invalid characters: ${pth}`);
error.code = 'EINVAL';
throw error;
}
}
};
function mkdir(dir, mode) {
checkPath(dir);
return fsp2.mkdir(dir, { mode: mode ?? 511, recursive: true });
}
var mkdirp = mkdir;
function mkdirSync(dir, mode) {
checkPath(dir);
return fs2.mkdirSync(dir, { mode: mode ?? 511, recursive: true });
}
var mkdirpSync = mkdirSync;
async function emptyDir(dir) {
await rm(dir);
await mkdir(dir);
}
function emptyDirSync(dir) {
rmSync(dir);
mkdirSync(dir);
}
function copyDirSync(srcDir, destDir) {
fs3.mkdirSync(destDir, { recursive: true });
for (const file of fs3.readdirSync(srcDir)) {
const srcFile = path2.resolve(srcDir, file);
const destFile = path2.resolve(destDir, file);
copySync(srcFile, destFile);
}
}
function copySync(src, dest) {
const stat = fs3.statSync(src);
if (stat.isDirectory()) {
copyDirSync(src, dest);
} else {
fs3.copyFileSync(src, dest);
}
}
async function copyDir(srcDir, destDir) {
await mkdir(destDir);
const files = await fsp3.readdir(srcDir);
await Promise.all(
files.map(file =>
(async () => {
const srcFile = path2.resolve(srcDir, file);
const destFile = path2.resolve(destDir, file);
await copy(srcFile, destFile);
})(),
),
);
}
async function copy(src, dest) {
const stat = fs3.statSync(src);
if (stat.isDirectory()) {
await copyDir(src, dest);
} else {
await fsp3.copyFile(src, dest);
}
}
import fs4 from 'node:fs';
import fsp4 from 'node:fs/promises';
function readFileSync(path3) {
return fs4.readFileSync(path3, 'utf8');
}
function readFile(path3) {
return fsp4.readFile(path3, 'utf8');
}
function writeFileSync(path3, data) {
fs4.writeFileSync(path3, data, 'utf8');
}
function writeFile(path3, data) {
return fsp4.writeFile(path3, data, 'utf8');
}
function readJsonSync(path3) {
if (fs4.existsSync(path3)) {
return JSON.parse(readFileSync(path3));
}
}
async function readJson(path3) {
if (fs4.existsSync(path3)) {
const text = await readFile(path3);
return JSON.parse(text);
}
}
function writeJsonSync(path3, data, options) {
const opts = Object.assign({ replacer: null, space: 2 }, options);
writeFileSync(
path3,
typeof data === 'string' ? data : JSON.stringify(data, opts.replacer, opts.space),
);
}
function writeJson(path3, data, options) {
const opts = Object.assign({ replacer: null, space: 2 }, options);
return writeFile(
path3,
typeof data === 'string' ? data : JSON.stringify(data, opts.replacer, opts.space),
);
}
var src_default = {};
export {
copy,
copySync,
src_default as default,
emptyDir,
emptyDirSync,
mkdir,
mkdirSync,
mkdirp,
mkdirpSync,
readFile,
readFileSync,
readJson,
readJsonSync,
removeFile,
removeFileSync,
rm,
rmSync,
writeFile,
writeFileSync,
writeJson,
writeJsonSync,
};