Skip to content

Commit 7f5e4b0

Browse files
committed
:update: 更新功能
1 parent 85f1ebd commit 7f5e4b0

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

bin/micro-app.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ program
1515
.version(require('../package').version, '-v, --version')
1616
.option('init', 'Init a config file')
1717
.option('-l, --list', 'Show micros list')
18-
.option('-s, --show <show>', 'Show alias & shared list')
19-
.option('-u, --update <update>', 'Update moicros')
18+
.option('-s, --show <name>', 'Show alias & shared list')
19+
.option('-u, --update', 'Update moicros')
2020
.option('-d, --deploy', 'Deploy current commit to container')
2121
.parse(process.argv);
2222

@@ -52,11 +52,11 @@ if (program.show) {
5252
}
5353

5454
if (program.update) {
55-
const name = program.update;
55+
const name = program.args[0] || '*';
5656
require('../libs/update')(name);
5757
}
5858

5959
if (program.deploy) {
60-
const args = {};
61-
require('../libs/deploy')(args);
60+
const bHook = program.args.includes('hooks');
61+
require('../libs/deploy')(bHook);
6262
}

libs/deploy.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const microApp = require('@micro-app/core');
77
const chalk = require('chalk').default;
88
const logger = microApp.logger;
99

10-
module.exports = args => {
10+
module.exports = isHook => {
1111
const microAppConfig = microApp.self();
1212
if (!microAppConfig) return;
1313

@@ -24,10 +24,28 @@ module.exports = args => {
2424
}
2525
const gitPath = gitURL.replace(/^git\+/ig, '').split('#')[0];
2626
const gitBranch = deployCfg.branch || gitURL.split('#')[1] || 'master';
27+
const gitMessage = deployCfg.message && ` | ${deployCfg.message}` || '';
2728

28-
const currBranch = ((shelljs.exec('git rev-parse --abbrev-ref HEAD', { silent: true }) || {}).stdout || '').trim();
29-
const commitHash = ((shelljs.exec(`git rev-parse origin/${currBranch}`, { silent: true }) || {}).stdout || '').trim();
30-
// const commitHash = ((shelljs.exec('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
29+
const gitUser = deployCfg.user || {};
30+
if (!gitUser.name || typeof gitUser.name !== 'string') {
31+
gitUser.name = ((shelljs.exec('git config user.name', { silent: true }) || {}).stdout || '').trim();
32+
}
33+
if (!gitUser.email || typeof gitUser.email !== 'string') {
34+
gitUser.email = ((shelljs.exec('git config user.email', { silent: true }) || {}).stdout || '').trim();
35+
}
36+
37+
let commitHash = '';
38+
if (isHook) {
39+
commitHash = ((shelljs.exec('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
40+
} else {
41+
const currBranch = ((shelljs.exec('git rev-parse --abbrev-ref HEAD', { silent: true }) || {}).stdout || '').trim();
42+
commitHash = ((shelljs.exec(`git rev-parse origin/${currBranch}`, { silent: true }) || {}).stdout || '').trim();
43+
}
44+
45+
if (!commitHash || typeof commitHash !== 'string') {
46+
logger.logo(`${chalk.yellow('Not Found commit Hash!')}`);
47+
return;
48+
}
3149

3250
const gitRoot = path.resolve(microAppConfig.root, '.git');
3351
if (fs.statSync(gitRoot).isDirectory()) {
@@ -41,6 +59,8 @@ module.exports = args => {
4159
logger.logo(`Deploy: ${chalk.blueBright(gitPath)}`);
4260
logger.logo(`Branch: ${chalk.blueBright(gitBranch)}`);
4361
logger.logo(`Hash: ${chalk.blueBright(commitHash)}`);
62+
logger.logo(`Name: ${chalk.blueBright(gitUser.name)}`);
63+
logger.logo(`Email: ${chalk.blueBright(gitUser.email)}`);
4464
const result = shelljs.exec(execStr, { silent: true });
4565
if (result.code) {
4666
logger.logo(`${result.code}: ${chalk.yellow(result.stderr.trim().split('\n').reverse()[0])}`);
@@ -70,8 +90,15 @@ module.exports = args => {
7090
}
7191
fs.writeFileSync(path.resolve(deployDir, 'package.json'), JSON.stringify(pkg, null, 4), 'utf8');
7292

93+
// git config
94+
if (gitUser.name && typeof gitUser.name === 'string') {
95+
shelljs.exec(`git config user.name ${gitUser.name}`, { silent: true, cwd: deployDir });
96+
}
97+
if (gitUser.email && typeof gitUser.email === 'string') {
98+
shelljs.exec(`git config user.email ${gitUser.email}`, { silent: true, cwd: deployDir });
99+
}
73100
// commit + push
74-
const { code } = shelljs.exec(`git commit -a -m "auto deploy ${MICRO_APP_CONFIG_NAME}"`, { cwd: deployDir });
101+
const { code } = shelljs.exec(`git commit -a -m ":package: auto deploy ${MICRO_APP_CONFIG_NAME} - ${commitHash.substr(0, 8)}${gitMessage}"`, { cwd: deployDir });
75102
if (code === 0) {
76103
const { code } = shelljs.exec('git push', { cwd: deployDir });
77104
if (code === 0) {

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/cli",
3-
"version": "0.0.19",
3+
"version": "0.0.24",
44
"description": "micro app cli",
55
"bin": {
66
"micro-app": "./bin/micro-app.js",

0 commit comments

Comments
 (0)