Files
certd/packages/plugins/src/abstract-plugin/index.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-12-16 00:35:05 +08:00
import fs from 'fs'
2020-12-21 00:32:17 +08:00
import logger from '../utils/util.log.js'
import dayjs from 'dayjs'
import Sleep from '../utils/util.sleep.js'
2020-12-16 00:35:05 +08:00
export class AbstractPlugin {
2020-12-21 00:32:17 +08:00
constructor () {
this.logger = logger
}
appendTimeSuffix (name) {
if (name == null) {
name = 'certd'
}
return name + '-' + dayjs().format('YYYYMMDD-HHmmss')
}
2020-12-16 00:35:05 +08:00
async executeFromContextFile (options = {}) {
const { contextPath } = options
const contextJson = fs.readFileSync(contextPath)
const context = JSON.parse(contextJson)
2020-12-26 01:37:53 +08:00
options.context = context
await this.doExecute(options)
fs.writeFileSync(JSON.stringify(context))
2020-12-16 00:35:05 +08:00
}
2020-12-26 01:37:53 +08:00
async doExecute (options) {
try {
return await this.execute(options)
} catch (e) {
logger.error('插件执行出错:', e)
throw e
}
}
/**
* 执行
* @param options
* @returns {Promise<void>}
*/
async execute (options) {
console.error('请实现此方法,context:', options.context)
}
async doRollback (options) {
try {
return await this.rollback(options)
} catch (e) {
logger.error('插件rollback出错', e)
throw e
}
}
/**
* 回退如有必要
* @param options
*/
async rollback (options) {
console.error('请实现此方法,rollback:', options.context)
2020-12-16 00:35:05 +08:00
}
2020-12-21 00:32:17 +08:00
getAccessProvider (accessProvider, accessProviders) {
if (typeof accessProvider === 'string' && accessProviders) {
accessProvider = accessProviders[accessProvider]
}
return accessProvider
}
async sleep (time) {
await Sleep(time)
}
2020-12-16 00:35:05 +08:00
}