@softvisio/core
Version:
Softisio core
76 lines (55 loc) • 1.58 kB
JavaScript
import fs from "node:fs";
import path from "node:path";
import { pipeline } from "node:stream/promises";
import File from "#lib/file";
import Counter from "#lib/threads/counter";
export default class FileTree {
// properties
get size () {
return this.
}
get isEmpty () {
return !this.
}
// public
has ( path ) {
return this.
}
get ( path ) {
return this.
}
delete ( path ) {
delete this.
}
add ( file ) {
file = File.new( file );
this.
return file;
}
async write ( dirname ) {
var error;
dirname = path.resolve( dirname );
const counter = new Counter();
for ( const file of this.
counter.value++;
this.
.catch( e => ( error = e ) )
.finally( () => counter.value-- );
}
await counter.wait();
if ( error ) throw error;
}
[ Symbol.iterator ] () {
return this.
}
// private
async
const fullPath = path.join( dirname, file.path ),
targetDirname = path.dirname( fullPath );
await fs.promises.mkdir( targetDirname, {
"recursive": true,
} );
return pipeline( file.stream(), fs.createWriteStream( fullPath ) );
}
}