UNPKG

@auto-canary/exec

Version:

Tap into select hooks and run a command on the terminal

144 lines 5.69 kB
"use strict"; /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@auto-it/core"); const make_hooks_1 = require("@auto-it/core/dist/utils/make-hooks"); const t = tslib_1.__importStar(require("io-ts")); const fromentries_1 = tslib_1.__importDefault(require("fromentries")); const child_process_1 = require("child_process"); const changelogHooks = Object.keys(make_hooks_1.makeChangelogHooks()); const onCreateChangelog = t.partial(fromentries_1.default(changelogHooks.map(name => [name, t.string]))); const logParseHooks = Object.keys(make_hooks_1.makeLogParseHooks()); const onCreateLogParse = t.partial(fromentries_1.default(logParseHooks.map(name => [name, t.string]))); const releaseHooks = Object.keys(make_hooks_1.makeReleaseHooks()); const onCreateReleaseOptions = [ 'onCreateChangelog', 'onCreateLogParse' ]; const onCreateRelease = t.partial(fromentries_1.default(releaseHooks .map(name => [name, t.string]) .filter(([hook]) => !onCreateReleaseOptions.includes(hook)))); const rootHooks = Object.keys(make_hooks_1.makeHooks()); const complextRootOptions = [ 'onCreateRelease', 'onCreateChangelog', 'onCreateLogParse', 'validateConfig' ]; const rootOptions = t.partial(fromentries_1.default(rootHooks .map(name => [name, t.string]) .filter(([hook]) => !complextRootOptions.includes(hook)))); const pluginOptions = t.intersection([ rootOptions, t.partial({ onCreateRelease, onCreateLogParse, onCreateChangelog }) ]); /** Put args in envirnment */ const createEnv = (args) => (Object.assign(Object.assign({}, process.env), fromentries_1.default(args.map((arg, index) => [`ARG_${index}`, JSON.stringify(arg)])))); /** Tap a hook if possible */ const tapHook = (name, hook, command) => { if (!hook) { return; } const hookType = hook.constructor.name; if (name === 'getRepository' || name === 'getAuthor' || name === 'makeRelease' || name === 'modifyConfig' || name === 'next' || name === 'canary' || name === 'parseCommit' || name === 'addToBody' || name === 'renderChangelogLine' || name === 'renderChangelogTitle' || name === 'renderChangelogAuthor' || name === 'renderChangelogAuthorLine') { hook.tap(`exec ${name}`, (...args) => { const value = child_process_1.execSync(command, { stdio: ['pipe', 'inherit'], encoding: 'utf8', env: createEnv(args) }); if (name !== 'canary') { return JSON.parse(value); } try { return JSON.parse(value); } catch (error) { // canary hook can just return a string return value; } }); } else if (name === 'omitCommit' || name === 'omitReleaseNotes') { hook.tap(`exec ${name}`, (...args) => { const value = child_process_1.execSync(command, { stdio: ['pipe', 'inherit'], encoding: 'utf8', env: createEnv(args) }); if (value === 'true') { return true; } }); } else if (hookType === 'SyncHook' || hookType === 'AsyncSeriesHook' || hookType === 'AsyncParallelHook' || name === 'createChangelogTitle' || name === 'getPreviousVersion') { hook.tap(`exec ${name}`, (...args) => child_process_1.execSync(command, { encoding: 'utf8', env: createEnv(args), stdio: name === 'createChangelogTitle' || name === 'getPreviousVersion' ? ['pipe', 'inherit'] : 'inherit' }).trim()); } }; /** Tap into select hooks and run a command on the terminal */ class ExecPlugin { /** Initialize the plugin with it's options */ constructor(options) { /** The name of the plugin */ this.name = 'exec'; this.options = options; } /** Tap into auto plugin points. */ apply(auto) { auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => { if (name === this.name || name === `@auto-it/${this.name}`) { return core_1.validatePluginConfiguration(this.name, pluginOptions, options); } }); Object.entries(this.options).forEach(([name, command]) => command && this.applyHook(auto, name, command)); } /** Apply a hook to auto */ applyHook(auto, key, command) { const name = key; if (name === 'onCreateRelease') { auto.hooks.onCreateRelease.tap(this.name, release => { Object.entries(command).map(([key, command]) => command && tapHook(key, release.hooks[key], command)); }); } else if (name === 'onCreateChangelog') { auto.hooks.onCreateChangelog.tap(this.name, changelog => { Object.entries(command).map(([key, command]) => command && tapHook(key, changelog.hooks[key], command)); }); } else if (name === 'onCreateLogParse') { auto.hooks.onCreateLogParse.tap(this.name, logParse => { Object.entries(command).map(([key, command]) => command && tapHook(key, logParse.hooks[key], command)); }); } else { tapHook(name, auto.hooks[name], command); } } } exports.default = ExecPlugin; //# sourceMappingURL=index.js.map