Skip to content

Commit 68ae884

Browse files
committed
Added renderer config flags and package.
Added parseConfigFromCLI method to core.
1 parent cd40c2c commit 68ae884

File tree

14 files changed

+154
-28
lines changed

14 files changed

+154
-28
lines changed

packages/doxdox-cli/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import doxdox, {
1818
getProjectPackage,
1919
getRootDirPath,
2020
loadPlugin,
21+
parseConfigFromCLI,
2122
parseIgnoreConfig,
2223
sanitizePath
2324
} from 'doxdox-core';
@@ -109,6 +110,8 @@ const overridePackage = String(
109110
}
110111
);
111112

113+
const cliConfig = parseConfigFromCLI(args.raw);
114+
112115
const nodeModulesDir = await findParentNodeModules(
113116
dirname(sanitizePath(import.meta.url))
114117
);
@@ -141,7 +144,11 @@ const overridePackage = String(
141144
name: overrideName || pkg.name || 'Untitled Project',
142145
description: overrideDescription || pkg.description || '',
143146
version: pkg.version,
144-
homepage: pkg.homepage
147+
homepage: pkg.homepage,
148+
config: {
149+
...pkg.doxdoxConfig,
150+
...cliConfig
151+
}
145152
});
146153

147154
if (overrideOutput) {

packages/doxdox-core/src/types.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
export interface Options {
2-
name?: string;
3-
description?: string;
4-
version?: string;
5-
homepage?: string;
6-
}
1+
export type Config = {
2+
[key in string]?: string | boolean;
3+
};
74

85
export interface Doc extends Options {
96
files: File[];
@@ -24,8 +21,17 @@ export interface Method {
2421
private: boolean;
2522
}
2623

27-
export interface Package extends Options {
24+
export interface Options extends Package {
25+
config?: Config;
26+
}
27+
28+
export interface Package {
2829
exports?: string;
30+
name?: string;
31+
description?: string;
32+
version?: string;
33+
homepage?: string;
34+
doxdoxConfig?: Config;
2935
}
3036

3137
export interface Param {

packages/doxdox-core/src/utils.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getRootDirPath,
1010
isDirectory,
1111
isFile,
12+
parseConfigFromCLI,
1213
parseIgnoreConfig,
1314
sanitizePath,
1415
slugify
@@ -98,6 +99,27 @@ describe('utils', () => {
9899
});
99100
});
100101

102+
describe('parseConfigFromCLI', () => {
103+
it('parse config', () => {
104+
expect(
105+
parseConfigFromCLI([
106+
['--version', true],
107+
['-c', 'key1=value1'],
108+
['-c', 'key2=value2'],
109+
['--config', 'boolean1=true'],
110+
['--config', 'boolean2=false']
111+
])
112+
).toEqual(
113+
expect.objectContaining({
114+
key1: 'value1',
115+
key2: 'value2',
116+
boolean1: true,
117+
boolean2: false
118+
})
119+
);
120+
});
121+
});
122+
101123
describe('parseIgnoreConfig', () => {
102124
it('parse ignore config', () => {
103125
expect(

packages/doxdox-core/src/utils.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,17 @@ export const getProjectPackage = async (cwd: string): Promise<Package> => {
8888
const projectPackagePath = await findFileInPath(cwd);
8989

9090
if (projectPackagePath) {
91-
const { name, description, version, exports, homepage } = JSON.parse(
92-
await fs.readFile(projectPackagePath, 'utf8')
93-
);
94-
95-
return { name, description, version, exports, homepage };
91+
const { name, description, version, exports, homepage, doxdoxConfig } =
92+
JSON.parse(await fs.readFile(projectPackagePath, 'utf8'));
93+
94+
return {
95+
name,
96+
description,
97+
version,
98+
exports,
99+
homepage,
100+
doxdoxConfig
101+
};
96102
}
97103

98104
return {};
@@ -141,6 +147,33 @@ export const isFile = async (path: string): Promise<boolean> => {
141147
}
142148
};
143149

150+
/**
151+
* Parse config key/value pairs from raw CLI flags.
152+
*
153+
* console.log(await parseConfigFromCLI([['-c', 'key=value']]));
154+
*
155+
* @param {[string, string | boolean][]} rawFlags Raw flags from the CLI.
156+
* @return {{ [key in string]: string | boolean }} Configs key/value pairs.
157+
* @public
158+
*/
159+
160+
export const parseConfigFromCLI = (
161+
rawFlags: [string, string | boolean][]
162+
): {
163+
[key in string]: string | boolean;
164+
} =>
165+
rawFlags
166+
.filter(([flag]) => ['-c', '--config'].includes(flag))
167+
.reduce((all, [, config]) => {
168+
const [key, value] = String(config).split('=');
169+
170+
if (['true', 'false'].includes(value)) {
171+
return { ...all, [key]: value === 'true' ? true : false };
172+
} else {
173+
return { ...all, [key]: value };
174+
}
175+
}, {});
176+
144177
/**
145178
* Parse contents of ignore file.
146179
*

packages/doxdox-renderer-bootstrap/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import hljs from 'highlight.js';
66

77
import { Doc, File, Method } from 'doxdox-core';
88

9+
import { CustomConfig } from './types';
10+
911
const md = new MarkdownIt({
1012
html: true,
1113
linkify: true,
@@ -205,10 +207,15 @@ export default async (doc: Doc): Promise<string> => `<!DOCTYPE html>
205207
Documentation generated with
206208
<a href="https://github.com/docsbydoxdox/doxdox">doxdox</a>.
207209
</p>
208-
<p>
210+
${
211+
!doc.config ||
212+
(doc.config as CustomConfig)['hideGeneratedTimestamp'] !== true
213+
? `<p>
209214
Generated on
210215
${new Date().toDateString()} ${new Date().toTimeString()}
211-
</p>
216+
</p>`
217+
: ''
218+
}
212219
</div>
213220
</footer>
214221
<script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Config } from 'doxdox-core';
2+
3+
export interface CustomConfig extends Config {
4+
hideGeneratedTimestamp?: boolean;
5+
}

packages/doxdox-renderer-dash/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { getRootDirPath, slugify } from 'doxdox-core';
1616

1717
import { Doc, Method } from 'doxdox-core';
1818

19+
import { CustomConfig } from './types';
20+
1921
const md = new MarkdownIt({
2022
html: true,
2123
linkify: true,
@@ -99,10 +101,15 @@ ${param.description ? `<p>${param.description}</p>` : ''}`
99101
Documentation generated with
100102
<a href="https://github.com/docsbydoxdox/doxdox">doxdox</a>.
101103
</p>
102-
<p>
104+
${
105+
!doc.config ||
106+
(doc.config as CustomConfig)['hideGeneratedTimestamp'] !== true
107+
? `<p>
103108
Generated on
104109
${new Date().toDateString()} ${new Date().toTimeString()}
105-
</p>
110+
</p>`
111+
: ''
112+
}
106113
</div>
107114
</footer>
108115
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Config } from 'doxdox-core';
2+
3+
export interface CustomConfig extends Config {
4+
hideGeneratedTimestamp?: boolean;
5+
}

packages/doxdox-renderer-github-wiki/src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import admzip from 'adm-zip';
44

55
import { markdownTable } from 'markdown-table';
66

7-
import { Doc, Method } from 'doxdox-core';
7+
import { Config, Doc, Method } from 'doxdox-core';
88

9-
const renderMethod = (method: Method) => `## ${method.fullName}
9+
import { CustomConfig } from './types';
10+
11+
const renderMethod = (method: Method, config: Config | undefined) => `## ${
12+
method.fullName
13+
}
1014
1115
${method.description}
1216
@@ -37,9 +41,11 @@ ${param.description || ''}`
3741
}
3842
3943
Documentation generated with [doxdox](https://github.com/docsbydoxdox/doxdox)
40-
41-
Generated on ${new Date().toDateString()} ${new Date().toTimeString()}
42-
`;
44+
${
45+
!config || (config as CustomConfig)['hideGeneratedTimestamp'] !== true
46+
? `\nGenerated on ${new Date().toDateString()} ${new Date().toTimeString()}\n`
47+
: ''
48+
}`;
4349

4450
export default async (doc: Doc): Promise<string | Buffer> => {
4551
const zip = new admzip();
@@ -50,7 +56,7 @@ export default async (doc: Doc): Promise<string | Buffer> => {
5056
file.methods.map(async method =>
5157
zip.addFile(
5258
join(file.path, `${method.name}.md`),
53-
Buffer.from(renderMethod(method), 'utf-8')
59+
Buffer.from(renderMethod(method, doc.config), 'utf-8')
5460
)
5561
)
5662
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Config } from 'doxdox-core';
2+
3+
export interface CustomConfig extends Config {
4+
hideGeneratedTimestamp?: boolean;
5+
}

packages/doxdox-renderer-markdown/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { markdownTable } from 'markdown-table';
22

33
import { Doc, File, Method } from 'doxdox-core';
44

5+
import { CustomConfig } from './types';
6+
57
const renderMethod = (method: Method) => `## ${method.fullName}
68
79
${method.description}
@@ -49,6 +51,9 @@ ${doc.files
4951
.trim()}
5052
5153
Documentation generated with [doxdox](https://github.com/docsbydoxdox/doxdox)
52-
53-
Generated on ${new Date().toDateString()} ${new Date().toTimeString()}
54-
`;
54+
${
55+
!doc.config ||
56+
(doc.config as CustomConfig)['hideGeneratedTimestamp'] !== true
57+
? `\nGenerated on ${new Date().toDateString()} ${new Date().toTimeString()}\n`
58+
: ''
59+
}`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Config } from 'doxdox-core';
2+
3+
export interface CustomConfig extends Config {
4+
hideGeneratedTimestamp?: boolean;
5+
}

packages/doxdox-renderer-pdf/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { create } from 'html-pdf';
88

99
import { Doc, File, Method } from 'doxdox-core';
1010

11+
import { CustomConfig } from './types';
12+
1113
const md = new MarkdownIt({
1214
html: true,
1315
linkify: true,
@@ -149,10 +151,16 @@ export default async (doc: Doc): Promise<Buffer> => {
149151
Documentation generated with
150152
<a href="https://github.com/docsbydoxdox/doxdox">doxdox</a>.
151153
</p>
152-
<p>
154+
${
155+
!doc.config ||
156+
(doc.config as CustomConfig)['hideGeneratedTimestamp'] !==
157+
true
158+
? `<p>
153159
Generated on
154160
${new Date().toDateString()} ${new Date().toTimeString()}
155-
</p>
161+
</p>`
162+
: ''
163+
}
156164
</div>
157165
</footer>
158166
</body>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Config } from 'doxdox-core';
2+
3+
export interface CustomConfig extends Config {
4+
hideGeneratedTimestamp?: boolean;
5+
}

0 commit comments

Comments
 (0)