-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbinding.ts
More file actions
93 lines (86 loc) · 2.21 KB
/
binding.ts
File metadata and controls
93 lines (86 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { createRequire } from 'node:module';
import type {
CategoryConfig,
PluginAnswer,
PluginSetupBinding,
} from '@code-pushup/models';
import {
answerBoolean,
answerNonEmptyArray,
singleQuote,
} from '@code-pushup/utils';
import { PLUGIN_SLUG, PLUGIN_TITLE } from './constants.js';
const { name: PACKAGE_NAME } = createRequire(import.meta.url)(
'../../package.json',
) as typeof import('../../package.json');
const DEFAULT_PATTERNS: [string, ...string[]] = [
'src/**/*.ts',
'src/**/*.js',
'!**/node_modules',
];
const CATEGORY: CategoryConfig = {
slug: 'docs',
title: 'Documentation',
description: 'Measures how much of your code is **documented**.',
refs: [
{
type: 'group',
plugin: PLUGIN_SLUG,
slug: 'documentation-coverage',
weight: 1,
},
],
};
type JsDocsOptions = {
patterns: [string, ...string[]];
categories: boolean;
};
export const jsDocsSetupBinding = {
slug: PLUGIN_SLUG,
title: PLUGIN_TITLE,
packageName: PACKAGE_NAME,
prompts: async () => [
{
key: 'jsdocs.patterns',
message: 'Source file patterns (comma-separated)',
type: 'input',
default: DEFAULT_PATTERNS.join(', '),
},
{
key: 'jsdocs.categories',
message: 'Add JSDocs categories?',
type: 'confirm',
default: true,
},
],
generateConfig: (answers: Record<string, PluginAnswer>) => {
const options = parseAnswers(answers);
return {
imports: [
{ moduleSpecifier: PACKAGE_NAME, defaultImport: 'jsDocsPlugin' },
],
pluginInit: formatPluginInit(options.patterns),
...(options.categories ? { categories: [CATEGORY] } : {}),
};
},
} satisfies PluginSetupBinding;
function parseAnswers(answers: Record<string, PluginAnswer>): JsDocsOptions {
return {
patterns: answerNonEmptyArray(
answers,
'jsdocs.patterns',
DEFAULT_PATTERNS[0],
),
categories: answerBoolean(answers, 'jsdocs.categories'),
};
}
function formatPluginInit([first, ...rest]: [string, ...string[]]): string[] {
if (rest.length === 0) {
return [`jsDocsPlugin(${singleQuote(first)}),`];
}
return [
'jsDocsPlugin([',
...[first, ...rest].map(p => ` ${singleQuote(p)},`),
']),',
];
}