- 
                Notifications
    You must be signed in to change notification settings 
- Fork 16
feat(utils): implement custom logger with groups and tasks #1129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
      
        
          +1,843
        
        
          −149
        
        
          
        
      
    
  
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            14 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      7b29052
              
                feat(utils): add utility for rounding to max decimals, use in formatD…
              
              
                matejchalk 4c40923
              
                feat(utils): add settlePromise helper for convenient rejected promise…
              
              
                matejchalk 1368c77
              
                feat(utils): add unix timestamp conversion
              
              
                matejchalk 0568a94
              
                feat(utils): implement custom logger with groups and tasks
              
              
                matejchalk 034319c
              
                test(utils): test log levels and log groups
              
              
                matejchalk 8c4ecfb
              
                test(utils): test logger's spinners
              
              
                matejchalk a7d7975
              
                fix(utils): ensure multiline logs during spinner are indented properly
              
              
                matejchalk 46cf102
              
                test(utils): test spinners combined with groups
              
              
                matejchalk ef748e5
              
                feat(utils): throw if invalid group/spinner combinations used in logger
              
              
                matejchalk 5815f82
              
                feat(utils): export shared logger instance
              
              
                matejchalk df56f5b
              
                chore(utils): add logger demo script
              
              
                matejchalk e1db0f2
              
                fix(utils): avoid formatting milliseconds with decimals
              
              
                matejchalk cb83b9e
              
                docs(utils): add jsdocs descriptions to public logger methods
              
              
                matejchalk d3ff1a9
              
                test(utils): make spinner symbols os-indepedent in tests
              
              
                matejchalk File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| import ansis from 'ansis'; | ||
| import { logger } from '../src/index.js'; | ||
|  | ||
| async function sleep(delay: number) { | ||
| return new Promise(resolve => { | ||
| setTimeout(resolve, delay); | ||
| }); | ||
| } | ||
|  | ||
| logger.setVerbose(process.argv.includes('--verbose')); | ||
|  | ||
| const errorStage = process.argv | ||
| .find(arg => arg.startsWith('--error=')) | ||
| ?.split('=')[1]; | ||
|  | ||
| try { | ||
| logger.info(ansis.bold.blue('Code PushUp CLI v0.80.1')); | ||
| logger.newline(); | ||
|  | ||
| await logger.task('Importing code-pushup.config.ts', async () => { | ||
| await sleep(500); | ||
|  | ||
| return 'Loaded configuration from code-pushup.config.ts'; | ||
| }); | ||
| logger.debug('2 plugins:'); | ||
| logger.debug('• ESLint'); | ||
| logger.debug('• Lighthouse'); | ||
|  | ||
| await logger.group( | ||
| `Running plugin "ESLint" ${ansis.gray('[1/2]')}`, | ||
| async () => { | ||
| const bin = 'npx eslint . --format=json'; | ||
| await logger.command(bin, async () => { | ||
| await sleep(3000); | ||
| if (errorStage === 'plugin') { | ||
| logger.info('Configuration file not found.'); | ||
| throw new Error(`Command ${ansis.bold(bin)} exited with code 1`); | ||
| } | ||
| logger.debug('All files pass linting.'); | ||
| }); | ||
|  | ||
| logger.info('Found 0 lint problems'); | ||
|  | ||
| logger.warn( | ||
| 'Metadata not found for rule @angular-eslint/template/eqeqeq', | ||
| ); | ||
|  | ||
| return 'Completed "ESLint" plugin execution'; | ||
| }, | ||
| ); | ||
|  | ||
| await logger.group( | ||
| `Running plugin "Lighthouse" ${ansis.gray('[2/2]')}`, | ||
| async () => { | ||
| await logger.task( | ||
| `Executing ${ansis.bold('runLighthouse')} function`, | ||
| async () => { | ||
| await sleep(8000); | ||
| return `Executed ${ansis.bold('runLighthouse')} function`; | ||
| }, | ||
| ); | ||
|  | ||
| logger.debug('Lighthouse category scores:'); | ||
| logger.debug('• Accessibility: 100'); | ||
| logger.debug('• SEO: 84'); | ||
|  | ||
| return 'Completed "Lighthouse" plugin execution'; | ||
| }, | ||
| ); | ||
|  | ||
| logger.info(ansis.bold('Collected report')); | ||
| logger.newline(); | ||
|  | ||
| await logger.task(ansis.bold('Uploading report to portal'), async () => { | ||
| logger.debug( | ||
| 'Sent GraphQL mutation to https://api.code-pushup.example.com/graphql (organization: "example", project: "website")', | ||
| ); | ||
| await sleep(2000); | ||
| if (errorStage === 'core') { | ||
| throw new Error('GraphQL error'); | ||
| } | ||
| return ansis.bold('Uploaded report to portal'); | ||
| }); | ||
| } catch (error) { | ||
| logger.newline(); | ||
| console.error(error); | ||
| logger.newline(); | ||
| logger.error(ansis.bold(`Code PushUp CLI failed (see error above)`)); | ||
| // eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit | ||
| process.exit(1); | ||
| } | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| export function dateToUnixTimestamp(date: Date): number { | ||
| return Math.round(date.getTime() / 1000); | ||
| } | 
  
    
      This file contains hidden or 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,9 @@ | ||
| import { dateToUnixTimestamp } from './dates.js'; | ||
|  | ||
| describe('dateToUnixTimestamp', () => { | ||
| it('should convert date to number of seconds since epoch', () => { | ||
| expect(dateToUnixTimestamp(new Date('1970-01-01T01:00:04.567Z'))).toBe( | ||
| 3605, // 1 hour is 3600 seconds + 4.567 seconds is rounded up to 5 seconds | ||
| ); | ||
| }); | ||
| }); | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.