react-native/scripts/scm-utils.js
anirudh.bharti 7cdb87eb19 chore: replace mkdirp with mkdir fs (#46388)
Summary:
This pull request replaces the use of mkdirp with Node.js's built-in fs.mkdirSync({ recursive: true }) function, which is available in Node.js version 10.12.0 and above. This change reduces the number of external dependencies and simplifies the codebase by using the native capabilities of Node.js.

The motivation behind this change is to remove the unnecessary mkdirp dependency, as Node.js natively supports recursive directory creation since version 10.12.0. This streamlines the code and reduces the reliance on external libraries.

## Changelog:

[INTERNAL] [REMOVED] - Replaced mkdirp with fs.mkdirSync({ recursive: true }) in build scripts and codegen. Requires Node.js 10.12.0 and above.

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

Test Plan: I ran the build and codegen scripts locally with Node.js version 10.12.0 and above after replacing mkdirp, ensuring the scripts work as expected. No issues were encountered, and all processes, including directory creation and file handling, function correctly.

Reviewed By: cortinico

Differential Revision: D62852488

Pulled By: huntie

fbshipit-source-id: 76f44102a80b499521c156308d276a17d279ce38
2024-09-17 07:03:44 -07:00

119 lines
2.5 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.
*
* @flow strict-local
* @format
*/
'use strict';
const fs = require('fs');
const path = require('path');
const {cp, echo, exec, exit} = require('shelljs');
/*::
type Commit = string
*/
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 /*::<T>*/(
command /*: () => T */,
errorMessage /*: string */,
gracefulExit /*: boolean */ = false,
// $FlowFixMe[incompatible-return] Asserts return value
) /*: T */ {
if (isGitRepo()) {
return command();
} else {
echo(errorMessage);
exit(gracefulExit ? 0 : 1);
}
}
function isTaggedLatest(commitSha /*: Commit */) /*: boolean */ {
return (
exec(`git rev-list -1 latest | grep ${commitSha}`, {
silent: true,
}).stdout.trim() === commitSha
);
}
function getBranchName() /*: string */ {
return exec('git rev-parse --abbrev-ref HEAD', {
silent: true,
}).stdout.trim();
}
function getCurrentCommit() /*: Commit */ {
return isGitRepo()
? exec('git rev-parse HEAD', {
silent: true,
}).stdout.trim()
: 'TEMP';
}
function saveFiles(filePaths /*: Array<string> */, tmpFolder /*: string */) {
for (const filePath of filePaths) {
const dirName = path.dirname(filePath);
if (dirName !== '.') {
const destFolder = `${tmpFolder}/${dirName}`;
fs.mkdirSync(destFolder, {recursive: true});
}
cp(filePath, `${tmpFolder}/${filePath}`);
}
}
function revertFiles(filePaths /*: Array<string> */, tmpFolder /*: string */) {
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);
}
}
}
// git restore for local path
function restore(repoPath /*: string */) {
const result = exec('git restore .', {
cwd: repoPath,
});
if (result.code !== 0) {
throw new Error(result.stderr);
}
return;
}
module.exports = {
exitIfNotOnGit,
getCurrentCommit,
getBranchName,
isTaggedLatest,
revertFiles,
saveFiles,
restore,
};