Skip to content

Commit 6cacc6d

Browse files
huntiefacebook-github-bot
authored andcommitted
Relocate and export updatePackageJson script util
Summary: Changelog: [Internal] Reviewed By: lunaleaps Differential Revision: D54423651 fbshipit-source-id: e6705b055bf1f1d70d86d2c6978b837bd0807081
1 parent b919ff6 commit 6cacc6d

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

scripts/releases/set-version/index.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,14 @@
1515
import type {PackageJson} from '../../utils/monorepo';
1616
*/
1717

18-
const {getPackages, getWorkspaceRoot} = require('../../utils/monorepo');
18+
const {
19+
getPackages,
20+
getWorkspaceRoot,
21+
updatePackageJson,
22+
} = require('../../utils/monorepo');
1923
const {setReactNativeVersion} = require('../set-rn-version');
20-
const {promises: fs} = require('fs');
21-
const path = require('path');
2224
const yargs = require('yargs');
2325

24-
async function updatePackageJson(
25-
packagePath /*: string */,
26-
packageJson /*: PackageJson */,
27-
newPackageVersions /*: $ReadOnly<{[string]: string}> */,
28-
) /*: Promise<void> */ {
29-
const packageName = packageJson.name;
30-
31-
if (packageName in newPackageVersions) {
32-
packageJson.version = newPackageVersions[packageName];
33-
}
34-
35-
for (const dependencyField of ['dependencies', 'devDependencies']) {
36-
const deps = packageJson[dependencyField];
37-
38-
if (deps == null) {
39-
continue;
40-
}
41-
42-
for (const dependency in newPackageVersions) {
43-
if (dependency in deps) {
44-
deps[dependency] = newPackageVersions[dependency];
45-
}
46-
}
47-
}
48-
49-
return fs.writeFile(
50-
path.join(packagePath, 'package.json'),
51-
JSON.stringify(packageJson, null, 2) + '\n',
52-
);
53-
}
54-
5526
/**
5627
* Sets a singular version for the entire monorepo.
5728
*

scripts/utils/monorepo.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
const {REPO_ROOT} = require('../consts');
13-
const fs = require('fs');
13+
const {promises: fs} = require('fs');
1414
const glob = require('glob');
1515
const path = require('path');
1616

@@ -91,7 +91,7 @@ async function parsePackageInfo(
9191
) /*: Promise<[string, PackageInfo]> */ {
9292
const packagePath = path.dirname(packageJsonPath);
9393
const packageJson /*: PackageJson */ = JSON.parse(
94-
await fs.promises.readFile(packageJsonPath, 'utf-8'),
94+
await fs.readFile(packageJsonPath, 'utf-8'),
9595
);
9696

9797
return [
@@ -104,7 +104,52 @@ async function parsePackageInfo(
104104
];
105105
}
106106

107+
/**
108+
* Update a given package with the package versions.
109+
*/
110+
async function updatePackageJson(
111+
packagePath /*: string */,
112+
packageJson /*: PackageJson */,
113+
newPackageVersions /*: $ReadOnly<{[string]: string}> */,
114+
) /*: Promise<void> */ {
115+
const packageName = packageJson.name;
116+
117+
if (packageName in newPackageVersions) {
118+
packageJson.version = newPackageVersions[packageName];
119+
}
120+
121+
for (const dependencyField of ['dependencies', 'devDependencies']) {
122+
const deps = packageJson[dependencyField];
123+
124+
if (deps == null) {
125+
continue;
126+
}
127+
128+
for (const dependency in newPackageVersions) {
129+
if (dependency in deps) {
130+
deps[dependency] = newPackageVersions[dependency];
131+
}
132+
}
133+
}
134+
135+
return writePackageJson(path.join(packagePath, 'package.json'), packageJson);
136+
}
137+
138+
/**
139+
* Write a `package.json` file to disk.
140+
*/
141+
async function writePackageJson(
142+
packageJsonPath /*: string */,
143+
packageJson /*: PackageJson */,
144+
) /*: Promise<void> */ {
145+
return fs.writeFile(
146+
packageJsonPath,
147+
JSON.stringify(packageJson, null, 2) + '\n',
148+
);
149+
}
150+
107151
module.exports = {
108152
getPackages,
109153
getWorkspaceRoot,
154+
updatePackageJson,
110155
};

0 commit comments

Comments
 (0)