Skip to content

Commit

Permalink
fix: move updateTemplateVersion.js to commonjs + prettier (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakef committed Jun 21, 2024
1 parent 1ed1a65 commit e6c5d58
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"publishConfig": {
"access": "public"
},
"scripts": {},
"type": "commonjs",
"files": [
"template/*",
"template.config.js"
Expand All @@ -15,6 +17,6 @@
"homepage": "https://github.com/react-native-community/template/tree/main",
"repository": {
"type": "git",
"url": "https://github.com/react-native-community/template.git"
"url": "git+https://github.com/react-native-community/template.git"
}
}
6 changes: 3 additions & 3 deletions scripts/lib/lib.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const {execSync} = require('child_process');
const { execSync } = require('child_process');

const registry = getNpmRegistryUrl();

function getNpmRegistryUrl() {
try {
return execSync("npm config get registry").toString().trim();
return execSync('npm config get registry').toString().trim();
} catch {
return "https://registry.npmjs.org/";
return 'https://registry.npmjs.org/';
}
}

Expand Down
51 changes: 41 additions & 10 deletions scripts/updateReactNativeVersion.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require("fs");
const { npmMaterializeVersion } = require("./lib/lib.js");
const fs = require('fs');
const { npmMaterializeVersion } = require('./lib/lib.js');

function updateDependencies(pkgJson, update) {
console.log("Changing package.json dependencies:");
console.log('Changing package.json dependencies:');

for (const [pkg, value] of Object.entries(update.dependencies ?? {})) {
const old = pkgJson.dependencies[pkg];
Expand All @@ -27,7 +27,7 @@ function updateDependencies(pkgJson, update) {
return pkgJson;
}

const REACT_NATIVE_SCOPE = "@react-native/";
const REACT_NATIVE_SCOPE = '@react-native/';

/**
* Packages that are scoped under @react-native need a consistent version
Expand All @@ -43,30 +43,61 @@ function normalizeReactNativeDeps(deps, version) {
}

async function main(version) {
const PKG_JSON_PATH = "template/package.json";
const PKG_JSON_PATH = 'template/package.json';
// Update the react-native dependency if using the new @react-native-community/template.
// We can figure this out as it ships with react-native@1000.0.0 set to a dummy version.
let pkgJson = JSON.parse(fs.readFileSync(PKG_JSON_PATH, "utf8"));
let pkgJson = JSON.parse(fs.readFileSync(PKG_JSON_PATH, 'utf8'));

// Materialize a tag to a version. E.g. next -> 0.75.0-rc.0
const concreteVersion = await npmMaterializeVersion("react-native", version);
const concreteVersion = await npmMaterializeVersion('react-native', version);
console.log(
`Normalizing: react-native@${version} -> react-native@${concreteVersion}`,
);

pkgJson = updateDependencies(pkgJson, {
dependencies: {
"react-native": concreteVersion,
'react-native': concreteVersion,
...normalizeReactNativeDeps(pkgJson.dependencies, concreteVersion),
},
devDependencies: {
...normalizeReactNativeDeps(pkgJson.devDependencies, concreteVersion),
},
});

const updated = JSON.stringify(pkgJson, null, 2);
console.log(`Writing update package.json to ${PKG_JSON_PATH}:\n\n${updated}`);
let updated = JSON.stringify(pkgJson, null, 2);
console.log(
`\nWriting update template/package.json to ${PKG_JSON_PATH}:\n\n${updated}`,
);
fs.writeFileSync(PKG_JSON_PATH, updated);

// 2. Update the scripts.version field in the top level package.json. This lets us leverage
// the https://registry.npmjs.org/@react-native-community/template API:
//
// "name": "@react-native-community/template",
// "dist-tags": {
// ...
// "0.75-stable": "0.75.0-rc.0"
// },
// "versions": {
// "0.75.0-rc.0": {
// "name": "@react-native-community/template",
// "version": "0.75.0-rc.0",
// "scripts": {
// "version": "0.75.1"
// ...
// },
//
// We can then correlate earlier version of react-native with earlier template. Significantly
// none of this is 'user facing'.
const PKG_JSON_ROOT_PATH = './package.json';
pkgJson = JSON.parse(fs.readFileSync(PKG_JSON_ROOT_PATH, 'utf8'));
pkgJson.scripts ??= {};
pkgJson.scripts.version = concreteVersion;
updated = JSON.stringify(pkgJson, null, 2);
console.log(
`\nWriting update package.json to ${PKG_JSON_ROOT_PATH}:\n\n${updated}`,
);
fs.writeFileSync(PKG_JSON_ROOT_PATH, updated);
}

if (require.main === module) {
Expand Down
12 changes: 6 additions & 6 deletions scripts/updateTemplateVersion.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const fs = require("fs");
const { readFileSync, writeFileSync } = require('fs');

/**
* Function to get the Nightly version of the package, similar to the one used in React Native.
* Version will look as follows:
* `0.75.0-nightly-20241010-abcd1234`
*/
function getNightlyVersion(originalVersion) {
const version = originalVersion.split("-")[0];
const version = originalVersion.split('-')[0];
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
Expand All @@ -17,19 +17,19 @@ function getNightlyVersion(originalVersion) {
}

if (!process.argv[2]) {
console.error("Please provide a version to update the template to.");
console.error('Please provide a version to update the template to.');
process.exit(1);
}
const targetVersion = process.argv[2];

// We first update version of the template we're about to publish.
const packageJsonData = fs.readFileSync("package.json", "utf8");
const packageJsonData = readFileSync('package.json', 'utf8');
const packageJson = JSON.parse(packageJsonData);
if (targetVersion === "nightly") {
if (targetVersion === 'nightly') {
packageJson.version = getNightlyVersion(packageJson.version);
} else {
packageJson.version = targetVersion;
}
const updatedPackageJsonData = JSON.stringify(packageJson, null, 2);
fs.writeFileSync("package.json", updatedPackageJsonData, "utf8");
writeFileSync('package.json', updatedPackageJsonData, 'utf8');
console.log(`Template version updated to ${packageJson.version}`);

0 comments on commit e6c5d58

Please sign in to comment.