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