Skip to content

Commit

Permalink
chore: replace temp library with tmp (#633)
Browse files Browse the repository at this point in the history
* use tmp library instead of temp

* less changes

* remove cleanup call

* enforce graceful cleanup
  • Loading branch information
r4zendev authored Oct 7, 2024
1 parent 7c4a6f5 commit f25d7b7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 43 deletions.
2 changes: 0 additions & 2 deletions bin/__tests__/jscodeshift-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ jest.setTimeout(600000); // 10 minutes
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const temp = require('temp');
const testUtils = require('../../utils/testUtils');
const {chdir} = require('process');

const createTransformWith = testUtils.createTransformWith;
const createTempFileWith = testUtils.createTempFileWith;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"neo-async": "^2.5.0",
"picocolors": "^1.0.1",
"recast": "^0.23.9",
"temp": "^0.9.4",
"tmp": "^0.2.3",
"write-file-atomic": "^5.0.1"
},
"peerDependencies": {
Expand Down
17 changes: 7 additions & 10 deletions src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const fs = require('graceful-fs');
const path = require('path');
const http = require('http');
const https = require('https');
const temp = require('temp');
const ignores = require('./ignoreFiles');

const tmp = require('tmp');
tmp.setGracefulCleanup();

const availableCpus = Math.max(require('os').cpus().length - 1, 1);
const CHUNK_SIZE = 50;

Expand Down Expand Up @@ -160,7 +162,6 @@ function getAllFiles(paths, filter) {
}

function run(transformFile, paths, options) {
let usedRemoteScript = false;
const cpus = options.cpus ? Math.min(availableCpus, options.cpus) : availableCpus;
const extensions =
options.extensions && options.extensions.split(',').map(ext => '.' + ext);
Expand All @@ -179,7 +180,6 @@ function run(transformFile, paths, options) {
}

if (/^http/.test(transformFile)) {
usedRemoteScript = true;
return new Promise((resolve, reject) => {
// call the correct `http` or `https` implementation
(transformFile.indexOf('https') !== 0 ? http : https).get(transformFile, (res) => {
Expand All @@ -190,13 +190,13 @@ function run(transformFile, paths, options) {
})
.on('end', () => {
const ext = path.extname(transformFile);
temp.open({ prefix: 'jscodeshift', suffix: ext }, (err, info) => {
tmp.file({ prefix: 'jscodeshift', postfix: ext }, (err, path, fd) => {
if (err) return reject(err);
fs.write(info.fd, contents, function (err) {
fs.write(fd, contents, function (err) {
if (err) return reject(err);
fs.close(info.fd, function(err) {
fs.close(fd, function(err) {
if (err) return reject(err);
transform(info.path).then(resolve, reject);
transform(path).then(resolve, reject);
});
});
});
Expand Down Expand Up @@ -310,9 +310,6 @@ function run(transformFile, paths, options) {
process.exit(1);
}
}
if (usedRemoteScript) {
temp.cleanupSync();
}
return Object.assign({
stats: statsCounter,
timeElapsed: timeElapsed
Expand Down
6 changes: 3 additions & 3 deletions utils/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

const fs = require('fs');
const path = require('path');
const temp = require('temp');
const tmp = require('tmp');

function renameFileTo(oldPath, newFilename, extension = '') {
const projectPath = path.dirname(oldPath);
Expand All @@ -21,8 +21,8 @@ function renameFileTo(oldPath, newFilename, extension = '') {
}

function createTempFileWith(content, filename, extension) {
const info = temp.openSync({ suffix: extension });
let filePath = info.path;
const info = tmp.fileSync({ postfix: extension });
let filePath = info.name;
fs.writeSync(info.fd, content);
fs.closeSync(info.fd);
if (filename) {
Expand Down
32 changes: 5 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2429,18 +2429,6 @@ minimatch@^3.0.4, minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2:
dependencies:
brace-expansion "^1.1.7"

minimist@^1.2.6:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

mkdirp@^0.5.1:
version "0.5.6"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.6.tgz#7def03d2432dcae4ba1d611445c48396062255f6"
integrity sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==
dependencies:
minimist "^1.2.6"

mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
Expand Down Expand Up @@ -2738,13 +2726,6 @@ rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

rimraf@~2.6.2:
version "2.6.3"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==
dependencies:
glob "^7.1.3"

run-parallel@^1.1.9:
version "1.2.0"
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.2.0.tgz#66d1368da7bdf921eb9d95bd1a9229e7f21a43ee"
Expand Down Expand Up @@ -2904,14 +2885,6 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

temp@^0.9.4:
version "0.9.4"
resolved "https://registry.yarnpkg.com/temp/-/temp-0.9.4.tgz#cd20a8580cb63635d0e4e9d4bd989d44286e7620"
integrity sha512-yYrrsWnrXMcdsnu/7YMYAofM1ktpL5By7vZhf15CrXijWWrEYZks5AXBudalfSWJLlnen/QUJUB5aoB0kqZUGA==
dependencies:
mkdirp "^0.5.1"
rimraf "~2.6.2"

test-exclude@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e"
Expand All @@ -2931,6 +2904,11 @@ tiny-invariant@^1.3.3:
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.3.3.tgz#46680b7a873a0d5d10005995eb90a70d74d60127"
integrity sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==

tmp@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.3.tgz#eb783cc22bc1e8bebd0671476d46ea4eb32a79ae"
integrity sha512-nZD7m9iCPC5g0pYmcaxogYKggSfLsdxl8of3Q/oIbqCqLLIO9IAF0GWjX1z9NZRHPiXv8Wex4yDCaZsgEw0Y8w==

[email protected]:
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
Expand Down

0 comments on commit f25d7b7

Please sign in to comment.