Skip to content

Commit 0ee21e3

Browse files
committed
chore(utils): add logger demo script
1 parent 009bc0e commit 0ee21e3

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import ansis from 'ansis';
2+
import { logger } from '../src/index.js';
3+
4+
async function sleep(delay: number) {
5+
return new Promise(resolve => {
6+
setTimeout(resolve, delay);
7+
});
8+
}
9+
10+
logger.setVerbose(process.argv.includes('--verbose'));
11+
12+
const errorStage = process.argv
13+
.find(arg => arg.startsWith('--error='))
14+
?.split('=')[1];
15+
16+
try {
17+
logger.info(ansis.bold.blue('Code PushUp CLI v0.80.1'));
18+
logger.newline();
19+
20+
await logger.task('Importing code-pushup.config.ts', async () => {
21+
await sleep(500);
22+
23+
return 'Loaded configuration from code-pushup.config.ts';
24+
});
25+
logger.debug('2 plugins:');
26+
logger.debug('• ESLint');
27+
logger.debug('• Lighthouse');
28+
29+
await logger.group(
30+
`Running plugin "ESLint" ${ansis.gray('[1/2]')}`,
31+
async () => {
32+
const bin = 'npx eslint . --format=json';
33+
await logger.command(bin, async () => {
34+
await sleep(3000);
35+
if (errorStage === 'plugin') {
36+
logger.info('Configuration file not found.');
37+
throw new Error(`Command ${ansis.bold(bin)} exited with code 1`);
38+
}
39+
logger.debug('All files pass linting.');
40+
});
41+
42+
logger.info('Found 0 lint problems');
43+
44+
logger.warn(
45+
'Metadata not found for rule @angular-eslint/template/eqeqeq',
46+
);
47+
48+
return 'Completed "ESLint" plugin execution';
49+
},
50+
);
51+
52+
await logger.group(
53+
`Running plugin "Lighthouse" ${ansis.gray('[2/2]')}`,
54+
async () => {
55+
await logger.task(
56+
`Executing ${ansis.bold('runLighthouse')} function`,
57+
async () => {
58+
await sleep(8000);
59+
return `Executed ${ansis.bold('runLighthouse')} function`;
60+
},
61+
);
62+
63+
logger.debug('Lighthouse category scores:');
64+
logger.debug('• Accessibility: 100');
65+
logger.debug('• SEO: 84');
66+
67+
return 'Completed "Lighthouse" plugin execution';
68+
},
69+
);
70+
71+
logger.info(ansis.bold('Collected report'));
72+
logger.newline();
73+
74+
await logger.task(ansis.bold('Uploading report to portal'), async () => {
75+
logger.debug(
76+
'Sent GraphQL mutation to https://api.code-pushup.example.com/graphql (organization: "example", project: "website")',
77+
);
78+
await sleep(2000);
79+
if (errorStage === 'core') {
80+
throw new Error('GraphQL error');
81+
}
82+
return ansis.bold('Uploaded report to portal');
83+
});
84+
} catch (error) {
85+
logger.newline();
86+
console.error(error);
87+
logger.newline();
88+
logger.error(ansis.bold(`Code PushUp CLI failed (see error above)`));
89+
// eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit
90+
process.exit(1);
91+
}

packages/utils/project.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,29 @@
2020
}
2121
},
2222
"unit-test": {},
23-
"int-test": {}
23+
"int-test": {},
24+
"demo-logger": {
25+
"executor": "nx:run-commands",
26+
"options": {
27+
"command": "npx tsx --tsconfig=tsconfig.base.json packages/utils/mocks/logger-demo.ts"
28+
},
29+
"configurations": {
30+
"ci": {
31+
"env": {
32+
"CI": "true"
33+
}
34+
},
35+
"verbose": {
36+
"args": ["--verbose"]
37+
},
38+
"error-core": {
39+
"args": ["--error=core"]
40+
},
41+
"error-plugin": {
42+
"args": ["--error=plugin"]
43+
}
44+
}
45+
}
2446
},
2547
"tags": ["scope:shared", "type:util", "publishable"]
2648
}

0 commit comments

Comments
 (0)