@fivethree/billy-core
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -1,2 +0,2 @@ | ||
import { LaneType, JobType, HookType, HookName } from './types'; | ||
import { LaneType, LaneContext, JobType, HookType, HookName } from './types'; | ||
export declare class Core { | ||
@@ -15,4 +15,5 @@ lanes: LaneType[]; | ||
processAsyncArray(array: any[], asyncFunc: any): Promise<void>; | ||
private getLaneContext; | ||
runHook(hookName: HookName, lane: LaneType): Promise<void>; | ||
getHook(type: 'BEFORE_ALL' | 'AFTER_ALL' | 'AFTER_EACH' | 'BEFORE_EACH' | 'ERROR'): HookType; | ||
getLaneContext(lane: LaneType): LaneContext; | ||
runHook(lane: LaneType, ...args: any[]): Promise<any>; | ||
schedule(): JobType[]; | ||
@@ -19,0 +20,0 @@ cancelScheduled(): void; |
@@ -36,3 +36,3 @@ "use strict"; | ||
this.hooks = []; | ||
this.appDir = path.dirname(require.main.filename); | ||
this.appDir = path.resolve(path.dirname(require.main.filename) + '/../..'); | ||
} | ||
@@ -54,3 +54,3 @@ run() { | ||
getParamLanes() { | ||
const packageJSON = this.parseJSON(`${this.appDir}/../../package.json`); | ||
const packageJSON = this.parseJSON(`${this.appDir}/package.json`); | ||
let program = commander | ||
@@ -86,5 +86,5 @@ .version(packageJSON.version); | ||
]); | ||
yield this.runHook('BEFORE_ALL', answer.lane); | ||
yield this.runHook(this.getHook('BEFORE_ALL').lane); | ||
yield this.takeLane(answer.lane); | ||
yield this.runHook('AFTER_ALL', answer.lane); | ||
yield this.runHook(this.getHook('AFTER_ALL').lane); | ||
}); | ||
@@ -95,14 +95,8 @@ } | ||
try { | ||
yield this.runHook('BEFORE_EACH', lane); | ||
console.log(chalk.green(`taking lane ${lane.name}`)); | ||
const ret = yield this.instance[lane.name](this.getLaneContext(lane), ...args); | ||
yield this.runHook('AFTER_EACH', lane); | ||
return ret; | ||
} | ||
catch (err) { | ||
const errorHook = this.hooks.find(hook => hook.name === 'ERROR'); | ||
if (errorHook) { | ||
yield errorHook.lane.lane(err, this.getLaneContext(lane), ...args); | ||
} | ||
throw new Error(err.message || err); | ||
yield this.runHook(this.getHook('ERROR').lane); | ||
console.error(err); | ||
} | ||
@@ -113,7 +107,7 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.runHook('BEFORE_ALL', lanes[0]); | ||
yield this.runHook(this.getHook('BEFORE_ALL').lane); | ||
yield this.processAsyncArray(lanes, (lane) => __awaiter(this, void 0, void 0, function* () { | ||
yield this.takeLane(lane); | ||
})); | ||
yield this.runHook('AFTER_ALL', lanes[lanes.length - 1]); | ||
yield this.runHook(this.getHook('AFTER_ALL').lane); | ||
}); | ||
@@ -129,2 +123,5 @@ } | ||
} | ||
getHook(type) { | ||
return this.hooks.find(hook => hook.name === type); | ||
} | ||
getLaneContext(lane) { | ||
@@ -137,10 +134,12 @@ const context = { | ||
} | ||
runHook(hookName, lane) { | ||
runHook(lane, ...args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const hooks = this.hooks.filter(hook => hook.name === hookName); | ||
if (hooks.length > 0) { | ||
yield this.processAsyncArray(hooks, (hook) => __awaiter(this, void 0, void 0, function* () { | ||
yield hook.lane.lane(this.getLaneContext(lane)); | ||
})); | ||
try { | ||
const ret = yield this.instance[lane.name](this.getLaneContext(lane), ...args); | ||
return ret; | ||
} | ||
catch (err) { | ||
yield this.runHook(this.getHook('ERROR').lane); | ||
console.error(err); | ||
} | ||
}); | ||
@@ -151,10 +150,10 @@ } | ||
.forEach(job => { | ||
console.log('schedule job', job.name); | ||
const instance = scheduler.scheduleJob(job.schedule, (fireDate) => { | ||
const instance = scheduler.scheduleJob(job.schedule, (fireDate) => __awaiter(this, void 0, void 0, function* () { | ||
console.log('run scheduled lane ' + job.lane.name + ': ' + fireDate); | ||
this.takeLane(job.lane); | ||
}); | ||
yield this.runHook(this.getHook('BEFORE_ALL').lane); | ||
yield this.takeLane(job.lane); | ||
yield this.runHook(this.getHook('AFTER_ALL').lane); | ||
})); | ||
job.scheduler = instance; | ||
}); | ||
console.log('all jobs', this.jobs); | ||
return this.jobs; | ||
@@ -203,2 +202,12 @@ } | ||
this.app.instance = new target(); | ||
this.app.lanes | ||
.forEach((lane, index) => __awaiter(this, void 0, void 0, function* () { | ||
const func = this.app.instance[lane.name].bind(this.app.instance); | ||
this.app.instance[lane.name] = (...args) => __awaiter(this, void 0, void 0, function* () { | ||
yield this.app.runHook(this.app.getHook('BEFORE_EACH').lane); | ||
console.log(chalk.green(`taking lane ${lane.name}`)); | ||
yield func(...args); | ||
yield this.app.runHook(this.app.getHook('AFTER_EACH').lane); | ||
}); | ||
})); | ||
this.app.run(); | ||
@@ -210,3 +219,3 @@ }; | ||
const lanes = this.app.lanes; | ||
lanes.push({ name: propertyKey, description: description, lane: target[propertyKey] }); | ||
lanes.push({ name: propertyKey, description: description }); | ||
}; | ||
@@ -216,3 +225,3 @@ } | ||
return (target, propertyKey, descriptor) => { | ||
const job = { name: propertyKey, lane: { name: propertyKey, description: null, lane: target[propertyKey] }, schedule: schedule, scheduler: null }; | ||
const job = { name: propertyKey, lane: { name: propertyKey, description: null }, schedule: schedule, scheduler: null }; | ||
this.app.jobs.push(job); | ||
@@ -223,3 +232,3 @@ }; | ||
return (target, propertyKey, descriptor) => { | ||
const hook = { name: name, lane: { name: propertyKey, description: null, lane: target[propertyKey] } }; | ||
const hook = { name: name, lane: { name: propertyKey, description: null } }; | ||
this.app.hooks.push(hook); | ||
@@ -226,0 +235,0 @@ }; |
@@ -5,3 +5,2 @@ import { Core } from './core'; | ||
description: string; | ||
lane: Function; | ||
args?: any[]; | ||
@@ -8,0 +7,0 @@ } |
{ | ||
"name": "@fivethree/billy-core", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "cli plugin system core.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -23,3 +23,3 @@ import { LaneType, LaneContext, JobType, HookType, HookName } from './types'; | ||
hooks: HookType[] = []; | ||
appDir = path.dirname(require.main.filename); | ||
appDir = path.resolve(path.dirname(require.main.filename) + '/../..'); | ||
instance: any; | ||
@@ -42,3 +42,3 @@ | ||
getParamLanes(): LaneType[] { | ||
const packageJSON = this.parseJSON(`${this.appDir}/../../package.json`); | ||
const packageJSON = this.parseJSON(`${this.appDir}/package.json`); | ||
let program = commander | ||
@@ -80,7 +80,8 @@ .version(packageJSON.version); | ||
await this.runHook('BEFORE_ALL', answer.lane); | ||
await this.runHook(this.getHook('BEFORE_ALL').lane); | ||
await this.takeLane(answer.lane); | ||
await this.runHook('AFTER_ALL', answer.lane); | ||
await this.runHook(this.getHook('AFTER_ALL').lane); | ||
} | ||
@@ -90,13 +91,8 @@ | ||
try { | ||
await this.runHook('BEFORE_EACH', lane); | ||
console.log(chalk.green(`taking lane ${lane.name}`)); | ||
const ret = await this.instance[lane.name](this.getLaneContext(lane), ...args) | ||
await this.runHook('AFTER_EACH', lane); | ||
return ret; | ||
} catch (err) { | ||
const errorHook = this.hooks.find(hook => hook.name === 'ERROR'); | ||
if (errorHook) { | ||
await errorHook.lane.lane(err, this.getLaneContext(lane), ...args); | ||
} | ||
throw new Error(err.message || err); | ||
await this.runHook(this.getHook('ERROR').lane); | ||
console.error(err); | ||
} | ||
@@ -106,7 +102,7 @@ } | ||
async takeMultiple(lanes: LaneType[]) { | ||
await this.runHook('BEFORE_ALL', lanes[0]); | ||
await this.runHook(this.getHook('BEFORE_ALL').lane); | ||
await this.processAsyncArray(lanes, async (lane) => { | ||
await this.takeLane(lane); | ||
}) | ||
await this.runHook('AFTER_ALL', lanes[lanes.length - 1]); | ||
await this.runHook(this.getHook('AFTER_ALL').lane); | ||
} | ||
@@ -120,4 +116,8 @@ | ||
getHook(type: 'BEFORE_ALL' | 'AFTER_ALL' | 'AFTER_EACH' | 'BEFORE_EACH' | 'ERROR'): HookType { | ||
return this.hooks.find(hook => hook.name === type); | ||
} | ||
private getLaneContext(lane: LaneType): LaneContext { | ||
getLaneContext(lane: LaneType): LaneContext { | ||
const context = { | ||
@@ -130,8 +130,10 @@ lane: lane, | ||
async runHook(hookName: HookName, lane: LaneType) { | ||
const hooks = this.hooks.filter(hook => hook.name === hookName); | ||
if (hooks.length > 0) { | ||
await this.processAsyncArray(hooks, async (hook: HookType) => { | ||
await hook.lane.lane(this.getLaneContext(lane)); | ||
}) | ||
async runHook(lane: LaneType, ...args) { | ||
try { | ||
const ret = await this.instance[lane.name](this.getLaneContext(lane), ...args) | ||
return ret; | ||
} catch (err) { | ||
await this.runHook(this.getHook('ERROR').lane); | ||
console.error(err); | ||
} | ||
@@ -143,10 +145,10 @@ } | ||
.forEach(job => { | ||
console.log('schedule job', job.name) | ||
const instance = scheduler.scheduleJob(job.schedule, (fireDate) => { | ||
const instance = scheduler.scheduleJob(job.schedule, async (fireDate) => { | ||
console.log('run scheduled lane ' + job.lane.name + ': ' + fireDate); | ||
this.takeLane(job.lane) | ||
await this.runHook(this.getHook('BEFORE_ALL').lane); | ||
await this.takeLane(job.lane); | ||
await this.runHook(this.getHook('AFTER_ALL').lane); | ||
}); | ||
job.scheduler = instance; | ||
}); | ||
console.log('all jobs', this.jobs); | ||
return this.jobs; | ||
@@ -195,3 +197,14 @@ } | ||
this.app.instance = new target(); | ||
this.app.lanes | ||
.forEach(async (lane: LaneType, index) => { | ||
const func = this.app.instance[lane.name].bind(this.app.instance) | ||
this.app.instance[lane.name] = async (...args) => { | ||
await this.app.runHook(this.app.getHook('BEFORE_EACH').lane); | ||
console.log(chalk.green(`taking lane ${lane.name}`)); | ||
await func(...args); | ||
await this.app.runHook(this.app.getHook('AFTER_EACH').lane); | ||
} | ||
}); | ||
this.app.run(); | ||
@@ -204,3 +217,3 @@ } | ||
const lanes: LaneType[] = this.app.lanes; | ||
lanes.push({ name: propertyKey, description: description, lane: target[propertyKey] }); | ||
lanes.push({ name: propertyKey, description: description }); | ||
} | ||
@@ -211,3 +224,3 @@ } | ||
return (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => { | ||
const job: JobType = { name: propertyKey, lane: { name: propertyKey, description: null, lane: target[propertyKey] }, schedule: schedule, scheduler: null } | ||
const job: JobType = { name: propertyKey, lane: { name: propertyKey, description: null }, schedule: schedule, scheduler: null } | ||
this.app.jobs.push(job); | ||
@@ -219,3 +232,3 @@ } | ||
return (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => { | ||
const hook: HookType = { name: name, lane: { name: propertyKey, description: null, lane: target[propertyKey] } } | ||
const hook: HookType = { name: name, lane: { name: propertyKey, description: null } } | ||
this.app.hooks.push(hook); | ||
@@ -222,0 +235,0 @@ } |
@@ -6,3 +6,3 @@ import { Core } from './core'; | ||
description: string; | ||
lane: Function; | ||
// lane: Function; | ||
args?: any[]; | ||
@@ -9,0 +9,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22201
551