-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
151 lines (127 loc) · 5.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const ghpages = require('gh-pages');
module.exports = (api, projectOptions) => {
api.registerCommand('gh-pages', {
description: 'publish to GitHub pages',
usage: 'vue-cli-service gh-pages [options]',
options: {
'-d, --dist <dist>': `Base directory for all source files (default: ${projectOptions.outputDir})`,
'-s, --src <src>': `Pattern used to select which files to publish (default: ${ghpages.defaults.src})`,
'-b, --branch <branch>': `Name of the branch you are pushing to (default: ${ghpages.defaults.branch})`,
'-e, --dest <dest>': `Target directory within the destination branch (relative to the root) (default: ${ghpages.defaults.dest})`,
'-a, --add': 'Only add, and never remove existing files',
'-x, --silent': 'Do not output the repository url',
'-m, --message <message>': `Commit message (default: ${ghpages.defaults.message})`,
'-g, --tag <tag>': 'Add tag to commit',
' --git <git>': `Path to git executable (default: ${ghpages.defaults.git})`,
'-t, --dotfiles': 'Include dotfiles',
'-r, --repo <repo>': 'URL of the repository you are pushing to',
'-p, --depth <depth>': `Depth for clone (default: ${ghpages.defaults.depth})`,
'-o, --remote <name>': `The name of the remote (default: ${ghpages.defaults.remote})`,
'-u, --user <address>': 'The name and email of the user (defaults to the git config). Format is "Your Name <[email protected]>".',
'-v, --remove <pattern>': `Remove files that match the given pattern (ignored if used together with --add). (default: ${ghpages.defaults.only})`,
'-n, --no-push': 'Commit only (with no push)',
' --help': 'Output usage information'
}
}, async args => {
const isFunction = require('lodash.isfunction');
const pluginOptions = projectOptions.pluginOptions ? projectOptions.pluginOptions.ghPages || {} : {};
const dir = args.d || args.dist || pluginOptions.dir || projectOptions.outputDir;
const options = { ...pluginOptions };
// src
if (args.s || args.src) {
options.src = args.s || args.src;
}
// branch
if (args.b || args.branch) {
options.branch = args.b || args.branch;
}
// dest
if (args.e || args.dest) {
options.dest = args.e || args.dest;
}
// add
if (args.a || args.add) {
options.add = Boolean(args.a || args.add);
}
// silent
if (args.x || args.silent) {
options.silent = Boolean(args.x || args.silent);
}
// message
if (args.m || args.message) {
options.message = args.m || args.message;
}
if (isFunction(options.message)) {
options.message = await options.message();
}
// tag
if (args.g || args.tag) {
options.tag = args.g || args.tag;
}
if (isFunction(options.tag)) {
options.tag = await options.tag();
}
// git
if (args.git) {
options.git = args.git;
}
// dotfiles
if (args.t || args.dotfiles) {
options.dotfiles = Boolean(args.t || args.dotfiles);
}
// repo
if (args.r || args.repo) {
options.repo = args.r || args.repo;
}
// depth
if (args.p || args.depth) {
options.depth = args.p || args.depth;
}
// remote
if (args.o || args.remote) {
options.remote = args.o || args.remote;
}
// user
if (args.u || args.user) {
const addr = require('email-addresses');
const parts = addr.parseOneAddress(args.u || args.user);
if (!parts) {
throw new Error(
`Could not parse name and email from user option "${args.u || args.user}" ` +
'(format should be "Your Name <[email protected]>")'
);
}
options.user = { name: parts.name, email: parts.address };
}
// only
if (args.v || args.remove) {
options.only = args.v || args.remove;
}
// push
if (args.n) {
options.push = false;
} else if (Object.prototype.hasOwnProperty.call(args, 'push')) {
options.push = args.push;
}
await publish(api, api.resolve(dir), options);
});
};
async function publish (api, dir, options) {
const path = require('path');
const { chalk, logger, spinner } = require('@vue/cli-shared-utils');
logger.log();
const dirShort = path.relative(api.service.context, dir);
spinner.logWithSpinner(`Publishing ${chalk.cyan(dirShort)} to GitHub Pages...`);
return new Promise((resolve, reject) => {
ghpages.publish(dir, options, err => {
spinner.stopSpinner(false);
if (err) {
return reject(err);
}
if (!options.silent) {
logger.done('Published to GitHub Pages.');
}
resolve();
});
});
}