Skip to content

Commit

Permalink
feat: read js config and add onFail to config (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmdoganjf authored Jan 3, 2024
1 parent 9c01697 commit 9738b5c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jotforminc/zenith",
"packageManager": "yarn@1.22.21",
"version": "2.0.2",
"version": "2.1.0",
"description": "",
"main": "./build/index.js",
"files": [
Expand Down
23 changes: 22 additions & 1 deletion src/classes/ConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,35 @@ class ConfigHelper {

pipe: PipeConfigArray;

onFail?: (failedTarget: string, details: unknown) => void;

constructor() {
const configPath = process.env.ZENITH_CONFIG_PATH || 'zenith.json';
const config = JSON.parse(readFileSync(path.join(ROOT_PATH, configPath), { encoding: 'utf-8' })) as ZenithConfigType;
const config = this.parseConfig(path.join(ROOT_PATH, configPath));
this.buildConfigJSON = config.buildConfig;
this.pipe = config.pipe;
this.projects = config.projects;
this.ignoreFiles = this.getIgnoreFiles(config.ignore);
this.appDirectories = config.appDirectories;
this.onFail = config.onFail;
}

parseConfig(configPath: string): ZenithConfigType {
if (!existsSync(configPath)) {
throw new Error('Zenith config file not found');
}
// check if config is json or js
const extension = path.extname(configPath);
if (extension === '.js') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config = (require(configPath)) as ZenithConfigType;
return config;
}
if (extension === '.json') {
const config = readFileSync(configPath, { encoding: 'utf-8' });
return JSON.parse(config) as ZenithConfigType;
}
throw new Error('Zenith config file must be a json or js file');
}

getConfig(configName: string, root: string): TargetObject {
Expand Down
2 changes: 1 addition & 1 deletion src/classes/WorkerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class WorkerHelper {
return execution;

} catch (error) {
if (error instanceof Error) return error;
if (error instanceof Error) throw error;
throw new Error('Executing worker failed');
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types/ConfigTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export type ZenithConfigType = {
appDirectories: string[]
buildConfig: BuildConfig,
projects: ProjectConfig,
onFail?: (failedTarget: string, details: unknown) => void;
}
4 changes: 3 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Logger from './utils/logger';
import { ROOT_PATH } from './utils/constants';
import { readableToBuffer } from './utils/functions';
import { configManagerInstance } from './config';
import ConfigHelperInstance from './classes/ConfigHelper';
import { ExecError } from './types/BuildTypes';
import HybridCacher from './classes/Cache/HybridCacher';

Expand All @@ -25,10 +26,11 @@ const execute = async (buildPath: string, targetCommand: string, hash: string, r
}
return { output: commandOutput };
} catch (error) {
if (ConfigHelperInstance.onFail) ConfigHelperInstance.onFail(root, error);
if (error && typeof error === 'object' && 'stderr' in error) {
const execErr = error as ExecError;
Logger.log(2, 'ERR-W-E-1 :: output => ', execErr.stdout);
return execErr;
throw execErr;
}
Logger.log(2, 'ERR-W-E-3 :: output => ', error);
return new Error(String(error));
Expand Down

0 comments on commit 9738b5c

Please sign in to comment.