How to use the mkdirpSync function from fs-extra

Find comprehensive JavaScript fs-extra.mkdirpSync code examples handpicked from public code repositorys.

fs-extra.mkdirpSync creates a new directory and any necessary subdirectories synchronously.

155
156
157
158
159
160
161
162
163
let deployedEnvelopePath = this.deploymentPathForDevice(job_status.owner, job_status.udid) + "/build.json";
let envelopeString = JSON.stringify(buildEnvelope, null, 4);
let buffer = Buffer.from(envelopeString + "\n");

try {
    fs.mkdirpSync(envelopeFolder); // lgtm [js/path-injection]
} catch (e) {
    console.log("ℹ️ [info] No need to create envelopeFolder, is this another issue?", {e});
}
fork icon8
star icon20
watch icon0

+ 3 other calls in file

513
514
515
516
517
518
519
520
521
522
const dstPath = srcPath.replace(/\.html$/, '') + '/index.html';
const dstPathDir = path.dirname(dstPath);
const srcPathRel = path.relative(dstPathDir, srcPath);

// Ensure directory exists
fs.mkdirpSync(dstPathDir);

// Change into the directory and create a relative symlink
chdir(dstPathDir, () => {
  fs.ensureSymlinkSync(srcPathRel, 'index.html');
fork icon1
star icon8
watch icon1

+ 3 other calls in file

How does fs-extra.mkdirpSync work?

fs-extra.mkdirpSync is a method in the fs-extra library that creates a new directory path and its subdirectories synchronously, recursively creating all directories needed to fulfill the given path.

29
30
31
32
33
34
35
36
37
38
  })
  // 批量插入user_role
  await sequelize.query(`insert into user_role (userId, roleId) values${values.join(',')}`)
  // 然后创建用户独立空间: 即文件目录
  const dir = path.join(__dirname, '../resource', res.id)
  fse.mkdirpSync(dir)
  return res
} catch (error) {
  throw error
}
fork icon1
star icon5
watch icon1

19
20
21
22
23
24
25
26
27
28
29
    const regex1 = /\n\/\/ SPDX.*\n/gm;


    let i = 0;
    const newSource = source.replace(regex1, (m) => (!i++ ? m : ''));


    fse.mkdirpSync(path.join(env.config.paths.flattened, path.dirname(file)));
    fse.writeFileSync(path.join(env.config.paths.flattened, file), newSource);
  }
});
fork icon0
star icon0
watch icon3

+ 3 other calls in file

Ai Example

1
2
3
4
const fse = require("fs-extra");

// Create a new directory and any necessary parent directories
fse.mkdirpSync("/path/to/new/directory");

In this example, fs-extra is first imported and then the mkdirpSync method is called with the path to the new directory as its argument. This method will create the new directory and any necessary parent directories synchronously, blocking the execution of any further code until it has completed. If the directory already exists, mkdirpSync will do nothing.

31
32
33
34
35
36
37
38
39
40
try {
    const dirName = path.dirname(filePath);
    // check if folder already exist
    // else create one
    if (!fs.existsSync(dirName)) {
        fs.mkdirpSync(dirName);
    }

    fs.writeFileSync(filePath, data);
    // Print a success message.
fork icon0
star icon0
watch icon2

+ 12 other calls in file

63
64
65
66
67
68
69
70
71
72
  if (!this.cachedStagingPath) {
    const parentDir = path.join(
      common.baseTempDir(this.opts),
      `${this.opts.platform}-${this.opts.arch}`
    )
    fs.mkdirpSync(parentDir)
    this.cachedStagingPath = fs.mkdtempSync(path.join(parentDir, `${common.generateFinalBasename(this.opts)}-`))
  }
  return this.cachedStagingPath
}
fork icon0
star icon0
watch icon1

+ 3 other calls in file

255
256
257
258
259
260
261
262
263
264
async function deploy(project, branch, timestamp, logName) {
  const lockFile = `${root}/locks/deploy.lock`;
  const logFile = `${root}/logs/deployment/${logName}.txt`;
  const shortName = branch.shortName || project.shortName || project.name;
  const dir = `${root}/apps/${shortName}`;
  await fs.mkdirpSync(dir);
  const checkout = `${dir}/checkout`;
  const current = `${dir}/current`;
  const deployTo = `${dir}/deployments/${timestamp}`;
  let stopped = false;
fork icon0
star icon0
watch icon0

+ 5 other calls in file

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)