react-native/packages/eslint-plugin-specs/prepack.js
Lorenzo Sciandra 8441c4a6f7 fix npm lifecycle hook (#34273)
Summary:
In `0.70-stable` we started seeing `yarn run lint` fail; turns out, this is happening because the latest release of `react-native/eslint-plugin-specs` 0.0.4 (from ea8d8e2f49) is faulty: the underlying reason is that since 0.0.2 there was this local workaround in place 2d06e6a4c9 (that changes a flag from false to true) but in NPM 8 the lifecycle hook `prepublish` has [been deprecated](https://docs.npmjs.com/cli/v8/using-npm/scripts#life-cycle-scripts) so this pre publishing script to change the flag was not run (you can easily verify by checking the node_module and see `react-native-modules.js` the flag on L17 is set to false).

This PR addresses it by moving to the new lifecycle hook `prepack` (and modifies the other files accordingly).

After this PR is merged, we'll need to cherry pick it into 0.70 and do both a new release from the 0.70 branch and one from the main branch to have new versions of this module with the flag set correctly.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[General] [Fixed] - Fix eslint-plugin-specs prepack npm lifecycle hook now that we use npm 8

Pull Request resolved: https://github.com/facebook/react-native/pull/34273

Test Plan: Go in the `eslint-plugin-specs` folder, run `npm pack`, unzip the generated tar file, go into `package`, verify that L17 of `react-native-modules.js` is correctly changed to `const PACKAGE_USAGE = true;` (instead of false - which is what happen without this fix).

Reviewed By: GijsWeterings

Differential Revision: D38151433

Pulled By: dmitryrykun

fbshipit-source-id: 7c4f13dae70eb731d57cbafa90f7be05c9bb8576
2022-07-26 06:47:51 -07:00

45 lines
978 B
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and 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,
);
}
},
);
});