Skip to content

Commit

Permalink
Rename/combine index and cli modules
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Aug 11, 2021
1 parent f007382 commit eb4d858
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 154 deletions.
2 changes: 1 addition & 1 deletion bin/release-it.js
Expand Up @@ -2,7 +2,7 @@

import updater from 'update-notifier';
import parseArgs from 'yargs-parser';
import release from '../lib/index.js';
import release from '../lib/cli.js';
import { readJSON } from '../lib/util.js';

const pkg = readJSON(new URL('../package.json', import.meta.url));
Expand Down
14 changes: 13 additions & 1 deletion lib/cli.js
@@ -1,5 +1,6 @@
import { readJSON } from '../lib/util.js';
import { readJSON } from './util.js';
import Log from './log.js';
import runTasks from './index.js';

const pkg = readJSON(new URL('../package.json', import.meta.url));

Expand Down Expand Up @@ -27,3 +28,14 @@ For more details, please see https://github.com/release-it/release-it`;
export let version = () => log.log(`v${pkg.version}`);

export let help = () => log.log(helpText);

export default async options => {
if (options.version) {
version();
} else if (options.help) {
help();
} else {
return runTasks(options);
}
return Promise.resolve();
};
146 changes: 136 additions & 10 deletions lib/index.js
@@ -1,16 +1,142 @@
import { version, help } from './cli.js';
import runTasks from './tasks.js';
import _ from 'lodash';
import { getPlugins } from './plugin/factory.js';
import Logger from './log.js';
import Config from './config.js';
import Shell from './shell.js';
import Prompt from './prompt.js';
import Spinner from './spinner.js';
import Metrics from './metrics.js';
import { reduceUntil, parseVersion } from './util.js';
import handleDeprecated from './deprecated.js';
import Plugin from './plugin/Plugin.js';

export default async options => {
if (options.version) {
version();
} else if (options.help) {
help();
} else {
return runTasks(options);
const runTasks = async (opts, di) => {
let container = {};

try {
Object.assign(container, di);
container.config = container.config || new Config(opts);

const { config } = container;
const { isCI, isVerbose, verbosityLevel, isDryRun } = config;

container.log = container.log || new Logger({ isCI, isVerbose, verbosityLevel, isDryRun });
container.spinner = container.spinner || new Spinner({ container, config });
container.prompt = container.prompt || new Prompt({ container: { config } });
container.metrics = new Metrics({ isEnabled: config.isCollectMetrics });
container.shell = container.shell || new Shell({ container });

const { log, metrics, shell, spinner } = container;

const options = handleDeprecated(config.getContext(), log);

metrics.trackEvent('start', options);

const { hooks } = options;

const runHook = async (...name) => {
const scripts = hooks[name.join(':')];
if (!scripts || !scripts.length) return;
const context = config.getContext();
const external = true;
for (const script of _.castArray(scripts)) {
const task = () => shell.exec(script, { external }, context);
await spinner.show({ task, label: script, context, external });
}
};

const runLifeCycleHook = async (plugin, name, ...args) => {
if (plugin === _.first(plugins)) await runHook('before', name);
await runHook('before', plugin.namespace, name);
const willHookRun = (await plugin[name](...args)) !== false;
if (willHookRun) {
await runHook('after', plugin.namespace, name);
}
if (plugin === _.last(plugins)) await runHook('after', name);
};

const [internal, external] = await getPlugins(config, container);
let plugins = [...external, ...internal];

for (const plugin of plugins) {
await runLifeCycleHook(plugin, 'init');
}

const { increment, isPreRelease, preReleaseId } = options.version;

const name = await reduceUntil(plugins, plugin => plugin.getName());
const latestVersion = await reduceUntil(plugins, plugin => plugin.getLatestVersion());
const changelog = await reduceUntil(plugins, plugin => plugin.getChangelog(latestVersion));

const incrementBase = { latestVersion, increment, isPreRelease, preReleaseId };

let version;
if (config.isIncrement) {
incrementBase.increment = await reduceUntil(plugins, plugin => plugin.getIncrement(incrementBase));
version = await reduceUntil(plugins, plugin => plugin.getIncrementedVersionCI(incrementBase));
} else {
version = latestVersion;
}

if (config.isReleaseVersion) {
log.log(version);
process.exit(0);
}

config.setContext({ name, latestVersion, version, changelog });

const action = config.isIncrement ? 'release' : 'update';
const suffix = version && config.isIncrement ? `${latestVersion}...${version}` : `currently at ${latestVersion}`;

log.obtrusive(`🚀 Let's ${action} ${name} (${suffix})`);

log.preview({ title: 'changelog', text: changelog });

if (config.isIncrement) {
version = version || (await reduceUntil(plugins, plugin => plugin.getIncrementedVersion(incrementBase)));
}

config.setContext(parseVersion(version));

if (config.isPromptOnlyVersion) {
config.setCI(true);
}

for (const hook of ['beforeBump', 'bump', 'beforeRelease']) {
for (const plugin of plugins) {
const args = hook === 'bump' ? [version] : [];
await runLifeCycleHook(plugin, hook, ...args);
}
}

plugins = [...internal, ...external];

for (const hook of ['release', 'afterRelease']) {
for (const plugin of plugins) {
await runLifeCycleHook(plugin, hook);
}
}

await metrics.trackEvent('end');

log.log(`🏁 Done (in ${Math.floor(process.uptime())}s.)`);

return {
name,
changelog,
latestVersion,
version
};
} catch (err) {
const { log, metrics } = container;
if (metrics) {
await metrics.trackException(err);
}
log ? log.error(err.message || err) : console.error(err); // eslint-disable-line no-console
throw err;
}
return Promise.resolve();
};

export default runTasks;

export { Plugin };
139 changes: 0 additions & 139 deletions lib/tasks.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/plugins.js
Expand Up @@ -5,7 +5,7 @@ import Log from '../lib/log.js';
import Spinner from '../lib/spinner.js';
import Config from '../lib/config.js';
import { parseGitUrl } from '../lib/util.js';
import runTasks from '../lib/tasks.js';
import runTasks from '../lib/index.js';
import MyPlugin from './stub/plugin.js';
import ReplacePlugin from './stub/plugin-replace.js';
import ContextPlugin from './stub/plugin-context.js';
Expand Down
2 changes: 1 addition & 1 deletion test/tasks.interactive.js
Expand Up @@ -7,7 +7,7 @@ import Log from '../lib/log.js';
import Spinner from '../lib/spinner.js';
import Prompt from '../lib/prompt.js';
import Config from '../lib/config.js';
import runTasks from '../lib/tasks.js';
import runTasks from '../lib/index.js';
import { mkTmpDir, gitAdd } from './util/helpers.js';
import ShellStub from './stub/shell.js';
import { interceptPublish as interceptGitLabPublish } from './stub/gitlab.js';
Expand Down
2 changes: 1 addition & 1 deletion test/tasks.js
Expand Up @@ -6,7 +6,7 @@ import sinon from 'sinon';
import Log from '../lib/log.js';
import Spinner from '../lib/spinner.js';
import Config from '../lib/config.js';
import runTasks from '../lib/tasks.js';
import runTasks from '../lib/index.js';
import { mkTmpDir, gitAdd, getArgs } from './util/helpers.js';
import ShellStub from './stub/shell.js';
import {
Expand Down

0 comments on commit eb4d858

Please sign in to comment.