Skip to content

Commit

Permalink
fix(Standalone): Fix internal npm installation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Sep 28, 2021
1 parent cd9b823 commit 5a583a9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions commands/plugin-install.js
Expand Up @@ -110,9 +110,9 @@ const addPluginToServerlessFile = async ({ configurationFilePath, pluginName })
};

const npmInstall = async (name, { serviceDir }) => {
const npmCommand = await npmCommandDeferred;
const { command, args } = await npmCommandDeferred;
try {
await spawn(npmCommand, ['install', '--save-dev', name], {
await spawn(command, [...args, 'install', '--save-dev', name], {
cwd: serviceDir,
stdio: 'pipe',
// To parse quotes used in module versions. E.g. 'serverless@"^1.60.0 || 2"'
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/interactive-setup/service.js
Expand Up @@ -222,9 +222,9 @@ module.exports = {

if (hasPackageJson) {
process.stdout.write(`\nInstalling dependencies with "npm" in "${projectName}" folder\n`);
const npmCommand = await npmCommandDeferred;
const { command, args } = await npmCommandDeferred;
try {
await spawn(npmCommand, ['install'], { cwd: projectDir });
await spawn(command, [...args, 'install'], { cwd: projectDir });
} catch (err) {
if (err.code === 'ENOENT') {
process.stdout.write(
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/plugin/install.js
Expand Up @@ -192,8 +192,8 @@ class PluginInstall {
}

async npmInstall(name) {
return npmCommandDeferred.then((npmCommand) =>
childProcess.execAsync(`${npmCommand} install --save-dev ${name}`, {
return npmCommandDeferred.then(({ command, args }) =>
childProcess.execAsync(`${command} ${args.join(' ')} install --save-dev ${name}`, {
stdio: 'ignore',
})
);
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/npm-command-deferred.js
Expand Up @@ -15,5 +15,7 @@ module.exports = fsp
}
)
.then((isNpmInstalledLocaly) => {
return isNpmInstalledLocaly ? `node ${localNpmBinPath}` : 'npm';
return isNpmInstalledLocaly
? { command: 'node', args: [localNpmBinPath] }
: { command: 'npm', args: [] };
});
10 changes: 5 additions & 5 deletions test/unit/lib/plugins/plugin/install.test.js
Expand Up @@ -264,7 +264,7 @@ describe('PluginInstall', () => {
process.chdir(savedCwd);
});

it('should install the plugin if it has not been installed yet', () => {
it('npmI', () => {
const packageJson = {
name: 'test-service',
description: '',
Expand All @@ -278,7 +278,7 @@ describe('PluginInstall', () => {
return expect(pluginInstall.pluginInstall()).to.be.fulfilled.then(() =>
Promise.all([
expect(
npmInstallStub.calledWithExactly('npm install --save-dev serverless-plugin-1@latest', {
npmInstallStub.calledWithExactly('npm install --save-dev serverless-plugin-1@latest', {
stdio: 'ignore',
})
).to.equal(true),
Expand All @@ -290,7 +290,7 @@ describe('PluginInstall', () => {
it('should generate a package.json file in the service directory if not present', () =>
expect(pluginInstall.pluginInstall()).to.be.fulfilled.then(() => {
expect(
npmInstallStub.calledWithExactly('npm install --save-dev serverless-plugin-1@latest', {
npmInstallStub.calledWithExactly('npm install --save-dev serverless-plugin-1@latest', {
stdio: 'ignore',
})
).to.equal(true);
Expand Down Expand Up @@ -539,7 +539,7 @@ describe('PluginInstall', () => {
});
return expect(pluginInstall.installPeerDependencies()).to.be.fulfilled.then(() => {
expect(
npmInstallStub.calledWithExactly('npm install --save-dev some-package@"*"', {
npmInstallStub.calledWithExactly('npm install --save-dev some-package@"*"', {
stdio: 'ignore',
})
).to.equal(true);
Expand All @@ -552,7 +552,7 @@ describe('PluginInstall', () => {
expect(fse.readJsonSync(servicePackageJsonFilePath)).to.be.deep.equal({
devDependencies: {},
});
expect(npmInstallStub.calledWithExactly('npm install', { stdio: 'ignore' })).to.equal(
expect(npmInstallStub.calledWithExactly('npm install', { stdio: 'ignore' })).to.equal(
false
);
});
Expand Down

0 comments on commit 5a583a9

Please sign in to comment.