Skip to content

Commit f812ed2

Browse files
committed
feat: 优化部分配置信息逻辑,升级部分依赖
1 parent 2491143 commit f812ed2

File tree

7 files changed

+1096
-779
lines changed

7 files changed

+1096
-779
lines changed

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
"scripts": {
1010
"prepublishOnly": "npm run test",
1111
"test": "jest",
12-
"release": "micro-app release --npm.skipChecks",
12+
"deploy": "micro-app deploy",
13+
"release": "micro-app release",
1314
"release:alpha": "npm run release -- --preRelease=alpha",
14-
"release:next": "npm run release -- --preRelease=next"
15+
"release:next": "npm run release -- --preRelease=next",
16+
"release:minor": "npm run release -- minor",
17+
"release:patch": "npm run release -- patch"
1518
},
1619
"files": [
1720
"src"
@@ -40,14 +43,14 @@
4043
"@micro-app/cli": ">=0.4.0"
4144
},
4245
"devDependencies": {
43-
"@micro-app/cli": "0.4.0",
44-
"@micro-app/shared-utils": "0.1.21",
45-
"@types/jest": "^26.0.21",
46-
"eslint-config-2o3t": "^2.0.1",
46+
"@micro-app/cli": "^0.4.2",
47+
"@micro-app/shared-utils": "^0.1.25",
48+
"@types/jest": "^26.0.24",
49+
"eslint-config-2o3t": "^2.0.2",
4750
"jest": "^26.6.3"
4851
},
4952
"dependencies": {
5053
"@release-it/conventional-changelog": "^2.0.1",
51-
"release-it": "^14.4.1"
54+
"release-it": "^14.11.5"
5255
}
5356
}
Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
114114

115115
if (bModify) {
116116
spinner.text = 'Push files...';
117-
await gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name: MICRO_APP_CONFIG_NAME, currBranch });
117+
const params = { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name: MICRO_APP_CONFIG_NAME, currBranch };
118+
await gitPush(api, params);
118119
spinner.succeed(chalk.green('Success!'));
119120
} else {
120121
spinner.succeed(chalk.yellow('NOT MODIFIED!'));
@@ -126,7 +127,8 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
126127
return false;
127128
}
128129

129-
module.exports = async function deployCommit(api, args, deployConfigs) {
130+
module.exports = async function deployCommit(api, args, deployConfig) {
131+
130132
const logger = api.logger;
131133
const root = api.root;
132134

@@ -135,49 +137,46 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
135137
}
136138

137139
// TODO 迁移到外部,且不中断(分成不同组类型,批量部署)
138-
return Promise.all(deployConfigs.map(async (deployConfig, index) => {
139-
140-
const gitURL = deployConfig.url || '';
141-
if (_.isEmpty(gitURL)) {
142-
logger.warn('repository is required!');
143-
return;
144-
}
145-
const gitBranch = getGitBranch(deployConfig);
146-
if (_.isEmpty(gitBranch)) {
147-
logger.warn('branch is required!');
148-
return;
149-
}
150-
const gitUser = getGitUser(deployConfig);
140+
const gitURL = deployConfig.url || '';
141+
if (_.isEmpty(gitURL)) {
142+
logger.warn('repository is required!');
143+
return;
144+
}
145+
const gitBranch = getGitBranch(deployConfig);
146+
if (_.isEmpty(gitBranch)) {
147+
logger.warn('branch is required!');
148+
return;
149+
}
150+
const gitUser = getGitUser(deployConfig);
151151

152-
const currBranch = getCurrBranch(deployConfig);
152+
const currBranch = getCurrBranch(deployConfig);
153153

154-
const commitHash = getCommitHash();
155-
if (_.isEmpty(commitHash)) {
156-
logger.warn('Not Found commit Hash!');
157-
return;
158-
}
154+
const commitHash = getCommitHash();
155+
if (_.isEmpty(commitHash)) {
156+
logger.warn('Not Found commit Hash!');
157+
return;
158+
}
159159

160-
const gitMessage = getGitMessage(api, { deployConfig, commitHash });
160+
const gitMessage = getGitMessage(api, { deployConfig, commitHash });
161161

162-
const gitRoot = path.resolve(root, CONSTANTS.GIT_NAME);
163-
if (!fs.existsSync(gitRoot)) {
164-
fs.mkdirpSync(gitRoot);
165-
}
166-
const deployDir = path.resolve(gitRoot, CONSTANTS.GIT_SCOPE_NAME);
162+
const gitRoot = path.resolve(root, CONSTANTS.GIT_NAME);
163+
if (!fs.existsSync(gitRoot)) {
164+
fs.mkdirpSync(gitRoot);
165+
}
166+
const deployDir = path.resolve(gitRoot, CONSTANTS.GIT_SCOPE_NAME);
167167

168-
const params = { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, currBranch };
169-
const bSuccessful = await runDeploy(api, params);
168+
const params = { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, currBranch };
169+
const bSuccessful = await runDeploy(api, params);
170170

171-
// 清空
172-
if (fs.existsSync(deployDir)) {
173-
fs.removeSync(deployDir);
174-
}
171+
// 清空
172+
if (fs.existsSync(deployDir)) {
173+
fs.removeSync(deployDir);
174+
}
175175

176-
if (!bSuccessful) {
177-
logger.error(`Fail${index && ` [${index}]`}! Check your config, please!`);
178-
process.exit(1);
179-
}
176+
if (!bSuccessful) {
177+
logger.error('Fail! Check your config, please!');
178+
process.exit(1);
179+
}
180180

181-
return params;
182-
}));
181+
return params;
183182
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function getGitBranch(deployConfig) {
6060
}
6161

6262
function getGitUser(deployConfig) {
63-
let userName = deployConfig.userName;
63+
let userName = deployConfig.userName || process.env.GITHUB_ACTOR;
6464
if (!userName) {
6565
userName = execGitSync([ 'config', 'user.name' ]);
6666
}

src/deploy/index.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ module.exports = function(api, opts = {}) {
1212
usage: 'micro-app deploy [options]',
1313
options: {
1414
'-': 'deploy last commit',
15-
'--type': '部署方式类型. (default: git)',
15+
'--type': '部署方式类型. (default: github)',
1616
'--message': 'git commit message.',
1717
'--name': 'git commit user name.',
1818
'--email': 'git commit user email.',
19-
'--config <config>': '指定配置文件路径, 相对于根路径. 默认为根目录下的: "micro-app.deploy.config.js"',
19+
'--config <config>': '指定配置文件路径, 相对于根路径. 默认为: "config/deploy.js"',
2020
},
2121
details: `
2222
Examples:
@@ -28,7 +28,7 @@ Examples:
2828
Config:
2929
{
3030
disabled: false, ${chalk.gray('// 是否禁用该功能')}
31-
repository: '', ${chalk.gray('// git 地址')}
31+
repository: '', ${chalk.gray('// github 地址')}
3232
${chalk.gray('branch: \'\',')}
3333
branch: { ${chalk.gray('// git branch')}
3434
name: '',
@@ -39,51 +39,55 @@ Config:
3939
name: '',
4040
email: '',
4141
},
42-
dest: '', ${chalk.gray('// git dest')}
42+
dest: '', ${chalk.gray('// git dest path')}
4343
cname: '', ${chalk.gray('// 如果是发布到自定义域名, default: false')}
4444
}
4545
`.trim(),
4646
}, args => {
4747
const logger = api.logger;
4848

4949
const parseConfig = require('./parseConfig');
50-
const deployConfig = parseConfig(api, args, opts);
50+
let deployConfigs = parseConfig(api, args, opts);
5151

52-
if (_.isEmpty(deployConfig)) {
52+
if (_.isEmpty(deployConfigs) || deployConfigs.length <= 0) {
5353
logger.error('[Deploy]', '无法加载到 Deploy 配置信息...');
5454
return;
5555
}
5656

57-
if (deployConfig && deployConfig.disabled) {
58-
logger.info('[Deploy]', '已禁用命令行 Deploy...');
59-
return;
60-
}
57+
deployConfigs = deployConfigs.filter((deployConfig, index) => {
58+
if (!deployConfig || deployConfig.disabled) {
59+
logger.info('[Deploy]', `"索引:${index}"`, '已禁用...');
60+
return false;
61+
}
62+
return true;
63+
});
6164

62-
const deployCmds = [
65+
const deployCmds = [ // 部署支持的类型
6366
{
64-
type: 'git',
65-
run: require('./git'),
67+
type: 'github',
68+
run: require('./github'),
6669
},
6770
].concat(api.applyPluginHooks('addCommandDeployType', []) || []);
6871

69-
// default: git
70-
args.type = args.type || 'git';
71-
7272
let chain = Promise.resolve();
7373

74-
chain = chain.then(() => api.applyPluginHooks('beforeCommandDeploy', { args, config: deployConfig }));
74+
chain = chain.then(() => api.applyPluginHooks('beforeCommandDeploy', { args, config: deployConfigs }));
7575

76-
const type = args.type;
77-
const allCmds = deployCmds.filter(item => item.type === type);
78-
if (allCmds.length > 0) {
79-
allCmds.forEach(item => {
80-
const run = item.run;
81-
chain = chain.then(() => run(api, args, deployConfig));
82-
});
83-
} else {
84-
chain = chain.then(() => logger.warn('[Deploy]', `Not Found type: ${type}!`));
85-
}
86-
chain = chain.then(() => api.applyPluginHooks('afterCommandDeploy', { args, config: deployConfig }));
76+
deployConfigs.forEach(deployConfig => {
77+
// default: github
78+
const type = args.type || deployConfig.type || 'github';
79+
const allCmds = deployCmds.filter(item => item.type === type);
80+
if (allCmds.length > 0) {
81+
allCmds.forEach(item => {
82+
const run = item.run;
83+
chain = chain.then(() => run(api, args, deployConfig));
84+
});
85+
} else {
86+
chain = chain.then(() => logger.warn('[Deploy]', `Not Found type: ${type}!`));
87+
}
88+
});
89+
90+
chain = chain.then(() => api.applyPluginHooks('afterCommandDeploy', { args, config: deployConfigs }));
8791

8892
return chain.catch(e => {
8993
logger.throw('[Deploy]', e);

src/deploy/parseConfig.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ function parseConfig(deployConfig, api, args) {
3838
return null;
3939
}
4040

41-
const type = deployConfig.type || 'git';
4241
const disabled = deployConfig.disabled || deployConfig.disable || false;
4342
const message = args.m || args.msg || args.message || deployConfig.message || '';
4443

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

4847
const cname = deployConfig.cname || deployConfig.CNAME || false;
@@ -52,7 +51,7 @@ function parseConfig(deployConfig, api, args) {
5251
const dist = dest || args.dist || deployConfig.dist || false;
5352

5453
const _otherConfig = {
55-
type, disabled,
54+
disabled,
5655
message,
5756
userName,
5857
userEmail,
@@ -71,7 +70,7 @@ function parseConfig(deployConfig, api, args) {
7170
}
7271
data.branch = branch;
7372
data.extends = _.get(deployConfig, 'branch.extends') || false;
74-
return Object.assign(data, _otherConfig);
73+
return Object.assign({}, deployConfig, _otherConfig, data);
7574
}
7675

7776
module.exports = function(api, args, opts) {
@@ -92,7 +91,7 @@ module.exports = function(api, args, opts) {
9291
const result = deployConfig.map(item => {
9392
const _config = Object.assign({}, opts, item || {});
9493
if (_.isEmpty(_config)) return null;
95-
return parseConfig(item, api, args);
94+
return parseConfig(_config, api, args);
9695
}).filter(item => !!item);
9796
if (_.isEmpty(result)) return null;
9897
return result;

0 commit comments

Comments
 (0)