How to use the cleanup function from temp

Find comprehensive JavaScript temp.cleanup code examples handpicked from public code repositorys.

temp.cleanup removes all the temporary files and directories created by temp.

1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
  jsonResultOrFailProm(res, gitPromise(req.body.command, req.body.path)).then(
    emitWorkingTreeChanged.bind(null, req.body.path)
  );
});
app.post(`${exports.pathPrefix}/testing/cleanup`, (req, res) => {
  temp.cleanup((err, cleaned) => {
    logger.info('Cleaned up: ' + JSON.stringify(cleaned));
    res.json({ result: cleaned });
  });
});
fork icon0
star icon0
watch icon0

+ 5 other calls in file

44
45
46
47
48
49
50
51
52
53
    editorElement = atom.views.getView(editor);
  });
});

afterEach(() => {
  temp.cleanup();
});

describe('When git-diff targets a file in a nested git-repository', () => {
  /***
fork icon0
star icon0
watch icon0

How does temp.cleanup work?

temp.cleanup is a function provided by the temp module in Node.js that deletes all temporary files and directories created by the temp module, based on their age or the number of files that have been created. When called, temp.cleanup iterates over all the temporary files and directories created by temp module and removes them if they match the criteria specified by the user. It returns a promise that resolves when all the files and directories have been removed. By default, temp.cleanup removes all files and directories that are more than 1 hour old or if there are more than 100 files created in the temporary directory. However, the behavior of temp.cleanup can be customized by passing options such as the age of the files to remove, or a filter function to remove only certain files.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const temp = require("temp");

// Create a temporary file
const path = temp.path({ prefix: "myPrefix-" });

// Use the temporary file

// ...

// Clean up the temporary file
temp.cleanup((err, stats) => {
  if (err) {
    console.error(err);
  } else {
    console.log("Temporary file deleted:", stats);
  }
});

In this example, temp.path() is used to create a temporary file with a given prefix. The file can then be used as needed. Finally, temp.cleanup() is called to delete the temporary file. When temp.cleanup() is called, it takes a callback function as an argument that will be called when the cleanup is complete. If there was an error during cleanup, the error object will be passed to the callback function, otherwise, the second argument will be a fs.Stats object containing information about the deleted file.