Files
react-native-devtools-frontend/scripts/component_bridges/cli.ts
T
Tim van der Lippe b0f499fbae Fix eslint import rules to warn on test import extensions
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>
2020-04-15 14:02:58 +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.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));
}