Skip to content

Commit 9071a3a

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Delete node calls from generate-artifacts-executor.js (facebook#41534)
Summary: Pull Request resolved: facebook#41534 This diff deletes calls to `node` from `generate-artifacts-executor.js`, and replaces them with normal `requires` of JS sources. This is a squashed version of (D51116291 ... D51158799). The following sequence of changes has been made: 1. Require and directly invoke `generate-specs-cli-executor` instead of using `node`. 2. Use `codegen-util` to get `RNCodegen` in `generate-provider-cli.js`. 3. Use `RNCodegen` directly instead of using `node`. 4. Move all implementation code from `combine-js-to-schema-cli.js` to `combine-js-to-schema.js`. 5. Decouple building the codegen from getting the codegen CLI. 6. Use `combine-js-to-schema` directly instead of using `node`. 7. Delete unit test that was testing node invocation. 8. Delete `nodeBinary` argument form `generate-codegen-artifacts.js` and its callsites. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D51158845 fbshipit-source-id: 5e039801c8045a42349f7cb6ca28e2df24634589
1 parent 81e368e commit 9071a3a

File tree

9 files changed

+119
-231
lines changed

9 files changed

+119
-231
lines changed

packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,9 @@
1111

1212
'use strict';
1313

14-
const combine = require('./combine-js-to-schema');
15-
const {filterJSFile, parseArgs} = require('./combine-utils');
16-
const fs = require('fs');
17-
const glob = require('glob');
18-
const path = require('path');
14+
const {combineSchemasInFileList} = require('./combine-js-to-schema');
15+
const {parseArgs} = require('./combine-utils');
1916

2017
const {platform, outfile, fileList} = parseArgs(process.argv);
2118

22-
const allFiles = [];
23-
fileList.forEach(file => {
24-
if (fs.lstatSync(file).isDirectory()) {
25-
const filePattern = path.sep === '\\' ? file.replace(/\\/g, '/') : file;
26-
const dirFiles = glob
27-
.sync(`${filePattern}/**/*.{js,ts,tsx}`, {
28-
nodir: true,
29-
// TODO: This will remove the need of slash substitution above for Windows,
30-
// but it requires glob@v9+; with the package currenlty relying on
31-
// [email protected]; and flow-typed repo not having definitions for glob@9+.
32-
// windowsPathsNoEscape: true,
33-
})
34-
.filter(element => filterJSFile(element, platform));
35-
allFiles.push(...dirFiles);
36-
} else if (filterJSFile(file)) {
37-
allFiles.push(file);
38-
}
39-
});
40-
41-
const combined = combine(allFiles);
42-
43-
// Warn users if there is no modules to process
44-
if (Object.keys(combined.modules).length === 0) {
45-
console.error(
46-
'No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules.',
47-
);
48-
}
49-
const formattedSchema = JSON.stringify(combined, null, 2);
50-
51-
if (outfile != null) {
52-
fs.writeFileSync(outfile, formattedSchema);
53-
} else {
54-
console.log(formattedSchema);
55-
}
19+
combineSchemasInFileList(fileList, platform, outfile);

packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import type {SchemaType} from '../../CodegenSchema.js';
1313

1414
const {FlowParser} = require('../../parsers/flow/parser');
1515
const {TypeScriptParser} = require('../../parsers/typescript/parser');
16+
const {filterJSFile} = require('./combine-utils');
1617
const fs = require('fs');
18+
const glob = require('glob');
1719
const path = require('path');
1820

1921
const flowParser = new FlowParser();
@@ -46,4 +48,44 @@ function combineSchemas(files: Array<string>): SchemaType {
4648
);
4749
}
4850

49-
module.exports = combineSchemas;
51+
function expandDirectoriesIntoFiles(
52+
fileList: Array<string>,
53+
platform: ?string,
54+
): Array<string> {
55+
return fileList
56+
.flatMap(file => {
57+
if (!fs.lstatSync(file).isDirectory()) {
58+
return [file];
59+
}
60+
const filePattern = path.sep === '\\' ? file.replace(/\\/g, '/') : file;
61+
return glob.sync(`${filePattern}/**/*.{js,ts,tsx}`, {
62+
nodir: true,
63+
// TODO: This will remove the need of slash substitution above for Windows,
64+
// but it requires glob@v9+; with the package currenlty relying on
65+
// [email protected]; and flow-typed repo not having definitions for glob@9+.
66+
// windowsPathsNoEscape: true,
67+
});
68+
})
69+
.filter(element => filterJSFile(element, platform));
70+
}
71+
72+
function combineSchemasInFileList(
73+
fileList: Array<string>,
74+
platform: ?string,
75+
outfile: string,
76+
): void {
77+
const expandedFileList = expandDirectoriesIntoFiles(fileList, platform);
78+
const combined = combineSchemas(expandedFileList);
79+
if (Object.keys(combined.modules).length === 0) {
80+
console.error(
81+
'No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules.',
82+
);
83+
}
84+
const formattedSchema = JSON.stringify(combined, null, 2);
85+
fs.writeFileSync(outfile, formattedSchema);
86+
}
87+
88+
module.exports = {
89+
combineSchemas,
90+
combineSchemasInFileList,
91+
};

packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,74 +12,11 @@
1212

1313
const fixtures = require('../__test_fixtures__/fixtures');
1414
const underTest = require('../generate-artifacts-executor');
15-
const child_process = require('child_process');
16-
const fs = require('fs');
1715
const path = require('path');
1816

1917
const reactNativeDependencyName = 'react-native';
2018
const rootPath = path.join(__dirname, '../../..');
2119

22-
describe('generateCode', () => {
23-
afterEach(() => {
24-
jest.resetModules();
25-
jest.resetAllMocks();
26-
});
27-
28-
beforeEach(() => {
29-
// Silence logs from printDeprecationWarningIfNeeded. Ideally, we should have test assertions on these warnings.
30-
jest.spyOn(console, 'log').mockImplementation();
31-
jest.spyOn(console, 'debug').mockImplementation();
32-
});
33-
34-
it('executeNodes with the right arguments', () => {
35-
// Define variables and expected values
36-
const iosOutputDir = 'app/ios/build/generated/ios';
37-
const library = {config: {name: 'library', type: 'all'}};
38-
const tmpDir = 'tmp';
39-
const node = 'usr/bin/node';
40-
const pathToSchema = 'app/build/schema.json';
41-
const rnRoot = path.join(__dirname, '../..');
42-
const libraryTypeArg = 'all';
43-
44-
const tmpOutputDir = path.join(tmpDir, 'out');
45-
46-
// mock used functions
47-
jest.spyOn(fs, 'mkdirSync').mockImplementation();
48-
jest.spyOn(child_process, 'execSync').mockImplementation();
49-
jest.spyOn(child_process, 'execFileSync').mockImplementation();
50-
51-
underTest._generateCode(iosOutputDir, library, tmpDir, node, pathToSchema);
52-
53-
expect(child_process.execFileSync).toHaveBeenCalledTimes(1);
54-
expect(child_process.execFileSync).toHaveBeenNthCalledWith(1, node, [
55-
`${path.join(rnRoot, 'generate-specs-cli.js')}`,
56-
'--platform',
57-
'ios',
58-
'--schemaPath',
59-
pathToSchema,
60-
'--outputDir',
61-
tmpOutputDir,
62-
'--libraryName',
63-
library.config.name,
64-
'--libraryType',
65-
libraryTypeArg,
66-
]);
67-
expect(child_process.execSync).toHaveBeenCalledTimes(1);
68-
expect(child_process.execSync).toHaveBeenNthCalledWith(
69-
1,
70-
`cp -R ${tmpOutputDir}/* "${iosOutputDir}"`,
71-
);
72-
73-
expect(fs.mkdirSync).toHaveBeenCalledTimes(2);
74-
expect(fs.mkdirSync).toHaveBeenNthCalledWith(1, tmpOutputDir, {
75-
recursive: true,
76-
});
77-
expect(fs.mkdirSync).toHaveBeenNthCalledWith(2, iosOutputDir, {
78-
recursive: true,
79-
});
80-
});
81-
});
82-
8320
describe('extractLibrariesFromJSON', () => {
8421
it('throws if in react-native and no dependencies found', () => {
8522
let configFile = {};

packages/react-native/scripts/codegen/codegen-utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ function getCodegen() {
3232
return RNCodegen;
3333
}
3434

35+
function getCombineJSToSchema() {
36+
let combineJSToSchema;
37+
try {
38+
combineJSToSchema = require('../../packages/react-native-codegen/lib/cli/combine/combine-js-to-schema.js');
39+
} catch (e) {
40+
combineJSToSchema = require('@react-native/codegen/lib/cli/combine/combine-js-to-schema.js');
41+
}
42+
if (!combineJSToSchema) {
43+
throw 'combine-js-to-schema not found.';
44+
}
45+
return combineJSToSchema;
46+
}
47+
3548
module.exports = {
3649
getCodegen: getCodegen,
50+
getCombineJSToSchema: getCombineJSToSchema,
3751
};

0 commit comments

Comments
 (0)