mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-11 06:04:55 +08:00
* Missing the import statement to pull in the actual component itself. * It was including lifecycle methods in the output, which it does not need to do as no user code would ever directly call them, so the bridges do not have to include them. Change-Id: I98d698b90eb7608df95baaa94f980eab8ee0e604 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2287518 Auto-Submit: Jack Franklin <jacktfranklin@chromium.org> Reviewed-by: Paul Lewis <aerotwist@chromium.org> Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
74 lines
2.7 KiB
TypeScript
74 lines
2.7 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.js';
|
|
import {filePathToTypeScriptSourceFile, walkTree} from './walk_tree.js';
|
|
|
|
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 importStatement = `import './${baseName}.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, importStatement, 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));
|
|
}
|