Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed May 15, 2024
1 parent 016a5a6 commit 2a332ad
Show file tree
Hide file tree
Showing 30 changed files with 2,171 additions and 232 deletions.
67 changes: 67 additions & 0 deletions src/commands/build/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type {Run} from '../run';
import type {Mock} from 'vitest';
import type {BuildConfig} from '..';

import {expect, it, vi} from 'vitest';
import {Build} from '..';
import {handler as originalHandler} from '../handler';

export const handler = originalHandler as Mock;

// eslint-disable-next-line no-var
var resolveConfig: Mock;

vi.mock('../handler');
vi.mock('~/config', async (importOriginal) => {
resolveConfig = vi.fn((_path, {defaults, fallback}) => {
return defaults || fallback;
});

return {
...((await importOriginal()) as {}),
resolveConfig,
};
});

export async function runBuild(args: string) {
const build = new Build();

build.apply();

await build.parse(['node', 'index'].concat(args.split(' ')));
}

type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Record<any, any> ? DeepPartial<T[P]> : T[P];

Check warning on line 35 in src/commands/build/__tests__/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type

Check warning on line 35 in src/commands/build/__tests__/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
};

export function testConfig(name: string, args: string, result: DeepPartial<BuildConfig>): void;
export function testConfig(
name: string,
args: string,
config: DeepPartial<BuildConfig>,
result: DeepPartial<BuildConfig>,
): void;
export function testConfig(name: string, args: string, config: any, result?: any): void {

Check warning on line 45 in src/commands/build/__tests__/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type

Check warning on line 45 in src/commands/build/__tests__/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
it(name, async () => {
if (!result) {
result = config;
config = {};
}

resolveConfig.mockImplementation((_path, {defaults}) => {
return {
...defaults,
...config,
};
});

handler.mockImplementation((run: Run) => {
expect(run.config).toMatchObject(result as Partial<BuildConfig>);
});

await runBuild('--input ./input --output ./output ' + args);

expect(handler).toBeCalled();
});
}
134 changes: 134 additions & 0 deletions src/commands/build/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import {bold, underline} from 'chalk';
import {options as globalOptions} from '~/program/config';
import {option} from '~/config';
import {Stage} from '~/constants';

// need-to-sanitize-html

export enum OutputFormat {
md = 'md',
html = 'html',
}

const outputFormat = option({
flags: '-f, --output-format <value>',
defaultInfo: 'html',
choices: ['html', 'md'],
desc: `
Format of output files. (html or md)
If ${bold('html')} is selected, then renders md to static html files.
(See also ${underline('--static-content')} option)
If ${bold('md')} is selected, then renders md to prepared md filed
enriched by additional metadata.
(Useful for complex documentation servers with runtime rendering)
`,
});

const vars = option({
flags: '-v, --vars <json>',
desc: `
Pass list of variables directly to build.
Variables should be passed in JSON format.
Passed variables overrides the same in presets.yaml
Example:
{{PROGRAM}} -i ./ -o ./build -v '{"name":"test"}'
`,
parser: (value) => JSON.parse(value),
});

const varsPreset = option({
flags: '--vars-preset <value>',
desc: `
Select vars preset of documentation.
Selected preset will be merged with default section of presets.yaml
`,
});

const allowHTML = option({
flags: '--allowHTML',
desc: 'Allow to use HTML in Markdown files.',
defaultInfo: true,
deprecated: 'Use --allow-html for consistency.',
});

const allowHtml = option({
flags: '--allow-html',
desc: 'Allow to use HTML in Markdown files.',
defaultInfo: true,
});

const hidden = option({
flags: '--hidden <glob...>',
desc: `
Do not process paths matched by glob.
Example:
{{PROGRAM}} -i ./input -o ./output --hidden *.bad.md
`,
});

// TODO: options below are not beautified.
const addMapFile = option({
flags: '--add-map-file',
desc: 'Should add all paths of documentation into file.json.',
});

const removeHiddenTocItems = option({
flags: '--remove-hidden-toc-items',
desc: 'Remove from Toc all items marked as hidden.',
});

const resources = option({
flags: '--resource, --resources <value...>',
desc: 'Allow loading custom resources into statically generated pages.',
// parser: toArray,
});

const allowCustomResources = option({
flags: '--allow-custom-resources',
desc: 'Allow loading custom resources into statically generated pages.',
});

const staticContent = option({
flags: '--static-content',
desc: 'Allow loading custom resources into statically generated pages.',
});

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

const addSystemMeta = option({
flags: '--add-system-meta',
desc: 'Should add system section variables form presets into files meta data.',
});

const buildDisabled = option({
flags: '--build-disabled',
desc: 'Disable building.',
});

export const options = {
input: globalOptions.input,
output: globalOptions.output,
config: globalOptions.config,
outputFormat,
varsPreset,
vars,
allowHTML,
allowHtml,
addMapFile,
removeHiddenTocItems,
resources,
allowCustomResources,
staticContent,
hidden,
ignoreStage,
addSystemMeta,
buildDisabled,
};
16 changes: 16 additions & 0 deletions src/commands/build/features/contributors/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {option} from '~/config';

const contributors = option({
flags: '--contributors',
desc: 'Should attach contributors into files',
});

const ignoreAuthorPatterns = option({
flags: '--ignore-author-patterns <string>',
desc: 'Ignore authors if they contain passed string',
});

export const options = {
contributors,
ignoreAuthorPatterns,
};
30 changes: 30 additions & 0 deletions src/commands/build/features/contributors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {Build} from '../..';
import type {Command} from '~/config';
import {defined} from '~/config';
import {options} from './config';

export type ContributorsArgs = {
contributors?: boolean;
ignoreAuthorPatterns?: string[];
};

export type ContributorsConfig = {
contributors: boolean;
ignoreAuthorPatterns: string[];
};

export class Contributors {
apply(program: Build) {
program.hooks.Command.tap('Contributors', (command: Command) => {
command.addOption(options.contributors);
command.addOption(options.ignoreAuthorPatterns);
});

program.hooks.Config.tap('Contributors', (config, args) => {
config.contributors = defined('contributors', args, config) || false;
config.ignoreAuthorPatterns = defined('ignoreAuthorPatterns', args, config) || [];

return config;
});
}
}
18 changes: 18 additions & 0 deletions src/commands/build/features/linter/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {option} from '~/config';

const lint = option({
flags: '--lint',
desc: 'Toggle file linting.',
});

const lintDisabled = option({
flags: '--lint-disabled',
desc: 'Disable linting.',
hidden: true,
deprecated: 'Use --no-lint instead',
});

export const options = {
lint,
lintDisabled,
};
74 changes: 74 additions & 0 deletions src/commands/build/features/linter/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {describe, vi} from 'vitest';
import {testConfig as test} from '../../__tests__';

vi.mock('~/cmd/publish/upload');

describe('Build linter feature', () => {
describe('config', () => {
describe('lintDisabled', () => {
test('should handle default', '', {
lint: {
enabled: true,
config: {
'log-levels': {
MD033: 'disabled',
},
}

Check failure on line 16 in src/commands/build/features/linter/index.spec.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Insert `,`
},
});

test('should handle arg', '--lint-disabled', {
lint: {enabled: false}

Check failure on line 21 in src/commands/build/features/linter/index.spec.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Insert `,`
});

test(
'should handle config',
'',
{
lintDisabled: true,
},
{
lint: {enabled: false}

Check failure on line 31 in src/commands/build/features/linter/index.spec.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Insert `,`
},
);

test(
'should handle enabled allowHtml',
'',
{
lintDisabled: false,
allowHtml: true,
},
{
lint: {
enabled: true,
config: {
'log-levels': {
MD033: 'disabled',
},
}

Check failure on line 49 in src/commands/build/features/linter/index.spec.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Insert `,`
},
},
);

test(
'should handle disabled allowHtml',
'',
{
lintDisabled: false,
allowHtml: false,
},
{
lint: {
enabled: true,
config: {
'log-levels': {
MD033: 'error',
},
}

Check failure on line 68 in src/commands/build/features/linter/index.spec.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Insert `,`
},
},
);
});
});
});
Loading

0 comments on commit 2a332ad

Please sign in to comment.