-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathtransliterate.ts
More file actions
215 lines (192 loc) · 7.34 KB
/
transliterate.ts
File metadata and controls
215 lines (192 loc) · 7.34 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { Assembly, Docs, SPEC_FILE_NAME, Type, TypeKind, loadAssemblyFromPath } from '@jsii/spec';
import { promises as fs } from 'fs';
import { resolve } from 'path';
import { TargetLanguage } from '../languages';
import { targetName } from '../languages/target-language';
import { debug } from '../logging';
import { RosettaTabletReader, UnknownSnippetMode } from '../rosetta-reader';
import { typeScriptSnippetFromVisibleSource, ApiLocation } from '../snippet';
import { Mutable } from '../util';
import { extractSnippets } from './extract';
export interface TransliterateAssemblyOptions {
/**
* Whether to ignore any missing fixture files or literate markdown documents
* referenced by the assembly, instead of failing.
*
* @default false
*/
readonly loose?: boolean;
/**
* Whether transliteration should fail upon failing to compile an example that
* required live transliteration.
*
* @default false
*/
readonly strict?: boolean;
/**
* A pre-build translation tablet (as produced by `jsii-rosetta extract`).
*
* @default - Only the default tablet (`.jsii.tabl.json`) files will be used.
*/
readonly tablet?: string;
/**
* A directory to output translated assemblies to
*
* @default - assembly location
*/
readonly outdir?: string;
/**
* Whether or not to live-convert samples
*
* @default UnknownSnippetMode.FAIL
*/
readonly unknownSnippets?: UnknownSnippetMode;
}
/**
* Prepares transliterated versions of the designated assemblies into the
* selected taregt languages.
*
* @param assemblyLocations the directories which contain assemblies to
* transliterate.
* @param targetLanguages the languages into which to transliterate.
* @param tabletLocation an optional Rosetta tablet file to source
* pre-transliterated snippets from.
*
* @experimental
*/
export async function transliterateAssembly(
assemblyLocations: readonly string[],
targetLanguages: readonly TargetLanguage[],
options: TransliterateAssemblyOptions = {},
): Promise<void> {
// Start by doing an 'extract' for all these assemblies
//
// This will locate all examples that haven't been translated yet and translate
// them. Importantly: it will translate them in parallel, which is going to improve
// performance a lot. We ignore diagnostics.
const { tablet } = await extractSnippets(assemblyLocations, {
includeCompilerDiagnostics: true,
loose: options.loose,
cacheFromFile: options.tablet,
writeToImplicitTablets: false,
allowDirtyTranslations: true,
});
// Now do a regular "tablet reader" cycle, expecting everything to be translated already,
// and therefore it doesn't matter that we do this all in a single-threaded loop.
const rosetta = new RosettaTabletReader({
unknownSnippets: options?.unknownSnippets ?? UnknownSnippetMode.FAIL,
targetLanguages,
prefixDisclaimer: true,
});
// Put in the same caching tablet here
if (options.tablet) {
await rosetta.loadTabletFromFile(options.tablet);
}
// Any fresh translations we just came up with
rosetta.addTablet(tablet);
const assemblies = await loadAssemblies(assemblyLocations, rosetta);
for (const [location, loadAssembly] of assemblies.entries()) {
for (const language of targetLanguages) {
const now = new Date().getTime();
const result = loadAssembly();
if (result.targets?.[targetName(language)] == null) {
// This language is not supported by the assembly, so we skip it...
continue;
}
if (result.readme?.markdown) {
result.readme.markdown = rosetta.translateSnippetsInMarkdown(
{ api: 'moduleReadme', moduleFqn: result.name },
result.readme.markdown,
language,
true /* strict */,
);
}
for (const type of Object.values(result.types ?? {})) {
transliterateType(type, rosetta, language);
}
// eslint-disable-next-line no-await-in-loop
await fs.writeFile(
resolve(options?.outdir ?? location, `${SPEC_FILE_NAME}.${language}`),
JSON.stringify(result, null, 2),
);
const then = new Date().getTime();
debug(`Done transliterating ${result.name}@${result.version} to ${language} after ${then - now} milliseconds`);
}
}
rosetta.printDiagnostics(process.stderr);
if (rosetta.hasErrors && options.strict) {
throw new Error('Strict mode is enabled and some examples failed compilation!');
}
}
/**
* Given a set of directories containing `.jsii` assemblies, load all the
* assemblies into the provided `Rosetta` instance and return a map of
* directories to assembly-loading functions (the function re-loads the original
* assembly from disk on each invocation).
*
* @param directories the assembly-containing directories to traverse.
* @param rosetta the `Rosetta` instance in which to load assemblies.
*
* @returns a map of directories to a function that loads the `.jsii` assembly
* contained therein from disk.
*/
async function loadAssemblies(
directories: readonly string[],
rosetta: RosettaTabletReader,
): Promise<ReadonlyMap<string, AssemblyLoader>> {
const result = new Map<string, AssemblyLoader>();
for (const directory of directories) {
const loader = () => loadAssemblyFromPath(directory);
// eslint-disable-next-line no-await-in-loop
await rosetta.addAssembly(loader(), directory);
result.set(directory, loader);
}
return result;
}
type AssemblyLoader = () => Mutable<Assembly>;
function transliterateType(type: Type, rosetta: RosettaTabletReader, language: TargetLanguage): void {
transliterateDocs({ api: 'type', fqn: type.fqn }, type.docs);
switch (type.kind) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore 7029
case TypeKind.Class:
if (type.initializer) {
transliterateDocs({ api: 'initializer', fqn: type.fqn }, type.initializer.docs);
}
// fallthrough
case TypeKind.Interface:
for (const method of type.methods ?? []) {
transliterateDocs({ api: 'member', fqn: type.fqn, memberName: method.name }, method.docs);
for (const parameter of method.parameters ?? []) {
transliterateDocs(
{ api: 'parameter', fqn: type.fqn, methodName: method.name, parameterName: parameter.name },
parameter.docs,
);
}
}
for (const property of type.properties ?? []) {
transliterateDocs({ api: 'member', fqn: type.fqn, memberName: property.name }, property.docs);
}
break;
case TypeKind.Enum:
for (const member of type.members) {
transliterateDocs({ api: 'member', fqn: type.fqn, memberName: member.name }, member.docs);
}
break;
default:
throw new Error(`Unsupported type kind: ${(type as any).kind}`);
}
function transliterateDocs(api: ApiLocation, docs: Docs | undefined) {
if (docs?.remarks) {
docs.remarks = rosetta.translateSnippetsInMarkdown(api, docs.remarks, language, true /* strict */);
}
if (docs?.example) {
const location = { api, field: { field: 'example' } } as const;
const snippet = typeScriptSnippetFromVisibleSource(docs.example, location, true /* strict */);
const translation = rosetta.translateSnippet(snippet, language);
if (translation != null) {
docs.example = translation.source;
}
}
}
}