Skip to content

Commit

Permalink
feat(#35): multiple style sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Jul 18, 2024
1 parent 62c7bce commit ba14343
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/parser/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit ba14343

Please sign in to comment.