Skip to content

Commit

Permalink
[Refactor] copy getProjectTempDir from npm-lockfile v2, since v3 …
Browse files Browse the repository at this point in the history
…removes it
  • Loading branch information
ljharb committed Jan 7, 2022
1 parent 324a287 commit 60e4f8b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .eslintrc
Expand Up @@ -5,6 +5,17 @@

"rules": {
"array-bracket-newline": 0,
"func-name-matching": 0,
"func-style": 0,
},

"overrides": [
{
"files": "./getProjectTempDir.js",
"rules": {
"max-nested-callbacks": 0,
"sort-keys": 0,
},
},
],
}
3 changes: 2 additions & 1 deletion bin.js
Expand Up @@ -4,7 +4,6 @@

const npx = require('libnpx');
const getLockfile = require('npm-lockfile/getLockfile');
const getProjectTempDir = require('npm-lockfile/getProjectTempDir');
const finder = require('find-package-json');
const promisify = require('util.promisify');
const semver = require('semver');
Expand All @@ -16,6 +15,8 @@ const writeFile = promisify(require('fs').writeFile);
const copyFile = promisify(require('fs-copy-file'));
const { execSync } = require('child_process');

const getProjectTempDir = require('./getProjectTempDir');

const { filename: pkg } = finder(process.cwd()).next();
const pkgDir = path.dirname(pkg);

Expand Down
82 changes: 82 additions & 0 deletions getProjectTempDir.js
@@ -0,0 +1,82 @@
'use strict';

const tmp = require('tmp');
const nodeCleanup = require('node-cleanup');
const semver = require('semver');
const promisify = require('util.promisify');
const rimraf = require('rimraf');
const colors = require('colors/safe');

const path = require('path');
const { exec, execSync } = require('child_process');
const writeFile = promisify(require('fs').writeFile);

const cleanupHandlers = [];
const finalCleanup = function finalCleanup() {
for (let i = 0; i < cleanupHandlers.length; ++i) {
cleanupHandlers[i]();
}
};

let rootTempDir;
const getRootTempDir = function getRootTempDir(npmNeeded, logger = () => {}) {
if (!rootTempDir) {
logger(colors.blue('Creating root temp directory, to hold temporary lockfiles...'));
rootTempDir = new Promise((resolve, reject) => {
tmp.dir((err, tmpDir, cleanup) => {
if (err) {
reject(err);
} else {
resolve(tmpDir);
cleanupHandlers.push(cleanup);
nodeCleanup(finalCleanup);
}
});
}).then((tmpDir) => {
const npmV = execSync('npm --version', { encoding: 'utf-8', cwd: tmpDir }).trim();
logger(`${colors.blue('Checking npm version:')} \`npm --version\` -> v${npmV}`);
if (!semver.satisfies(npmV, npmNeeded)) {
const pkgContents = {
private: true,
name: 'npm-jail',
dependencies: {
npm: npmNeeded,
},
};
return writeFile(
path.join(tmpDir, 'package.json'),
JSON.stringify(pkgContents)
).then(() => new Promise((resolve, reject) => {
cleanupHandlers.unshift(() => {
rimraf.sync(path.join(tmpDir, '*'));
});
exec('npm install --no-package-lock --silent >/dev/null', { cwd: tmpDir }, (err) => {
if (err) {
reject(err);
} else {
resolve(tmpDir);
}
});
}));
}
return tmpDir;
});
}
return rootTempDir;
};

module.exports = function getProjectTempDir({ npmNeeded = '^6.9.0-0', logger = undefined } = {}) {
return getRootTempDir(npmNeeded, logger).then((rootDir) => {
const projectDir = path.join(rootDir, 'XXXXXX');
return new Promise((resolve, reject) => {
tmp.dir({ template: projectDir }, (err, tmpDir, cleanup) => {
if (err) {
reject(err);
} else {
resolve(tmpDir);
cleanupHandlers.unshift(cleanup);
}
});
});
});
};
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -39,8 +39,10 @@
"find-package-json": "^1.2.0",
"fs-copy-file": "^2.1.2",
"libnpx": "^10.2.4",
"node-cleanup": "^2.1.2",
"npm-lockfile": "^2.0.4",
"semver": "^6.3.0",
"tmp": "^0.1.0",
"util.promisify": "^1.1.1"
},
"devDependencies": {
Expand Down

0 comments on commit 60e4f8b

Please sign in to comment.