Skip to content

Commit 67de0c4

Browse files
authored
feat: test caching (#11)
* feat :: test caching * style: improve code quality * chore: version up * fix: remove endpoint * feat: command caching * fix: endpoint is not used in prod
1 parent fba3deb commit 67de0c4

8 files changed

Lines changed: 221 additions & 107 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.4.1",
4+
"version": "0.5.0",
55
"description": "",
66
"main": "./build/index.js",
77
"files": [

src/classes/BuildHelper.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +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';
8+
import { formatMissingProjects, formatTimeDiff, isOutputTxt } from '../utils/functions';
99
import Logger from '../utils/logger'
1010

1111
export default class BuildHelper extends WorkerHelper {
@@ -17,15 +17,17 @@ export default class BuildHelper extends WorkerHelper {
1717

1818
constructor(command) {
1919
super(command);
20+
this.command = command;
2021
}
2122

22-
async init(debug, compareWith) {
23+
async init(debug, compareWith, compareHash) {
2324
this.cacher = new Cacher().cacher;
25+
this.compareHash = compareHash;
2426
this.startTime = process.hrtime();
2527
if (debug) {
2628
this.debug = debug;
2729
this.compareWith = compareWith;
28-
const debugJSON = await this.cacher.getDebugFile(compareWith) || {};
30+
const debugJSON = await this.cacher.getDebugFile(compareWith, this.command) || {};
2931
Hasher.updateDebugJSON(debugJSON);
3032
}
3133
}
@@ -85,11 +87,13 @@ export default class BuildHelper extends WorkerHelper {
8587
async builder(buildProject) {
8688
this.totalCount++
8789
const root = ConfigHelper.projects[buildProject];
90+
const projectName = root.substr(root.lastIndexOf('/') + 1)
8891
// TODO: Non cacheable projects control
89-
const { build: { outputs, script } } = ConfigHelper.getConfig(buildProject, root);
92+
const config = ConfigHelper.getConfig(buildProject, root);
93+
const {outputs, script} = config[this.command];
9094
const buildPath = path.join(ROOT_PATH, root);
9195
const hash = Hasher.getHash(buildPath, script, this.debug, this.compareWith);
92-
const isCached = await this.cacher.isCached(hash, root, outputs);
96+
const isCached = await this.cacher.isCached(hash, root, outputs, script);
9397
if (this.compareWith) {
9498
const [changedFiles, newFiles] = Hasher.getUpdatedHashes();
9599
if (changedFiles.length || newFiles.length) {
@@ -101,25 +105,29 @@ export default class BuildHelper extends WorkerHelper {
101105
Logger.log(3, 'Cache does not exist for => ', buildProject, hash);
102106

103107
const startTime = process.hrtime();
104-
const output = await this.execute(buildPath, script, hash, root, outputs);
108+
const output = await this.execute(buildPath, script, hash, root, outputs, projectName);
105109
this.missingProjects.push({ buildProject, time: process.hrtime(startTime)});
106110
if (output instanceof Error) {
107111
// process.exit(0);
112+
Logger.log(2, 'Error in path ::', buildPath)
108113
throw new Error(output);
109114
}
115+
if (isOutputTxt(outputs)) {
116+
Logger.log(2, output.output)
117+
}
110118
this.built++
111119
} else {
112120
if (outputs.length) {
113121
for (const output of outputs) {
114122
// const outputPath = path.join(ROOT_PATH, root, output);
115123
Logger.log(3, 'Recovering from cache', buildProject, 'with hash => ', hash);
116-
const recoverResponse = await this.anotherJob(hash, root, output);
124+
const recoverResponse = await this.anotherJob(hash, root, output, script, this.compareHash);
117125
if (recoverResponse instanceof Error) {
118126
throw new Error(recoverResponse);
119127
}
120128
if (!recoverResponse) {
121129
// TODO: will remove in for loop sorry for shitty code anyone who sees it :((
122-
await this.execute(buildPath, script, hash, root, outputs);
130+
await this.execute(buildPath, script, hash, root, outputs, projectName);
123131
this.built++
124132
} else {
125133
this.fromCache++
@@ -150,13 +158,13 @@ export default class BuildHelper extends WorkerHelper {
150158
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌
151159
▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀
152160
`);
153-
Logger.log(2, `Total of ${this.totalCount} project${this.totalCount === 1 ? '' : 's'} has built.`);
154-
Logger.log(2, `${this.fromCache} project built from cache,`);
155-
Logger.log(2, `${this.built} project built without cache.`);
161+
Logger.log(2, `Total of ${this.totalCount} project${this.totalCount === 1 ? ' is' : 's are'} finished.`);
162+
Logger.log(2, `${this.fromCache} projects used from cache,`);
163+
Logger.log(2, `${this.built} projects used without cache.`);
156164
Logger.log(2, `Cache is missing for following projects => ${formatMissingProjects(this.missingProjects)}`);
157-
Logger.log(2, `Total build took ${formatTimeDiff(process.hrtime(this.startTime))}.`);
165+
Logger.log(2, `Total process took ${formatTimeDiff(process.hrtime(this.startTime))}.`);
158166
if (this.debug && process.env.ZENITH_DEBUG_ID) {
159-
this.cacher.updateDebugFile(Hasher.getDebugJSON());
167+
this.cacher.updateDebugFile(Hasher.getDebugJSON(), this.command);
160168
Logger.log(2, 'DEBUG JSON UPDATED');
161169
}
162170
}

0 commit comments

Comments
 (0)