Skip to content

Commit

Permalink
Replace mkdirp to fs.mkdir with recursive option
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSangHan committed Mar 14, 2020
1 parent 2f26478 commit 58e8216
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions karma.conf.js
Expand Up @@ -2,8 +2,8 @@

const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const os = require('os');
const utils = require('./lib/utils');
const baseBundleDirpath = path.join(__dirname, '.karma');

const hostname = os.hostname();
Expand Down Expand Up @@ -111,7 +111,7 @@ module.exports = config => {
console.error('No SauceLabs credentials present');
}
}
mkdirp.sync(bundleDirpath);
utils.mkdir(bundleDirpath);
} else {
console.error('CI mode disabled');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/init.js
Expand Up @@ -9,7 +9,7 @@

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

exports.command = 'init <path>';

Expand All @@ -24,7 +24,7 @@ exports.builder = yargs =>
exports.handler = argv => {
const destdir = argv.path;
const srcdir = path.join(__dirname, '..', '..');
mkdirp.sync(destdir);
utils.mkdir(destdir);
const css = fs.readFileSync(path.join(srcdir, 'mocha.css'));
const js = fs.readFileSync(path.join(srcdir, 'mocha.js'));
const tmpl = fs.readFileSync(
Expand Down
17 changes: 17 additions & 0 deletions lib/utils.js
Expand Up @@ -853,3 +853,20 @@ exports.supportsEsModules = function() {
}
}
};

/**
* Recursively mkdir, like `mkdirp`
*
* @description
* This function is to create a new directory and any necessary subdirectories at the directory.
* `fs.mkdir` with {recursive: true} can be a replacement for vulnerable `mkdirp`.
*
* @private
* @param {string} pathname - Directory path to create.
* @returns {undefined} void
*/
exports.mkdir = function(pathname) {
if (!fs.existsSync(pathname)) {
fs.mkdirSync(pathname, {recursive: true});
}
};
3 changes: 1 addition & 2 deletions test/integration/file-utils.spec.js
Expand Up @@ -4,7 +4,6 @@ var utils = require('../../lib/utils');
var fs = require('fs');
var path = require('path');
var os = require('os');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');

describe('file utils', function() {
Expand Down Expand Up @@ -119,7 +118,7 @@ describe('file utils', function() {
afterEach(removeTempDir);

function makeTempDir() {
mkdirp.sync(tmpDir);
utils.mkdir(tmpDir);
}

function removeTempDir() {
Expand Down

0 comments on commit 58e8216

Please sign in to comment.