// If dest is an existing directory, should copy inside.
constnewDest=destStat&&destStat.isDirectory()
?path.join(dest,path.basename(source))
:dest;
if(!(yieldioUtil.exists(source))){
thrownewError(`no such file or directory: ${source}`);
}
constsourceStat=yieldioUtil.stat(source);
if(sourceStat.isDirectory()){
if(!recursive){
thrownewError(`Failed to copy. ${source} is a directory, but tried to copy without recursive flag.`);
}
else{
yieldcpDirRecursive(source,newDest,0,force);
}
}
else{
if(path.relative(source,newDest)===''){
// a file cannot be copied to itself
thrownewError(`'${newDest}' and '${source}' are the same file`);
}
yieldcopyFile(source,newDest,force);
}
});
}
exports.cp=cp;
/**
* Moves a path.
*
* @param source source path
* @param dest destination path
* @param options optional. See MoveOptions.
*/
functionmv(source,dest,options={}){
return__awaiter(this,void0,void0,function*(){
if(yieldioUtil.exists(dest)){
letdestExists=true;
if(yieldioUtil.isDirectory(dest)){
// If dest is directory copy src into dest
dest=path.join(dest,path.basename(source));
destExists=yieldioUtil.exists(dest);
}
if(destExists){
if(options.force==null||options.force){
yieldrmRF(dest);
}
else{
thrownewError('Destination already exists');
}
}
}
yieldmkdirP(path.dirname(dest));
yieldioUtil.rename(source,dest);
});
}
exports.mv=mv;
/**
* Remove a path recursively with force
*
* @param inputPath path to remove
*/
functionrmRF(inputPath){
return__awaiter(this,void0,void0,function*(){
if(ioUtil.IS_WINDOWS){
// Node doesn't provide a delete operation, only an unlink function. This means that if the file is being used by another
// program (e.g. antivirus), it won't be deleted. To address this, we shell out the work to rd/del.
try{
if(yieldioUtil.isDirectory(inputPath,true)){
yieldexec(`rd /s /q "${inputPath}"`);
}
else{
yieldexec(`del /f /a "${inputPath}"`);
}
}
catch(err){
// if you try to delete a file that doesn't exist, desired result is achieved
// other errors are valid
if(err.code!=='ENOENT')
throwerr;
}
// Shelling out fails to remove a symlink folder with missing source, this unlink catches that
try{
yieldioUtil.unlink(inputPath);
}
catch(err){
// if you try to delete a file that doesn't exist, desired result is achieved
// other errors are valid
if(err.code!=='ENOENT')
throwerr;
}
}
else{
letisDir=false;
try{
isDir=yieldioUtil.isDirectory(inputPath);
}
catch(err){
// if you try to delete a file that doesn't exist, desired result is achieved
// other errors are valid
if(err.code!=='ENOENT')
throwerr;
return;
}
if(isDir){
yieldexec(`rm -rf "${inputPath}"`);
}
else{
yieldioUtil.unlink(inputPath);
}
}
});
}
exports.rmRF=rmRF;
/**
* Make a directory. Creates the full path with folders in between
* Will throw if it fails
*
* @param fsPath path to create
* @returns Promise<void>
*/
functionmkdirP(fsPath){
return__awaiter(this,void0,void0,function*(){
yieldioUtil.mkdirP(fsPath);
});
}
exports.mkdirP=mkdirP;
/**
* Returns path of a tool had the tool actually been invoked. Resolves via paths.
* If you check and the tool does not exist, it will throw.
*
* @param tool name of the tool
* @param check whether to check if tool exists
* @returns Promise<string> path to tool
*/
functionwhich(tool,check){
return__awaiter(this,void0,void0,function*(){
if(!tool){
thrownewError("parameter 'tool' is required");
}
// recursive when check=true
if(check){
constresult=yieldwhich(tool,false);
if(!result){
if(ioUtil.IS_WINDOWS){
thrownewError(`Unable to locate executable file: ${tool}. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.`);
}
else{
thrownewError(`Unable to locate executable file: ${tool}. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.`);