Skip to content

Commit dafe1e4

Browse files
authored
feat(zenith): add log level, add execution time to projects with missed cache (#4)
* feat: add logger and time info * chore: update package version
1 parent 53dd7d1 commit dafe1e4

5 files changed

Lines changed: 53 additions & 23 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@jotforminc/zenith",
33
"packageManager": "pnpm@7.18.0",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"description": "",
66
"main": "./build/index.js",
77
"files": [

src/classes/BuildHelper.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Cacher from './Cacher';
55
import Hasher from './Hasher';
66
import WorkerHelper from './WorkerHelper';
77
import ConfigHelper from './ConfigHelper';
8+
import { formatMissingProjects, formatTimeDiff } from '../utils/functions';
89

910
export default class BuildHelper extends WorkerHelper {
1011
projects = new Map();
@@ -17,8 +18,11 @@ export default class BuildHelper extends WorkerHelper {
1718
super(command);
1819
}
1920

20-
async init(debug, compareWith) {
21+
async init(debug, compareWith, logLevel, logFunction) {
2122
this.cacher = new Cacher().cacher;
23+
this.logLevel = logLevel;
24+
this.startTime = process.hrtime();
25+
this.log = logFunction(this.logLevel)
2226
if (debug) {
2327
this.debug = debug;
2428
this.compareWith = compareWith;
@@ -39,7 +43,7 @@ export default class BuildHelper extends WorkerHelper {
3943
});
4044
} catch (error) {
4145
if (error.code === 'ENOENT') {
42-
console.log('Package.json file not found in the project!');
46+
this.log(2, 'Package.json file not found in the project!');
4347
throw error;
4448
} else {
4549
throw error;
@@ -90,14 +94,16 @@ export default class BuildHelper extends WorkerHelper {
9094
if (this.compareWith) {
9195
const [changedFiles, newFiles] = Hasher.getUpdatedHashes();
9296
if (changedFiles.length || newFiles.length) {
93-
console.log(`Hash mismatched: \n Changed files => \n - ${changedFiles.join('\n')} \n New files => \n - ${newFiles.join('\n')}`);
97+
this.log(3, `Hash mismatched: \n Changed files => \n - ${changedFiles.join('\n')} \n New files => \n - ${newFiles.join('\n')}`);
9498
Hasher.emptyUpdatedHashes();
9599
}
96100
}
97101
if (!isCached) {
98-
console.log('Cache does not exist for => ', buildProject, hash);
99-
this.missingProjects.push(buildProject);
102+
this.log(3, 'Cache does not exist for => ', buildProject, hash);
103+
104+
const startTime = process.hrtime();
100105
const output = await this.execute(buildPath, script, hash, root, outputs);
106+
this.missingProjects.push({ buildProject, time: process.hrtime(startTime)});
101107
if (output instanceof Error) {
102108
// process.exit(0);
103109
throw new Error(output);
@@ -107,7 +113,7 @@ export default class BuildHelper extends WorkerHelper {
107113
if (outputs.length) {
108114
for (const output of outputs) {
109115
// const outputPath = path.join(ROOT_PATH, root, output);
110-
console.log('Recovering from cache', buildProject, 'with hash => ', hash);
116+
this.log(3, 'Recovering from cache', buildProject, 'with hash => ', hash);
111117
const recoverResponse = await this.anotherJob(hash, root, output);
112118
if (recoverResponse instanceof Error) {
113119
throw new Error(recoverResponse);
@@ -132,7 +138,7 @@ export default class BuildHelper extends WorkerHelper {
132138
if (!projects.length) {
133139
if (!stats.pendingTasks && !stats.activeTasks) {
134140
this.pool.terminate();
135-
console.log(`
141+
this.log(2, `
136142
▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄ ▄
137143
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌ ▐░░▌▐░░░░░░░░░░░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌
138144
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌ ▐░▐░▌▐░█▀▀▀▀▀▀▀█░▌▐░▌ ▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌
@@ -143,15 +149,16 @@ export default class BuildHelper extends WorkerHelper {
143149
▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▀
144150
▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌ ▄
145151
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌
146-
▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀
152+
▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀
147153
`);
148-
console.log(`Total of ${this.totalCount} project has built.`);
149-
console.log(`${this.fromCache} project built from cache,`)
150-
console.log(`${this.built} project built without cache.`);
151-
console.log(`Cache is missing for following projects => \n${this.missingProjects.join('\n')}`);
154+
this.log(2, `Total of ${this.totalCount} project${this.totalCount === 1 ? '' : 's'} has built.`);
155+
this.log(2, `${this.fromCache} project built from cache,`);
156+
this.log(2, `${this.built} project built without cache.`);
157+
this.log(2, `Cache is missing for following projects => ${formatMissingProjects(this.missingProjects)}`);
158+
this.log(2, `Total build took ${formatTimeDiff(process.hrtime(this.startTime))}.`);
152159
if (this.debug && process.env.ZENITH_DEBUG_ID) {
153160
this.cacher.updateDebugFile(Hasher.getDebugJSON());
154-
console.log('DEBUG JSON UPDATED');
161+
this.log(2, 'DEBUG JSON UPDATED');
155162
}
156163
}
157164
return;

src/classes/Runner.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export default class Runner {
88
.option('-p, --project <project>', 'Project name')
99
.option('-t, --target <target>', 'Target name')
1010
.option('-d, --debug', 'Debug mode')
11-
.option('-c, --compareWith <compareWith>', 'Compare with');
11+
.option('-c, --compareWith <compareWith>', 'Compare with')
12+
.option('-l, --logLevel <logLevel>', 'Log Level [1-2-3]: 1=silent, 2=default (log info after completion and errors), 3=verbose (missing/hit cache info', 2);
1213
program.parse(args);
1314
const options = program.opts();
1415
this.command = args.slice(-1);
@@ -29,14 +30,23 @@ export default class Runner {
2930
if (flags['--compareWith']) {
3031
this.compareWith = flags['--compareWith'];
3132
}
33+
if (options.logLevel) {
34+
this.logLevel = Number(options.logLevel);
35+
}
36+
37+
this.logFunction = logLevel => (level, ...args) => {
38+
if (logLevel >= level) console.log(...args);
39+
}
40+
this.log = this.logFunction(this.logLevel)
3241
}
3342

3443
async run() {
3544
if (this.command !== 'build') {
3645
return;
3746
}
3847
const Builder = new BuildHelper('build');
39-
await Builder.init(this.debug, this.compareWith);
48+
await Builder.init(this.debug, this.compareWith, this.logLevel, this.logFunction);
49+
this.log(2, 'Zenith started. Building...')
4050
if (this.project === 'all') {
4151
Builder.buildAll();
4252
} else {

src/classes/WorkerHelper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class WorkerHelper {
1212
async execute(buildPath, command, hash, root, outputs) {
1313
try {
1414
return await this.pool.exec('execute', [buildPath, command, hash, root, outputs], {
15-
on: message => console.log(message)
15+
on: message => this.log(3, message)
1616
})
1717
} catch (error) {
1818
console.log(error);
@@ -23,7 +23,7 @@ export default class WorkerHelper {
2323
async anotherJob(hash, root, output) {
2424
try {
2525
return await this.pool.exec('anotherJob', [hash, root, output], {
26-
on: message => console.log(message)
26+
on: message => this.log(3, message)
2727
});
2828
} catch (error) {
2929
console.log(error);

src/utils/functions.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
import { readFileSync, existsSync } from 'fs';
1+
import { readFileSync, existsSync } from "fs";
22

3-
const readFileAsJSON = path => {
3+
export const readFileAsJSON = (path) => {
44
if (existsSync(path)) {
5-
return JSON.parse(readFileSync(path, { encoding: 'utf-8' }));
5+
return JSON.parse(readFileSync(path, { encoding: "utf-8" }));
66
}
77
return {};
8-
}
9-
export default readFileAsJSON;
8+
};
9+
10+
export const formatMissingProjects = (missingProjects) =>
11+
missingProjects.reduce((acc, curr) => {
12+
return acc + `\n${curr.buildProject} ${formatTimeDiff(curr.time)}`;
13+
}, "");
14+
15+
export const formatTimeDiff = (time) => {
16+
const seconds = (time[0] + time[1] / 1e9).toFixed(3);
17+
if (seconds > 60) {
18+
const minutes = Math.floor(seconds / 60);
19+
return `(${minutes}m ${(seconds % 60).toFixed(3)}s)`;
20+
}
21+
return `(${seconds}s)`;
22+
};

0 commit comments

Comments
 (0)