Skip to content

Commit

Permalink
Refactor dual non/interactive flows into a single flow w/ steps
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 8, 2019
1 parent d15da98 commit 192e20f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions lib/prompt.js
Expand Up @@ -79,8 +79,8 @@ const prompts = {
}
};

module.exports = async (shouldPrompt, context, promptName, task) => {
if (!shouldPrompt) return noop;
module.exports = async ({ enabled = true, prompt: promptName, task }, context) => {
if (!enabled) return noop;
const prompt = Object.assign({}, prompts[promptName], {
name: promptName,
message: prompts[promptName].message(context),
Expand Down
72 changes: 35 additions & 37 deletions lib/tasks.js
Expand Up @@ -22,7 +22,7 @@ module.exports = async opts => {
const { isInteractive, isVerbose, isDryRun, isDebug } = config;
const log = new Logger({ isInteractive, isVerbose, isDryRun });
const metrics = new Metrics({ isEnabled: config.isCollectMetrics });
const s = new Spinner({ isInteractive, isVerbose, isDryRun, isDebug });
const spinner = new Spinner({ isInteractive, isVerbose, isDryRun, isDebug });

try {
const options = handleDeprecated(config.getOptions());
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = async opts => {
return changelog;
};

await s.show({ enabled: beforeStart, task: () => run(beforeStart), label: beforeStart, forced: true });
await spinner.show({ enabled: beforeStart, task: () => run(beforeStart), label: beforeStart, forced: true });

const v = new Version({ preReleaseId: options.preReleaseId, log });
v.setLatestVersion({ use, gitTag: latestTag, pkgVersion: options.npm.version, isRootDir });
Expand All @@ -81,15 +81,15 @@ module.exports = async opts => {

if (isInteractive && !v.version) {
const context = config.getOptions();
await prompt(true, context, 'incrementList', async increment => {
const task = async increment => {
if (increment) {
await v.bump({ increment });
} else {
await prompt(true, context, 'version', async version => {
v.version = version;
});
const task = async version => (v.version = version);
await prompt({ prompt: 'version', task }, context);
}
});
};
await prompt({ prompt: 'incrementList', task }, context);
}

v.validate();
Expand All @@ -101,27 +101,27 @@ module.exports = async opts => {
process.on('exit', () => gitClient.reset(pkgFiles));
}

await s.show({ enabled: beforeBump, task: () => run(beforeBump), label: beforeBump, forced: true });
await s.show({ task: () => shell.bump(pkgFiles, version), label: 'Bump version' });
await s.show({ enabled: afterBump, task: () => run(afterBump), label: afterBump, forced: true });
await spinner.show({ enabled: beforeBump, task: () => run(beforeBump), label: beforeBump, forced: true });
await spinner.show({ task: () => shell.bump(pkgFiles, version), label: 'Bump version' });
await spinner.show({ enabled: afterBump, task: () => run(afterBump), label: afterBump, forced: true });

if (isLateChangeLog) {
changelog = await getChangelog();
config.setRuntimeOptions({ changelog });
}

await s.show({ enabled: beforeStage, task: () => run(beforeStage), label: beforeStage, forced: true });
await spinner.show({ enabled: beforeStage, task: () => run(beforeStage), label: beforeStage, forced: true });
await gitClient.stage(pkgFiles);
await gitClient.stageDir();

if (options.dist.repo) {
const { scripts, repo, stageDir, files, baseDir, pkgFiles } = options.dist;
const { beforeStage } = scripts;
await s.show({ task: () => gitDistClient.clone(repo, stageDir), label: 'Clone' });
await spinner.show({ task: () => gitDistClient.clone(repo, stageDir), label: 'Clone' });
await shell.copy(files, stageDir, { cwd: baseDir });
await shell.pushd(stageDir);
await shell.bump(pkgFiles, version);
await s.show({ enabled: beforeStage, task: () => run(beforeStage), label: beforeStage, forced: true });
await spinner.show({ enabled: beforeStage, task: () => run(beforeStage), label: beforeStage, forced: true });
await gitDistClient.stageDir();
await shell.popd();
}
Expand All @@ -139,35 +139,33 @@ module.exports = async opts => {
const push = () => gitClient.push();
const ghRelease = () => ghClient.release({ version, isPreRelease, changelog });
const ghUploadAssets = () => ghClient.uploadAssets();
const ghReleaseAndUploadAssets = async () => (await ghRelease()) && (await ghUploadAssets());
const ghReleaser = isInteractive ? async () => (await ghRelease()) && (await ghUploadAssets()) : ghRelease;
const glRelease = () => glClient.release({ version, changelog });
const otpPrompt = isInteractive && (task => prompt(true, context, 'otp', task));
const otpPrompt = isInteractive ? task => prompt({ context, prompt: 'otp', task }) : null;
const publish = () => npmClient.publish({ version, isPreRelease, otpPrompt });

logPreview(log, 'changeset', await gitClient.status(), EOL);

if (!isInteractive) {
await s.show({ enabled: git.commit, task: commit, label: 'Git commit' });
await s.show({ enabled: git.tag, task: tag, label: 'Git tag' });
await s.show({ enabled: git.push, task: push, label: 'Git push' });
github.release && github.releaseNotes && logPreview(log, 'release notes', await ghClient.getNotes(), EOL);
await s.show({ enabled: github.release, task: ghRelease, label: 'GitHub release' });
await s.show({ enabled: github.assets, task: ghUploadAssets, label: 'GitHub upload assets' });
gitlab.release && gitlab.releaseNotes && logPreview(log, 'release notes', await glClient.getNotes(), EOL);
await s.show({ enabled: gitlab.release, task: glRelease, label: 'GitLab release' });
await s.show({ enabled: npm.publish && !npm.private, task: publish, label: 'npm publish' });
} else {
await prompt(git.commit, context, 'commit', commit);
await prompt(git.tag, context, 'tag', tag);
await prompt(git.push, context, 'push', push);
github.release && github.releaseNotes && logPreview(log, 'release notes', await ghClient.getNotes(), EOL);
await prompt(github.release, context, 'ghRelease', ghReleaseAndUploadAssets);
gitlab.release && gitlab.releaseNotes && logPreview(log, 'release notes', await glClient.getNotes(), EOL);
await prompt(gitlab.release, context, 'glRelease', glRelease);
await prompt(npm.publish && !npm.private, context, 'publish', publish);
}

await s.show({ enabled: afterRelease, task: () => run(afterRelease), label: afterRelease, forced: true });
const step = options => (isInteractive ? prompt(options, context) : spinner.show(options));

// Git
await step({ enabled: git.commit, task: commit, label: 'Git commit', prompt: 'commit' });
await step({ enabled: git.tag, task: tag, label: 'Git tag', prompt: 'tag' });
await step({ enabled: git.push, task: push, label: 'Git push', prompt: 'push' });

// GitHub
github.release && github.releaseNotes && logPreview(log, 'release notes', await ghClient.getNotes(), EOL);
await step({ enabled: github.release, task: ghReleaser, label: 'GitHub release', prompt: 'ghRelease' });
await step({ enabled: github.assets && !isInteractive, task: ghUploadAssets, label: 'GitHub upload assets' });

// GitLab
gitlab.release && gitlab.releaseNotes && logPreview(log, 'release notes', await glClient.getNotes(), EOL);
await step({ enabled: gitlab.release, task: glRelease, label: 'GitLab release', prompt: 'glRelease' });

// npm
await step({ enabled: npm.publish, task: publish, label: 'npm publish', prompt: 'publish' });

await spinner.show({ enabled: afterRelease, task: () => run(afterRelease), label: afterRelease, forced: true });

ghClient.isReleased && log.log(`🔗 ${ghClient.getReleaseUrl()}`);
glClient.isReleased && log.log(`🔗 ${glClient.getReleaseUrl()}`);
Expand Down

0 comments on commit 192e20f

Please sign in to comment.