Skip to content

Commit 7eaf7b1

Browse files
committed
:feat: 增强 git 功能
1 parent 0f44816 commit 7eaf7b1

File tree

12 files changed

+862
-160
lines changed

12 files changed

+862
-160
lines changed

.circleci/config.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

micro-app.deploy.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = {
44
disabled: false,
5-
git: '[email protected]:MicroAppJS/MicroApp-Plugin-Deploy-Command.git',
5+
git: '[email protected]:MicroAppJS/MicroApp-Plugin-Deploy-Command.git', // repository,repo
66
branch: {
77
name: 'gh-pages',
88
// extends: true,

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/plugin-deploy-command",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "[Plugin] auto deploy command plugin.",
55
"main": "src/index.js",
66
"scripts": {
@@ -30,11 +30,10 @@
3030
},
3131
"license": "MIT",
3232
"peerDependencies": {
33-
"@micro-app/core": ">=0.2.2"
33+
"@micro-app/cli": ">=0.3.0"
3434
},
3535
"devDependencies": {
36-
"@micro-app/cli": "0.2.0",
37-
"@micro-app/core": "0.2.2",
36+
"@micro-app/cli": "^0.3.0-alpha.2",
3837
"@types/jest": "^24.0.18",
3938
"babel-eslint": "^10.0.3",
4039
"coveralls": "^3.0.6",

src/commands/deploy/deploy.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* global expect */
44

5-
process.env.NODE_ENV = 'MICRO_APP_TEST';
5+
process.env.MICRO_APP_TEST = 'true';
66

77
const path = require('path');
88

src/commands/deploy/index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = function(api, opts = {}) {
99
usage: 'micro-app deploy [options]',
1010
options: {
1111
'-': 'deploy last commit',
12+
'--type': '部署方式类型. (default: github)',
1213
'--message': 'git commit message.',
1314
'--name': 'git commit user name.',
1415
'--email': 'git commit user email.',
@@ -42,30 +43,49 @@ Config:
4243
cname: '', ${chalk.gray('// 如果是发布到自定义域名, default: false')}
4344
}
4445
`.trim(),
45-
}, async args => {
46+
}, args => {
4647
const _ = require('lodash');
4748
const logger = api.logger;
4849

4950
const parseConfig = require('./parseConfig');
5051
const deployConfig = parseConfig(api, args, opts);
5152

5253
if (_.isEmpty(deployConfig)) {
53-
logger.error('无法加载到 Deploy 配置信息...');
54+
logger.error('[Deploy]', '无法加载到 Deploy 配置信息...');
5455
return;
5556
}
5657

5758
if (deployConfig && deployConfig.disabled) {
58-
logger.info('已禁用命令行 Deploy...');
59+
logger.info('[Deploy]', '已禁用命令行 Deploy...');
5960
return;
6061
}
6162

62-
api.applyPluginHooks('beforeCommandDeploy', { args, config: deployConfig });
63+
// default: github
64+
args.type = args.type || 'git';
6365

64-
const deployCommit = require('./deployCommit');
65-
const result = await deployCommit(api, args, deployConfig);
66+
let chain = Promise.resolve();
6667

67-
api.applyPluginHooks('afterCommandDeploy', { args, config: deployConfig });
68-
return result;
68+
chain = chain.then(() => api.applyPluginHooks('beforeCommandDeploy', { args, config: deployConfig }));
69+
70+
const type = args.type;
71+
switch (type) {
72+
case 'git':
73+
case 'github':
74+
{
75+
const gitCMD = require('../git');
76+
chain = chain.then(() => gitCMD(api, args, deployConfig));
77+
break;
78+
}
79+
default:
80+
chain = chain.then(() => {
81+
logger.warn('[Deploy]', `Not Found type: ${type}!`);
82+
});
83+
break;
84+
}
85+
86+
chain = chain.then(() => api.applyPluginHooks('afterCommandDeploy', { args, config: deployConfig }));
87+
88+
return chain;
6989
});
7090

7191
};

src/commands/deploy/parseConfig.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ function parseConfig(deployConfig, api, args) {
4646
const userEmail = args.email || args.userEmail || _.get(deployConfig, 'user.email') || '';
4747

4848
const cname = deployConfig.cname || false;
49-
const dist = args.dist || deployConfig.dist || false;
49+
50+
// dest,dist 相同
51+
const dest = args.dest || deployConfig.dest || false;
52+
const dist = dest || args.dist || deployConfig.dist || false;
5053

5154
const _otherConfig = {
5255
type, disabled,

src/commands/deploy/deployCommit.js renamed to src/commands/git/index.js

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const shelljs = require('shelljs');
44
const path = require('path');
5-
const { fs, _, chalk, tryRequire } = require('@micro-app/shared-utils');
5+
const { fs, _, chalk } = require('@micro-app/shared-utils');
66
const CONSTANTS = require('../../constants');
77

88
function execJS(execStr, options = {}) {
@@ -54,40 +54,6 @@ function createCNAMEFile({ deployConfig, deployDir }) {
5454
}
5555
}
5656

57-
function modifyFile(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage }) {
58-
const logger = api.logger;
59-
const microAppConfig = api.self;
60-
61-
const pkgPath = path.resolve(deployDir, 'package.json');
62-
const pkg = tryRequire(pkgPath) || {};
63-
const { dependencies = {}, devDependencies = {} } = pkg;
64-
const deps = Object.assign({}, dependencies, devDependencies);
65-
66-
const MICRO_APP_CONFIG_NAME = microAppConfig.packageName;
67-
if (deps[MICRO_APP_CONFIG_NAME]) {
68-
const gitp = deps[MICRO_APP_CONFIG_NAME];
69-
// update
70-
const ngitp = gitp.replace(/#[-_\d\w]+$/igm, `#${commitHash}`);
71-
72-
if (gitp === ngitp) { // not change
73-
return false;
74-
}
75-
if (ngitp) {
76-
if (dependencies[MICRO_APP_CONFIG_NAME]) {
77-
dependencies[MICRO_APP_CONFIG_NAME] = ngitp;
78-
}
79-
if (devDependencies[MICRO_APP_CONFIG_NAME]) {
80-
devDependencies[MICRO_APP_CONFIG_NAME] = ngitp;
81-
}
82-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4), 'utf8');
83-
84-
return true;
85-
}
86-
}
87-
88-
return false;
89-
}
90-
9157
async function clone(api, { deployDir, gitURL, gitBranch }) {
9258
const execStr = `git clone "${gitURL}" -b ${gitBranch} "${deployDir}"`;
9359
return await execJS(execStr);
@@ -115,7 +81,7 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
11581
throw new Error('modifyCommandDeployMessage() must be retrun { message } !!!');
11682
}
11783

118-
if (process.env.NODE_ENV === 'MICRO_APP_TEST') {
84+
if (process.env.MICRO_APP_TEST) {
11985
api.logger.debug('MICRO_APP_TEST --> Exit!!!');
12086
return;
12187
}
@@ -126,9 +92,7 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
12692
}
12793

12894
async function setup(deployDir) {
129-
if (!fs.existsSync(deployDir)) {
130-
fs.mkdirpSync(deployDir);
131-
}
95+
await fs.ensureDir(deployDir);
13296
await fs.emptyDir(deployDir);
13397

13498
const execStr = 'git init';
@@ -152,10 +116,11 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
152116
await setup(deployDir);
153117
const hasDist = deployConfig.dist;
154118
let bModify = false;
155-
if (!hasDist) { // 需要 clone
119+
if (!hasDist) { // 需要 clone, 且自动修改 package.json
156120
spinner.text = 'Cloning...';
157121
await clone(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser });
158122
spinner.text = 'Modify files...';
123+
const modifyFile = require('./modifyFile');
159124
bModify = modifyFile(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage });
160125
} else { // copy dist to deployDir
161126
const opts = {};
@@ -233,10 +198,11 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
233198
if (!bSuccessful) {
234199
logger.error(`Fail [${index}]! Check your config, please!`);
235200
}
236-
// 清空
237-
if (fs.existsSync(deployDir)) {
238-
fs.removeSync(deployDir);
239-
}
201+
}
202+
203+
// 清空
204+
if (fs.existsSync(deployDir)) {
205+
fs.removeSync(deployDir);
240206
}
241207

242208
return params;

src/commands/git/modifyFile.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const { fs, tryRequire } = require('@micro-app/shared-utils');
5+
6+
module.exports = modifyFile;
7+
8+
// TODO 需要匹配新版本
9+
function modifyFile(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage }) {
10+
const logger = api.logger;
11+
const microAppConfig = api.self;
12+
13+
const pkgPath = path.resolve(deployDir, 'package.json');
14+
logger.info('[Deploy]', `modify file "${pkgPath}"`);
15+
16+
const pkg = tryRequire(pkgPath) || {};
17+
const { dependencies = {}, devDependencies = {} } = pkg;
18+
const deps = Object.assign({}, dependencies, devDependencies);
19+
20+
const MICRO_APP_CONFIG_NAME = microAppConfig.packageName;
21+
if (deps[MICRO_APP_CONFIG_NAME]) {
22+
const gitp = deps[MICRO_APP_CONFIG_NAME];
23+
// update
24+
const ngitp = gitp.replace(/#[-_\d\w]+$/igm, `#${commitHash}`);
25+
26+
if (gitp === ngitp) { // not change
27+
return false;
28+
}
29+
if (ngitp) {
30+
if (dependencies[MICRO_APP_CONFIG_NAME]) {
31+
dependencies[MICRO_APP_CONFIG_NAME] = ngitp;
32+
}
33+
if (devDependencies[MICRO_APP_CONFIG_NAME]) {
34+
devDependencies[MICRO_APP_CONFIG_NAME] = ngitp;
35+
}
36+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4), 'utf8');
37+
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
}

src/commands/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const commands = [
4-
'version',
54
'deploy',
65
];
76

src/commands/version/index.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)