Skip to content

Commit

Permalink
[breaking] getLockfile/getProjectTempDir: take logger and `npmN…
Browse files Browse the repository at this point in the history
…eeded` as object options
  • Loading branch information
ljharb committed Feb 22, 2019
1 parent 8b60b22 commit a070471
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const {
demandOption: true
});

getLockfile(pkg, date === 'now' ? undefined : date, console.log.bind(console))
getLockfile(pkg, date === 'now' ? undefined : date, { logger: console.log.bind(console), npmNeeded: '^6.9.0-0' })
.then(lockfile => writeFile(output, lockfile))
.then(() => { console.log(chalk.green('Lockfile contents written!')); })
.catch(err => console.error(err));
4 changes: 2 additions & 2 deletions getLockfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const readFile = promisify(require('fs').readFile);

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

module.exports = function getLockfile(packageFile, date, logger = () => {}) {
module.exports = function getLockfile(packageFile, date, { npmNeeded, logger = () => {} } = {}) {
if (typeof packageFile !== 'string' || packageFile.length === 0) {
return Promise.reject(chalk.red(`\`packageFile\` must be a non-empty string; got ${inspect(packageFile)}`));
}
if (typeof date !== 'undefined' && !new Date(date).getTime()) {
return Promise.reject(chalk.red(`\`date\` must be a valid Date format if provided; got ${inspect(date)}`));
}
const tmpDirP = getProjectTempDir();
const tmpDirP = getProjectTempDir({ npmNeeded, logger });
const npmRC = path.join(path.dirname(packageFile), '.npmrc');
const copyPkg = tmpDirP.then(tmpDir => {
logger(chalk.blue(`Creating \`package.json\` in temp dir for ${date || '“now”'} lockfile`));
Expand Down
22 changes: 10 additions & 12 deletions getProjectTempDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ const path = require('path');
const { exec, execSync } = require('child_process');
const writeFile = promisify(require('fs').writeFile);

const npmNeeded = '^6.9.0-0';
const pkgContents = {
'private': true,
name: 'npm-jail',
dependencies: {
npm: npmNeeded
}
};

const cleanupHandlers = [];
const finalCleanup = function finalCleanup() {
for (let i = 0; i < cleanupHandlers.length; ++i) {
Expand All @@ -28,7 +19,7 @@ const finalCleanup = function finalCleanup() {
};

let rootTempDir;
const getRootTempDir = function getRootTempDir(logger = () => {}) {
const getRootTempDir = function getRootTempDir(npmNeeded, logger = () => {}) {
if (!rootTempDir) {
logger(chalk.blue('Creating root temp directory, to hold temporary lockfiles...'));
rootTempDir = new Promise((resolve, reject) => tmp.dir((err, tmpDir, cleanup) => {
Expand All @@ -42,6 +33,13 @@ const getRootTempDir = function getRootTempDir(logger = () => {}) {
var npmV = execSync('npm --version', { encoding: 'utf-8', cwd: tmpDir });
logger(`${chalk.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)
Expand All @@ -63,8 +61,8 @@ const getRootTempDir = function getRootTempDir(logger = () => {}) {
return rootTempDir;
};

module.exports = function getProjectTempDir(logger = undefined) {
return getRootTempDir(logger).then(rootDir => {
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) {
Expand Down

0 comments on commit a070471

Please sign in to comment.