Skip to content

Commit c45c13f

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Replace utils.parseArgs with yargs (#41924)
Summary: Pull Request resolved: #41924 `utils.parseArgs` are only available in Node >=18.3, we can't use this function because we target Node >=18.0. This diff replaces `utils.parseArgs` with `yargs`. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D52117818 fbshipit-source-id: 79223997874b6cfdea2ce38243b615a0dbb704a6
1 parent 1045b22 commit c45c13f

File tree

5 files changed

+44
-123
lines changed

5 files changed

+44
-123
lines changed

packages/react-native-codegen/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"hermes-estree": "0.18.0",
5353
"micromatch": "^4.0.4",
5454
"prettier": "2.8.8",
55-
"rimraf": "^3.0.2"
55+
"rimraf": "^3.0.2",
56+
"yargs": "^17.6.2"
5657
},
5758
"peerDependencies": {
5859
"@babel/preset-env": "^7.1.6"

packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js

+1-61
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,7 @@
1111

1212
'use-strict';
1313

14-
const {filterJSFile, parseArgs} = require('../combine-utils.js');
15-
16-
describe('parseArgs', () => {
17-
const nodeBin = 'node';
18-
const combineApp = 'app';
19-
const schemaJson = 'schema.json';
20-
const specFile1 = 'NativeSpec.js';
21-
const specFile2 = 'SpecNativeComponent.js';
22-
23-
describe('when no platform provided', () => {
24-
it('returns null platform, schema and fileList', () => {
25-
const {platform, outfile, fileList} = parseArgs([
26-
nodeBin,
27-
combineApp,
28-
schemaJson,
29-
specFile1,
30-
specFile2,
31-
]);
32-
33-
expect(platform).toBeNull();
34-
expect(outfile).toBe(schemaJson);
35-
expect(fileList).toStrictEqual([specFile1, specFile2]);
36-
});
37-
});
38-
39-
describe('when platform passed with --platform', () => {
40-
it('returns the platform, the schema and the fileList', () => {
41-
const {platform, outfile, fileList} = parseArgs([
42-
nodeBin,
43-
combineApp,
44-
'--platform',
45-
'ios',
46-
schemaJson,
47-
specFile1,
48-
specFile2,
49-
]);
50-
51-
expect(platform).toBe('ios');
52-
expect(outfile).toBe(schemaJson);
53-
expect(fileList).toStrictEqual([specFile1, specFile2]);
54-
});
55-
});
56-
57-
describe('when platform passed with -p', () => {
58-
it('returns the platform, the schema and the fileList', () => {
59-
const {platform, outfile, fileList} = parseArgs([
60-
nodeBin,
61-
combineApp,
62-
'-p',
63-
'android',
64-
schemaJson,
65-
specFile1,
66-
specFile2,
67-
]);
68-
69-
expect(platform).toBe('android');
70-
expect(outfile).toBe(schemaJson);
71-
expect(fileList).toStrictEqual([specFile1, specFile2]);
72-
});
73-
});
74-
});
14+
const {filterJSFile} = require('../combine-utils.js');
7515

7616
describe('filterJSFile', () => {
7717
describe('When the file is not a Spec file', () => {

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@
1414
const {
1515
combineSchemasInFileListAndWriteToFile,
1616
} = require('./combine-js-to-schema');
17-
const {parseArgs} = require('./combine-utils');
17+
const yargs = require('yargs');
1818

19-
const parsedArgs = parseArgs(process.argv);
19+
const argv = yargs
20+
.option('p', {
21+
alias: 'platform',
22+
})
23+
.option('e', {
24+
alias: 'exclude',
25+
})
26+
.parseSync();
2027

21-
const {platform, outfile, fileList, exclude} = parsedArgs;
28+
const [outfile, ...fileList] = argv._;
29+
const platform: ?string = argv.platform;
30+
const exclude: string = argv.exclude;
31+
const excludeRegExp: ?RegExp =
32+
exclude != null && exclude !== '' ? new RegExp(exclude) : null;
2233

23-
combineSchemasInFileListAndWriteToFile(fileList, platform, outfile, exclude);
34+
combineSchemasInFileListAndWriteToFile(
35+
fileList,
36+
platform != null ? platform.toLowerCase() : platform,
37+
outfile,
38+
excludeRegExp,
39+
);

packages/react-native-codegen/src/cli/combine/combine-schemas-cli.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@ import type {
1818

1919
const assert = require('assert');
2020
const fs = require('fs');
21-
const util = require('util');
21+
const yargs = require('yargs');
2222

23-
const {values: args} = util.parseArgs({
24-
options: {
25-
platform: {
26-
type: 'string',
27-
},
28-
output: {
29-
type: 'string',
30-
},
31-
['schema-query']: {
32-
type: 'string',
33-
},
34-
},
35-
});
36-
if (!['iOS', 'android'].includes(args.platform)) {
37-
throw new Error(`Invalid platform ${args.platform}`);
23+
const argv = yargs
24+
.option('p', {
25+
alias: 'platform',
26+
type: 'string',
27+
demandOption: true,
28+
})
29+
.option('o', {
30+
alias: 'output',
31+
})
32+
.option('s', {
33+
alias: 'schema-query',
34+
})
35+
.parseSync();
36+
37+
const platform: string = argv.platform.toLowerCase();
38+
const output: string = argv.output;
39+
const schemaQuery: string = argv.s;
40+
41+
if (!['ios', 'android'].includes(platform)) {
42+
throw new Error(`Invalid platform ${platform}`);
3843
}
39-
const platform = args.platform;
40-
const output = args.output;
41-
const schemaQuery: string = args['schema-query'];
4244

4345
if (!schemaQuery.startsWith('@')) {
4446
throw new Error(

packages/react-native-codegen/src/cli/combine/combine-utils.js

-38
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,6 @@
1212
'use strict';
1313

1414
const path = require('path');
15-
const util = require('util');
16-
17-
function parseArgs(args: string[]): {
18-
platform: ?string,
19-
outfile: string,
20-
fileList: string[],
21-
exclude: ?RegExp,
22-
} {
23-
const parsedArgs = util.parseArgs({
24-
args: args.slice(2),
25-
options: {
26-
platform: {
27-
short: 'p',
28-
type: 'string',
29-
},
30-
exclude: {
31-
short: 'e',
32-
type: 'string',
33-
},
34-
},
35-
allowPositionals: true,
36-
});
37-
38-
const {
39-
values: {platform, exclude},
40-
positionals: files,
41-
} = parsedArgs;
42-
43-
const [outfile, ...fileList] = files;
44-
45-
return {
46-
platform: platform ?? null,
47-
outfile,
48-
fileList,
49-
exclude: exclude != null && exclude !== '' ? new RegExp(exclude) : null,
50-
};
51-
}
5215

5316
/**
5417
* This function is used by the CLI to decide whether a JS/TS file has to be processed or not by the Codegen.
@@ -97,6 +60,5 @@ function filterJSFile(
9760
}
9861

9962
module.exports = {
100-
parseArgs,
10163
filterJSFile,
10264
};

0 commit comments

Comments
 (0)