Skip to content

Commit

Permalink
Add --release-draft-only
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Nov 5, 2020
1 parent fee5ab8 commit eb82913
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 61 deletions.
27 changes: 14 additions & 13 deletions readme.md
Expand Up @@ -50,19 +50,20 @@ $ np --help
patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3
Options
--any-branch Allow publishing from any branch
--branch Name of the release branch (default: master)
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
--no-publish Skips publishing
--preview Show tasks without actually executing them
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
--any-branch Allow publishing from any branch
--branch Name of the release branch (default: master)
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
--no-publish Skips publishing
--preview Show tasks without actually executing them
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--release-draft-only Only opens a GitHub release draft for the latest published version
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
Examples
$ np
Expand Down
35 changes: 20 additions & 15 deletions source/cli-implementation.js
Expand Up @@ -21,19 +21,20 @@ const cli = meow(`
${version.SEMVER_INCREMENTS.join(' | ')} | 1.2.3
Options
--any-branch Allow publishing from any branch
--branch Name of the release branch (default: master)
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
--no-publish Skips publishing
--preview Show tasks without actually executing them
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
--any-branch Allow publishing from any branch
--branch Name of the release branch (default: master)
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
--no-publish Skips publishing
--preview Show tasks without actually executing them
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--release-draft-only Only opens a GitHub release draft for the latest published version
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
Examples
$ np
Expand Down Expand Up @@ -65,6 +66,9 @@ const cli = meow(`
releaseDraft: {
type: 'boolean'
},
releaseDraftOnly: {
type: 'boolean'
},
tag: {
type: 'string'
},
Expand Down Expand Up @@ -113,14 +117,15 @@ updateNotifier({pkg: cli.pkg}).notify();
flags['2fa'] = flags['2Fa'];
}

const runPublish = flags.publish && !pkg.private;
const runPublish = !flags.releaseDraftOnly && flags.publish && !pkg.private;

const availability = flags.publish ? await isPackageNameAvailable(pkg) : {
isAvailable: false,
isUnknown: false
};

const version = cli.input.length > 0 ? cli.input[0] : false;
// Use current (latest) version when 'releaseDraftOnly', otherwise use the first argument.
const version = flags.releaseDraftOnly ? pkg.version : cli.input.length > 0 ? cli.input[0] : false;

const options = await ui({
...flags,
Expand Down
15 changes: 15 additions & 0 deletions source/git-util.js
Expand Up @@ -29,6 +29,21 @@ const firstCommit = async () => {
return stdout;
};

exports.tagBeforeCurrentOrFirstCommit = async () => {
const {stdout} = await execa('git', ['tag']);
const tags = stdout.split('\n');

if (tags.length === 0) {
return null;
}

if (tags.length === 1) {
return firstCommit();
}

return tags[tags.length - 2];
}

exports.latestTagOrFirstCommit = async () => {
let latest;
try {
Expand Down
53 changes: 28 additions & 25 deletions source/index.js
Expand Up @@ -48,8 +48,8 @@ module.exports = async (input = 'patch', options) => {
}

const pkg = util.readPkg(options.contents);
const runTests = options.tests && !options.yolo;
const runCleanup = options.cleanup && !options.yolo;
const runTests = !options.releaseDraftOnly && options.tests && !options.yolo;
const runCleanup = !options.releaseDraftOnly && options.cleanup && !options.yolo;
const pkgManager = options.yarn === true ? 'yarn' : 'npm';
const pkgManagerName = options.yarn === true ? 'Yarn' : 'npm';
const rootDir = pkgDir.sync();
Expand Down Expand Up @@ -102,11 +102,12 @@ module.exports = async (input = 'patch', options) => {
const tasks = new Listr([
{
title: 'Prerequisite check',
enabled: () => options.runPublish,
enabled: () => !options.releaseDraftOnly,
task: () => prerequisiteTasks(input, pkg, options)
},
{
title: 'Git',
enabled: () => options.runPublish,
task: () => gitTasks(options)
}
], {
Expand Down Expand Up @@ -167,28 +168,30 @@ module.exports = async (input = 'patch', options) => {
]);
}

tasks.add([
{
title: 'Bumping version using Yarn',
enabled: () => options.yarn === true,
skip: () => {
if (options.preview) {
return `[Preview] Command not executed: yarn version --new-version ${input}.`;
}
},
task: () => exec('yarn', ['version', '--new-version', input])
},
{
title: 'Bumping version using npm',
enabled: () => options.yarn === false,
skip: () => {
if (options.preview) {
return `[Preview] Command not executed: npm version ${input}.`;
}
if (!options.releaseDraftOnly) {
tasks.add([
{
title: 'Bumping version using Yarn',
enabled: () => options.yarn === true,
skip: () => {
if (options.preview) {
return `[Preview] Command not executed: yarn version --new-version ${input}.`;
}
},
task: () => exec('yarn', ['version', '--new-version', input])
},
task: () => exec('npm', ['version', input])
}
]);
{
title: 'Bumping version using npm',
enabled: () => options.yarn === false,
skip: () => {
if (options.preview) {
return `[Preview] Command not executed: npm version ${input}.`;
}
},
task: () => exec('npm', ['version', input])
}
]);
}

if (options.runPublish) {
tasks.add([
Expand Down Expand Up @@ -257,7 +260,7 @@ module.exports = async (input = 'patch', options) => {
}
});

if (options.releaseDraft) {
if (options.releaseDraft || options.releaseDraftOnly) {
tasks.add({
title: 'Creating release draft on GitHub',
enabled: () => isOnGitHub === true,
Expand Down
34 changes: 26 additions & 8 deletions source/ui.js
Expand Up @@ -10,9 +10,14 @@ const {prereleaseTags, checkIgnoreStrategy, getRegistryUrl, isExternalRegistry}
const version = require('./version');
const prettyVersionDiff = require('./pretty-version-diff');

const printCommitLog = async (repoUrl, registryUrl) => {
const latest = await git.latestTagOrFirstCommit();
const log = await git.commitLogFromRevision(latest);
const printCommitLog = async (repoUrl, registryUrl, latestTag) => {
const revision = latestTag ? await git.latestTagOrFirstCommit() : await git.tagBeforeCurrentOrFirstCommit();
if (revision === null) {
console.error(`The package hasn't been published yet.`);
process.exit(1);
}

const log = await git.commitLogFromRevision(revision);

if (!log) {
return {
Expand All @@ -21,7 +26,7 @@ const printCommitLog = async (repoUrl, registryUrl) => {
};
}

const commits = log.split('\n')
let commits = log.split('\n')
.map(commit => {
const splitIndex = commit.lastIndexOf(' ');
return {
Expand All @@ -30,6 +35,14 @@ const printCommitLog = async (repoUrl, registryUrl) => {
};
});

if (!latestTag) {
// Remove the version bump commit from the commit list.
const latestTag = await git.latestTag();
if (latestTag.match(commits[0].message)) {
commits = commits.slice(1);
}
}

const history = commits.map(commit => {
const commitMessage = util.linkifyIssues(repoUrl, commit.message);
const commitId = util.linkifyCommit(repoUrl, commit.id);
Expand All @@ -38,9 +51,9 @@ const printCommitLog = async (repoUrl, registryUrl) => {

const releaseNotes = nextTag => commits.map(commit =>
`- ${htmlEscape(commit.message)} ${commit.id}`
).join('\n') + `\n\n${repoUrl}/compare/${latest}...${nextTag}`;
).join('\n') + `\n\n${repoUrl}/compare/${revision}...${nextTag}`;

const commitRange = util.linkifyCommitRange(repoUrl, `${latest}...master`);
const commitRange = util.linkifyCommitRange(repoUrl, `${revision}...master`);

console.log(`${chalk.bold('Commits:')}\n${history}\n\n${chalk.bold('Commit Range:')}\n${commitRange}\n\n${chalk.bold('Registry:')}\n${registryUrl}\n`);

Expand Down Expand Up @@ -85,7 +98,11 @@ module.exports = async (options, pkg) => {
}
}

console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
if (options.releaseDraftOnly) {
console.log(`\nCreate a release draft on GitHub for ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
} else {
console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
}

const prompts = [
{
Expand Down Expand Up @@ -169,7 +186,8 @@ module.exports = async (options, pkg) => {
}
];

const {hasCommits, releaseNotes} = await printCommitLog(repoUrl, registryUrl);
const useLatestTag = !options.releaseDraftOnly;
const {hasCommits, releaseNotes} = await printCommitLog(repoUrl, registryUrl, useLatestTag);

if (options.version) {
return {
Expand Down

0 comments on commit eb82913

Please sign in to comment.