-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove Logger class & refactor Printer (#903)
* chore: remove Logger class & refactor Printer * fix: update API.md * fix: update API.md * fix: update API.md * fix: make constructor private * fix: remove yargs & use process.argv * bump package-lock * fix: update Printer to be singleton instead of static class * update changeset * update API.md * remove unused imports * remove unused imports * fix * removing no-console linter rule override * update API.md * fix test * .eslintrc * update integ test * revert back to stderr * error logs should write to stderr * update API.md * remove printRecord * remove async * spread printRecords, updating to singleton
- Loading branch information
Showing
46 changed files
with
639 additions
and
614 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,8 @@ | ||
--- | ||
'create-amplify': minor | ||
'@aws-amplify/cli-core': minor | ||
'@aws-amplify/sandbox': minor | ||
'@aws-amplify/backend-cli': minor | ||
--- | ||
|
||
Refactor Printer class & deprecate Logger |
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,96 @@ | ||
import { after, before, beforeEach, describe, it, mock } from 'node:test'; | ||
import assert from 'assert'; | ||
import { LogLevel, Printer } from './printer.js'; | ||
|
||
void describe('Printer', () => { | ||
const mockedWrite = mock.method(process.stdout, 'write'); | ||
let originalWrite: typeof process.stdout.write; | ||
|
||
before(() => { | ||
originalWrite = process.stdout.write; | ||
process.stdout.write = mockedWrite; | ||
}); | ||
|
||
after(() => { | ||
// restore write function after all tests. | ||
process.stdout.write = originalWrite; | ||
}); | ||
|
||
beforeEach(() => { | ||
mockedWrite.mock.resetCalls(); | ||
}); | ||
|
||
void it('log should print message followed by new line', () => { | ||
new Printer(LogLevel.INFO).log('hello world'); | ||
assert.strictEqual(mockedWrite.mock.callCount(), 2); | ||
assert.match( | ||
mockedWrite.mock.calls[0].arguments[0].toString(), | ||
/hello world/ | ||
); | ||
assert.match(mockedWrite.mock.calls[1].arguments[0].toString(), /\n/); | ||
}); | ||
|
||
void it('log should print message without new line', () => { | ||
new Printer(LogLevel.INFO).log('hello world', LogLevel.INFO, false); | ||
assert.strictEqual(mockedWrite.mock.callCount(), 1); | ||
assert.match( | ||
mockedWrite.mock.calls[0].arguments[0].toString(), | ||
/hello world/ | ||
); | ||
}); | ||
|
||
void it('log should not print debug logs by default', () => { | ||
new Printer(LogLevel.INFO).log('hello world', LogLevel.DEBUG); | ||
assert.strictEqual(mockedWrite.mock.callCount(), 0); | ||
}); | ||
|
||
void it('log should print debug logs when printer is configured with minimum log level >= DEBUG', () => { | ||
new Printer(LogLevel.DEBUG).log('hello world', LogLevel.DEBUG); | ||
assert.strictEqual(mockedWrite.mock.callCount(), 2); | ||
assert.match( | ||
mockedWrite.mock.calls[0].arguments[0].toString(), | ||
/hello world/ | ||
); | ||
assert.match(mockedWrite.mock.calls[1].arguments[0].toString(), /\n/); | ||
}); | ||
|
||
void it('log should not print debug logs by default', () => { | ||
new Printer(LogLevel.INFO).log('hello world', LogLevel.DEBUG); | ||
assert.strictEqual(mockedWrite.mock.callCount(), 0); | ||
}); | ||
|
||
void it('indicateProgress logs message & animates ellipsis if on TTY', async () => { | ||
process.stdout.isTTY = true; | ||
await new Printer(LogLevel.INFO).indicateProgress( | ||
'loading a long list', | ||
() => new Promise((resolve) => setTimeout(resolve, 3000)) | ||
); | ||
// filter out the escape characters. | ||
const logMessages = mockedWrite.mock.calls | ||
.filter((message) => | ||
message.arguments.toString().match(/loading a long list/) | ||
) | ||
.map((call) => call.arguments.toString()); | ||
|
||
logMessages.forEach((message) => { | ||
assert.match(message, /loading a long list(.*)/); | ||
}); | ||
}); | ||
|
||
void it('indicateProgress does not animates ellipsis if not TTY & prints log message once', async () => { | ||
process.stdout.isTTY = false; | ||
await new Printer(LogLevel.INFO).indicateProgress( | ||
'loading a long list', | ||
() => new Promise((resolve) => setTimeout(resolve, 1500)) | ||
); | ||
// filter out the escape characters. | ||
const logMessages = mockedWrite.mock.calls | ||
.filter((message) => | ||
message.arguments.toString().match(/loading a long list/) | ||
) | ||
.map((call) => call.arguments.toString()); | ||
|
||
assert.strictEqual(logMessages.length, 1); | ||
assert.match(logMessages[0], /loading a long list/); | ||
}); | ||
}); |
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
Oops, something went wrong.