mono/packages/fs/dist/file.js
2025-03-17 14:01:33 +01:00

141 lines
4.3 KiB
JavaScript

import * as fs from 'fs';
import { normalizeFileMode } from './utils/mode.js';
import { validateArgument, validateOptions } from './utils/validate.js';
import { sync as writeSync, async as writeASync } from './write.js';
import { ErrNotFile } from './errors.js';
import { EError } from './interfaces.js';
const promisedStat = fs.promises.stat;
const promisedChmod = fs.promises.chmod;
export function validateInput(methodName, path, options) {
const methodSignature = methodName + '(path, [criteria])';
validateArgument(methodSignature, 'path', path, ['string']);
validateOptions(methodSignature, 'criteria', options, {
content: ['string', 'buffer', 'object', 'array'],
jsonIndent: ['number'],
mode: ['string', 'number']
});
}
export function defaults(passedCriteria) {
const criteria = passedCriteria || {};
if (criteria.mode !== undefined) {
criteria.mode = normalizeFileMode(criteria.mode);
}
return criteria;
}
// ---------------------------------------------------------
// Sync
// ---------------------------------------------------------
const isFile = (path) => {
let stat;
try {
stat = fs.statSync(path);
}
catch (err) {
// Detection if path exists
if (err.code !== EError.NOEXISTS) {
throw err;
}
}
if (stat && !stat.isFile()) {
throw ErrNotFile(path);
}
return stat;
};
const checkContent = function (path, mode, options) {
if (options.content !== undefined) {
writeSync(path, options.content, {
mode: mode,
jsonIndent: options.jsonIndent
});
return true;
}
return false;
};
const checkMode = function (path, mode, options) {
if (options.mode !== undefined && options.mode !== mode) {
fs.chmodSync(path, options.mode);
}
};
const accept = (path, stat, options) => {
const mode = normalizeFileMode(stat.mode);
if (!checkContent(path, mode, options)) {
checkMode(path, mode, options);
}
};
const touch = (path, options) => {
const content = options.content !== undefined ? options.content : '';
writeSync(path, content, {
mode: options.mode,
jsonIndent: options.jsonIndent
});
};
export function sync(path, options) {
options = defaults(options);
const stat = isFile(path);
if (stat !== undefined) {
accept(path, stat, options);
}
else {
touch(path, options);
}
}
// ---------------------------------------------------------
// Async
// ---------------------------------------------------------
function isFileAsync(path) {
return new Promise((resolve, reject) => {
promisedStat(path)
.then((stat) => {
if ((stat).isFile()) {
resolve(stat);
}
else {
reject(ErrNotFile(path));
}
})
.catch((err) => (err.code === EError.NOEXISTS ? resolve(undefined) : reject(err)));
});
}
const checkModeAsync = (path, mode, options) => {
if (options.mode !== undefined && options.mode !== mode) {
return promisedChmod(path, options.mode);
}
return undefined;
};
const checkContentAsync = (path, mode, options) => {
return new Promise((resolve, reject) => {
if (options.content !== undefined) {
writeASync(path, options.content, {
mode: mode,
jsonIndent: options.jsonIndent
}).then(() => resolve(true)).catch(reject);
}
else {
resolve(false);
}
});
};
const writeAsync = async (path, stat, options) => {
const mode = normalizeFileMode(stat.mode);
return checkContentAsync(path, mode, options);
};
const touchAsync = (path, options) => {
return writeASync(path, options.content !== undefined ? options.content : '', {
mode: options.mode,
jsonIndent: options.jsonIndent
});
};
export async function async(path, options) {
return new Promise((resolve, reject) => {
options = defaults(options);
isFileAsync(path)
.then((stat) => {
if (stat !== undefined) {
return writeAsync(path, stat, options);
}
return touchAsync(path, options);
})
.then(resolve, reject);
});
}