Files
react-native/scripts/scm-utils.js
T
Vincenzo Vitale 10e47b891a Do not depend on an ENV variable when publishing and setting the RN version (#34746)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34746

The changes made in
https://github.com/facebook/react-native/pull/34694
introduced the need to have the env variable TMP_PUBLISH_DIR for the publishing and set-rn-version scripts to work.
This break any usage of set-rn-version when the env variable is not set upfront.

With this change, we are creating a temp folder in the scope that requires it (e.g. set-rn-version.js) and then passing the path to the save/revert functions.

## Changelog
[Internal] [Added] - Do not depend on an ENV variable when publishing and setting the RN version.

Reviewed By: cipolleschi

Differential Revision: D39683565

fbshipit-source-id: 21d85d1c16c4cb7324636ceb5eba626ff8cbb775
2022-09-22 07:34:50 -07:00

96 lines
2.0 KiB
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
*/
'use strict';
const {cp, echo, exec, exit} = require('shelljs');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
function isGitRepo() {
try {
return (
exec('git rev-parse --is-inside-work-tree', {
silent: true,
}).stdout.trim() === 'true'
);
} catch (error) {
echo(
`It wasn't possible to check if we are in a git repository. Details: ${error}`,
);
}
return false;
}
function exitIfNotOnGit(command, errorMessage, gracefulExit = false) {
if (isGitRepo()) {
return command();
} else {
echo(errorMessage);
exit(gracefulExit ? 0 : 1);
}
}
function isTaggedLatest(commitSha) {
return (
exec(`git rev-list -1 latest | grep ${commitSha}`, {
silent: true,
}).stdout.trim() === commitSha
);
}
function getBranchName() {
return exec('git rev-parse --abbrev-ref HEAD', {
silent: true,
}).stdout.trim();
}
function getCurrentCommit() {
return isGitRepo()
? exec('git rev-parse HEAD', {
silent: true,
}).stdout.trim()
: 'TEMP';
}
function saveFiles(filePaths, tmpFolder) {
for (const filePath of filePaths) {
const dirName = path.dirname(filePath);
if (dirName !== '.') {
const destFolder = `${tmpFolder}/${dirName}`;
mkdirp.sync(destFolder);
}
cp(filePath, `${tmpFolder}/${filePath}`);
}
}
function revertFiles(filePaths, tmpFolder) {
for (const filePath of filePaths) {
const absoluteTmpPath = `${tmpFolder}/${filePath}`;
if (fs.existsSync(absoluteTmpPath)) {
cp(absoluteTmpPath, filePath);
} else {
echo(
`It was not possible to revert ${filePath} since ${absoluteTmpPath} does not exist.`,
);
exit(1);
}
}
}
module.exports = {
exitIfNotOnGit,
getCurrentCommit,
getBranchName,
isTaggedLatest,
revertFiles,
saveFiles,
};