Skip to content

Commit

Permalink
Replace utils.parseArgs with yargs (#41924)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Dec 19, 2023
1 parent 1045b22 commit c45c13f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 123 deletions.
3 changes: 2 additions & 1 deletion packages/react-native-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"hermes-estree": "0.18.0",
"micromatch": "^4.0.4",
"prettier": "2.8.8",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"yargs": "^17.6.2"
},
"peerDependencies": {
"@babel/preset-env": "^7.1.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,7 @@

'use-strict';

const {filterJSFile, parseArgs} = require('../combine-utils.js');

describe('parseArgs', () => {
const nodeBin = 'node';
const combineApp = 'app';
const schemaJson = 'schema.json';
const specFile1 = 'NativeSpec.js';
const specFile2 = 'SpecNativeComponent.js';

describe('when no platform provided', () => {
it('returns null platform, schema and fileList', () => {
const {platform, outfile, fileList} = parseArgs([
nodeBin,
combineApp,
schemaJson,
specFile1,
specFile2,
]);

expect(platform).toBeNull();
expect(outfile).toBe(schemaJson);
expect(fileList).toStrictEqual([specFile1, specFile2]);
});
});

describe('when platform passed with --platform', () => {
it('returns the platform, the schema and the fileList', () => {
const {platform, outfile, fileList} = parseArgs([
nodeBin,
combineApp,
'--platform',
'ios',
schemaJson,
specFile1,
specFile2,
]);

expect(platform).toBe('ios');
expect(outfile).toBe(schemaJson);
expect(fileList).toStrictEqual([specFile1, specFile2]);
});
});

describe('when platform passed with -p', () => {
it('returns the platform, the schema and the fileList', () => {
const {platform, outfile, fileList} = parseArgs([
nodeBin,
combineApp,
'-p',
'android',
schemaJson,
specFile1,
specFile2,
]);

expect(platform).toBe('android');
expect(outfile).toBe(schemaJson);
expect(fileList).toStrictEqual([specFile1, specFile2]);
});
});
});
const {filterJSFile} = require('../combine-utils.js');

describe('filterJSFile', () => {
describe('When the file is not a Spec file', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@
const {
combineSchemasInFileListAndWriteToFile,
} = require('./combine-js-to-schema');
const {parseArgs} = require('./combine-utils');
const yargs = require('yargs');

const parsedArgs = parseArgs(process.argv);
const argv = yargs
.option('p', {
alias: 'platform',
})
.option('e', {
alias: 'exclude',
})
.parseSync();

const {platform, outfile, fileList, exclude} = parsedArgs;
const [outfile, ...fileList] = argv._;
const platform: ?string = argv.platform;
const exclude: string = argv.exclude;
const excludeRegExp: ?RegExp =
exclude != null && exclude !== '' ? new RegExp(exclude) : null;

combineSchemasInFileListAndWriteToFile(fileList, platform, outfile, exclude);
combineSchemasInFileListAndWriteToFile(
fileList,
platform != null ? platform.toLowerCase() : platform,
outfile,
excludeRegExp,
);
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,29 @@ import type {

const assert = require('assert');
const fs = require('fs');
const util = require('util');
const yargs = require('yargs');

const {values: args} = util.parseArgs({
options: {
platform: {
type: 'string',
},
output: {
type: 'string',
},
['schema-query']: {
type: 'string',
},
},
});
if (!['iOS', 'android'].includes(args.platform)) {
throw new Error(`Invalid platform ${args.platform}`);
const argv = yargs
.option('p', {
alias: 'platform',
type: 'string',
demandOption: true,
})
.option('o', {
alias: 'output',
})
.option('s', {
alias: 'schema-query',
})
.parseSync();

const platform: string = argv.platform.toLowerCase();
const output: string = argv.output;
const schemaQuery: string = argv.s;

if (!['ios', 'android'].includes(platform)) {
throw new Error(`Invalid platform ${platform}`);
}
const platform = args.platform;
const output = args.output;
const schemaQuery: string = args['schema-query'];

if (!schemaQuery.startsWith('@')) {
throw new Error(
Expand Down
38 changes: 0 additions & 38 deletions packages/react-native-codegen/src/cli/combine/combine-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,6 @@
'use strict';

const path = require('path');
const util = require('util');

function parseArgs(args: string[]): {
platform: ?string,
outfile: string,
fileList: string[],
exclude: ?RegExp,
} {
const parsedArgs = util.parseArgs({
args: args.slice(2),
options: {
platform: {
short: 'p',
type: 'string',
},
exclude: {
short: 'e',
type: 'string',
},
},
allowPositionals: true,
});

const {
values: {platform, exclude},
positionals: files,
} = parsedArgs;

const [outfile, ...fileList] = files;

return {
platform: platform ?? null,
outfile,
fileList,
exclude: exclude != null && exclude !== '' ? new RegExp(exclude) : null,
};
}

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

module.exports = {
parseArgs,
filterJSFile,
};

0 comments on commit c45c13f

Please sign in to comment.