From ba143430b4720fc42c16dc5d4699c6d7f3ece3f7 Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Tue, 16 Jul 2024 09:10:10 +0200 Subject: [PATCH] feat(#35): multiple style sheets --- src/parser/config.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/parser/config.ts b/src/parser/config.ts index 60aad88d..789c7ff8 100644 --- a/src/parser/config.ts +++ b/src/parser/config.ts @@ -30,6 +30,20 @@ const configPaths = [ path.join(homedir(), '.vivify.json'), ]; +// read contents of file at paths or files at paths +const getFileContents = (paths: string[] | string | undefined): string => { + if (paths === undefined) return ''; + const getFileContent = (p: string): string => { + const resolved = p[0] === '~' ? path.join(homedir(), p.slice(1)) : p; + return fs.existsSync(resolved) ? fs.readFileSync(resolved, 'utf8') : ''; + }; + + if (Array.isArray(paths)) { + return paths.map(getFileContent).join('\n'); + } + return getFileContent(paths); +}; + const getConfig = (): Config => { let config = undefined; // greedily get config @@ -44,11 +58,8 @@ const getConfig = (): Config => { if (config === undefined) return defaultConfig; // get styles - if (config.styles && config.styles.length > 0) { - const stylePath = - config.styles[0] === '~' ? path.join(homedir(), config.styles.slice(1)) : config.styles; - config.styles = fs.existsSync(stylePath) ? fs.readFileSync(stylePath, 'utf8') : ''; - } + config.styles = getFileContents(config.styles); + // fill missing values from default config for (const [key, value] of Object.entries(defaultConfig)) { if (config[key] === undefined) config[key] = value;