Skip to content

Commit

Permalink
fix: couple of bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Oct 15, 2024
1 parent e304a62 commit 47d403d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import path from 'node:path';
* Gets a Record of dependencies per file
* @param {*} inputFilePaths
* @param {*} resolver
* @returns {Record<string, string[]>} record of paths. As soon as the path in key changes, the array of files in the value need to be rebuilt.
* @returns {Promise<Record<string, string[]>>} record of paths. As soon as the path in key changes, the array of files in the value need to be rebuilt.
*/
export async function getDependencyGraph(inputFilePaths, resolver) {
export async function getDependencyGraph(inputRootDir, inputFilePaths, resolver) {
const deps = {};
for (const inputFilePath of inputFilePaths) {
if (/\.(md|css|html?)$/.test(inputFilePath)) {
const content = await (resolver ?? resolve)(inputFilePath);
const content = await (resolver ?? resolve)(path.normalize(path.join(inputRootDir, inputFilePath)));
if (typeof content !== 'string') {
continue;
}
Expand All @@ -24,7 +24,6 @@ export async function getDependencyGraph(inputFilePaths, resolver) {
deps[dependency].push(inputFilePath);
}
}
continue;
}
}
return deps;
Expand Down
21 changes: 8 additions & 13 deletions src/sissi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { serve } from './httpd.js';
import EventEmitter from 'node:stream';
import { readDataDir } from './data.js';
import { handleTemplateFile } from './transforms/template-data.js';
import { getDependencyGraph } from './dependency-graph.js';
import { resolve } from './resolver.js';

export class Sissi {

Expand All @@ -23,13 +25,14 @@ export class Sissi {
if (! this.data) {
this.data = await readDataDir(this.config);
}
const files = (await readdir(path.normalize(this.config.dir.input), {recursive: true})).filter(
const files = (filter instanceof Array) ? filter :
(await readdir(path.normalize(this.config.dir.input), {recursive: true})).filter(
(file) => {
if (! filter) return true;
if (filter instanceof RegExp) return filter.test(file);
}
);
const writtenFiles = []
const writtenFiles = [];
for (const file of files) {
writtenFiles.push(await this.processFile(file, eventEmitter));
}
Expand Down Expand Up @@ -75,17 +78,9 @@ export class Sissi {
}
lastExec.set(event.filename, performance.now());
console.log(`[${event.eventType}] ${event.filename}`);
const isIgnoredFile = (info.name.startsWith('_') && info.dir.includes(path.sep + '_'));
if (isIgnoredFile && info.ext === '.css') {
await this.build(/.css$/, eventEmitter);
continue;
}
if (isIgnoredFile && info.ext === '.html') {
await this.build(/.html$/, eventEmitter);
continue;
}
await this.processFile(event.filename, eventEmitter);

const deps = await getDependencyGraph(this.config.dir.input, await readdir(path.normalize(this.config.dir.input), {recursive: true}),
this.config.resolve || resolve);
await this.build([event.filename, ...(deps[event.filename]??[])], eventEmitter);
}
} catch (err) {
if (err.name === 'AbortError') return;
Expand Down
22 changes: 19 additions & 3 deletions tests/dependency-graph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ describe('Dependency Graph', () => {
}
}


it('should return a map, mapping each dependants per file', async () => {
it('should return a records, mapping each dependants per file', async () => {
const files = {
'index.html': withFrontmatter('# {{ title }}', {title: 'Hello', layout: 'base.html'}),
'_layouts/base.html': '<body>{{ content | safe }}</body>',
Expand All @@ -31,7 +30,24 @@ describe('Dependency Graph', () => {

assert.deepEqual(dependencies, {
'_layouts/base.html': ['index.html'],
})
});
});

it('should handle layouts depending on other layouts correctly', async () => {
const files = {
'index.html': withFrontmatter('# {{ title }}', {title: 'Hello', layout: 'base.html'}),
'_layouts/base.html': '<body>{{ content | safe }}</body>',
'_layouts/article.html': withFrontmatter('<article><h1>{{ title }}</h1>{{ content | safe }}</article>', {layout: 'base.html'})
};

const resolve = setupVFS(files);

const dependencies = await getDependencyGraph(Object.keys(files), resolve);

assert.deepEqual(dependencies, {
'_layouts/base.html': ['index.html', '_layouts/article.html'],
});
});


});

0 comments on commit 47d403d

Please sign in to comment.