How to use the mkdtemp function from fs-extra

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

fs-extra.mkdtemp is a function in the fs-extra library that creates a temporary directory with a unique name.

231
232
233
234
235
236
237
238
239
240
- fs.copyFile():复制文件
- fs.createReadStream():创建可读的文件流
- fs.createWriteStream():创建可写文件流
- fs.link():创建指向文件的新硬链接
- fs.mkdir(): 新建一个文件夹
- fs.mkdtemp():创建一个临时目录
- fs.open():设置文件模式
- fs.readdir():读取目录的内容
- fs.readFile():读取文件的内容。有关:fs.read()
- fs.readlink():读取符号链接的值
fork icon0
star icon12
watch icon1

+ 13 other calls in file

7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
  'truncate',
  'unlink',
  'utimes',
  'writeFile'
]
// fs.mkdtemp() was added in Node.js v5.10.0, so check if it exists
typeof fs.mkdtemp === 'function' && api.push('mkdtemp')


// Export all keys:
Object.keys(fs).forEach(key => {
fork icon0
star icon1
watch icon0

+ 8 other calls in file

How does fs-extra.mkdtemp work?

fs-extra.mkdtemp is a function in the fs-extra library that creates a temporary directory with a unique name. It takes two arguments: the prefix to use for the directory name, and an optional options object.

The prefix argument should be a string that will be used as the beginning of the directory name. The function will append a unique string of characters to the prefix to create a unique directory name.

The options object can include any of the following properties:

  • encoding: the character encoding to use when creating the directory (default is 'utf8')
  • mode: the permissions to set on the new directory (default is 0o700)
  • dir: the directory in which to create the temporary directory (default is the system's temporary directory)

When called, fs-extra.mkdtemp will create a new temporary directory with a unique name, using the specified prefix and options if provided. The function will return the path to the new directory.

Because fs-extra.mkdtemp is an asynchronous function, it returns a promise. When the promise resolves, the new directory has been created successfully.

Note that the directory created by fs-extra.mkdtemp will be deleted automatically when the process exits. If you need to delete the directory before the process exits, you can use fs-extra.remove to delete it.

41
42
43
44
45
46
47
48
49
50
  // Template should be an npm package. Fetch template info
  templatePackageInfo = await getTemplatePackageInfo(scope.template);
  console.log(`Installing ${chalk.yellow(templatePackageInfo.name)} template.`);

  // Download template repository to a temporary directory
  templateParentPath = await fse.mkdtemp(path.join(os.tmpdir(), 'strapi-'));
  templatePath = await downloadNpmTemplate(templatePackageInfo, templateParentPath);
}

// Make sure the downloaded template matches the required format
fork icon0
star icon0
watch icon1

+ 6 other calls in file

45
46
47
48
49
50
51
52
53
54
55
56
    strapi.telemetry.send('didSaveMediaWithAlternativeText');
  }
};


const createAndAssignTmpWorkingDirectoryToFiles = async (files) => {
  const tmpWorkingDirectory = await fse.mkdtemp(path.join(os.tmpdir(), 'strapi-upload-'));


  if (Array.isArray(files)) {
    files.forEach((file) => {
      file.tmpWorkingDirectory = tmpWorkingDirectory;
fork icon0
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const fs = require("fs-extra");

fs.mkdtemp("/tmp/myapp-")
  .then((dir) => {
    console.log(`Created temporary directory ${dir}`);
  })
  .catch((err) => {
    console.error("Error creating temporary directory:", err);
  });

In this example, we call fs-extra.mkdtemp with the prefix '/tmp/myapp-'. The function will append a unique string of characters to this prefix to create a unique directory name. When the promise returned by fs-extra.mkdtemp resolves, the then callback is called with the path to the new temporary directory. We log a success message to the console, including the path to the new directory. If an error occurs while creating the directory, the catch callback is called with an error object. We log an error message to the console, including the error object.

31
32
33
34
35
36
37
38
39
40
  }
}

async testSymlink (comboOpts, zipPath) {
  await fs.mkdirp(this.tempBase)
  const testPath = await fs.mkdtemp(path.join(this.tempBase, `symlink-test-${comboOpts.platform}-${comboOpts.arch}-`))
  const testFile = path.join(testPath, 'test')
  const testLink = path.join(testPath, 'testlink')

  try {
fork icon0
star icon0
watch icon1

+ 17 other calls in file

function icon

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