2020-04-26 20:33:39 +00:00
|
|
|
|
import path from 'path';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
import * as git from 'isomorphic-git';
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2021-04-01 01:36:11 +00:00
|
|
|
|
* An isometric-git FS client that can route to various client depending on what the filePath is.
|
2020-04-26 20:33:39 +00:00
|
|
|
|
*
|
2021-04-01 01:36:11 +00:00
|
|
|
|
* @param defaultFS – default client
|
|
|
|
|
* @param otherFS – map of path prefixes to clients
|
2020-04-26 20:33:39 +00:00
|
|
|
|
* @returns {{promises: *}}
|
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
|
export function routableFSClient(
|
|
|
|
|
defaultFS: git.PromiseFsClient,
|
|
|
|
|
otherFS: Record<string, git.PromiseFsClient>,
|
|
|
|
|
) {
|
2021-05-18 20:32:18 +00:00
|
|
|
|
const execMethod = async (method: string, filePath: string, ...args: any[]) => {
|
2020-04-26 20:33:39 +00:00
|
|
|
|
filePath = path.normalize(filePath);
|
|
|
|
|
|
|
|
|
|
for (const prefix of Object.keys(otherFS)) {
|
|
|
|
|
if (filePath.indexOf(path.normalize(prefix)) === 0) {
|
|
|
|
|
return otherFS[prefix].promises[method](filePath, ...args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uncomment this to debug operations
|
|
|
|
|
// console.log('[routablefs] Executing', method, filePath, { args });
|
|
|
|
|
// Fallback to default if no prefix matched
|
|
|
|
|
const result = await defaultFS.promises[method](filePath, ...args);
|
|
|
|
|
// Uncomment this to debug operations
|
|
|
|
|
// console.log('[routablefs] Executing', method, filePath, { args }, { result });
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
|
// @ts-expect-error -- TSCONVERSION declare and initialize together to avoid an error
|
|
|
|
|
const methods: git.CallbackFsClient = {};
|
2020-04-26 20:33:39 +00:00
|
|
|
|
methods.readFile = execMethod.bind(methods, 'readFile');
|
|
|
|
|
methods.writeFile = execMethod.bind(methods, 'writeFile');
|
|
|
|
|
methods.unlink = execMethod.bind(methods, 'unlink');
|
|
|
|
|
methods.readdir = execMethod.bind(methods, 'readdir');
|
|
|
|
|
methods.mkdir = execMethod.bind(methods, 'mkdir');
|
|
|
|
|
methods.rmdir = execMethod.bind(methods, 'rmdir');
|
|
|
|
|
methods.stat = execMethod.bind(methods, 'stat');
|
|
|
|
|
methods.lstat = execMethod.bind(methods, 'lstat');
|
|
|
|
|
methods.readlink = execMethod.bind(methods, 'readlink');
|
|
|
|
|
methods.symlink = execMethod.bind(methods, 'symlink');
|
|
|
|
|
return {
|
|
|
|
|
promises: methods,
|
|
|
|
|
};
|
|
|
|
|
}
|