insomnia/packages/insomnia-app/app/sync/git/fs-client.ts
Dimitri Mitropoulos 5f4c19da35
[TypeScript] Phase 1 & 2 (#3370)
Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-05-12 18:35:00 +12:00

43 lines
1.3 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
type FSWraps =
| typeof fs.promises.readFile
| typeof fs.promises.writeFile
| typeof fs.promises.unlink
| typeof fs.promises.readdir
| typeof fs.promises.mkdir
| typeof fs.promises.rmdir
| typeof fs.promises.stat
| typeof fs.promises.lstat
| typeof fs.promises.readlink
| typeof fs.promises.symlink;
/** This is a client for isomorphic-git. {@link https://isomorphic-git.org/docs/en/fs} */
export const fsClient = (basePath: string) => {
console.log(`[fsClient] Created in ${basePath}`);
mkdirp.sync(basePath);
const wrap = (fn: FSWraps) => async (filePath: string, ...args: Array<any>) => {
const modifiedPath = path.join(basePath, path.normalize(filePath));
// @ts-expect-error -- TSCONVERSION
return fn(modifiedPath, ...args);
};
return {
promises: {
readFile: wrap(fs.promises.readFile),
writeFile: wrap(fs.promises.writeFile),
unlink: wrap(fs.promises.unlink),
readdir: wrap(fs.promises.readdir),
mkdir: wrap(fs.promises.mkdir),
rmdir: wrap(fs.promises.rmdir),
stat: wrap(fs.promises.stat),
lstat: wrap(fs.promises.lstat),
readlink: wrap(fs.promises.readlink),
symlink: wrap(fs.promises.symlink),
},
};
};