Skip to content

Commit 1def8f8

Browse files
committed
:feat: 增强逻辑
1 parent 7eaf7b1 commit 1def8f8

File tree

7 files changed

+117
-83
lines changed

7 files changed

+117
-83
lines changed

micro-app.deploy.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ module.exports = {
1212
name: '',
1313
email: '',
1414
},
15-
dist: 'test/gitinfo',
15+
dest: 'test/gitinfo',
1616
// cname: 'www.2o3t.cn',
1717
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/plugin-deploy-command",
3-
"version": "0.1.2",
3+
"version": "0.1.4",
44
"description": "[Plugin] auto deploy command plugin.",
55
"main": "src/index.js",
66
"scripts": {

src/commands/deploy/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Config:
3939
name: '',
4040
email: '',
4141
},
42-
dist: '', ${chalk.gray('// git dist')}
42+
dest: '', ${chalk.gray('// git dest')}
4343
cname: '', ${chalk.gray('// 如果是发布到自定义域名, default: false')}
4444
}
4545
`.trim(),

src/commands/deploy/parseConfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ function parseConfig(deployConfig, api, args) {
4242
const disabled = deployConfig.disabled || deployConfig.disable || false;
4343
const message = args.m || args.msg || args.message || deployConfig.message || '';
4444

45-
const userName = args.name || args.userName || _.get(deployConfig, 'user.name') || '';
45+
const userName = args.name || args.userName || _.get(deployConfig, 'user.name') || process.env.GITHUB_ACTOR || '';
4646
const userEmail = args.email || args.userEmail || _.get(deployConfig, 'user.email') || '';
4747

48-
const cname = deployConfig.cname || false;
48+
const cname = deployConfig.cname || deployConfig.CNAME || false;
4949

5050
// dest,dist 相同
5151
const dest = args.dest || deployConfig.dest || false;

src/commands/git/index.js

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,7 @@ const shelljs = require('shelljs');
44
const path = require('path');
55
const { fs, _, chalk } = require('@micro-app/shared-utils');
66
const CONSTANTS = require('../../constants');
7-
8-
function execJS(execStr, options = {}) {
9-
return new Promise((resolve, reject) => {
10-
shelljs.exec(execStr, Object.assign({ silent: true, async: true, stdio: 'inherit' }, options), function(code, stdout, stderr) {
11-
if (code && stderr) {
12-
reject(new Error(stderr));
13-
} else {
14-
resolve(stdout);
15-
}
16-
});
17-
});
18-
}
19-
20-
function getGitBranch(deployConfig) {
21-
let gitBranch = deployConfig.branch || 'master';
22-
const currBranch = ((shelljs.exec('git rev-parse --abbrev-ref HEAD', { silent: true }) || {}).stdout || '').trim();
23-
// 继承当前分支
24-
if (currBranch && deployConfig.extends === true) {
25-
gitBranch = currBranch;
26-
} else if (deployConfig.branch) {
27-
gitBranch = deployConfig.branch;
28-
} else {
29-
gitBranch = 'master';
30-
}
31-
return gitBranch;
32-
}
33-
34-
function getGitUser(deployConfig) {
35-
let userName = deployConfig.userName;
36-
if (_.isEmpty(userName)) {
37-
userName = ((shelljs.exec('git config user.name', { silent: true }) || {}).stdout || '').trim();
38-
}
39-
let userEmail = deployConfig.userEmail;
40-
if (_.isEmpty(userEmail)) {
41-
userEmail = ((shelljs.exec('git config user.email', { silent: true }) || {}).stdout || '').trim();
42-
}
43-
return {
44-
name: userName || '',
45-
email: userEmail || '',
46-
};
47-
}
7+
const { execJS, getCurrBranch, getGitBranch, getGitUser } = require('./utils');
488

499
function createCNAMEFile({ deployConfig, deployDir }) {
5010
const cname = deployConfig.cname;
@@ -54,23 +14,32 @@ function createCNAMEFile({ deployConfig, deployDir }) {
5414
}
5515
}
5616

57-
async function clone(api, { deployDir, gitURL, gitBranch }) {
58-
const execStr = `git clone "${gitURL}" -b ${gitBranch} "${deployDir}"`;
59-
return await execJS(execStr);
60-
}
17+
async function setup(api, { deployDir, gitUser }) {
18+
await fs.ensureDir(deployDir);
19+
await fs.emptyDir(deployDir);
20+
21+
await execJS('git init', { cwd: deployDir });
6122

62-
async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name }) {
6323
// git config
6424
if (gitUser.name && typeof gitUser.name === 'string') {
6525
await execJS(`git config user.name ${gitUser.name}`, { cwd: deployDir });
6626
}
6727
if (gitUser.email && typeof gitUser.email === 'string') {
6828
await execJS(`git config user.email ${gitUser.email}`, { cwd: deployDir });
6929
}
30+
}
31+
32+
async function clone(api, { deployDir, gitURL, gitBranch }) {
33+
const execStr = `git clone "${gitURL}" -b ${gitBranch} "${deployDir}"`;
34+
return await execJS(execStr);
35+
}
36+
37+
async function gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name }) {
38+
const currBranch = getCurrBranch();
7039
// commit + push
7140
const { message } = api.applyPluginHooks('modifyCommandDeployMessage', {
7241
args, config: deployConfig,
73-
message: `:rocket: auto deploy from ${name} [${gitBranch}] - ${commitHash.substr(0, 8)}${gitMessage}`,
42+
message: `:rocket: auto deploy from ${name} [${currBranch || gitBranch}] - ${commitHash.substr(0, 8)}${gitMessage}`,
7443
branch: gitBranch,
7544
gitMessage,
7645
commitHash,
@@ -86,17 +55,36 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
8655
return;
8756
}
8857

89-
await execJS('git add -A', { cwd: deployDir });
90-
await execJS(`git commit -a -m "${message}"`, { cwd: deployDir });
91-
await execJS(`git push -u "${gitURL}" HEAD:${gitBranch} --force`, { cwd: deployDir });
58+
try {
59+
await execJS('git add -A', { cwd: deployDir });
60+
await execJS(`git commit -a -m "${message}"`, { cwd: deployDir });
61+
await execJS(`git push -u "${gitURL}" HEAD:${gitBranch} --force`, { cwd: deployDir });
62+
} catch (error) {
63+
throw error;
64+
}
65+
66+
return true;
9267
}
9368

94-
async function setup(deployDir) {
95-
await fs.ensureDir(deployDir);
96-
await fs.emptyDir(deployDir);
69+
function getCommitHash(api, { isHooks, gitBranch }) {
70+
let commitHash = '';
71+
if (isHooks) {
72+
commitHash = ((shelljs.exec('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
73+
} else {
74+
commitHash = ((shelljs.exec(`git rev-parse origin/${gitBranch}`, { silent: true }) || {}).stdout || '').trim();
75+
}
76+
return commitHash;
77+
}
9778

98-
const execStr = 'git init';
99-
return await execJS(execStr, { cwd: deployDir });
79+
function getGitMessage(api, { deployConfig, commitHash }) {
80+
let gitMessage = deployConfig.message && ` | ${deployConfig.message}` || '';
81+
if (!gitMessage) {
82+
const msg = ((shelljs.exec(`git log --pretty=format:“%s” ${commitHash} -1`, { silent: true }) || {}).stdout || '').trim();
83+
if (msg) {
84+
gitMessage = ` | ${msg}`;
85+
}
86+
}
87+
return gitMessage;
10088
}
10189

10290
async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage }) {
@@ -113,7 +101,8 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
113101
const spinner = logger.spinner('Auto Deploy...');
114102
spinner.start();
115103
try {
116-
await setup(deployDir);
104+
await setup(api, { deployDir, gitUser });
105+
117106
const hasDist = deployConfig.dist;
118107
let bModify = false;
119108
if (!hasDist) { // 需要 clone, 且自动修改 package.json
@@ -134,7 +123,7 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
134123

135124
if (bModify) {
136125
spinner.text = 'Push files...';
137-
await push(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name: MICRO_APP_CONFIG_NAME });
126+
await gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name: MICRO_APP_CONFIG_NAME });
138127
spinner.succeed(chalk.green('Success!'));
139128
} else {
140129
spinner.succeed(chalk.yellow('NOT MODIFIED!'));
@@ -166,24 +155,13 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
166155
}
167156
const gitUser = getGitUser(deployConfig);
168157

169-
let commitHash = '';
170-
if (isHooks) {
171-
commitHash = ((shelljs.exec('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
172-
} else {
173-
commitHash = ((shelljs.exec(`git rev-parse origin/${gitBranch}`, { silent: true }) || {}).stdout || '').trim();
174-
}
158+
const commitHash = getCommitHash(api, { isHooks, gitBranch });
175159
if (_.isEmpty(commitHash)) {
176160
logger.warn('Not Found commit Hash!');
177161
return;
178162
}
179163

180-
let gitMessage = deployConfig.message && ` | ${deployConfig.message}` || '';
181-
if (!gitMessage) {
182-
const msg = ((shelljs.exec(`git log --pretty=format:“%s” ${commitHash} -1`, { silent: true }) || {}).stdout || '').trim();
183-
if (msg) {
184-
gitMessage = ` | ${msg}`;
185-
}
186-
}
164+
const gitMessage = getGitMessage(api, { deployConfig, commitHash });
187165

188166
const gitRoot = path.resolve(root, CONSTANTS.GIT_NAME);
189167
if (!fs.existsSync(gitRoot)) {
@@ -192,19 +170,18 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
192170
const deployDir = path.resolve(gitRoot, CONSTANTS.GIT_SCOPE_NAME);
193171

194172
const params = { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage };
195-
196-
if (fs.statSync(gitRoot).isDirectory()) {
197-
const bSuccessful = await runDeploy(api, params);
198-
if (!bSuccessful) {
199-
logger.error(`Fail [${index}]! Check your config, please!`);
200-
}
201-
}
173+
const bSuccessful = await runDeploy(api, params);
202174

203175
// 清空
204176
if (fs.existsSync(deployDir)) {
205177
fs.removeSync(deployDir);
206178
}
207179

180+
if (!bSuccessful) {
181+
logger.error(`Fail [${index}]! Check your config, please!`);
182+
process.exitCode = 1;
183+
}
184+
208185
return params;
209186
}));
210187
};

src/commands/git/utils.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const shelljs = require('shelljs');
4+
const { _ } = require('@micro-app/shared-utils');
5+
6+
module.exports = {
7+
execJS,
8+
getCurrBranch,
9+
getGitBranch,
10+
getGitUser,
11+
};
12+
13+
function execJS(execStr, options = {}) {
14+
return new Promise((resolve, reject) => {
15+
shelljs.exec(execStr, Object.assign({ silent: true, async: true, stdio: 'inherit' }, options), function(code, stdout, stderr) {
16+
if (code && stderr) {
17+
reject(new Error(stderr));
18+
} else {
19+
resolve(stdout);
20+
}
21+
});
22+
});
23+
}
24+
25+
function getCurrBranch() {
26+
const currBranch = ((shelljs.exec('git rev-parse --abbrev-ref HEAD', { silent: true }) || {}).stdout || '').trim();
27+
return currBranch;
28+
}
29+
30+
function getGitBranch(deployConfig) {
31+
let gitBranch = deployConfig.branch || 'master';
32+
const currBranch = getCurrBranch();
33+
// 继承当前分支
34+
if (currBranch && deployConfig.extends === true) {
35+
gitBranch = currBranch;
36+
} else if (deployConfig.branch) {
37+
gitBranch = deployConfig.branch;
38+
} else {
39+
gitBranch = 'master';
40+
}
41+
return gitBranch;
42+
}
43+
44+
function getGitUser(deployConfig) {
45+
let userName = deployConfig.userName;
46+
if (_.isEmpty(userName)) {
47+
userName = ((shelljs.exec('git config user.name', { silent: true }) || {}).stdout || '').trim();
48+
}
49+
let userEmail = deployConfig.userEmail;
50+
if (_.isEmpty(userEmail)) {
51+
userEmail = ((shelljs.exec('git config user.email', { silent: true }) || {}).stdout || '').trim();
52+
}
53+
return {
54+
name: userName || 'Git Deploy Anonymous',
55+
email: userEmail || '[email protected]',
56+
};
57+
}

src/constants/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
module.exports = {
44
NAME: 'deploy',
55
GIT_NAME: '.git',
6-
GIT_SCOPE_NAME: '.gitMicroDeployCache',
6+
GIT_SCOPE_NAME: '.tempMicroAppDeployCache',
77
};

0 commit comments

Comments
 (0)