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

Prepare to Toc service implementation #948

Merged
merged 9 commits into from
Dec 18, 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
715 changes: 252 additions & 463 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"@diplodoc/openapi-extension": "^2.6.0",
"@diplodoc/prettier-config": "^2.0.0",
"@diplodoc/search-extension": "^1.1.5",
"@diplodoc/transform": "^4.38.2",
"@diplodoc/transform": "^4.40.1",
"@diplodoc/tsconfig": "^1.0.2",
"@gravity-ui/page-constructor": "^5.29.1",
"@octokit/core": "4.2.4",
Expand All @@ -86,7 +86,7 @@
"@types/shelljs": "0.8.15",
"@types/tar-stream": "^2.2.2",
"@types/yargs": "17.0.24",
"@vitest/coverage-v8": "^1.2.1",
"@vitest/coverage-v8": "^2.1.8",
"ajv": "^8.11.0",
"async": "^3.2.4",
"axios": "^1.6.7",
Expand All @@ -111,7 +111,7 @@
"ts-dedent": "^2.2.0",
"typescript": "^5.4.5",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^1.1.3",
"vitest": "^2.1.8",
"vitest-when": "^0.5.0",
"walk-sync": "^3.0.0"
},
Expand Down
88 changes: 67 additions & 21 deletions src/commands/build/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {Run} from '../run';
import type {BuildConfig, BuildRawConfig} from '..';
import type {Mock, MockInstance} from 'vitest';

import {join} from 'node:path';
import {Mock, describe, expect, it, vi} from 'vitest';
import {describe, expect, it, vi} from 'vitest';
import {when} from 'vitest-when';
import {Build} from '..';
import {Run} from '../run';
import {handler as originalHandler} from '../handler';
import {withConfigUtils} from '~/config';

Expand All @@ -14,6 +15,7 @@ export const handler = originalHandler as Mock;
var resolveConfig: Mock;

vi.mock('shelljs');
vi.mock('../legacy-config');
vi.mock('../handler');
vi.mock('../run', async (importOriginal) => {
return {
Expand All @@ -32,8 +34,58 @@ vi.mock('~/config', async (importOriginal) => {
};
});

const Mocked = Symbol('Mocked');

export type RunSpy = Run & {
glob: MockInstance<Run['glob']>;
copy: MockInstance<Run['copy']>;
read: MockInstance<Run['read']>;
write: MockInstance<Run['write']>;
[Mocked]: boolean;
};

export function setupRun(config: DeepPartial<BuildConfig>, run?: Run): RunSpy {
run =
run ||
new Run({
input: '/dev/null/input',
output: '/dev/null/output',
...config,
} as BuildConfig);

const stringify = (arg: unknown) => {
if (typeof arg === 'object' && arg) {
return JSON.stringify(arg);
}

return String(arg);
};

const impl =
(method: string) =>
(...args: unknown[]) => {
throw new Error(
`Method ${method} with args\n${args.map(stringify).join('\n')} not implemented.`,
);
};

for (const method of ['glob', 'copy', 'read', 'write'] as string[]) {
// @ts-ignore
vi.spyOn(run, method).mockImplementation(impl(method));
}

for (const method of ['proc', 'info', 'warn', 'error'] as string[]) {
// @ts-ignore
vi.spyOn(run.logger, method).mockImplementation(() => {});
}

(run as RunSpy)[Mocked] = true;

return run as RunSpy;
}

type BuildState = {
globs?: Hash<string[]>;
globs?: Hash<NormalizedPath[]>;
files?: Hash<string>;
};
export function setupBuild(state: BuildState = {}): Build & {run: Run} {
Expand All @@ -43,21 +95,17 @@ export function setupBuild(state: BuildState = {}): Build & {run: Run} {
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 (!(run as RunSpy)[Mocked]) {
setupRun({}, run);
}

when(run.copy).calledWith(expect.anything(), expect.anything()).thenResolve();
when(run.copy)
.calledWith(expect.anything(), expect.anything(), expect.anything())
.thenResolve();
when(run.write).calledWith(expect.anything(), expect.anything()).thenResolve();
when(run.glob).calledWith('**/toc.yaml', expect.anything()).thenResolve([]);
when(run.glob).calledWith('**/presets.yaml', expect.anything()).thenResolve([]);

if (state.globs) {
for (const [pattern, files] of Object.entries(state.globs)) {
Expand All @@ -67,9 +115,7 @@ export function setupBuild(state: BuildState = {}): Build & {run: Run} {

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);
when(run.read).calledWith(join(run.input, file)).thenResolve(content);
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/commands/build/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ const staticContent = option({
});

const ignoreStage = option({
flags: '--ignore-stage <value>',
defaultInfo: Stage.SKIP,
desc: 'Ignore tocs with stage.',
flags: '--ignore-stage <value...>',
defaultInfo: [Stage.SKIP],
desc: 'Ignore tocs with selected stages.',
});

const addSystemMeta = option({
Expand Down
1 change: 1 addition & 0 deletions src/commands/build/core/meta/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {addSourcePath} from './utils';
17 changes: 17 additions & 0 deletions src/commands/build/core/meta/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {composeFrontMatter, extractFrontMatter} from '@diplodoc/transform/lib/frontmatter';

export function addSourcePath(fileContent: string, sourcePath: string) {
const [frontMatter, strippedContent] = extractFrontMatter(fileContent, sourcePath);

if (frontMatter.sourcePath) {
return fileContent;
}

return composeFrontMatter(
{
...frontMatter,
sourcePath,
},
strippedContent,
);
}
31 changes: 21 additions & 10 deletions src/commands/build/core/vars/VarsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {merge} from 'lodash';
import {dump, load} from 'js-yaml';

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

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

type VarsServiceHooks = {
Expand All @@ -30,9 +31,11 @@ type VarsServiceHooks = {
export class VarsService {
hooks: VarsServiceHooks;

private run: Run;
get entries() {
return [...Object.entries(this.cache)];
}

private fs: Run['fs'];
private run: Run;

private logger: Run['logger'];

Expand All @@ -42,7 +45,6 @@ export class VarsService {

constructor(run: Run) {
this.run = run;
this.fs = run.fs;
this.logger = run.logger;
this.config = run.config;
this.hooks = {
Expand All @@ -51,15 +53,28 @@ export class VarsService {
};
}

async init() {
const presets = await this.run.glob('**/presets.yaml', {
cwd: this.run.input,
ignore: this.config.ignore,
});

for (const preset of presets) {
await this.load(preset);
}
}

async load(path: RelativePath) {
path = normalizePath(path);

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);
this.logger.proc(file);

const scopes = [];

Expand All @@ -69,7 +84,7 @@ export class VarsService {

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

Expand Down Expand Up @@ -98,8 +113,4 @@ export class VarsService {
lineWidth: 120,
});
}

entries() {
return Object.entries(this.cache);
}
}
Loading
Loading