66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
// @push.rocks scope
|
|
import * as smartdata from '@push.rocks/smartdata';
|
|
import * as smartunique from '@push.rocks/smartunique';
|
|
import * as smarttime from '@push.rocks/smarttime';
|
|
import * as smartlog from '@push.rocks/smartlog';
|
|
import * as smartfsModule from '@push.rocks/smartfs';
|
|
import * as smarthash from '@push.rocks/smarthash';
|
|
import * as smartpath from '@push.rocks/smartpath';
|
|
import * as path from 'path';
|
|
|
|
// third party
|
|
import { MerkleTree } from 'merkletreejs';
|
|
|
|
const smartfs = new smartfsModule.SmartFs(
|
|
new smartfsModule.SmartFsProviderNode(),
|
|
);
|
|
|
|
const smartfile = {
|
|
fs: {
|
|
ensureDir: async (dirPath: string): Promise<void> => {
|
|
await smartfs.directory(dirPath).create();
|
|
},
|
|
toBuffer: async (filePath: string): Promise<Buffer> => {
|
|
return (await smartfs.file(filePath).read()) as Buffer;
|
|
},
|
|
toStringSync: async (filePath: string): Promise<string> => {
|
|
return (await smartfs.file(filePath).encoding('utf8').read()) as string;
|
|
},
|
|
fileExists: async (filePath: string): Promise<boolean> => {
|
|
return await smartfs.file(filePath).exists();
|
|
},
|
|
listFileTree: async (dirPath: string, pattern: string): Promise<string[]> => {
|
|
const suffix = pattern.replace(/^\*\*\/\*/, '');
|
|
try {
|
|
const entries = await smartfs.directory(dirPath).recursive().list();
|
|
return entries
|
|
.filter((entry) => entry.isFile && entry.path.endsWith(suffix))
|
|
.map((entry) => path.relative(dirPath, entry.path));
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message.includes('ENOENT')) {
|
|
return [];
|
|
}
|
|
throw error;
|
|
}
|
|
},
|
|
},
|
|
memory: {
|
|
toFs: async (content: string | Buffer, filePath: string): Promise<void> => {
|
|
await smartfs.directory(path.dirname(filePath)).create();
|
|
await smartfs.file(filePath).write(content);
|
|
},
|
|
},
|
|
};
|
|
|
|
export {
|
|
smartdata,
|
|
smartunique,
|
|
smarttime,
|
|
smartlog,
|
|
smartfs,
|
|
smartfile,
|
|
smarthash,
|
|
smartpath,
|
|
MerkleTree,
|
|
};
|