-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
2,171 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Verify Files
|
||
}; | ||
|
||
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 GitHub Actions / Verify Files
|
||
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(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
} | ||
}, | ||
}); | ||
|
||
test('should handle arg', '--lint-disabled', { | ||
lint: {enabled: false} | ||
}); | ||
|
||
test( | ||
'should handle config', | ||
'', | ||
{ | ||
lintDisabled: true, | ||
}, | ||
{ | ||
lint: {enabled: false} | ||
}, | ||
); | ||
|
||
test( | ||
'should handle enabled allowHtml', | ||
'', | ||
{ | ||
lintDisabled: false, | ||
allowHtml: true, | ||
}, | ||
{ | ||
lint: { | ||
enabled: true, | ||
config: { | ||
'log-levels': { | ||
MD033: 'disabled', | ||
}, | ||
} | ||
}, | ||
}, | ||
); | ||
|
||
test( | ||
'should handle disabled allowHtml', | ||
'', | ||
{ | ||
lintDisabled: false, | ||
allowHtml: false, | ||
}, | ||
{ | ||
lint: { | ||
enabled: true, | ||
config: { | ||
'log-levels': { | ||
MD033: 'error', | ||
}, | ||
} | ||
}, | ||
}, | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.