-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
203 lines (170 loc) · 6.36 KB
/
main.ts
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
import { Language, LanguagesBag } from "budgie";
import chalk from "chalk";
import { ExitCode } from "./codes";
import { convertFiles } from "./conversions/convertFiles";
import { ConversionStatus } from "./converters/converter";
import { createConvertersBag } from "./converters/convertersBag";
import { IFileSystem } from "./fileSystem";
import { ILogger } from "./logger";
import { IBudgieProjectMetadata } from "./postprocessing/metadata";
import { postprocess } from "./postprocessing/postprocess";
import { preprocessFiles } from "./preprocessing/preprocessFiles";
import { queueAsyncActions } from "./utils/asyncQueue";
/**
* Dependencies to set up and run a runner.
*/
export interface IMainDependencies {
/**
* Base or root directory to ignore from the beginning of file paths, such as "src/", if not "".
*/
baseDirectory?: string;
/**
* Unique file paths to convert.
*/
filePaths: ReadonlySet<string>;
/**
* Reads and writes files.
*/
fileSystem: IFileSystem;
/**
* Names of output language(s) to convert to.
*/
languageNames?: ReadonlyArray<string>;
/**
* Logs information on significant events.
*/
logger: ILogger;
/**
* Namespace before path names, such as "Budgie", if not "".
*/
namespace?: string;
/**
* Budgie configuration project, if provided.
*/
project?: string;
/**
* TypeScript configuration project, if provided.
*/
typescriptConfig?: string;
}
/**
* Reads a set of file paths into memory.
*
* @param filePaths Unique file paths to read in.
* @param fileSystem Reads and writes files.
* @returns File contents of the files, keyed by file path.
*/
const readFilesFromSystem = async (filePaths: ReadonlySet<string>, fileSystem: IFileSystem) => {
const map = new Map<string, string>();
await queueAsyncActions(
Array.from(filePaths).map((filePath: string) => async () => {
map.set(filePath, await fileSystem.readFile(filePath));
}),
);
return map;
};
const getProjectMetadata = async (project: string | undefined, fileSystem: IFileSystem) => {
if (project === undefined) {
return undefined;
}
try {
return JSON.parse(await fileSystem.readFile(project)) as IBudgieProjectMetadata;
} catch (error) {
return error;
}
};
/**
* Validates Budgie settings, sets up a conversion runner, and runs it.
*
* @param dependencies Dependencies to set up and run a runner.
*/
export const main = async (dependencies: IMainDependencies): Promise<ExitCode> => {
const printAvailableLanguages = (languageNames: ReadonlyArray<string>) => {
dependencies.logger.log("Available languages:");
for (const languageName of languageNames) {
dependencies.logger.log(` ${languageName}`);
}
};
const getLanguagesFromNames = (languageNames: ReadonlyArray<string> | undefined): Language[] | undefined => {
const languagesBag = new LanguagesBag();
const supportedLanguageNames = languagesBag.getLanguageNames();
if (languageNames === undefined || languageNames.length === 0) {
dependencies.logger.error("You must provide a -l/--language.");
printAvailableLanguages(supportedLanguageNames);
return undefined;
}
const languages = [];
for (const languageName of languageNames) {
if (languageNames.indexOf(languageName) === -1) {
dependencies.logger.error(`Unknown language name: '${chalk.bold(languageName)}'.`);
printAvailableLanguages(languageNames);
return undefined;
}
languages.push(languagesBag.getLanguageByName(languageName));
}
return languages;
};
const run = async (): Promise<ExitCode> => {
// 0a: Retrieve corresponding Budgie languages for -l/--language
const languages = getLanguagesFromNames(dependencies.languageNames);
if (languages === undefined) {
return ExitCode.Error;
}
// 0b: Read project metadata if a Budgie project file is provided
const metadata = await getProjectMetadata(dependencies.project, dependencies.fileSystem);
if (metadata instanceof Error) {
return ExitCode.Error;
}
// 0c: Create language preprocessor converters per known language type
const existingFileContents = await readFilesFromSystem(dependencies.filePaths, dependencies.fileSystem);
const convertersBag = createConvertersBag({
baseDirectory: dependencies.baseDirectory,
existingFileContents,
fileSystem: dependencies.fileSystem,
logger: dependencies.logger,
metadata,
outputNamespace: dependencies.namespace,
typescriptConfig: dependencies.typescriptConfig,
});
// 1: Preprocess any known language types, such as .ts, to .bg files
const preprocessResult = await preprocessFiles({
convertersBag,
filePaths: dependencies.filePaths,
fileSystem: dependencies.fileSystem,
languages,
logger: dependencies.logger,
});
if (preprocessResult.status === ConversionStatus.Failed) {
return ExitCode.Error;
}
// 2: Convert all .bg files to the output language
const conversionResults = await convertFiles({
baseDirectory: dependencies.baseDirectory,
budgieFilePaths: preprocessResult.budgieFilePaths,
existingFileContents,
fileSystem: dependencies.fileSystem,
languages,
logger: dependencies.logger,
outputNamespace: dependencies.namespace,
typescriptConfig: dependencies.typescriptConfig,
});
if (conversionResults.status === ConversionStatus.Failed) {
return ExitCode.Error;
}
// 3: Create project metadata files (and soon exports)
await postprocess({
fileSystem: dependencies.fileSystem,
languages,
logger: dependencies.logger,
project: dependencies.project,
});
return ExitCode.Ok;
};
try {
return await run();
} catch (error) {
dependencies.logger.error(error.stack);
return ExitCode.Error;
}
};
export type IMain = typeof main;