-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CLI): Deprecations logger (#7741)
Co-authored-by: Ahmed Abdelwahab <ahmed.abdelwahab@prophecylabs.com> Co-authored-by: Mariusz Nowak <medyk@medikoo.com>
- Loading branch information
1 parent
aa48f0a
commit 6f32f23
Showing
5 changed files
with
161 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- | ||
title: Serverless Framework Deprecations | ||
menuText: Deprecations | ||
layout: Doc | ||
--> | ||
|
||
# Serverless Framework Deprecations |
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,48 @@ | ||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const weakMemoizee = require('memoizee/weak'); | ||
|
||
const disabledCodesByEnv = extractCodes(process.env.SLS_DEPRECATION_DISABLE); | ||
|
||
const loggedDeprecations = new Set(); | ||
|
||
function extractCodes(codesStr) { | ||
if (!codesStr) { | ||
return new Set(); | ||
} | ||
return new Set(codesStr.split(',')); | ||
} | ||
|
||
const resolveDeprecatedByService = weakMemoizee(serviceConfig => { | ||
let disabledDeprecations = []; | ||
if (typeof serviceConfig.disabledDeprecations === 'string') { | ||
disabledDeprecations = [serviceConfig.disabledDeprecations]; | ||
} else { | ||
disabledDeprecations = Array.from(serviceConfig.disabledDeprecations || []); | ||
} | ||
return new Set(disabledDeprecations); | ||
}); | ||
|
||
function writeDeprecation(code, message) { | ||
process.stdout.write( | ||
`Serverless: ${chalk.redBright(`Deprecation Notice: ${message}`)}\n ${chalk.dim( | ||
`More Info: https://www.serverless.com/framework/docs/deprecations/#${code}` | ||
)}\n` | ||
); | ||
} | ||
|
||
module.exports = (code, message, { serviceConfig } = {}) => { | ||
if (loggedDeprecations.has(code) || disabledCodesByEnv.has(code) || disabledCodesByEnv.has('*')) { | ||
return; | ||
} | ||
if (serviceConfig) { | ||
const serviceDisabledCodes = resolveDeprecatedByService(serviceConfig); | ||
if (serviceDisabledCodes.has(code) || serviceDisabledCodes.has('*')) { | ||
return; | ||
} | ||
} | ||
|
||
loggedDeprecations.add(code); | ||
writeDeprecation(code, message); | ||
}; |
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,100 @@ | ||
'use strict'; | ||
const sandbox = require('sinon'); | ||
const expect = require('chai').expect; | ||
const mockRequire = require('mock-require'); | ||
const overrideEnv = require('process-utils/override-env'); | ||
const runServerless = require('../../tests/utils/run-serverless'); | ||
const fixtures = require('../../tests/fixtures'); | ||
|
||
describe('#logDeprecation()', () => { | ||
let stdoutWriteSpy; | ||
let restoreEnv; | ||
let originalEnv; | ||
let logDeprecation; | ||
|
||
beforeEach(() => { | ||
logDeprecation = mockRequire.reRequire('./logDeprecation'); | ||
stdoutWriteSpy = sandbox.spy(process.stdout, 'write'); | ||
({ originalEnv, restoreEnv } = overrideEnv()); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
restoreEnv(); | ||
}); | ||
|
||
it('Should log deprecation message if not disabled and first time', () => { | ||
logDeprecation('code1', 'Start using depreication log'); | ||
const message = stdoutWriteSpy.args.join('\n'); | ||
expect(stdoutWriteSpy.called).to.equal(true); | ||
expect(message).to.have.string('Deprecation Notice'); | ||
expect(message).to.have.string('https://www.serverless.com/framework/docs/deprecations/#code1'); | ||
}); | ||
|
||
it('Should not log deprecation if disabled in env.SLS_DEPRECATION_DISABLE', () => { | ||
process.env.SLS_DEPRECATION_DISABLE = 'code1'; | ||
logDeprecation = mockRequire.reRequire('./logDeprecation'); | ||
logDeprecation('code1', 'Start using depreication log'); | ||
expect(stdoutWriteSpy.called).to.equal(false); | ||
}); | ||
|
||
it('Should not log deprecation if disabled in serviceConfig', () => { | ||
// We need original process env variables for npm-conf | ||
Object.assign(process.env, originalEnv); | ||
return fixtures | ||
.extend('function', { disabledDeprecations: ['code1'] }) | ||
.then(fixturePath => runServerless({ cwd: fixturePath, cliArgs: ['package'] })) | ||
.then(serverless => { | ||
stdoutWriteSpy.clear; | ||
const serviceConfig = serverless.service; | ||
stdoutWriteSpy.resetHistory(); | ||
logDeprecation('code1', 'Start using depreication log', { serviceConfig }); | ||
expect(stdoutWriteSpy.called).to.equal(false); | ||
}); | ||
}); | ||
|
||
it('Should not log deprecation if disabled by wildcard in env', () => { | ||
process.env.SLS_DEPRECATION_DISABLE = '*'; | ||
logDeprecation = mockRequire.reRequire('./logDeprecation'); | ||
logDeprecation('code1', 'Start using depreication log'); | ||
expect(stdoutWriteSpy.called).to.equal(false); | ||
}); | ||
|
||
it('Should not log deprecation if disabled by wildcard in service config', () => { | ||
Object.assign(process.env, originalEnv); | ||
return fixtures | ||
.extend('function', { disabledDeprecations: '*' }) | ||
.then(fixturePath => runServerless({ cwd: fixturePath, cliArgs: ['package'] })) | ||
.then(serverless => { | ||
stdoutWriteSpy.clear; | ||
const serviceConfig = serverless.service; | ||
stdoutWriteSpy.resetHistory(); | ||
logDeprecation('code1', 'Start using depreication log', { serviceConfig }); | ||
expect(stdoutWriteSpy.called).to.equal(false); | ||
}); | ||
}); | ||
|
||
it('Should not log deprecation twice', () => { | ||
logDeprecation('code1', 'Start using depreication log'); | ||
logDeprecation('code1', 'Start using depreication log'); | ||
expect(stdoutWriteSpy.callCount).to.equal(1); | ||
}); | ||
|
||
it('Should ignore serviceConfig.disabledDeprecations if it is an object without an error', () => { | ||
Object.assign(process.env, originalEnv); | ||
return fixtures | ||
.extend('function', { | ||
disabledDeprecations: { | ||
code1: true, | ||
}, | ||
}) | ||
.then(fixturePath => runServerless({ cwd: fixturePath, cliArgs: ['package'] })) | ||
.then(serverless => { | ||
stdoutWriteSpy.clear; | ||
const serviceConfig = serverless.service; | ||
stdoutWriteSpy.resetHistory(); | ||
logDeprecation('code1', 'Start using depreication log', { serviceConfig }); | ||
expect(stdoutWriteSpy.called).to.equal(true); | ||
}); | ||
}); | ||
}); |