-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automagically install missing dependancies (#306)
* run prettier * initial try * minor bugfixes * fix secu vuln and add yarn support * minor bugfix * cleanup output * test command instead of lockfile * pivot back to finding the package/yarn file * tiny cleanup
- Loading branch information
1 parent
56ef993
commit 6f493f0
Showing
11 changed files
with
110 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const {spawn} = require('child_process'); | ||
const config = require('./config'); | ||
const path = require('path'); | ||
|
||
async function getLocation(dir) { | ||
try { | ||
return await config.resolve(dir, ['yarn.lock']); | ||
} catch (e) { | ||
try { | ||
return await config.resolve(dir, ['package.json']); | ||
} catch (e) { | ||
// TODO: log a warning | ||
return dir; | ||
} | ||
} | ||
} | ||
|
||
module.exports = async function(dir, name) { | ||
let location = await getLocation(dir); | ||
|
||
return new Promise((resolve, reject) => { | ||
let install; | ||
let options = {}; | ||
|
||
if (location.indexOf('yarn.lock') > -1) { | ||
options.cwd = path.dirname(location); | ||
install = spawn('yarn', ['add', name, '--dev'], options); | ||
} else { | ||
options.cwd = path.dirname(location); | ||
install = spawn('npm', ['install', name, '--save-dev'], options); | ||
} | ||
|
||
install.stdout.on('data', data => { | ||
// TODO: Log this using logger | ||
data | ||
.toString() | ||
.split('\n') | ||
.forEach(message => { | ||
if (message !== '') { | ||
console.log(message); | ||
} | ||
}); | ||
}); | ||
|
||
install.stderr.on('data', data => { | ||
// TODO: Log this using logger | ||
data | ||
.toString() | ||
.split('\n') | ||
.forEach(message => { | ||
if (message !== '') { | ||
console.log(message); | ||
} | ||
}); | ||
}); | ||
|
||
install.on('close', code => { | ||
if (code !== 0) { | ||
return reject(new Error(`Failed to install ${name}.`)); | ||
} | ||
return resolve(); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
const {dirname} = require('path'); | ||
const resolve = require('resolve'); | ||
const install = require('./installPackage'); | ||
|
||
const cache = new Map(); | ||
|
||
module.exports = function(name, path) { | ||
async function localRequire(name, path, triedInstall = false) { | ||
let basedir = dirname(path); | ||
let key = basedir + ':' + name; | ||
let resolved = cache.get(key); | ||
if (!resolved) { | ||
resolved = resolve.sync(name, {basedir}); | ||
try { | ||
resolved = resolve.sync(name, {basedir}); | ||
} catch (e) { | ||
if (e.code === 'MODULE_NOT_FOUND' && triedInstall === false) { | ||
// TODO: Make this use logger | ||
console.log(`INSTALLING ${name}...\n`); | ||
await install(process.cwd(), name); | ||
return localRequire(name, path, true); | ||
} | ||
throw e; | ||
} | ||
cache.set(key, resolved); | ||
} | ||
|
||
return require(resolved); | ||
}; | ||
} | ||
|
||
module.exports = localRequire; |