diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-02-05 08:24:10 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-02-05 09:13:04 -0400 |
commit | f10d5559731a18097dcd6921e2bb343c36269407 (patch) | |
tree | a38d7877998072811e30b34636b14cd3ab69f2f6 | |
parent | f0af4edfa87e9518703b7c9ab35244911f0a3ac2 (diff) |
handle missing data files more gracefully
-rw-r--r-- | src/data/yaml.js | 37 | ||||
-rw-r--r-- | src/util/io.js | 4 |
2 files changed, 38 insertions, 3 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js index b00d68ee..d6791574 100644 --- a/src/data/yaml.js +++ b/src/data/yaml.js @@ -4,7 +4,7 @@ import * as path from 'path'; import yaml from 'js-yaml'; -import {readFile} from 'fs/promises'; +import {readFile, stat} from 'fs/promises'; import {inspect as nodeInspect} from 'util'; import T from './things/index.js'; @@ -945,6 +945,30 @@ export async function loadAndProcessDataDocuments({dataPath}) { ? await callAsync(dataStep.file, dataPath) : dataStep.file); + const statResult = await callAsync(() => + stat(file).then( + () => true, + error => { + if (error.code === 'ENOENT') { + return false; + } else { + throw error; + } + })); + + if (statResult === false) { + const saveResult = call(dataStep.save, { + [documentModes.allInOne]: [], + [documentModes.oneDocumentTotal]: {}, + }[documentMode]); + + if (!saveResult) return; + + Object.assign(wikiDataResult, saveResult); + + return; + } + const readResult = await callAsync(readFile, file, 'utf-8'); if (!readResult) { @@ -992,7 +1016,16 @@ export async function loadAndProcessDataDocuments({dataPath}) { let files = ( typeof dataStep.files === 'function' - ? await callAsync(dataStep.files, dataPath) + ? await callAsync(() => + dataStep.files(dataPath).then( + files => files, + error => { + if (error.code === 'ENOENT') { + return []; + } else { + throw error; + } + })) : dataStep.files ); diff --git a/src/util/io.js b/src/util/io.js index 6cc89b56..12e87f4d 100644 --- a/src/util/io.js +++ b/src/util/io.js @@ -12,7 +12,9 @@ export async function findFiles(dataPath, { try { files = await readdir(dataPath); } catch (error) { - throw new AggregateError([error], `Failed to list files from ${dataPath}`); + throw Object.assign( + new AggregateError([error], `Failed to list files from ${dataPath}`), + {code: error.code}); } return files |