Urara-Blog/node_modules/.pnpm-store/v3/files/6d/6f3865a5b9fcf7d182cd7fc941254d5ca3182914f665a0c4087f5e6c13e15258179193a4d848fa46fa6a0346510b9aba1c4e9e02e7388072c46b8583d1be5b
2022-08-14 01:14:53 +08:00

52 lines
1.3 KiB
Text

import * as fs from 'graceful-fs';
import { dirname } from 'path';
import mkdirp from 'mkdirp';
import resolvePath from '../utils/resolvePath';
export const writeFile = asyncMethod( 'writeFile' );
export const appendFile = asyncMethod( 'appendFile' );
export const writeFileSync = syncMethod( 'writeFileSync' );
export const appendFileSync = syncMethod( 'appendFileSync' );
function normaliseArguments ( args ) {
args = Array.prototype.slice.call( args, 0 );
let opts = {};
if ( typeof args[ args.length - 1 ] === 'object' && !( args[ args.length - 1 ] instanceof Buffer ) ) {
opts = args.pop();
}
return { opts, data: args.pop(), dest: resolvePath( args ) };
}
function asyncMethod ( methodName ) {
return function () {
const { dest, data, opts } = normaliseArguments( arguments );
return new Promise( ( fulfil, reject ) => {
mkdirp( dirname( dest ), err => {
if ( err ) {
reject( err );
} else {
fs[ methodName ]( dest, data, opts, err => {
if ( err ) {
reject( err );
} else {
fulfil( data );
}
});
}
});
});
};
}
function syncMethod ( methodName ) {
return function () {
const { dest, data } = normaliseArguments( arguments );
mkdirp.sync( dirname( dest ) );
return fs[ methodName ]( dest, data );
};
}