From 2d06e6a4c9261bb7790cf217b66145415301bc54 Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Wed, 8 Dec 2021 20:22:47 -0800 Subject: [PATCH] eslint-plugin-specs prepublish step Summary: #Changelog: [Internal] - This is an attempt to fix public publishing of eslint-plugin-specs. Currently, internal consumption of this package assumes access to `react-native-codegen/src` but for external usage, we leverage the published `react-native-codegen` which transforms files out to the `lib` folder vs. `src`. For a similar-ish suit, this change is adding a prepublish step that will very basically update the references. Reviewed By: mdvacca Differential Revision: D32910080 fbshipit-source-id: f5e508090cbbf5097a848ddef3b721002a6c6277 --- packages/eslint-plugin-specs/package.json | 5 ++- packages/eslint-plugin-specs/prepublish.js | 44 +++++++++++++++++++ .../react-native-modules.js | 31 +++++++++---- 3 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 packages/eslint-plugin-specs/prepublish.js diff --git a/packages/eslint-plugin-specs/package.json b/packages/eslint-plugin-specs/package.json index 4e60ce91ec3..7b6eb53772d 100644 --- a/packages/eslint-plugin-specs/package.json +++ b/packages/eslint-plugin-specs/package.json @@ -1,6 +1,6 @@ { "name": "@react-native/eslint-plugin-specs", - "version": "0.0.1", + "version": "0.0.2", "description": "ESLint rules to validate NativeModule and Component Specs", "main": "index.js", "repository": { @@ -8,6 +8,9 @@ "url": "git@github.com:facebook/react-native.git", "directory": "packages/eslint-plugin-specs" }, + "scripts": { + "prepublish": "node prepublish.js" + }, "dependencies": { "@babel/core": "^7.14.0", "@babel/plugin-transform-flow-strip-types": "^7.0.0", diff --git a/packages/eslint-plugin-specs/prepublish.js b/packages/eslint-plugin-specs/prepublish.js new file mode 100644 index 00000000000..0d4ab29f9cc --- /dev/null +++ b/packages/eslint-plugin-specs/prepublish.js @@ -0,0 +1,44 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +const fs = require('fs'); + +/** + * script to prepare package for publish. + * + * Due to differences to how we consume internal packages, update a flag + */ + +fs.readFile('./react-native-modules.js', 'utf8', function (readError, source) { + if (readError != null) { + return console.error( + 'Failed to read react-native-modules.js for publish', + readError, + ); + } + + const result = source.replace( + 'const PACKAGE_USAGE = false;', + 'const PACKAGE_USAGE = true;', + ); + + fs.writeFile( + './react-native-modules.js', + result, + 'utf8', + function (writeError) { + if (writeError != null) { + return console.error( + 'Failed to update react-native-modules.js for publish', + writeError, + ); + } + }, + ); +}); diff --git a/packages/eslint-plugin-specs/react-native-modules.js b/packages/eslint-plugin-specs/react-native-modules.js index c5f19fc0c30..73e7c662b41 100644 --- a/packages/eslint-plugin-specs/react-native-modules.js +++ b/packages/eslint-plugin-specs/react-native-modules.js @@ -13,6 +13,8 @@ const path = require('path'); const withBabelRegister = require('./with-babel-register'); +// We run yarn prepublish before publishing package which will set this value to true +const PACKAGE_USAGE = false; const ERRORS = { misnamedHasteModule(hasteModuleName) { return `Module ${hasteModuleName}: All files using TurboModuleRegistry must start with Native.`; @@ -24,15 +26,28 @@ let RNParserUtils; function requireModuleParser() { if (RNModuleParser == null || RNParserUtils == null) { - const config = { - only: [/react-native-codegen\/src\//], - plugins: [require('@babel/plugin-transform-flow-strip-types').default], - }; + // If using this externally, we leverage react-native-codegen as published form + if (!PACKAGE_USAGE) { + const config = { + only: [/react-native-codegen\/src\//], + plugins: [require('@babel/plugin-transform-flow-strip-types').default], + }; - withBabelRegister(config, () => { - RNModuleParser = require('react-native-codegen/src/parsers/flow/modules'); - RNParserUtils = require('react-native-codegen/src/parsers/flow/utils'); - }); + withBabelRegister(config, () => { + RNModuleParser = require('react-native-codegen/src/parsers/flow/modules'); + RNParserUtils = require('react-native-codegen/src/parsers/flow/utils'); + }); + } else { + const config = { + only: [/react-native-codegen\/lib\//], + plugins: [require('@babel/plugin-transform-flow-strip-types').default], + }; + + withBabelRegister(config, () => { + RNModuleParser = require('react-native-codegen/lib/parsers/flow/modules'); + RNParserUtils = require('react-native-codegen/lib/parsers/flow/utils'); + }); + } } return {