1 | #!/usr/bin/env node
|
2 |
|
3 | const ghpages = require('../lib/index.js');
|
4 | const {Command} = require('commander');
|
5 | const path = require('path');
|
6 | const pkg = require('../package.json');
|
7 | const addr = require('email-addresses');
|
8 |
|
9 | function publish(dist, config) {
|
10 | return new Promise((resolve, reject) => {
|
11 | const basePath = path.resolve(process.cwd(), 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 | const program = new Command()
|
24 | .version(pkg.version)
|
25 | .requiredOption(
|
26 | '-d, --dist <dist>',
|
27 | 'Base directory for all source files'
|
28 | )
|
29 | .option(
|
30 | '-s, --src <src>',
|
31 | 'Pattern used to select which files to publish',
|
32 | ghpages.defaults.src
|
33 | )
|
34 | .option(
|
35 | '-b, --branch <branch>',
|
36 | 'Name of the branch you are pushing to',
|
37 | ghpages.defaults.branch
|
38 | )
|
39 | .option(
|
40 | '-e, --dest <dest>',
|
41 | 'Target directory within the destination branch (relative to the root)',
|
42 | ghpages.defaults.dest
|
43 | )
|
44 | .option('-a, --add', 'Only add, and never remove existing files')
|
45 | .option('-x, --silent', 'Do not output the repository url')
|
46 | .option(
|
47 | '-m, --message <message>',
|
48 | 'commit message',
|
49 | ghpages.defaults.message
|
50 | )
|
51 | .option('-g, --tag <tag>', 'add tag to commit')
|
52 | .option('--git <git>', 'Path to git executable', ghpages.defaults.git)
|
53 | .option('-t, --dotfiles', 'Include dotfiles')
|
54 | .option('--nojekyll', 'Add a .nojekyll file to disable Jekyll')
|
55 | .option(
|
56 | '--cname <CNAME>',
|
57 | 'Add a CNAME file with the name of your custom domain'
|
58 | )
|
59 | .option('-r, --repo <repo>', 'URL of the repository you are pushing to')
|
60 | .option('-p, --depth <depth>', 'depth for clone', ghpages.defaults.depth)
|
61 | .option(
|
62 | '-o, --remote <name>',
|
63 | 'The name of the remote',
|
64 | ghpages.defaults.remote
|
65 | )
|
66 | .option(
|
67 | '-u, --user <address>',
|
68 | 'The name and email of the user (defaults to the git config). Format is "Your Name <email@example.com>".'
|
69 | )
|
70 | .option(
|
71 | '-v, --remove <pattern>',
|
72 | 'Remove files that match the given pattern ' +
|
73 | '(ignored if used together with --add).',
|
74 | ghpages.defaults.remove
|
75 | )
|
76 | .option('-n, --no-push', 'Commit only (with no push)')
|
77 | .option(
|
78 | '-f, --no-history',
|
79 | 'Push force new commit without parent history'
|
80 | )
|
81 | .option(
|
82 | '--before-add <file>',
|
83 | 'Execute the function exported by <file> before "git add"'
|
84 | )
|
85 | .parse(args);
|
86 |
|
87 | const options = program.opts();
|
88 |
|
89 | let user;
|
90 | if (options.user) {
|
91 | const parts = addr.parseOneAddress(options.user);
|
92 | if (!parts) {
|
93 | throw new Error(
|
94 | `Could not parse name and email from user option "${options.user}" ` +
|
95 | '(format should be "Your Name <email@example.com>")'
|
96 | );
|
97 | }
|
98 | user = {name: parts.name, email: parts.address};
|
99 | }
|
100 | let beforeAdd;
|
101 | if (options.beforeAdd) {
|
102 | const m = require(require.resolve(options.beforeAdd, {
|
103 | paths: [process.cwd()],
|
104 | }));
|
105 |
|
106 | if (typeof m === 'function') {
|
107 | beforeAdd = m;
|
108 | } else if (typeof m === 'object' && typeof m.default === 'function') {
|
109 | beforeAdd = m.default;
|
110 | } else {
|
111 | throw new Error(
|
112 | `Could not find function to execute before adding files in ` +
|
113 | `"${options.beforeAdd}".\n `
|
114 | );
|
115 | }
|
116 | }
|
117 |
|
118 | const config = {
|
119 | repo: options.repo,
|
120 | silent: !!options.silent,
|
121 | branch: options.branch,
|
122 | src: options.src,
|
123 | dest: options.dest,
|
124 | message: options.message,
|
125 | tag: options.tag,
|
126 | git: options.git,
|
127 | depth: options.depth,
|
128 | dotfiles: !!options.dotfiles,
|
129 | nojekyll: !!options.nojekyll,
|
130 | cname: options.cname,
|
131 | add: !!options.add,
|
132 | remove: options.remove,
|
133 | remote: options.remote,
|
134 | push: !!options.push,
|
135 | history: !!options.history,
|
136 | user: user,
|
137 | beforeAdd: beforeAdd,
|
138 | };
|
139 |
|
140 | return publish(options.dist, config);
|
141 | });
|
142 | }
|
143 |
|
144 | if (require.main === module) {
|
145 | main(process.argv)
|
146 | .then(() => {
|
147 | process.stdout.write('Published\n');
|
148 | })
|
149 | .catch((err) => {
|
150 | process.stderr.write(`${err.stack}\n`, () => process.exit(1));
|
151 | });
|
152 | }
|
153 |
|
154 | module.exports = main;
|
155 | exports = module.exports;
|