2020-10-05 16:20:57 +00:00
|
|
|
import * as plugins from './smartfile.plugins';
|
|
|
|
import * as interpreter from './smartfile.interpreter';
|
2017-03-04 20:10:46 +00:00
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
import { Smartfile } from './smartfile.classes.smartfile';
|
2017-03-04 20:10:46 +00:00
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
import * as memory from './smartfile.memory';
|
2016-06-24 01:36:51 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ Checks =============================
|
|
|
|
===============================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2019-06-27 08:42:35 +00:00
|
|
|
export const fileExistsSync = (filePath): boolean => {
|
2018-07-03 06:55:09 +00:00
|
|
|
let fileExistsBool: boolean = false;
|
2017-03-04 20:10:46 +00:00
|
|
|
try {
|
2018-07-03 06:55:09 +00:00
|
|
|
plugins.fsExtra.readFileSync(filePath);
|
|
|
|
fileExistsBool = true;
|
2017-03-04 20:10:46 +00:00
|
|
|
} catch (err) {
|
2018-07-03 06:55:09 +00:00
|
|
|
fileExistsBool = false;
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return fileExistsBool;
|
|
|
|
};
|
2016-06-24 01:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const fileExists = async (filePath): Promise<boolean> => {
|
2019-02-17 20:07:22 +00:00
|
|
|
const done = plugins.smartpromise.defer<boolean>();
|
2020-08-10 20:58:22 +00:00
|
|
|
plugins.fs.access(filePath, 4, (err) => {
|
2019-02-17 20:07:22 +00:00
|
|
|
err ? done.resolve(false) : done.resolve(true);
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-06-24 01:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if given path points to an existing directory
|
|
|
|
*/
|
2019-06-27 08:42:35 +00:00
|
|
|
export const isDirectory = (pathArg): boolean => {
|
2017-05-07 18:50:59 +00:00
|
|
|
try {
|
2018-07-03 06:55:09 +00:00
|
|
|
return plugins.fsExtra.statSync(pathArg).isDirectory();
|
2017-05-07 18:50:59 +00:00
|
|
|
} catch (err) {
|
2018-07-03 06:55:09 +00:00
|
|
|
return false;
|
2017-05-07 18:50:59 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-24 01:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given path points to an existing file
|
|
|
|
*/
|
2019-06-27 08:42:35 +00:00
|
|
|
export const isFile = (pathArg): boolean => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return plugins.fsExtra.statSync(pathArg).isFile();
|
|
|
|
};
|
2016-06-24 01:36:51 +00:00
|
|
|
|
2016-06-23 15:42:08 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ FS ACTIONS =========================
|
|
|
|
===============================================================*/
|
|
|
|
|
2016-09-24 19:42:45 +00:00
|
|
|
/**
|
|
|
|
* copies a file from A to B on the local disk
|
|
|
|
*/
|
2019-06-27 08:42:35 +00:00
|
|
|
export const copy = async (fromArg: string, toArg: string): Promise<boolean> => {
|
|
|
|
const done = plugins.smartpromise.defer<boolean>();
|
2020-08-10 20:58:22 +00:00
|
|
|
plugins.fsExtra.copy(fromArg, toArg, {}, (err) => {
|
2019-06-27 08:42:35 +00:00
|
|
|
if (err) {
|
|
|
|
throw new Error(`Could not copy from ${fromArg} to ${toArg}: ${err}`);
|
|
|
|
}
|
|
|
|
done.resolve(true);
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-09-24 19:42:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* copies a file SYNCHRONOUSLY from A to B on the local disk
|
|
|
|
*/
|
2019-06-27 08:42:35 +00:00
|
|
|
export const copySync = (fromArg: string, toArg: string): boolean => {
|
2018-07-03 06:55:09 +00:00
|
|
|
plugins.fsExtra.copySync(fromArg, toArg);
|
|
|
|
return true;
|
|
|
|
};
|
2016-09-24 19:42:45 +00:00
|
|
|
|
2016-06-28 04:57:51 +00:00
|
|
|
/**
|
|
|
|
* ensures that a directory is in place
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const ensureDir = async (dirPathArg: string) => {
|
|
|
|
await plugins.fsExtra.ensureDir(dirPathArg);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-28 04:57:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ensures that a directory is in place
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const ensureDirSync = (dirPathArg: string) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
plugins.fsExtra.ensureDirSync(dirPathArg);
|
|
|
|
};
|
2016-06-28 04:57:51 +00:00
|
|
|
|
2017-01-01 01:45:53 +00:00
|
|
|
/**
|
|
|
|
* ensure an empty directory
|
|
|
|
* @executes ASYNC
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const ensureEmptyDir = async (dirPathArg: string) => {
|
|
|
|
await plugins.fsExtra.ensureDir(dirPathArg);
|
|
|
|
await plugins.fsExtra.emptyDir(dirPathArg);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2017-01-01 01:45:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ensure an empty directory
|
|
|
|
* @executes SYNC
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const ensureEmptyDirSync = (dirPathArg: string) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
plugins.fsExtra.ensureDirSync(dirPathArg);
|
|
|
|
plugins.fsExtra.emptyDirSync(dirPathArg);
|
|
|
|
};
|
2017-01-01 01:45:53 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
2016-09-24 19:42:45 +00:00
|
|
|
* ensures that a file is on disk
|
|
|
|
* @param filePath the filePath to ensureDir
|
|
|
|
* @param the fileContent to place into a new file in case it doesn't exist yet
|
|
|
|
* @returns Promise<void>
|
|
|
|
* @exec ASYNC
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const ensureFile = async (filePathArg, initFileStringArg): Promise<void> => {
|
2018-07-03 06:55:09 +00:00
|
|
|
ensureFileSync(filePathArg, initFileStringArg);
|
|
|
|
};
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
2016-09-24 19:42:45 +00:00
|
|
|
* ensures that a file is on disk
|
|
|
|
* @param filePath the filePath to ensureDir
|
|
|
|
* @param the fileContent to place into a new file in case it doesn't exist yet
|
|
|
|
* @returns Promise<void>
|
|
|
|
* @exec SYNC
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const ensureFileSync = (filePathArg: string, initFileStringArg: string): void => {
|
2017-03-04 20:10:46 +00:00
|
|
|
if (fileExistsSync(filePathArg)) {
|
2018-07-03 06:55:09 +00:00
|
|
|
return null;
|
2017-03-04 20:10:46 +00:00
|
|
|
} else {
|
2018-07-03 06:55:09 +00:00
|
|
|
memory.toFsSync(initFileStringArg, filePathArg);
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-09-20 15:56:49 +00:00
|
|
|
|
2016-09-24 19:42:45 +00:00
|
|
|
/**
|
|
|
|
* removes a file or folder from local disk
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const remove = async (pathArg: string): Promise<void> => {
|
|
|
|
await plugins.fsExtra.remove(pathArg);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* removes a file SYNCHRONOUSLY from local disk
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const removeSync = (pathArg: string): void => {
|
2018-07-03 06:55:09 +00:00
|
|
|
plugins.fsExtra.removeSync(pathArg);
|
|
|
|
};
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-09-29 12:17:46 +00:00
|
|
|
/**
|
|
|
|
* removes an array of filePaths from disk
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const removeMany = async (filePathArrayArg: string[]) => {
|
|
|
|
const promiseArray: Array<Promise<void>> = [];
|
|
|
|
for (const filePath of filePathArrayArg) {
|
2018-07-03 06:55:09 +00:00
|
|
|
promiseArray.push(remove(filePath));
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2019-09-27 09:00:17 +00:00
|
|
|
await Promise.all(promiseArray);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-09-29 12:17:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* like removeFilePathArray but SYNCHRONOUSLY
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const removeManySync = (filePathArrayArg: string[]): void => {
|
|
|
|
for (const filePath of filePathArrayArg) {
|
2018-07-03 06:55:09 +00:00
|
|
|
removeSync(filePath);
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-09-29 12:17:46 +00:00
|
|
|
|
2016-06-23 15:42:08 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ Write/Read =========================
|
|
|
|
===============================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePathArg
|
|
|
|
* @param fileTypeArg
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const toObjectSync = (filePathArg, fileTypeArg?) => {
|
2019-01-27 02:11:10 +00:00
|
|
|
const fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8');
|
2018-07-03 06:55:09 +00:00
|
|
|
let fileType;
|
2020-10-05 16:20:57 +00:00
|
|
|
fileTypeArg ? (fileType = fileTypeArg) : (fileType = interpreter.filetype(filePathArg));
|
|
|
|
return interpreter.objectFile(fileString, fileType);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-23 15:42:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* reads a file content to a String
|
|
|
|
*/
|
2019-06-27 08:42:35 +00:00
|
|
|
export const toStringSync = (filePath: string): string => {
|
2020-03-04 16:31:13 +00:00
|
|
|
const encoding = plugins.smartmime.getEncoding(filePath);
|
2020-10-05 16:20:57 +00:00
|
|
|
let fileString: string | Buffer = plugins.fsExtra.readFileSync(filePath, encoding);
|
|
|
|
if (Buffer.isBuffer(fileString)) {
|
|
|
|
fileString = fileString.toString('binary');
|
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return fileString;
|
|
|
|
};
|
2017-03-04 20:10:46 +00:00
|
|
|
|
2021-11-30 15:34:35 +00:00
|
|
|
export const toBuffer = async (filePath: string): Promise<Buffer> => {
|
|
|
|
return plugins.fsExtra.readFile(filePath);
|
|
|
|
};
|
|
|
|
|
2020-03-04 16:31:13 +00:00
|
|
|
export const toBufferSync = (filePath: string): Buffer => {
|
|
|
|
return plugins.fsExtra.readFileSync(filePath);
|
2020-03-15 18:58:46 +00:00
|
|
|
};
|
2020-03-04 16:31:13 +00:00
|
|
|
|
2019-09-27 09:00:17 +00:00
|
|
|
export const fileTreeToHash = async (dirPathArg: string, miniMatchFilter: string) => {
|
|
|
|
const fileTreeObject = await fileTreeToObject(dirPathArg, miniMatchFilter);
|
|
|
|
let combinedString = '';
|
|
|
|
for (const smartfile of fileTreeObject) {
|
|
|
|
combinedString += smartfile.contentBuffer.toString();
|
|
|
|
}
|
|
|
|
const hash = await plugins.smarthash.sha256FromString(combinedString);
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a smartfile array from a directory
|
|
|
|
* @param dirPathArg the directory to start from
|
|
|
|
* @param miniMatchFilter a minimatch filter of what files to include
|
|
|
|
*/
|
2019-06-26 19:17:58 +00:00
|
|
|
export const fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string) => {
|
2017-04-30 13:37:34 +00:00
|
|
|
// handle absolute miniMatchFilter
|
2018-07-03 06:55:09 +00:00
|
|
|
let dirPath: string;
|
2017-04-30 13:37:34 +00:00
|
|
|
if (plugins.path.isAbsolute(miniMatchFilter)) {
|
2018-07-03 06:55:09 +00:00
|
|
|
dirPath = '/';
|
2017-04-30 13:37:34 +00:00
|
|
|
} else {
|
2020-10-11 15:34:24 +00:00
|
|
|
dirPath = plugins.smartpath.transform.toAbsolute(dirPathArg);
|
2017-04-30 13:37:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 19:17:58 +00:00
|
|
|
const fileTree = await listFileTree(dirPath, miniMatchFilter);
|
|
|
|
const smartfileArray: Smartfile[] = [];
|
2019-09-27 09:00:17 +00:00
|
|
|
for (const filePath of fileTree) {
|
|
|
|
const readPath = ((): string => {
|
2017-08-02 11:10:30 +00:00
|
|
|
if (!plugins.path.isAbsolute(filePath)) {
|
2018-07-03 06:55:09 +00:00
|
|
|
return plugins.path.join(dirPath, filePath);
|
2017-08-02 11:10:30 +00:00
|
|
|
} else {
|
2018-07-03 06:55:09 +00:00
|
|
|
return filePath;
|
2017-08-02 11:10:30 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
})();
|
2019-09-27 09:00:17 +00:00
|
|
|
const fileContentString = toStringSync(readPath);
|
2017-05-01 17:49:34 +00:00
|
|
|
|
2017-05-07 21:16:25 +00:00
|
|
|
// push a read file as Smartfile
|
2018-07-03 06:55:09 +00:00
|
|
|
smartfileArray.push(
|
|
|
|
new Smartfile({
|
2019-06-26 19:17:58 +00:00
|
|
|
contentBuffer: Buffer.from(fileContentString),
|
2018-07-03 06:55:09 +00:00
|
|
|
base: dirPath,
|
2020-08-10 20:58:22 +00:00
|
|
|
path: filePath,
|
2018-07-03 06:55:09 +00:00
|
|
|
})
|
|
|
|
);
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return smartfileArray;
|
|
|
|
};
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* lists Folders in a directory on local disk
|
2018-11-22 22:23:26 +00:00
|
|
|
* @returns Promise with an array that contains the folder names
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listFolders = async (pathArg: string, regexFilter?: RegExp): Promise<string[]> => {
|
2019-01-27 02:11:10 +00:00
|
|
|
return listFoldersSync(pathArg, regexFilter);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* lists Folders SYNCHRONOUSLY in a directory on local disk
|
2016-06-28 04:57:51 +00:00
|
|
|
* @returns an array with the folder names as strings
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listFoldersSync = (pathArg: string, regexFilter?: RegExp): string[] => {
|
2020-08-10 20:58:22 +00:00
|
|
|
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter((file) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory();
|
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
if (regexFilter) {
|
2020-08-10 20:58:22 +00:00
|
|
|
folderArray = folderArray.filter((fileItem) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return regexFilter.test(fileItem);
|
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return folderArray;
|
|
|
|
};
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists Files in a directory on local disk
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listFiles = async (pathArg: string, regexFilter?: RegExp): Promise<string[]> => {
|
2019-01-27 02:11:10 +00:00
|
|
|
return listFilesSync(pathArg, regexFilter);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists Files SYNCHRONOUSLY in a directory on local disk
|
|
|
|
* @returns an array with the folder names as strings
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listFilesSync = (pathArg: string, regexFilter?: RegExp): string[] => {
|
2020-08-10 20:58:22 +00:00
|
|
|
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter((file) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
|
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
if (regexFilter) {
|
2020-08-10 20:58:22 +00:00
|
|
|
fileArray = fileArray.filter((fileItem) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return regexFilter.test(fileItem);
|
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return fileArray;
|
|
|
|
};
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists all items (folders AND files) in a directory on local disk
|
2016-09-17 21:11:44 +00:00
|
|
|
* @returns Promise<string[]>
|
2016-06-28 06:40:22 +00:00
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listAllItems = async (pathArg: string, regexFilter?: RegExp): Promise<string[]> => {
|
2019-01-27 02:11:10 +00:00
|
|
|
return listAllItemsSync(pathArg, regexFilter);
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-17 21:11:44 +00:00
|
|
|
* lists all items (folders AND files) in a directory on local disk
|
2016-06-28 06:40:22 +00:00
|
|
|
* @returns an array with the folder names as strings
|
2016-09-17 21:11:44 +00:00
|
|
|
* @executes SYNC
|
2016-06-28 06:40:22 +00:00
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listAllItemsSync = (pathArg: string, regexFilter?: RegExp): string[] => {
|
2020-08-10 20:58:22 +00:00
|
|
|
let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter((file) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
|
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
if (regexFilter) {
|
2020-08-10 20:58:22 +00:00
|
|
|
allItmesArray = allItmesArray.filter((fileItem) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return regexFilter.test(fileItem);
|
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return allItmesArray;
|
|
|
|
};
|
2016-06-28 06:40:22 +00:00
|
|
|
|
2016-09-17 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* lists a file tree using a miniMatch filter
|
2016-09-30 14:16:11 +00:00
|
|
|
* note: if the miniMatch Filter is an absolute path, the cwdArg will be omitted
|
2016-09-17 21:11:44 +00:00
|
|
|
* @returns Promise<string[]> string array with the absolute paths of all matching files
|
|
|
|
*/
|
2019-09-27 09:00:17 +00:00
|
|
|
export const listFileTree = async (
|
|
|
|
dirPathArg: string,
|
|
|
|
miniMatchFilter: string,
|
|
|
|
absolutePathsBool: boolean = false
|
|
|
|
): Promise<string[]> => {
|
2019-01-27 02:11:10 +00:00
|
|
|
const done = plugins.smartpromise.defer<string[]>();
|
2017-03-04 20:10:46 +00:00
|
|
|
|
|
|
|
// handle absolute miniMatchFilter
|
2018-07-03 06:55:09 +00:00
|
|
|
let dirPath: string;
|
2017-03-04 20:10:46 +00:00
|
|
|
if (plugins.path.isAbsolute(miniMatchFilter)) {
|
2018-07-03 06:55:09 +00:00
|
|
|
dirPath = '/';
|
2017-03-04 20:10:46 +00:00
|
|
|
} else {
|
2018-07-03 06:55:09 +00:00
|
|
|
dirPath = dirPathArg;
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 02:11:10 +00:00
|
|
|
const options = {
|
2017-05-26 23:07:32 +00:00
|
|
|
cwd: dirPath,
|
2017-05-27 00:54:22 +00:00
|
|
|
nodir: true,
|
2020-08-10 20:58:22 +00:00
|
|
|
dot: true,
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2017-03-04 20:10:46 +00:00
|
|
|
plugins.glob(miniMatchFilter, options, (err, files: string[]) => {
|
|
|
|
if (err) {
|
2018-07-03 06:55:09 +00:00
|
|
|
console.log(err);
|
|
|
|
done.reject(err);
|
2016-09-30 14:16:11 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
done.resolve(files);
|
|
|
|
});
|
2019-09-27 09:00:17 +00:00
|
|
|
|
2019-06-26 19:17:58 +00:00
|
|
|
let fileList = await done.promise;
|
|
|
|
if (absolutePathsBool) {
|
2020-08-10 20:58:22 +00:00
|
|
|
fileList = fileList.map((filePath) => {
|
2019-06-26 19:17:58 +00:00
|
|
|
return plugins.path.resolve(plugins.path.join(dirPath, filePath));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileList;
|
2018-07-03 06:55:09 +00:00
|
|
|
};
|
2021-12-02 23:24:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* checks wether a file is ready for processing
|
|
|
|
*/
|
2021-12-20 14:11:21 +00:00
|
|
|
export const waitForFileToBeReady = async (filePathArg: string): Promise<void> => {
|
|
|
|
if (!plugins.path.isAbsolute(filePathArg)) {
|
2021-12-02 23:24:10 +00:00
|
|
|
filePathArg = plugins.path.resolve(filePathArg);
|
2021-12-20 14:11:21 +00:00
|
|
|
}
|
|
|
|
const limitedArray = new plugins.lik.LimitedArray<number>(3);
|
|
|
|
let fileReady = false;
|
|
|
|
while (!fileReady) {
|
|
|
|
const stats = await plugins.fsExtra.stat(filePathArg);
|
|
|
|
limitedArray.addOne(stats.size);
|
|
|
|
if (
|
|
|
|
limitedArray.array.length < 3 ||
|
|
|
|
!(limitedArray.array[0] === limitedArray[1] && limitedArray.array[1] === limitedArray[2])
|
|
|
|
) {
|
|
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
} else {
|
|
|
|
fileReady = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|