This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { isWindows } from './system'; | ||
import log from './logger'; | ||
import { exec } from 'teen_process'; | ||
|
||
|
||
/** | ||
* Internal utility to link global package to local context | ||
* | ||
* @returns {string} - name of the package to link | ||
* @throws {Error} If the command fails | ||
*/ | ||
async function linkGlobalPackage (packageName) { | ||
try { | ||
log.debug(`Linking package '${packageName}'`); | ||
const cmd = isWindows() ? 'npm.cmd' : 'npm'; | ||
await exec(cmd, ['link', packageName], {timeout: 20000}); | ||
} catch (err) { | ||
const msg = `Unable to load package '${packageName}', linking failed: ${err.message}`; | ||
log.debug(msg); | ||
if (err.stderr) { | ||
// log the stderr if there, but do not add to thrown error as it is | ||
// _very_ verbose | ||
log.debug(err.stderr); | ||
} | ||
throw new Error(msg); | ||
} | ||
} | ||
|
||
/** | ||
* Utility function to extend node functionality, allowing us to require | ||
* modules that are installed globally. If the package cannot be required, | ||
* this will attempt to link the package and then re-require it | ||
* | ||
* @param {string} packageName - the name of the package to be required | ||
* @returns {object} - the package object | ||
* @throws {Error} If the package is not found locally or globally | ||
*/ | ||
async function requirePackage (packageName) { | ||
try { | ||
log.debug(`Loading local package '${packageName}'`); | ||
return require(packageName); | ||
} catch (err) { | ||
log.debug(`Failed to load package '${packageName}': ${err.message}`); | ||
await linkGlobalPackage(packageName); | ||
} | ||
try { | ||
log.debug(`Retrying load of local package '${packageName}'`); | ||
return require(packageName); | ||
} catch (err) { | ||
log.errorAndThrow(`Unable to load package '${packageName}': ${err.message}`); | ||
} | ||
} | ||
|
||
export { requirePackage }; |
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,5 @@ | ||
{ | ||
"rules": { | ||
"func-names": 0 | ||
} | ||
} |
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,21 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import { node } from '..'; | ||
|
||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('node utilities', function () { | ||
describe('requirePackage', function () { | ||
it('should be able to require a local package', async function () { | ||
await node.requirePackage('chai').should.not.be.rejected; | ||
}); | ||
it('should be able to require a global package', async function () { | ||
await node.requirePackage('npm').should.not.be.rejected; | ||
}); | ||
it('should fail to find uninstalled package', async function () { | ||
await node.requirePackage('appium-foo-driver').should.eventually.be.rejectedWith(/Unable to load package/); | ||
}); | ||
}); | ||
}); |