Skip to content

Commit

Permalink
Implement output file transform hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mjswensen committed Jun 19, 2024
1 parent 26415a1 commit 0bc5360
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ npm install themer
1. An array of [`ColorSet` objects](#create-custom-colorsets), or string identifiers of [`themer`'s built-in color sets](#themer-color-sets)
2. An array of [`Template` objects](#create-custom-templates), or string identifiers of [`themer`'s built-in templates](#themer-templates)
3. A `RenderOptions` object used to specify the resolution of the outputted wallpaper images
4. (Optional) an `OutputFileTransform` async generator function that transforms the files generated by the provided templates. This function runs between each template's `render` and `renderInstructions` functions.

The objects yielded by the generator are `OutputFile`s.

Expand Down
1 change: 1 addition & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export {
allBuiltInTemplates,
type BuiltInTemplate,
} from './template/all.js';
export { type OutputFileTransform } from './transform/index.js';
export { themer as default } from './themer.js';
46 changes: 46 additions & 0 deletions cli/src/themer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import test from 'ava';
import template from './fixture/template.js';
import { themer } from './themer.js';
import { OutputFileTransform } from './transform/index.js';
import { basename } from 'node:path';

const backupFile: OutputFileTransform = async function* (file) {
yield file;
yield {
...file,
path: `${file.path}.bak`,
};
};

test('supports file post-processing', async (t) => {
const files = [];
for await (const file of themer(
['default'],
[template],
{ wallpaperSizes: [] },
backupFile,
)) {
files.push(file);
}

t.plan(9);

const expectedPaths = [
'Default/Test/themer-default-dark.txt',
'Default/Test/themer-default-dark.txt.bak',
'Default/Test/themer-default-light.txt',
'Default/Test/themer-default-light.txt.bak',
];

const readme = files.find((file) => file.path.endsWith('README.md'));
t.assert(readme, 'No README file in output');

for (const path of expectedPaths) {
t.assert(
files.find((file) => file.path === path),
`Output files must contain ${path}`,
);
const base = basename(path);
t.assert(readme?.content.includes(base), `README must contain ${base}`);
}
});
18 changes: 11 additions & 7 deletions cli/src/themer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { ColorSet, prepareColorSet } from './color-set/index.js';
import { BuiltInColorSet, resolveColorSet } from './color-set/all.js';
import type { Template, OutputFile, RenderOptions } from './template/index.js';
import { BuiltInTemplate, resolveTemplate } from './template/all.js';
import { OutputFileTransform, noopTransform } from './transform/index.js';

export async function* themer(
colorSets: (BuiltInColorSet | ColorSet)[],
templates: (BuiltInTemplate | Template)[],
options: RenderOptions,
transform: OutputFileTransform = noopTransform,
): AsyncGenerator<OutputFile> {
for (const colorSet of colorSets) {
const resolvedColorSet = resolveColorSet(colorSet);
Expand All @@ -16,13 +18,15 @@ export async function* themer(
const rootDir = fullColorSet.name;
for (const template of resolvedTemplates) {
const templatePaths: string[] = [];
for await (const file of template.render(fullColorSet, options)) {
const path = `${template.name}/${file.path}`;
yield {
...file,
path: `${rootDir}/${path}`,
};
templatePaths.push(path);
for await (const renderedFile of template.render(fullColorSet, options)) {
for await (const file of transform(renderedFile)) {
const path = `${template.name}/${file.path}`;
yield {
...file,
path: `${rootDir}/${path}`,
};
templatePaths.push(path);
}
}
instructions.push(`## ${template.name}`);
instructions.push(
Expand Down
9 changes: 9 additions & 0 deletions cli/src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OutputFile } from '../template/index.js';

export type OutputFileTransform = (
file: OutputFile,
) => AsyncGenerator<OutputFile>;

export const noopTransform: OutputFileTransform = async function* (file) {
yield file;
};

0 comments on commit 0bc5360

Please sign in to comment.