/
index.js
146 lines (115 loc) Β· 4.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 { reduceUntil, parseVersion } from './util.js';
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, isChangelog, isReleaseVersion } = 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.shell = container.shell || new Shell({ container });
const { log, shell, spinner } = container;
const options = config.getContext();
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())) || '0.0.0';
const changelog = await reduceUntil(plugins, plugin => plugin.getChangelog(latestVersion));
if (isChangelog) {
console.log(changelog);
process.exit(0);
}
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;
}
config.setContext({ name, latestVersion, version, changelog });
if (!isReleaseVersion) {
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)));
}
if (!version) {
log.obtrusive(`No new version to release`);
}
if (isReleaseVersion) {
console.log(version);
process.exit(0);
}
if (version) {
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);
}
}
}
log.log(`π Done (in ${Math.floor(process.uptime())}s.)`);
return {
name,
changelog,
latestVersion,
version
};
} catch (err) {
const { log } = container;
log ? log.error(err.message || err) : console.error(err); // eslint-disable-line no-console
throw err;
}
};
export default runTasks;
/** @public */
export { default as Config } from './config.js';
/** @public */
export { default as Plugin } from './plugin/Plugin.js';