Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement presets processing #931

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
343 changes: 306 additions & 37 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"@octokit/core": "4.2.4",
"@types/async": "^3.2.15",
"@types/chalk": "2.2.0",
"@types/glob": "^8.1.0",
"@types/html-escaper": "^3.0.0",
"@types/js-yaml": "4.0.9",
"@types/json-stringify-safe": "^5.0.3",
Expand All @@ -95,7 +94,7 @@
"commander": "^12.0.0",
"csp-header": "^5.2.1",
"esbuild": "^0.23.1",
"glob": "^8.0.3",
"glob": "^10.4.5",
"html-escaper": "^3.0.3",
"husky": "8.0.3",
"js-yaml": "4.1.0",
Expand All @@ -113,6 +112,7 @@
"typescript": "^5.4.5",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^1.1.3",
"vitest-when": "^0.5.0",
"walk-sync": "^3.0.0"
},
"engines": {
Expand Down
52 changes: 51 additions & 1 deletion src/commands/build/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {Run} from '../run';
import type {BuildConfig, BuildRawConfig} from '..';

import {join} from 'node:path';
import {Mock, describe, expect, it, vi} from 'vitest';
import {when} from 'vitest-when';
import {Build} from '..';
import {handler as originalHandler} from '../handler';
import {withConfigUtils} from '~/config';
Expand All @@ -13,6 +15,12 @@ var resolveConfig: Mock;

vi.mock('shelljs');
vi.mock('../handler');
vi.mock('../run', async (importOriginal) => {
return {
...((await importOriginal()) as {}),
copy: vi.fn(),
};
});
vi.mock('~/config', async (importOriginal) => {
resolveConfig = vi.fn((_path, {defaults, fallback}) => {
return defaults || fallback;
Expand All @@ -24,11 +32,53 @@ vi.mock('~/config', async (importOriginal) => {
};
});

export async function runBuild(args: string) {
type BuildState = {
globs?: Hash<string[]>;
files?: Hash<string>;
};
export function setupBuild(state: BuildState = {}): Build & {run: Run} {
const build = new Build();

build.apply();
build.hooks.BeforeAnyRun.tap('Tests', (run) => {
(build as Build & {run: Run}).run = run;

// @ts-ignore
run.glob = vi.fn(() => []);
run.copy = vi.fn();
run.write = vi.fn();
run.fs.writeFile = vi.fn();
// @ts-ignore
run.fs.readFile = vi.fn();
// @ts-ignore
run.logger.proc = vi.fn();
// @ts-ignore
run.logger.info = vi.fn();
// @ts-ignore
run.logger.warn = vi.fn();
// @ts-ignore
run.logger.error = vi.fn();

if (state.globs) {
for (const [pattern, files] of Object.entries(state.globs)) {
when(run.glob).calledWith(pattern, expect.anything()).thenResolve(files);
}
}

if (state.files) {
for (const [file, content] of Object.entries(state.files)) {
when(run.fs.readFile)
.calledWith(join(run.input, file), expect.anything())
.thenResolve(content);
}
}
});

return build as Build & {run: Run};
}

export async function runBuild(args: string, build?: Build) {
build = build || setupBuild();
await build.parse(['node', 'index'].concat(args.split(' ')));
}

Expand Down
105 changes: 105 additions & 0 deletions src/commands/build/core/vars/VarsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type {Preset, Presets} from './types';

import {dirname, join} from 'node:path';
import {merge} from 'lodash';
import {dump, load} from 'js-yaml';

import {Run} from '~/commands/build';
import {freeze, own} from '~/utils';
import {AsyncParallelHook, AsyncSeriesWaterfallHook} from 'tapable';

export type VarsServiceConfig = {
varsPreset: string;
vars: Hash;
};

type VarsServiceHooks = {
/**
* Async waterfall hook.
* Called after any presets.yaml was loaded.
*/
PresetsLoaded: AsyncSeriesWaterfallHook<[Presets, RelativePath]>;
/**
* Async parallel hook.
* Called after vars was resolved on any level.
* Vars data is sealed here.
*/
Resolved: AsyncParallelHook<[Preset, RelativePath]>;
};

export class VarsService {
hooks: VarsServiceHooks;

private run: Run;

private fs: Run['fs'];

private logger: Run['logger'];

private config: VarsServiceConfig;

private cache: Record<RelativePath, Hash> = {};

constructor(run: Run) {
this.run = run;
this.fs = run.fs;
this.logger = run.logger;
this.config = run.config;
this.hooks = {
PresetsLoaded: new AsyncSeriesWaterfallHook(['presets', 'path']),
Resolved: new AsyncParallelHook(['vars', 'path']),
};
}

async load(path: RelativePath) {
const varsPreset = this.config.varsPreset || 'default';
const file = join(dirname(path), 'presets.yaml');

if (this.cache[file]) {
return this.cache[file];
}

this.logger.proc(path);

const scopes = [];

if (dirname(path) !== '.') {
scopes.push(await this.load(dirname(path)));
}

try {
const presets = await this.hooks.PresetsLoaded.promise(
load(await this.fs.readFile(join(this.run.input, file), 'utf8')) as Presets,
file,
);

scopes.push(presets['default']);

if (varsPreset && varsPreset !== 'default') {
scopes.push(presets[varsPreset] || {});
}
} catch (error) {
if (!own(error, 'code') || error.code !== 'ENOENT') {
throw error;
}
}

scopes.push(this.config.vars);

this.cache[file] = freeze(merge({}, ...scopes));

await this.hooks.Resolved.promise(this.cache[file], file);

return this.cache[file];
}

dump(presets: Hash): string {
return dump(presets, {
lineWidth: 120,
});
}

entries() {
return Object.entries(this.cache);
}
}
57 changes: 57 additions & 0 deletions src/commands/build/core/vars/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`vars > service > load > should allow content extending in PresetsLoaded hook 1`] = `
"field1: value1
field2: value2
"
`;

exports[`vars > service > load > should allow content updating in PresetsLoaded hook 1`] = `
"field1: value2
"
`;

exports[`vars > service > load > should load presets file default scope 1`] = `
"field1: value1
field2: value2
"
`;

exports[`vars > service > load > should load presets file target scope 1`] = `
"field1: value3
field2: value2
"
`;

exports[`vars > service > load > should load super layers 1`] = `
"field1: value1
override1: value1
override2: value1
override3: value1
override4: value1
field2: value1
sub1: value1
sub2: value1
override5: value1
override6: value1
subsub1: value1
subsub2: value1
"
`;

exports[`vars > service > load > should override default presets with vars 1`] = `
"field1: value6
field2: value2
"
`;

exports[`vars > service > load > should override target presets with vars 1`] = `
"field1: value6
field2: value2
"
`;

exports[`vars > service > load > should use vars if presets not found 1`] = `
"field1: value6
"
`;
Loading
Loading