From 3ad118aa763e058a1dfc2580175c736989fb6974 Mon Sep 17 00:00:00 2001 From: Dmytro Rykun Date: Tue, 19 Dec 2023 02:22:52 -0800 Subject: [PATCH] Replace utils.parseArgs with yargs (#41924) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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] Differential Revision: D52117818 fbshipit-source-id: 4c7443ef6eaee8da297d4a1c4fbbd3616675476f --- packages/react-native-codegen/package.json | 3 +- .../combine/__tests__/combine-utils-test.js | 62 +------------------ .../cli/combine/combine-js-to-schema-cli.js | 24 +++++-- .../src/cli/combine/combine-schemas-cli.js | 40 ++++++------ .../src/cli/combine/combine-utils.js | 38 ------------ 5 files changed, 44 insertions(+), 123 deletions(-) diff --git a/packages/react-native-codegen/package.json b/packages/react-native-codegen/package.json index bc53457e773391..2eead8269979fc 100644 --- a/packages/react-native-codegen/package.json +++ b/packages/react-native-codegen/package.json @@ -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" diff --git a/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js b/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js index 117dcb0f596772..7f523b758229b2 100644 --- a/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js +++ b/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js @@ -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', () => { diff --git a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js index 790b47d4fcf966..f2c98912efeba4 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js +++ b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js @@ -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, +); diff --git a/packages/react-native-codegen/src/cli/combine/combine-schemas-cli.js b/packages/react-native-codegen/src/cli/combine/combine-schemas-cli.js index d8425e10ef018a..f7a7b6f6ef0a28 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-schemas-cli.js +++ b/packages/react-native-codegen/src/cli/combine/combine-schemas-cli.js @@ -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( diff --git a/packages/react-native-codegen/src/cli/combine/combine-utils.js b/packages/react-native-codegen/src/cli/combine/combine-utils.js index a54a17ce26b504..8f120fb687f6ba 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-utils.js +++ b/packages/react-native-codegen/src/cli/combine/combine-utils.js @@ -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. @@ -97,6 +60,5 @@ function filterJSFile( } module.exports = { - parseArgs, filterJSFile, };