Skip to content

Commit ccea0bf

Browse files
committed
fix: use temp in grammar-utils
1 parent bf7a549 commit ccea0bf

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

lib/grammar-utils.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
// Require some libs used for creating temporary files
44
import os from "os"
5-
import fs from "fs"
5+
import * as fs from "fs"
66
import path from "path"
7-
import { v1 as uuidv1 } from "uuid"
7+
import * as temp from "temp"
8+
temp.track()
89

910
// Public: GrammarUtils - utilities for determining how to run code
1011
const GrammarUtils = {
@@ -17,17 +18,10 @@ const GrammarUtils = {
1718
// Returns the {String} filepath of the new file
1819
createTempFileWithCode(code, extension = "") {
1920
try {
20-
if (!fs.existsSync(this.tempFilesDir)) {
21-
fs.mkdirSync(this.tempFilesDir)
22-
}
23-
24-
const tempFilePath = this.tempFilesDir + path.sep + uuidv1() + extension
25-
26-
const file = fs.openSync(tempFilePath, "w")
27-
fs.writeSync(file, code)
28-
fs.closeSync(file)
29-
30-
return tempFilePath
21+
const tempFile = temp.openSync({ dir: this.tempFilesDir, suffix: extension })
22+
fs.writeSync(tempFile.fd, code)
23+
fs.closeSync(tempFile.fd)
24+
return tempFile.path
3125
} catch (error) {
3226
throw new Error(`Error while creating temporary file (${error})`)
3327
}
@@ -37,14 +31,7 @@ const GrammarUtils = {
3731
// {GrammarUtils::createTempFileWithCode}
3832
deleteTempFiles() {
3933
try {
40-
if (fs.existsSync(this.tempFilesDir)) {
41-
const files = fs.readdirSync(this.tempFilesDir)
42-
if (files.length) {
43-
files.forEach((file) => fs.unlinkSync(this.tempFilesDir + path.sep + file))
44-
}
45-
return fs.rmdirSync(this.tempFilesDir)
46-
}
47-
return null
34+
return temp.cleanupSync()
4835
} catch (error) {
4936
throw new Error(`Error while deleting temporary files (${error})`)
5037
}

0 commit comments

Comments
 (0)