Skip to content

Commit

Permalink
chore: replace mkdirp with mkdir fs (#46388)
Browse files Browse the repository at this point in the history
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: #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
  • Loading branch information
anirudhdream11 authored and facebook-github-bot committed Sep 17, 2024
1 parent 3c17bea commit 7cdb87e
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 63 deletions.
18 changes: 0 additions & 18 deletions flow-typed/npm/mkdirp_v0.5.x.js

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"metro-babel-register": "^0.80.10",
"metro-memory-fs": "^0.80.10",
"micromatch": "^4.0.4",
"mkdirp": "^0.5.1",
"node-fetch": "^2.2.0",
"nullthrows": "^1.1.1",
"prettier": "2.8.8",
Expand Down
1 change: 0 additions & 1 deletion packages/react-native-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"hermes-parser": "0.23.1",
"invariant": "^2.2.4",
"jscodeshift": "^0.14.0",
"mkdirp": "^0.5.1",
"nullthrows": "^1.1.1",
"yargs": "^17.6.2"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-codegen/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const micromatch = require('micromatch');
const mkdirp = require('mkdirp');
const path = require('path');
const prettier = require('prettier');
const prettierConfig = JSON.parse(
Expand Down Expand Up @@ -60,7 +59,8 @@ function getBuildPath(file, buildFolder) {
function buildFile(file, silent) {
const destPath = getBuildPath(file, BUILD_DIR);

mkdirp.sync(path.dirname(destPath));
fs.mkdirSync(path.dirname(destPath), {recursive: true});

if (micromatch.isMatch(file, IGNORE_PATTERN)) {
silent ||
process.stdout.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

const RNCodegen = require('../../generators/RNCodegen.js');
const fs = require('fs');
const mkdirp = require('mkdirp');

const args = process.argv.slice(2);
if (args.length < 3) {
Expand All @@ -39,7 +38,7 @@ if (schemaText == null) {
throw new Error(`Can't find schema at ${schemaPath}`);
}

mkdirp.sync(outputDirectory);
fs.mkdirSync(outputDirectory, {recursive: true});

let schema;
try {
Expand Down
1 change: 0 additions & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
"memoize-one": "^5.0.0",
"metro-runtime": "^0.81.0-alpha.0",
"metro-source-map": "^0.81.0-alpha.0",
"mkdirp": "^0.5.1",
"nullthrows": "^1.1.1",
"pretty-format": "^29.7.0",
"promise": "^8.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,23 @@ describe('generateSpec', () => {
const platform = 'ios';
const libraryType = 'all';
const schemaPath = './';
const componentsOutputDir = normalize(
'app/ios/build/generated/ios/react/renderer/components/library',
);
const modulesOutputDir = normalize('app/ios/build/generated/ios/library');
const outputDirectory = normalize('app/ios/build/generated/ios');
const libraryName = 'library';
const packageName = 'com.library';
const generators = ['componentsIOS', 'modulesIOS', 'modulesCxx'];

// Create a mock for fs.mkdirSync
const mkdirSyncMock = jest.fn();

jest.mock('fs', () => ({
readFileSync: (path, encoding) => {
expect(path).toBe(schemaPath);
expect(encoding).toBe('utf-8');
return fixtures.schemaText;
},
}));

let mkdirpSyncInvoked = 0;
jest.mock('mkdirp', () => ({
sync: folder => {
if (mkdirpSyncInvoked === 0) {
expect(folder).toBe(outputDirectory);
}

if (mkdirpSyncInvoked === 1) {
expect(folder).toBe(componentsOutputDir);
}

if (mkdirpSyncInvoked === 2) {
expect(folder).toBe(modulesOutputDir);
}

mkdirpSyncInvoked += 1;
},
mkdirSync: mkdirSyncMock, // Use the mock for mkdirSync
readdirSync: jest.fn().mockReturnValue([]),
renameSync: jest.fn(),
}));

// We cannot mock directly the `RNCodegen` object because the
Expand Down Expand Up @@ -83,6 +66,9 @@ describe('generateSpec', () => {
libraryType,
);

expect(mkdirpSyncInvoked).toBe(1);
expect(mkdirSyncMock).toHaveBeenCalledTimes(1);
expect(mkdirSyncMock).toHaveBeenCalledWith(outputDirectory, {
recursive: true,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const generateSpecsCLIExecutor = require('./generate-specs-cli-executor');
const {execSync} = require('child_process');
const fs = require('fs');
const glob = require('glob');
const mkdirp = require('mkdirp');
const os = require('os');
const path = require('path');

Expand Down Expand Up @@ -513,7 +512,7 @@ function createComponentProvider(schemas, supportedApplePlatforms) {
'React',
'Fabric',
);
mkdirp.sync(outputDir);
fs.mkdirSync(outputDir, {recursive: true});
utils.getCodegen().generateFromSchemas(
{
schemas: schemas,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

const utils = require('./codegen-utils');
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');

const GENERATORS = {
Expand All @@ -33,7 +32,7 @@ function createOutputDirectoryIfNeeded(outputDirectory, libraryName) {
if (!outputDirectory) {
outputDirectory = path.resolve(__dirname, '..', 'Libraries', libraryName);
}
mkdirp.sync(outputDirectory);
fs.mkdirSync(outputDirectory, {recursive: true});
}

/**
Expand Down Expand Up @@ -95,7 +94,7 @@ function generateSpecFromInMemorySchema(
// * this subdir structure is Android-only, not applicable to iOS
const files = fs.readdirSync(outputDirectory);
const jniOutputDirectory = `${outputDirectory}/jni/react/renderer/components/${libraryName}`;
mkdirp.sync(jniOutputDirectory);
fs.mkdirSync(jniOutputDirectory, {recursive: true});
files
.filter(f => f.endsWith('.h') || f.endsWith('.cpp'))
.forEach(f => {
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native/scripts/generate-provider-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

const utils = require('./codegen/codegen-utils');
const fs = require('fs');
const mkdirp = require('mkdirp');
const yargs = require('yargs');

const argv = yargs
Expand Down Expand Up @@ -49,7 +48,7 @@ function generateProvider(platform, schemaListPath, outputDirectory) {
if (!outputDirectory) {
throw new Error('outputDir is required');
}
mkdirp.sync(outputDirectory);
fs.mkdirSync(outputDirectory, {recursive: true});

let schemaPaths;
try {
Expand Down
10 changes: 5 additions & 5 deletions scripts/__tests__/scm-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {isTaggedLatest, revertFiles, saveFiles} = require('../scm-utils');

let execResult = null;
const cpMock = jest.fn();
const mkdirpSyncMock = jest.fn();
const mkdirSyncMock = jest.fn();
jest
.mock('shelljs', () => ({
exec: () => {
Expand All @@ -29,16 +29,14 @@ jest
}))
.mock('fs', () => ({
existsSync: jest.fn().mockImplementation(_ => true),
mkdirSync: mkdirSyncMock,
}))
.mock('path', () => ({
dirname: jest
.fn()
.mockImplementation(filePath =>
filePath.includes('/') ? filePath.split('/')[0] : '.',
),
}))
.mock('mkdirp', () => ({
sync: mkdirpSyncMock,
}));

describe('scm-utils', () => {
Expand All @@ -64,7 +62,9 @@ describe('scm-utils', () => {
it('it should save files in the temp folder', () => {
const tmpFolder = '/tmp';
saveFiles(['package.json', 'android/package.json'], tmpFolder);
expect(mkdirpSyncMock).toHaveBeenCalledWith(`${tmpFolder}/android`);
expect(mkdirSyncMock).toHaveBeenCalledWith(`${tmpFolder}/android`, {
recursive: true,
});
expect(cpMock).toHaveBeenNthCalledWith(
1,
'package.json',
Expand Down
3 changes: 1 addition & 2 deletions scripts/scm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'use strict';

const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const {cp, echo, exec, exit} = require('shelljs');

Expand Down Expand Up @@ -75,7 +74,7 @@ function saveFiles(filePaths /*: Array<string> */, tmpFolder /*: string */) {
const dirName = path.dirname(filePath);
if (dirName !== '.') {
const destFolder = `${tmpFolder}/${dirName}`;
mkdirp.sync(destFolder);
fs.mkdirSync(destFolder, {recursive: true});
}
cp(filePath, `${tmpFolder}/${filePath}`);
}
Expand Down

0 comments on commit 7cdb87e

Please sign in to comment.