How to use tmp

Comprehensive tmp code examples:

How to use tmp.default:

31
32
33
34
35
36
37
38
39
40
        expect((0, common_1.serialize)((0, common_1.makeRef)({ num: 32 }))).toBe('32 0 R');
    });
});
describe('PDF generation', () => {
    it('should initialize a PDF with the correct metadata', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        const pdfPath = tmp_1.default.tmpNameSync({
            prefix: 'pdiiif-test-',
            postfix: '.pdf',
        });
        const nodeStream = fs_1.default.createWriteStream(pdfPath);

How to use tmp.setGracefulCleanup:

54
55
56
57
58
59
60
61
62
63
const testCafeConfiguration = new TestCafeConfiguration();
let keyFileContent          = null;
let keyFile                 = null;

consoleWrapper.init();
tmp.setGracefulCleanup();

beforeEach(() => {
    keyFile        = tmp.fileSync();
    keyFileContent = Buffer.from(nanoid());

How to use tmp.file:

88
89
90
91
92
93
94
95
96
97
tmp.file({ postfix: '.html' }, function (err, tmpHtmlPath, tmpHtmlFd) {
  if (err) return outputStream.emit('error', err)
  fs.closeSync(tmpHtmlFd)

  // Create tmp file to save PDF to
  tmp.file({ postfix: '.pdf' }, function (err, tmpPdfPath, tmpPdfFd) {
    if (err) return outputStream.emit('error', err)
    fs.closeSync(tmpPdfFd)

    var htmlToTmpHtmlFile = fs.createWriteStream(tmpHtmlPath)

How to use tmp.tmpName:

134
135
136
137
138
139
140
141
142
143
  res.resume();
  reject(error);
  return;
}

tmp.tmpName(
  function _tempNameGenerated(err, tmpFilename) {
    if (err) {
      reject(err);
      return;

How to use tmp.dir:

670
671
672
673
674
675
676
677
678
679
680


function getTemporaryDirectory() {
  return new Promise((resolve, reject) => {
    // Unsafe cleanup lets us recursively delete the directory if it contains
    // contents; by default it only allows removal if it's empty
    tmp.dir({ unsafeCleanup: true }, (err, tmpdir, callback) => {
      if (err) {
        reject(err);
      } else {
        resolve({

How to use tmp.tmpNameSync:

197
198
199
200
201
202
203
204
205
206
A synchronous version of the above.

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

const name = tmp.tmpNameSync();
console.log('Created temporary filename: ', name);
```

## Advanced usage

How to use tmp.fileSync:

122
123
124
125
126
127
128
129
130
131
A synchronous version of the above.

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

const tmpobj = tmp.fileSync();
console.log('File: ', tmpobj.name);
console.log('Filedescriptor: ', tmpobj.fd);
  
// If we don't need the file anymore we could manually call the removeCallback

How to use tmp.dirSync:

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();
```