How to use the dirSync function from tmp

Find comprehensive JavaScript tmp.dirSync code examples handpicked from public code repositorys.

tmp.dirSync creates a temporary directory synchronously.

165
166
167
168
169
170
171
172
173
174
A synchronous version of the above.

```javascript
const tmp = require('tmp');

const tmpobj = tmp.dirSync();
console.log('Dir: ', tmpobj.name);
// Manual cleanup
tmpobj.removeCallback();
```
fork icon97
star icon711
watch icon7

+ 3 other calls in file

94
95
96
97
98
99
100
101
102
103
A synchronous version of the above.

```javascript
var tmp = require('tmp');

var tmpobj = tmp.dirSync();
console.log("Dir: ", tmpobj.name);
// Manual cleanup
tmpobj.removeCallback();
```
fork icon97
star icon0
watch icon2

+ 5 other calls in file

How does tmp.dirSync work?

The tmp.dirSync() method of the tmp module in Node.js creates a unique temporary directory synchronously on the system's default directory for temporary files, and returns an object that contains the path to the created directory and a method to remove it.

61
62
63
64
65
66
67
68
69
70
71
  const filepath = path.join(__dirname, '..', 'test-fixtures', filename)
  return fs.readFileSync(filepath)
}


exports.createTempStructure = function (files) {
  const tmpdir = tmp.dirSync({ unsafeCleanup: true }).name
  _.each(files, (content, filepath) => {
    fs.ensureFileSync(`${tmpdir}/${filepath}`)
    fs.writeFileSync(`${tmpdir}/${filepath}`, content)
  })
fork icon80
star icon649
watch icon20

+ 4 other calls in file

101
102
103
104
105
106
107
108
109

  return tmp.tmpNameSync({ postfix: extension })
}

Processor.prototype._tmpDir = function () {
  return tmp.dirSync().name
}

module.exports = exports = Processor
fork icon53
star icon262
watch icon18

Ai Example

1
2
3
4
5
6
const tmp = require("tmp");

// Create a temporary directory with default options
const tmpDir = tmp.dirSync();

console.log(tmpDir.name); // Outputs path to the created temporary directory

In this example, the tmp.dirSync() function creates a temporary directory with default options and returns an object containing information about the created directory. The name property of this object contains the path to the directory, which is then printed to the console.

61
62
63
64
65
66
67
68
69
70
71
exports.cssPlugin = (options = {}) => {
    return {
        name: 'esbuild-css-modules-plugin',
        setup(build) {
            const rootDir = process.cwd();
            const tmpDirPath = tmp.dirSync().name;
            const { outdir, bundle } = build.initialOptions;


            build.onResolve(
                { filter: /\.modules?\.css$/, namespace: 'file' },
fork icon15
star icon45
watch icon6

63
64
65
66
67
68
69
70
71
72
    expect(emptypassword).toStrictEqual(false);
    emptypassword = await userModelJson.checkUserPassword("skos_user", "");
    expect(emptypassword).toStrictEqual(false);
});
test("append a new user with add()", async () => {
    tmpDir = tmp.dirSync({ unsafeCleanup: true });
    fs.mkdirSync(path.join(tmpDir.name, "users"));
    const usersPath = path.join(tmpDir.name, "users", "users.json");
    fs.writeFileSync(usersPath, "{}");
    const tmpUserModelJson = new UserModelJson(usersPath);
fork icon9
star icon3
watch icon4

115
116
117
118
119
120
121
122
123
  });
}).then((stats) => {
  if (!stats.isDirectory()) {
    console.time('zipExtract');
    tmp.setGracefulCleanup();
    const tmpDirObj = tmp.dirSync({});
    const zip = new AdmZip(inputPath);
    zip.extractAllTo(tmpDirObj.name.toString(), true);
    console.timeEnd('zipExtract');
fork icon2
star icon3
watch icon0

233
234
235
236
237
238
239
240
241
242
    else {
        throw new Error(`Unsupported platform: ${platform}`);
    }
}
static async createFlatTxz(paths, outputPath) {
    const tmpDir = tmp.dirSync();
    const stagingDir = path_1.default.join(tmpDir.name, "staging");
    fs_1.default.mkdirSync(stagingDir);
    core.debug(`Created tmp dir: ${tmpDir.name}`);
    for (const p of paths) {
fork icon1
star icon2
watch icon3

25
26
27
28
29
30
31
32
33
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json(errors);

// create workspace if one does not exist for this session
if (!req.session.workspace || !fs.existsSync(req.session.workspace)) {
  req.session.workspace = tmp.dirSync().name;
}

let task = new Task();
fork icon0
star icon1
watch icon2

66
67
68
69
70
71
72
73
74
75
// a temporary directory so that they don't conflict.
updateTheReactVersionThatDevToolsReads(
  ReactVersion + '-' + sha + '-' + dateString
);
buildForChannel('stable', '', '');
const stableDir = tmp.dirSync().name;
crossDeviceRenameSync('./build', stableDir);
processStable(stableDir);
updateTheReactVersionThatDevToolsReads(
  ReactVersion + '-experimental-' + sha + '-' + dateString
fork icon0
star icon0
watch icon1

8
9
10
11
12
13
14
15
16
17
18
const tmp = require('tmp');
const fs = require('fs');
const path = require('path');


test.beforeEach(t => {
  const p = tmp.dirSync();
  t.context.cleanup = () => p.removeCallback();
  t.context.dir = p.name;
  fs.mkdirSync(p.name, {recursive: true});
});
fork icon0
star icon0
watch icon0

50
51
52
53
54
55
56
57
58
59
60
  );
  console.warn('fetched', versionData);
}


async function run() {
  const t = tmp.dirSync();
  try {
    // webpack is a peerDependency of workbox-webpack-plugin, and needs to be
    // manually installed.
    childProcess.execFileSync('npm', ['install', 'webpack'], {
fork icon0
star icon0
watch icon0

84
85
86
87
88
89
90
91
92
93
94
  downloadPlatform[os.platform()]
}.${os.arch() === 'arm64' ? 'arm64' : 'x86_64'}&release=${release}`;


let tempDirObject;
try {
  tempDirObject = tmp.dirSync();
} catch (e) {
  console.error('');
  console.error('');
  console.error('****************************');
fork icon0
star icon0
watch icon0

62
63
64
65
66
67
68
69
70
71

before(function(done) {
  // Cache the current working directory.
  curDir = process.cwd();
  // Create a temp directory.
  tmpDir = tmp.dirSync({gracefulCleanup: true, unsafeCleanup: true});
  // Switch to the temp directory.
  process.chdir(tmpDir.name);
  var sails = new Sails();
  (new Sails()).load({
fork icon0
star icon0
watch icon0

7
8
9
10
11
12
13
14
15
16
17
18
19
20


const currentVersion = require('../package').version;
const EMBER_PATH = require.resolve('../bin/ember');
const isStable = !currentVersion.includes('-beta');


let tmpdir = tmp.dirSync();


const GITHUB_TOKEN = process.env.GITHUB_TOKEN;


if (!GITHUB_TOKEN) {
fork icon0
star icon0
watch icon0