Skip to content

Commit

Permalink
Add preview mode (#399)
Browse files Browse the repository at this point in the history
Co-authored-by: Itai Steinherz <itaisteinherz@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
3 people committed Feb 22, 2020
1 parent cd046ca commit 5f52e81
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<sub>(does not apply to external registries)</sub>
- Opens a prefilled GitHub Releases draft after publish
- Warns about the possibility of extraneous files being published
- See exactly what will be executed with [preview mode](https://github.com/sindresorhus/np/issues/391), without pushing or publishing anything remotely
- Supports [GitHub Packages](https://github.com/features/packages)


Expand Down Expand Up @@ -69,6 +70,7 @@ $ np --help
--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
Expand Down Expand Up @@ -101,6 +103,7 @@ Currently, these are the flags you can configure:
- `tests` - Run `npm test` (`true` by default).
- `yolo` - Skip cleanup and testing (`false` by default).
- `publish` - Publish (`true` by default).
- `preview` - Show tasks without actually executing them (`false` by default).
- `tag` - Publish under a given dist-tag (`latest` by default).
- `yarn` - Use yarn if possible (`true` by default).
- `contents` - Subdirectory to publish (`.` by default).
Expand Down
9 changes: 9 additions & 0 deletions source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const cli = meow(`
--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
Expand Down Expand Up @@ -67,6 +68,9 @@ const cli = meow(`
},
contents: {
type: 'string'
},
preview: {
type: 'boolean'
}
}
});
Expand Down Expand Up @@ -103,6 +107,11 @@ updateNotifier({pkg: cli.pkg}).notify();

console.log(); // Prints a newline for readability
const newPkg = await np(options.version, options);

if (options.preview) {
return;
}

console.log(`\n ${newPkg.name} ${newPkg.version} published 🎉`);
})().catch(error => {
console.error(`\n${logSymbols.error} ${error.message}`);
Expand Down
38 changes: 36 additions & 2 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ module.exports = async (input = 'patch', options) => {

// The default parameter is a workaround for https://github.com/Tapppi/async-exit-hook/issues/9
exitHook((callback = () => {}) => {
if (publishStatus === 'FAILED') {
if (options.preview) {
callback();
} else if (publishStatus === 'FAILED') {
(async () => {
await rollback();
callback();
Expand Down Expand Up @@ -173,11 +175,21 @@ module.exports = async (input = 'patch', options) => {
{
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}.`;
}
},
task: () => exec('npm', ['version', input])
}
]);
Expand All @@ -186,6 +198,12 @@ module.exports = async (input = 'patch', options) => {
tasks.add([
{
title: `Publishing package using ${pkgManagerName}`,
skip: () => {
if (options.preview) {
const args = publish.getPackagePublishArguments(options);
return `[Preview] Command not executed: ${pkgManager} ${args.join(' ')}.`;
}
},
task: (context, task) => {
let hasError = false;

Expand All @@ -209,6 +227,12 @@ module.exports = async (input = 'patch', options) => {
tasks.add([
{
title: 'Enabling two-factor authentication',
skip: () => {
if (options.preview) {
const args = enable2fa.getEnable2faArgs(pkg.name, options);
return `[Preview] Command not executed: npm ${args.join(' ')}.`;
}
},
task: (context, task) => enable2fa(task, pkg.name, {otp: context.otp})
}
]);
Expand All @@ -224,6 +248,10 @@ module.exports = async (input = 'patch', options) => {
return 'Upstream branch not found; not pushing.';
}

if (options.preview) {
return '[Preview] Command not executed: git push --follow-tags.';
}

if (publishStatus === 'FAILED' && runPublish) {
return 'Couldn\'t publish package to npm; not pushing.';
}
Expand All @@ -234,7 +262,13 @@ module.exports = async (input = 'patch', options) => {
tasks.add({
title: 'Creating release draft on GitHub',
enabled: () => isOnGitHub === true,
skip: () => !options.releaseDraft,
skip: () => {
if (options.preview) {
return '[Preview] GitHub Releases draft will not be opened in preview mode.';
}

return !options.releaseDraft;
},
task: () => releaseTaskHelper(options, pkg)
});

Expand Down
8 changes: 6 additions & 2 deletions source/npm/enable-2fa.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ const {from} = require('rxjs');
const {catchError} = require('rxjs/operators');
const handleNpmError = require('./handle-npm-error');

const enable2fa = (packageName, options) => {
const getEnable2faArgs = (packageName, options) => {
const args = ['access', '2fa-required', packageName];

if (options && options.otp) {
args.push('--otp', options.otp);
}

return execa('npm', args);
return args;
};

const enable2fa = (packageName, options) => execa('npm', getEnable2faArgs(packageName, options));

module.exports = (task, packageName, options) =>
from(enable2fa(packageName, options)).pipe(
catchError(error => handleNpmError(error, task, otp => enable2fa(packageName, {otp})))
);

module.exports.getEnable2faArgs = getEnable2faArgs;
8 changes: 6 additions & 2 deletions source/npm/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {from} = require('rxjs');
const {catchError} = require('rxjs/operators');
const handleNpmError = require('./handle-npm-error');

const pkgPublish = (pkgManager, options) => {
const getPackagePublishArguments = options => {
const args = ['publish'];

if (options.contents) {
Expand All @@ -23,9 +23,11 @@ const pkgPublish = (pkgManager, options) => {
args.push('--access', 'public');
}

return execa(pkgManager, args);
return args;
};

const pkgPublish = (pkgManager, options) => execa(pkgManager, getPackagePublishArguments(options));

module.exports = (context, pkgManager, task, options) =>
from(pkgPublish(pkgManager, options)).pipe(
catchError(error => handleNpmError(error, task, otp => {
Expand All @@ -34,3 +36,5 @@ module.exports = (context, pkgManager, task, options) =>
return pkgPublish(pkgManager, {...options, otp});
}))
);

module.exports.getPackagePublishArguments = getPackagePublishArguments;

0 comments on commit 5f52e81

Please sign in to comment.