mono/packages/fs/dist/append.js
2025-01-23 08:30:04 +01:00

33 lines
1.2 KiB
JavaScript

import * as fs from 'node:fs';
import { sync as writeSync } from './write.js';
import { validateArgument, validateOptions } from './utils/validate.js';
export const validateInput = (methodName, path, data, options) => {
const methodSignature = methodName + '(path, data, [options])';
validateArgument(methodSignature, 'path', path, ['string']);
validateArgument(methodSignature, 'data', data, ['string', 'buffer']);
validateOptions(methodSignature, 'options', options, {
mode: ['string', 'number']
});
};
// ---------------------------------------------------------
// SYNC
// ---------------------------------------------------------
export const sync = (path, data, options) => {
try {
fs.appendFileSync(path, data, options ? { encoding: options.encoding, mode: options.mode } : {});
}
catch (err) {
if (err.code === 'ENOENT') {
// Parent directory doesn't exist, so just pass the task to `write`,
// which will create the folder and file.
writeSync(path, data, options);
}
else {
throw err;
}
}
};
export const async = (path, data, options) => {
return fs.promises.appendFile(path, data, options);
};