mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-11 14:14:36 +08:00
Tests should also use imports that end with an extension, particularly as it can silently fail in Karma. We discovered this issue in https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2144163/4/test/e2e/application/cookies.ts#10 R=janscheffler@chromium.org Change-Id: Ic379cb90b8e201b55a24010dc45565f6f45d910e Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2150450 Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org> Commit-Queue: Jack Franklin <jacktfranklin@chromium.org> Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Auto-Submit: Tim van der Lippe <tvanderlippe@chromium.org>
71 lines
2.6 KiB
TypeScript
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.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 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));
|
|
}
|