HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode.com/node_modules/sander/src/specialMethods/open.js
import { dirname } from 'path';
import * as fs from 'graceful-fs';
import mkdirp from 'mkdirp';
import resolvePath from '../utils/resolvePath';

function normaliseArguments ( args ) {
	let options;
	let flags;
	let i;

	if ( typeof args[ args.length - 1 ] === 'object' ) {
		options = args[ args.length - 1 ];
		flags = args[ args.length - 2 ];
		i = args.length - 2;
	} else {
		options = {};
		flags = args[ args.length - 1 ];
		i = args.length - 1;
	}

	let pathargs = new Array( i );
	while ( i-- ) {
		pathargs[i] = args[i];
	}

	const resolvedPath = resolvePath( pathargs );

	return { resolvedPath, options, flags };
}

function bailIfExists ( src, flags, mode ) {
	let alreadyExists;

	try {
		fs.statSync( src );
		alreadyExists = true;
	} catch ( err ) {
		if ( err.code !== 'ENOENT' ) {
			throw err;
		}
	}

	if ( alreadyExists ) {
		// attempt the operation = that way, we get the intended error message
		// TODO can't we just do this in the first place?
		fs.openSync( src, flags, mode );
	}
}

export function open () {
	const { resolvedPath: src, options, flags } = normaliseArguments( arguments );

	if ( /^.x/.test( flags ) ) {
		bailIfExists( src, flags, options.mode );
	}

	return new Promise( ( fulfil, reject ) => {
		function open () {
			fs.open( src, flags, options.mode, ( err, fd ) => {
				if ( err ) {
					reject( err );
				} else {
					fulfil( fd );
				}
			});
		}

		// create dirs if necessary
		if ( /^[wa]/.test( flags ) ) {
			mkdirp( dirname( src ), err => {
				if ( err ) {
					reject( err );
				} else {
					open();
				}
			});
		} else {
			open();
		}
	});
}


export function openSync () {
	const { resolvedPath: src, options, flags } = normaliseArguments( arguments );

	if ( /^.x/.test( flags ) ) {
		bailIfExists( src, flags, options.mode );
	}

	// create dirs if necessary
	if ( /^[wa]/.test( flags ) ) {
		mkdirp.sync( dirname( src ) );
	}

	return fs.openSync( src, flags, options.mode );
}