How to use the sync function from mkdirp

Find comprehensive JavaScript mkdirp.sync code examples handpicked from public code repositorys.

mkdirp.sync is a Node.js module that creates a new directory and any necessary subdirectories synchronously.

15
16
17
18
19
20
21
22
23
24
// FIXME
process.setMaxListeners(0);

const maybeMkdirp = (dir) => {
  try {
    mkdirp.sync(dir);
  } catch (e) {
    if (e.code !== 'EEXIST') throw e;
  }
}
fork icon53
star icon131
watch icon10

+ 3 other calls in file

16
17
18
19
20
21
22
23
24
25
  // (https://ninja-build.org/manual.html#_depfile)
  return `${outputName}: ${depPaths.join(' ')}`
}

function writeDepfileContentSync(filePath, content) {
  mkdirp.sync(path.dirname(filePath))
  fs.writeFileSync(filePath, content, { encoding: 'utf8' })
}

class GenerateDepfilePlugin {
fork icon734
star icon0
watch icon109

How does mkdirp.sync work?

The mkdirp.sync function is a synchronous implementation of creating a new directory and any necessary subdirectories in a given path by recursively creating directories if they don't exist. If the directory already exists, it won't do anything.

98
99
100
101
102
103
104
105
  if (path.isAbsolute(config.output)) outputDir = config.output;
  else outputDir = path.join(testRoot, config.output);

  if (!fileExists(outputDir)) {
    output.print(`creating output directory: ${outputDir}`);
    mkdirp.sync(outputDir);
  }
};
fork icon706
star icon0
watch icon106

+ 3 other calls in file

37
38
39
40
41
42
43
44
45
46
47
48


var uploadPrefix = "upload";


function createFolderIfNeeded(path) {
    if (!fs.existsSync(path)) {
        mkdirp.sync(path, function(err) {
            if (err) console.error(err)
        })
    }
}
fork icon376
star icon962
watch icon27

+ 4 other calls in file

Ai Example

1
2
3
4
const mkdirp = require("mkdirp");

// Create a new directory if it doesn't already exist
mkdirp.sync("/path/to/new/directory");

In this example, we're using mkdirp.sync to create a new directory at the specified path, /path/to/new/directory. If the directory already exists, nothing happens. If it doesn't exist, it will be created, including any parent directories that need to be created as well. The sync method is used to perform the operation synchronously, which means that the function will block until it has completed, and will not return until the directory has been created.

21
22
23
24
25
26
27
28
29
30
31
    // Ignore error
  }


  if (currentCssText !== cssText) {
    // TODO: Remove unnecessary dependency on mkdirp
    mkdirp.sync(path.dirname(outputFilename))
    fs.writeFileSync(outputFilename, cssText)
  }
}

fork icon7
star icon415
watch icon15

+ 8 other calls in file

167
168
169
170
171
172
173
174
175
176
function copyEventImage(inFilepath, eventId) {
  const extname = path.extname(inFilepath)
  const outUrl = `event-images/${eventId}${extname}`
  const outFilepath = `public/${outUrl}`
  console.log(`* Copying image "${outFilepath}"`)
  mkdirp.sync(path.dirname(outFilepath))
  fs.copyFileSync(inFilepath, outFilepath)
  return outUrl
}

fork icon62
star icon195
watch icon24

930
931
932
933
934
935
936
937
938
939
940
941
        return err;
    }
};


exports.asFiles = function asFiles(json, outDir) {
    mkdirp.sync(outDir);


    Object.keys(json).forEach((moduleName) => {
        const licenseFile = json[moduleName].licenseFile;

fork icon22
star icon77
watch icon0

7
8
9
10
11
12
13
14
15
16
17
18
const zlib = require('zlib');
const childProcess = require('child_process');


exports.mkdirp = function (dir, mode) {
  mode = mode || '0755';
  mkdirp.sync(dir, mode);
};


exports.md5 = function (str) {
  if (typeof str !== 'string') {
fork icon12
star icon36
watch icon7

126
127
128
129
130
131
132
133
134
135
136
137
    return d + "d " + h + "h " + m + "m " + s + "s";
  }
}


async function writeAt(filePath, jsonPath, value) {
  mkdirp.sync(getDirName(filePath));


  return fs
    .readJson(filePath)
    .then(function (json) {
fork icon14
star icon16
watch icon2

30
31
32
33
34
35
36
37
38
39
  '--rc-path', 'remark-build.config.js',
];

var build = function() {
  rimraf.sync(program.dist);
  mkdirp.sync(program.dist);

  var files = GetFiles(path.join(program.src, '**', '*.md'));
  if (!files.length) {
    log.error('Source directory ' + process.src + ' doesnt exist or contains no md files!');
fork icon6
star icon0
watch icon3

+ 7 other calls in file

201
202
203
204
205
206
207
208
209
210
    time: 100000
});

filesArray.reverse().forEach(function (fileInfo) {
    var folder = path.dirname(fileInfo.target);
    mkdirp.sync(folder);
    var file = fs.createWriteStream(fileInfo.target);
    baseRequest(fileInfo.source)
        .on('error', function(err) {
            console.log(err);
fork icon4
star icon2
watch icon3

+ 3 other calls in file

63
64
65
66
67
68
69
70
71

// Prepare configuration file with masked email address
let settingsDir = config.app.logDirectory + "/" + testName;
let settingsFile = settingsDir + "/settings.json";
if (!fs.existsSync(settingsDir)) {
  mkdirp.sync(settingsDir);
}

let fullEmail = urlTests[testName]["emailAlertsTo"];
fork icon1
star icon0
watch icon0

+ 4 other calls in file

61
62
63
64
65
66
67
68
69
70
ExtractPosts.prototype = {
  putPosts: function (postsdetails, key) {
    var self = this;
    var folderpath = entriesFolderPath + "/" + key;
    if (!fs.existsSync(folderpath)) {
      mkdirp.sync(folderpath);
      helper.writeFile(path.join(folderpath, "en-us.json"));
    }
    var contenttype = helper.readFile(path.join(folderpath, "en-us.json"));

fork icon0
star icon1
watch icon1

+ 4 other calls in file

623
624
625
626
627
628
629
630
631
//console.log(response);
//Create Path
let dirpath = path.dirname(savepath);
if (!fs.existsSync(dirpath)) {
    console.log("Creating path: " + dirpath);
    mkdirp.sync(dirpath);
}
var out = fs.createWriteStream(savepath);
res.body.pipe(out);
fork icon0
star icon2
watch icon11

+ 3 other calls in file

215
216
217
218
219
220
221
222
223
224
225
226
      rimraf.sync("Build/createThirdPartyNpm");
    });
}


gulp.task("build", async function () {
  mkdirp.sync("Build");


  fs.writeFileSync(
    "Build/package.json",
    JSON.stringify({
fork icon0
star icon1
watch icon1

+ 9 other calls in file

3839
3840
3841
3842
3843
3844
3845
3846
3847
3848

if (options.reporterOptions && options.reporterOptions.output) {
  if (!fs.createWriteStream) {
    throw new Error('file output not supported in browser');
  }
  mkdirp.sync(path.dirname(options.reporterOptions.output));
  self.fileStream = fs.createWriteStream(options.reporterOptions.output);
}

runner.on('pending', function(test) {
fork icon0
star icon1
watch icon1

134
135
136
137
138
139
140
141
142
143
 */
exports.getSync = function(key, options = {}) {
  const fileName = _.partial(utils.getFileName, key, {
    dataPath: options.dataPath
  })();
  mkdirp.sync(path.dirname(fileName));
  let objectJSON = {};
  try {
    const object = fs.readFileSync(fileName, 'utf-8');
    objectJSON = JSON.parse(object);
fork icon0
star icon1
watch icon2

+ 11 other calls in file

20
21
22
23
24
25
26
27
28
29
30
31
32


const outputLogFilePath = path.join(__dirname, nconf.get('logFile') || 'logs/output.log');


const logDir = path.dirname(outputLogFilePath);
if (!fs.existsSync(logDir)) {
    mkdirp.sync(path.dirname(outputLogFilePath));
}


const output = logrotate({ file: outputLogFilePath, size: '1m', keep: 3, compress: true });
const silent = nconf.get('silent') === 'false' ? false : nconf.get('silent') !== false;
fork icon1
star icon0
watch icon5

118
119
120
121
122
123
124
125
126
127
// Write the binary data to disk
let filename = path.join(
    process.env.DICOM_STORE_ROOTPATH,
    relativeFilename
);
mkdirp.sync(
    path.join(
        process.env.DICOM_STORE_ROOTPATH,
        `files/bulkData/${shortInstanceUID}`
    )
fork icon0
star icon0
watch icon1

+ 11 other calls in file

482
483
484
485
486
487
488
489
490
491
for (let input of inputs) {
  let inputContent = input.filename
  let pssh = input.pssh
  inputContent = path.resolve(inputContent)
  let outputContent = `${rootDir}/${this.util.randomFileName('.mp4')}`
  mkdirp.sync(rootDir)
  let promise = null
  let cacheId = `${pssh}`
  let key = await this.redisClient.getAsync(cacheId)
  if (!key) {
fork icon0
star icon0
watch icon1

+ 64 other calls in file