Skip to content

Commit

Permalink
refactor: Upgrade @serverless/utils to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 27, 2022
1 parent 90f00b7 commit 74d9c70
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 99 deletions.
36 changes: 0 additions & 36 deletions lib/cli/interactive-setup/deploy-progress-plugin.js

This file was deleted.

54 changes: 5 additions & 49 deletions lib/cli/interactive-setup/deploy.js
Expand Up @@ -6,13 +6,10 @@ const { legacy, writeText, style } = require('@serverless/utils/log');
const promptWithHistory = require('@serverless/utils/inquirer/prompt-with-history');
const { doesServiceInstanceHaveLinkedProvider } = require('./utils');
const _ = require('lodash');
const overrideStdoutWrite = require('process-utils/override-stdout-write');
const { getDashboardInteractUrl } = require('@serverless/dashboard-plugin/lib/dashboard');
const AWS = require('aws-sdk');
const isAuthenticated = require('@serverless/dashboard-plugin/lib/isAuthenticated');

const isModernStyle = Number(process.env.SLS_DEV_LOG_MODE) & 1;

const printMessage = ({
serviceName,
hasBeenDeployed,
Expand Down Expand Up @@ -102,15 +99,6 @@ const printMessage = ({
);
};

const configurePlugin = (serverless, originalStdWrite) => {
serverless.pluginManager.addPlugin(require('./deploy-progress-plugin'));
const interactivePlugin = serverless.pluginManager.plugins.find(
(plugin) => plugin.constructor.name === 'InteractiveDeployProgress'
);
interactivePlugin.progress._writeOriginalStdout = (data) => originalStdWrite(data);
return interactivePlugin;
};

module.exports = {
async isApplicable(context) {
const { configuration, serviceDir, options } = context;
Expand Down Expand Up @@ -172,43 +160,11 @@ module.exports = {
options: {},
});

let interactiveOutputPlugin;

if (isModernStyle) {
// With modern logs do not hijack Framework output
//
// This is a hack to disable local fallback for interactive setup
// After https://github.com/serverless/serverless/issues/1720 is addressed
// We can change the approach to deployment in interactive setup and remove this hack
serverless.isLocallyInstalled = true;
await serverless.init();
delete serverless.isLocallyInstalled;
await serverless.run();
context.serverless = serverless;
} else {
try {
await overrideStdoutWrite(
() => {},
async (originalStdWrite) => {
// This is a hack to disable local fallback for interactive setup
// After https://github.com/serverless/serverless/issues/1720 is addressed
// We can change the approach to deployment in interactive setup and remove this hack
serverless.isLocallyInstalled = true;
await serverless.init();
interactiveOutputPlugin = configurePlugin(serverless, originalStdWrite);
// Remove previously set `isLocallyInstalled` as it was only needed to avoid local fallback in `init()`
delete serverless.isLocallyInstalled;
await serverless.run();
context.serverless = serverless;
}
);
} catch (err) {
if (interactiveOutputPlugin) {
interactiveOutputPlugin.handleError();
}
throw err;
}
}
serverless.isLocallyInstalled = true;
await serverless.init();
delete serverless.isLocallyInstalled;
await serverless.run();
context.serverless = serverless;

printMessage({
initialContext: initial,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"@serverless/components": "^3.18.2",
"@serverless/dashboard-plugin": "^5.5.4",
"@serverless/platform-client": "^4.3.0",
"@serverless/utils": "^5.20.3",
"@serverless/utils": "^6.0.0",
"ajv": "^6.12.6",
"ajv-keywords": "^3.5.2",
"archiver": "^5.3.0",
Expand Down
2 changes: 1 addition & 1 deletion test/integrationBasic.test.js
Expand Up @@ -111,7 +111,7 @@ describe('Service Lifecyle Integration Test', function () {
spawnOptions
);
const output = stripAnsi(listDeploys.toString());
const match = output.match(new RegExp('Datetime: (.+)'));
const match = output.match(new RegExp('Timestamp: (.+)'));
if (match) {
timestamp = match[1];
}
Expand Down
1 change: 0 additions & 1 deletion test/unit/commands/doctor.test.js
Expand Up @@ -12,7 +12,6 @@ const serverlessPath = path.resolve(__dirname, '../../../scripts/serverless.js')
describe('test/unit/commands/doctor.test.js', async () => {
before(() => {
process.env.SLS_DEPRECATION_NOTIFICATION_MODE = 'warn:summary';
process.env.SLS_DEV_LOG_MODE = '3';
});

it('should print health status after command which triggered deprecation', async () => {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/lib/classes/PluginManager.test.js
Expand Up @@ -120,6 +120,10 @@ describe('PluginManager', () => {
},
},
},
other: {
usage: 'Deploy to other infrastructure',
lifecycleEvents: ['resources', 'functions'],
},
},
},
};
Expand Down
22 changes: 11 additions & 11 deletions test/unit/scripts/serverless.test.js
Expand Up @@ -27,7 +27,7 @@ describe('test/unit/scripts/serverless.test.js', () => {
})
).stdoutBuffer
);
expect(output).to.include('You can run commands with');
expect(output).to.include('serverless <command> <options>');
});

it('should report with an error invalid configuration', async () => {
Expand All @@ -38,7 +38,7 @@ describe('test/unit/scripts/serverless.test.js', () => {
throw new Error('Unexpected');
} catch (error) {
expect(error.code).to.equal(1);
expect(String(error.stdoutBuffer)).to.include('Your Environment Information');
expect(String(error.stdoutBuffer)).to.include('Environment: ');
}
});

Expand All @@ -50,7 +50,7 @@ describe('test/unit/scripts/serverless.test.js', () => {
throw new Error('Unexpected');
} catch (error) {
expect(error.code).to.equal(1);
expect(String(error.stdoutBuffer)).to.include('Your Environment Information');
expect(String(error.stdoutBuffer)).to.include('Environment: ');
}
});

Expand All @@ -62,7 +62,7 @@ describe('test/unit/scripts/serverless.test.js', () => {
throw new Error('Unexpected');
} catch (error) {
expect(error.code).to.equal(1);
expect(String(error.stdoutBuffer)).to.include('Your Environment Information');
expect(String(error.stdoutBuffer)).to.include('Environment: ');
}
});

Expand All @@ -72,9 +72,9 @@ describe('test/unit/scripts/serverless.test.js', () => {
await spawn('node', [serverlessPath, '--help'], {
cwd: (await programmaticFixturesEngine.setup('locallyInstalledServerless')).servicePath,
})
).stdoutBuffer
).stderrBuffer
);
expect(output).to.include('Running "serverless" installed locally');
expect(output).to.include('Running "serverless" from node_modules');
});

it('should handle no service related commands', async () => {
Expand All @@ -85,7 +85,7 @@ describe('test/unit/scripts/serverless.test.js', () => {
})
).stdoutBuffer
);
expect(output).to.include('To install a plugin run');
expect(output).to.include('Install a plugin by running');
});

it('should resolve variables', async () => {
Expand Down Expand Up @@ -222,12 +222,12 @@ describe('test/unit/scripts/serverless.test.js', () => {
})
).stdoutBuffer
);
expect(output).to.include('Documentation: http://slss.io/docs');
expect(output).to.include('serverless <command> <options>');
});

it('should print general --help to stdout', async () => {
const output = String((await spawn('node', [serverlessPath, '--help'])).stdoutBuffer);
expect(output).to.include('Documentation: http://slss.io/docs');
expect(output).to.include('serverless <command> <options>');
});

it('should print command --help to stdout', async () => {
Expand All @@ -249,15 +249,15 @@ describe('test/unit/scripts/serverless.test.js', () => {
(await spawn('node', [serverlessPath, '--help-interactive'])).stdoutBuffer
);
expect(output).to.include('Interactive CLI');
expect(output).to.not.include('General Commands');
expect(output).to.not.include('Main commands');
});

it('should show help when running container command', async () => {
// Note: Arbitrarily picked "plugin" command for testing
const output = stripAnsi(
String((await spawn('node', [serverlessPath, 'plugin'])).stdoutBuffer)
);
expect(output).to.include('plugin install .......');
expect(output).to.include('plugin install');
});

it('should crash in required option is missing', async () => {
Expand Down

0 comments on commit 74d9c70

Please sign in to comment.