Sharec logo Sharec

Hooks

You can define your hooks in the runtime configuration finle. With hooks you can customize any field in FlowContext.

Hook should match following signature:

type FlowStep = (context: FlowContext) => Promise<FlowContext>

At this moment sharec supports two hooks: beforeMerge and afterMerge.

Using hooks

You can install and use any external hook:

const prettierHook = require('sharec-prettier-hook')

module.exports = {
  afterMerge: prettierHook,
}

Writting your own hook

Hooks authoring is very simple:

// .sharecrc.js
const { EOL } = require('os')

module.exports = {
  // here we're logging whole the context before `mergeConfigsPackages` step
  beforeMerge: async (context) => {
    console.log('Before merge hooks has been fired: ', context)
    return context
  },
  // in this step we're removing EOL from the all files
  afterMerge: async (context) => {
    const eolRe = new RegExp(`${EOL}$`)

    for (const config in context.mergedConfigs) {
      if (!eolRe.test(context.mergedConfigs[config])) continue

      context.mergedConfigs[config] = context.mergedConfigs[config].replace(eolRe, '')
    }

    return context
  },
}

Official hooks