forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringValuesTypingsGenerator.ts
More file actions
95 lines (81 loc) · 2.39 KB
/
StringValuesTypingsGenerator.ts
File metadata and controls
95 lines (81 loc) · 2.39 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
94
95
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { EOL } from 'os';
import {
ITypingsGeneratorOptions,
TypingsGenerator
} from './TypingsGenerator';
/**
* @public
*/
export interface IStringValueTyping {
exportName: string;
comment?: string;
}
/**
* @public
*/
export interface IStringValueTypings {
typings: IStringValueTyping[];
}
/**
* @public
*/
export interface IStringValuesTypingsGeneratorOptions extends ITypingsGeneratorOptions<IStringValueTypings> {
exportAsDefault?: boolean;
}
const EXPORT_AS_DEFAULT_INTERFACE_NAME: string = 'IExport';
/**
* This is a simple tool that generates .d.ts files for non-TS files that can be represented as
* a simple set of named string exports.
*
* @public
*/
export class StringValuesTypingsGenerator extends TypingsGenerator {
public constructor(options: IStringValuesTypingsGeneratorOptions) {
super({
...options,
parseAndGenerateTypings: (fileContents: string, filePath: string) => {
const stringValueTypings: IStringValueTypings = options.parseAndGenerateTypings(fileContents, filePath);
const outputLines: string[] = [];
let indent: string = '';
if (options.exportAsDefault) {
outputLines.push(
`export interface ${EXPORT_AS_DEFAULT_INTERFACE_NAME} {`
);
indent = ' ';
}
for (const stringValueTyping of stringValueTypings.typings) {
const { exportName, comment } = stringValueTyping;
if (comment && comment.trim() !== '') {
outputLines.push(
`${indent}/**`,
`${indent} * ${comment.replace(/\*\//g, '*\\/')}`,
`${indent} */`
);
}
if (options.exportAsDefault) {
outputLines.push(
`${indent}${exportName}: string;`,
''
);
} else {
outputLines.push(
`export declare const ${exportName}: string;`,
''
);
}
}
if (options.exportAsDefault) {
outputLines.push(
'}',
'',
`declare const strings: ${EXPORT_AS_DEFAULT_INTERFACE_NAME};`,
'export default strings;'
);
}
return outputLines.join(EOL);
}
});
}
}