UNPKG

3.49 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const ghpages = require('../lib/index');
4const program = require('commander');
5const path = require('path');
6const pkg = require('../package.json');
7const addr = require('email-addresses');
8
9function publish(config) {
10 return new Promise((resolve, reject) => {
11 const basePath = path.resolve(process.cwd(), program.dist);
12 ghpages.publish(basePath, config, err => {
13 if (err) {
14 return reject(err);
15 }
16 resolve();
17 });
18 });
19}
20
21function main(args) {
22 return Promise.resolve().then(() => {
23 program
24 .version(pkg.version)
25 .option('-d, --dist <dist>', 'Base directory for all source files')
26 .option(
27 '-s, --src <src>',
28 'Pattern used to select which files to publish',
29 ghpages.defaults.src
30 )
31 .option(
32 '-b, --branch <branch>',
33 'Name of the branch you are pushing to',
34 ghpages.defaults.branch
35 )
36 .option(
37 '-e, --dest <dest>',
38 'Target directory within the destination branch (relative to the root)',
39 ghpages.defaults.dest
40 )
41 .option('-a, --add', 'Only add, and never remove existing files')
42 .option('-x, --silent', 'Do not output the repository url')
43 .option(
44 '-m, --message <message>',
45 'commit message',
46 ghpages.defaults.message
47 )
48 .option('-g, --tag <tag>', 'add tag to commit')
49 .option('--git <git>', 'Path to git executable', ghpages.defaults.git)
50 .option('-t, --dotfiles', 'Include dotfiles')
51 .option('-r, --repo <repo>', 'URL of the repository you are pushing to')
52 .option('-p, --depth <depth>', 'depth for clone', ghpages.defaults.depth)
53 .option(
54 '-o, --remote <name>',
55 'The name of the remote',
56 ghpages.defaults.remote
57 )
58 .option(
59 '-u, --user <address>',
60 'The name and email of the user (defaults to the git config). Format is "Your Name <email@example.com>".'
61 )
62 .option(
63 '-v, --remove <pattern>',
64 'Remove files that match the given pattern ' +
65 '(ignored if used together with --add).',
66 ghpages.defaults.only
67 )
68 .option('-n, --no-push', 'Commit only (with no push)')
69 .option(
70 '-f, --no-history',
71 'Push force new commit without parent history'
72 )
73 .parse(args);
74
75 let user;
76 if (program.user) {
77 const parts = addr.parseOneAddress(program.user);
78 if (!parts) {
79 throw new Error(
80 `Could not parse name and email from user option "${program.user}" ` +
81 '(format should be "Your Name <email@example.com>")'
82 );
83 }
84 user = {name: parts.name, email: parts.address};
85 }
86
87 const config = {
88 repo: program.repo,
89 silent: !!program.silent,
90 branch: program.branch,
91 src: program.src,
92 dest: program.dest,
93 message: program.message,
94 tag: program.tag,
95 git: program.git,
96 depth: program.depth,
97 dotfiles: !!program.dotfiles,
98 add: !!program.add,
99 only: program.remove,
100 remote: program.remote,
101 push: !!program.push,
102 history: !!program.history,
103 user: user
104 };
105
106 return publish(config);
107 });
108}
109
110if (require.main === module) {
111 main(process.argv)
112 .then(() => {
113 process.stdout.write('Published\n');
114 })
115 .catch(err => {
116 process.stderr.write(`${err.message}\n`, () => process.exit(1));
117 });
118}
119
120exports = module.exports = main;