Skip to content

Commit 9738b5c

Browse files
authored
feat: read js config and add onFail to config (#57)
1 parent 9c01697 commit 9738b5c

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

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": "[email protected]",
4-
"version": "2.0.2",
4+
"version": "2.1.0",
55
"description": "",
66
"main": "./build/index.js",
77
"files": [

src/classes/ConfigHelper.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,35 @@ class ConfigHelper {
1616

1717
pipe: PipeConfigArray;
1818

19+
onFail?: (failedTarget: string, details: unknown) => void;
20+
1921
constructor() {
2022
const configPath = process.env.ZENITH_CONFIG_PATH || 'zenith.json';
21-
const config = JSON.parse(readFileSync(path.join(ROOT_PATH, configPath), { encoding: 'utf-8' })) as ZenithConfigType;
23+
const config = this.parseConfig(path.join(ROOT_PATH, configPath));
2224
this.buildConfigJSON = config.buildConfig;
2325
this.pipe = config.pipe;
2426
this.projects = config.projects;
2527
this.ignoreFiles = this.getIgnoreFiles(config.ignore);
2628
this.appDirectories = config.appDirectories;
29+
this.onFail = config.onFail;
30+
}
31+
32+
parseConfig(configPath: string): ZenithConfigType {
33+
if (!existsSync(configPath)) {
34+
throw new Error('Zenith config file not found');
35+
}
36+
// check if config is json or js
37+
const extension = path.extname(configPath);
38+
if (extension === '.js') {
39+
// eslint-disable-next-line @typescript-eslint/no-var-requires
40+
const config = (require(configPath)) as ZenithConfigType;
41+
return config;
42+
}
43+
if (extension === '.json') {
44+
const config = readFileSync(configPath, { encoding: 'utf-8' });
45+
return JSON.parse(config) as ZenithConfigType;
46+
}
47+
throw new Error('Zenith config file must be a json or js file');
2748
}
2849

2950
getConfig(configName: string, root: string): TargetObject {

src/classes/WorkerHelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class WorkerHelper {
2929
return execution;
3030

3131
} catch (error) {
32-
if (error instanceof Error) return error;
32+
if (error instanceof Error) throw error;
3333
throw new Error('Executing worker failed');
3434
}
3535
}

src/types/ConfigTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ export type ZenithConfigType = {
4646
appDirectories: string[]
4747
buildConfig: BuildConfig,
4848
projects: ProjectConfig,
49+
onFail?: (failedTarget: string, details: unknown) => void;
4950
}

src/worker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Logger from './utils/logger';
66
import { ROOT_PATH } from './utils/constants';
77
import { readableToBuffer } from './utils/functions';
88
import { configManagerInstance } from './config';
9+
import ConfigHelperInstance from './classes/ConfigHelper';
910
import { ExecError } from './types/BuildTypes';
1011
import HybridCacher from './classes/Cache/HybridCacher';
1112

@@ -25,10 +26,11 @@ const execute = async (buildPath: string, targetCommand: string, hash: string, r
2526
}
2627
return { output: commandOutput };
2728
} catch (error) {
29+
if (ConfigHelperInstance.onFail) ConfigHelperInstance.onFail(root, error);
2830
if (error && typeof error === 'object' && 'stderr' in error) {
2931
const execErr = error as ExecError;
3032
Logger.log(2, 'ERR-W-E-1 :: output => ', execErr.stdout);
31-
return execErr;
33+
throw execErr;
3234
}
3335
Logger.log(2, 'ERR-W-E-3 :: output => ', error);
3436
return new Error(String(error));

0 commit comments

Comments
 (0)