UNPKG

@netwerk-digitaal-erfgoed/ld-workbench

Version:

LDWorkbench is a Linked Data Transformation tool designed to use only SPARQL as transformation language.

52 lines 1.89 kB
import { createWriteStream, existsSync, statSync, mkdirSync, createReadStream, } from 'fs'; import { isFile, isFilePathString } from './utils/guards.js'; import { dirname } from 'path'; import chalk from 'chalk'; import { pipeline as streamPipeline } from 'stream/promises'; export default class File { constructor($path, skipExistsCheck = false) { this.$path = $path; this.skipExistsCheck = skipExistsCheck; this.validate(); } validate() { if (this.$isValid !== undefined) return this; if (!isFilePathString(this.$path)) { throw new Error(`The filename \`${this.$path}\` should start with \`file://\``); } this.$path = this.$path.replace(/^file:\/\//, ''); if (!this.skipExistsCheck && (!existsSync(this.$path) || !statSync(this.$path).isFile())) { throw new Error(`File not found: \`${this.$path}\``); } this.$isValid = true; return this; } get path() { return this.$path; } getStream(append = false) { if (existsSync(this.$path)) { // throw new Error(`File already exists: \`${this.$path}\``) } if (!existsSync(dirname(this.$path))) { mkdirSync(dirname(this.$path), { recursive: true }); } return createWriteStream(this.$path, append ? { flags: 'a' } : {}); } toString() { return this.$path; } static is(value) { return isFile(value); } async write(pipeline, progress) { const stageNames = Array.from(pipeline.stages.keys()); for (const stageName of stageNames) { progress.suffixText(chalk.bold(stageName)); await streamPipeline(createReadStream(pipeline.stages.get(stageName).destinationPath), this.getStream(true)); } } } //# sourceMappingURL=file.js.map