Skip to content

Commit

Permalink
Force requireWithInstall (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
agerard-godaddy authored Dec 15, 2022
1 parent 6c8c8a3 commit 2d72640
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/gasket-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/utils`

- force install with npm ([#496])

### 6.35.2
- add requireWithInstall & tryResolve utils ([#492])

Expand Down Expand Up @@ -70,3 +72,4 @@
[#348]: https://github.com/godaddy/gasket/pull/348
[#436]: https://github.com/godaddy/gasket/pull/436
[#492]: https://github.com/godaddy/gasket/pull/492
[#496]: https://github.com/godaddy/gasket/pull/496
16 changes: 8 additions & 8 deletions packages/gasket-utils/lib/require-with-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ async function getPkgManager(root) {
try {
await fs.readFile(path.join(root, 'yarn.lock'), 'utf8');
return {
pkgMananger: 'yarn',
pkgManager: 'yarn',
cmd: 'add',
flag: '--dev',
flags: ['--dev'],
logMsg: (pkg) =>
`requireWithInstall - installing "${pkg}" with "yarn" - saving as a devDependency`
};
} catch (err) {
return {
pkgMananger: 'npm',
pkgManager: 'npm',
cmd: 'install',
flag: '--no-save',
flags: ['--no-save', '--force'],
logMsg: (pkg) =>
`requireWithInstall - installing "${pkg}" with "npm" - save as a devDependency to avoid this`
};
Expand All @@ -45,15 +45,15 @@ async function requireWithInstall(dependency, gasket) {

if (modulePath) return require(modulePath);

const { pkgMananger, cmd, flag, logMsg } = await getPkgManager(root);
const { pkgManager, cmd, flags, logMsg } = await getPkgManager(root);
const pkg = dependency.match(rePackage)[0];

const manager = new PackageManager({ packageManager: pkgMananger, dest: root });
const manager = new PackageManager({ packageManager: pkgManager, dest: root });
logger.info(logMsg(pkg));
try {
await manager.exec(cmd, [pkg, flag]);
await manager.exec(cmd, [pkg, ...flags]);
} catch (err) {
logger.error(`requireWithInstall - Failed to install "${pkg}" using "${pkgMananger}"`);
logger.error(`requireWithInstall - Failed to install "${pkg}" using "${pkgManager}"`);
throw err;
}
return require(resolve(dependency, resolveOptions));
Expand Down
8 changes: 4 additions & 4 deletions packages/gasket-utils/test/require-with-install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe('requireWithInstall', function () {
.equals('npm');
assume(packageManagerExecStub.args[0][0])
.equals('install');
assume(packageManagerExecStub.args[0][1][0])
.equals('my-package');
assume(packageManagerExecStub.args[0][1])
.eqls(['my-package', '--no-save', '--force']);
});

it('does not install when package is present', async function () {
Expand Down Expand Up @@ -104,8 +104,8 @@ describe('requireWithInstall', function () {
.equals('yarn');
assume(packageManagerExecStub.args[0][0])
.equals('add');
assume(packageManagerExecStub.args[0][1][0])
.equals('my-package');
assume(packageManagerExecStub.args[0][1])
.eqls(['my-package', '--dev']);
});

it('does not install when package is present', async function () {
Expand Down

0 comments on commit 2d72640

Please sign in to comment.