Skip to content

Commit

Permalink
feat(#181): relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Sep 5, 2024
1 parent b665180 commit 1c01961
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,20 @@ const configPaths = [
];

// read contents of file at paths or files at paths
const getFileContents = (paths: string[] | string | undefined): string => {
const getFileContents = (
paths: string[] | string | undefined,
baseDir: string | undefined,
): string => {
if (paths === undefined) return '';

const getFileContent = (p: string): string => {
const resolved = p[0] === '~' ? path.join(homedir(), p.slice(1)) : p;
let resolved = p;
if (resolved[0] === '~') {
resolved = path.join(homedir(), p.slice(1));
}
if (resolved[0] !== '/' && baseDir !== undefined) {
resolved = path.join(baseDir, resolved);
}
return fs.existsSync(resolved) ? fs.readFileSync(resolved, 'utf8') : '';
};

Expand All @@ -60,11 +70,13 @@ const getFileContents = (paths: string[] | string | undefined): string => {

const config = ((): Config => {
let config = undefined;
let configBaseDir = undefined;
// greedily find config
for (const cp of configPaths) {
if (!fs.existsSync(cp)) continue;
try {
config = JSON.parse(fs.readFileSync(cp, 'utf8'));
configBaseDir = path.dirname(cp);
} catch {}
break;
}
Expand All @@ -77,9 +89,9 @@ const config = ((): Config => {
}

// get styles, scripts and ignore files
config.styles = getFileContents(config.styles);
config.scripts = getFileContents(config.scripts);
config.dirListIgnore = getFileContents(config.dirListIgnore)
config.styles = getFileContents(config.styles, configBaseDir);
config.scripts = getFileContents(config.scripts, configBaseDir);
config.dirListIgnore = getFileContents(config.dirListIgnore, configBaseDir)
.split('\n')
.filter((pattern) => pattern !== '' && pattern[0] !== '#');

Expand Down

0 comments on commit 1c01961

Please sign in to comment.