Files
react-native-devtools-frontend/scripts/component_bridges/cli.ts
T
Jack Franklin d3f547209d Component bridges generation
This CL introduces a script to generate `_bridges.js` files for TypeScript web components.

For further details see:

* `scripts/component_bridges/README.md` in this CL.
* Modernising UI components: https://docs.google.com/document/d/1Asq9BDFFvnsqhDbpu2Ez92pugPN2-8nq50O6sNO0SpY/edit
* Building bridges to a TypeScript world: https://docs.google.com/document/d/1qpa5QSSHrvvo_w73GV0hOJRug4N0_9JfFDw01d-NaCE/edit#heading=h.7nki9mck5t64

Change-Id: I95656acd6d22f5e09d178875dd3bcaf47da75a9c
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2108761
Reviewed-by: Paul Lewis <aerotwist@chromium.org>
Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
2020-03-26 11:36:00 +00:00

71 lines
2.6 KiB
TypeScript

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as fs from 'fs';
import * as path from 'path';
import {generateClosureBridge, GeneratedCode} from './generate_closure';
import {filePathToTypeScriptSourceFile, walkTree} from './walk_tree';
const chromeLicense = `// Copyright ${new Date().getFullYear()} The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
`;
export const writeToDisk = (inputFilePath: string, generatedCode: GeneratedCode) => {
const dir = path.dirname(inputFilePath);
const baseName = path.basename(inputFilePath, '.ts');
const outputFileName = `${baseName}_bridge.js`;
const interfaces = generatedCode.interfaces
.map(interfacePart => {
return interfacePart.join('\n');
})
.join('\n');
const classDeclaration = generatedCode.closureClass.join('\n');
const creatorFunction = generatedCode.creatorFunction.join('\n');
const relativeFilePath = path.relative(process.cwd(), inputFilePath);
const byHandWarning = `/**
* WARNING: do not modify this file by hand!
* it was automatically generated by the bridge generator
* if you made changes to the source code and need to update this file, run:
* npm run generate-bridge-file ${relativeFilePath}
*/
`;
// extra \n to ensure ending with a linebreak at end of file
const finalCode = [chromeLicense, byHandWarning, interfaces, classDeclaration, creatorFunction].join('\n') + '\n';
fs.writeFileSync(path.join(dir, outputFileName), finalCode, {encoding: 'utf8'});
return {
output: path.join(dir, outputFileName),
code: finalCode,
};
};
export const parseTypeScriptComponent = (componentSourceFilePath: string) => {
const file = filePathToTypeScriptSourceFile(componentSourceFilePath);
const state = walkTree(file, componentSourceFilePath);
const generatedCode = generateClosureBridge(state);
return writeToDisk(componentSourceFilePath, generatedCode);
};
const main = (args: string[]) => {
const bridgeComponentPath = path.resolve(process.cwd(), args[0]);
if (!bridgeComponentPath || !fs.existsSync(bridgeComponentPath)) {
throw new Error(`Could not find bridgeComponent path ${bridgeComponentPath}`);
}
const {output} = parseTypeScriptComponent(bridgeComponentPath);
console.log('Wrote bridge file to', output);
};
if (require.main === module) {
main(process.argv.slice(2));
}