Skip to content

Commit

Permalink
eslint-plugin-specs prepublish step
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Luna Wei authored and facebook-github-bot committed Dec 9, 2021
1 parent 60e60a9 commit 2d06e6a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
5 changes: 4 additions & 1 deletion packages/eslint-plugin-specs/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"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": {
"type": "git",
"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",
Expand Down
44 changes: 44 additions & 0 deletions packages/eslint-plugin-specs/prepublish.js
Original file line number Diff line number Diff line change
@@ -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,
);
}
},
);
});
33 changes: 24 additions & 9 deletions packages/eslint-plugin-specs/react-native-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
Expand All @@ -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],
};

withBabelRegister(config, () => {
RNModuleParser = require('react-native-codegen/src/parsers/flow/modules');
RNParserUtils = require('react-native-codegen/src/parsers/flow/utils');
});
// 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');
});
} 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 {
Expand Down

0 comments on commit 2d06e6a

Please sign in to comment.