UNPKG

1.86 kBPlain TextView Raw
1import * as Bluebird from 'bluebird';
2import { spinner, Command, unwrap } from 'denali-cli';
3import { exec, ExecOptions } from 'child_process';
4import { sync as readPkg } from 'read-pkg';
5
6const run = Bluebird.promisify<string, string, ExecOptions>(exec);
7
8/**
9 * Publish an addon to the npm registry.
10 *
11 * @package commands
12 */
13export default class PublishCommand extends Command {
14
15 /* tslint:disable:completed-docs typedef */
16 static commandName = 'publish';
17 static description = 'Publish an addon to the npm registry.';
18 static longDescription = unwrap`
19 Publishes an addon to the npm registry. Runs tests builds the
20 addon, and publishes the dist/ directory to the registry.`;
21
22 static runsInApp = true;
23
24 static flags = {
25 skipTests: {
26 description: 'Do not run tests before publishing',
27 default: false,
28 type: <any>'boolean'
29 }
30 };
31
32 async run(argv: any) {
33 await this.build();
34 if (!argv.skipTests) {
35 await this.runTests();
36 }
37 await this.publish();
38 }
39
40 protected async runTests() {
41 await spinner.start('Running tests');
42 try {
43 await run('npm test', {});
44 } catch (error) {
45 await spinner.fail('Tests failed, halting publish');
46 throw error;
47 }
48 await spinner.succeed('Tests passed');
49 }
50
51 protected async build() {
52 await spinner.start('Building');
53 try {
54 await run('npm run build', {});
55 } catch (error) {
56 await spinner.fail('Build failed, halting publish');
57 throw error;
58 }
59 await spinner.succeed('Addon built');
60 }
61
62 protected async publish() {
63 await spinner.start('Publishing');
64 try {
65 await run('npm publish', {});
66 } catch (error) {
67 await spinner.fail('Publish failed');
68 throw error;
69 }
70 let pkg = readPkg();
71 await spinner.succeed(`${ pkg.name } ${ pkg.version } published!`);
72 }
73
74}