Skip to content

Commit 6d9b223

Browse files
committed
🎨 remove dep shelljs.
1 parent 1def8f8 commit 6d9b223

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "@micro-app/plugin-deploy-command",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "[Plugin] auto deploy command plugin.",
55
"main": "src/index.js",
66
"scripts": {
7+
"prepublishOnly": "npm run test",
78
"test": "jest"
89
},
910
"files": [
@@ -40,8 +41,5 @@
4041
"eslint": "^5.16.0",
4142
"eslint-config-2o3t": "^1.1.17",
4243
"jest": "^24.9.0"
43-
},
44-
"dependencies": {
45-
"shelljs": "^0.8.3"
4644
}
4745
}

src/commands/deploy/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Config:
7979
default:
8080
chain = chain.then(() => {
8181
logger.warn('[Deploy]', `Not Found type: ${type}!`);
82+
// TODO others type
8283
});
8384
break;
8485
}

src/commands/git/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22

3-
const shelljs = require('shelljs');
43
const path = require('path');
5-
const { fs, _, chalk } = require('@micro-app/shared-utils');
4+
const { fs, _, chalk, execa, Env } = require('@micro-app/shared-utils');
65
const CONSTANTS = require('../../constants');
7-
const { execJS, getCurrBranch, getGitBranch, getGitUser } = require('./utils');
6+
const { execGit, getCurrBranch, getGitBranch, getGitUser } = require('./utils');
87

98
function createCNAMEFile({ deployConfig, deployDir }) {
109
const cname = deployConfig.cname;
@@ -18,23 +17,22 @@ async function setup(api, { deployDir, gitUser }) {
1817
await fs.ensureDir(deployDir);
1918
await fs.emptyDir(deployDir);
2019

21-
await execJS('git init', { cwd: deployDir });
20+
await execGit([ 'init' ], { cwd: deployDir });
2221

2322
// git config
2423
if (gitUser.name && typeof gitUser.name === 'string') {
25-
await execJS(`git config user.name ${gitUser.name}`, { cwd: deployDir });
24+
await execGit([ 'config', 'user.name', gitUser.name ], { cwd: deployDir });
2625
}
2726
if (gitUser.email && typeof gitUser.email === 'string') {
28-
await execJS(`git config user.email ${gitUser.email}`, { cwd: deployDir });
27+
await execGit([ 'config', 'user.email', gitUser.email ], { cwd: deployDir });
2928
}
3029
}
3130

3231
async function clone(api, { deployDir, gitURL, gitBranch }) {
33-
const execStr = `git clone "${gitURL}" -b ${gitBranch} "${deployDir}"`;
34-
return await execJS(execStr);
32+
return await execGit([ 'clone', gitURL, '-b', gitBranch, deployDir ], { cwd: deployDir });
3533
}
3634

37-
async function gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name }) {
35+
function gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name }) {
3836
const currBranch = getCurrBranch();
3937
// commit + push
4038
const { message } = api.applyPluginHooks('modifyCommandDeployMessage', {
@@ -50,36 +48,34 @@ async function gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch,
5048
throw new Error('modifyCommandDeployMessage() must be retrun { message } !!!');
5149
}
5250

51+
let chain = Promise.resolve();
52+
5353
if (process.env.MICRO_APP_TEST) {
5454
api.logger.debug('MICRO_APP_TEST --> Exit!!!');
55-
return;
55+
return chain;
5656
}
5757

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-
}
58+
chain = chain.then(() => execGit([ 'add', '-A' ], { cwd: deployDir }));
59+
chain = chain.then(() => execGit([ 'commit', '-a', '-m', message ], { cwd: deployDir }));
60+
chain = chain.then(() => execGit([ 'push', '-u', gitURL, `HEAD:${gitBranch}`, '--force' ], { cwd: deployDir }));
6561

66-
return true;
62+
return chain;
6763
}
6864

6965
function getCommitHash(api, { isHooks, gitBranch }) {
7066
let commitHash = '';
7167
if (isHooks) {
72-
commitHash = ((shelljs.exec('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
68+
commitHash = ((execa.commandSync('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
7369
} else {
74-
commitHash = ((shelljs.exec(`git rev-parse origin/${gitBranch}`, { silent: true }) || {}).stdout || '').trim();
70+
commitHash = ((execa.commandSync(`git rev-parse origin/${gitBranch}`, { silent: true }) || {}).stdout || '').trim();
7571
}
7672
return commitHash;
7773
}
7874

7975
function getGitMessage(api, { deployConfig, commitHash }) {
8076
let gitMessage = deployConfig.message && ` | ${deployConfig.message}` || '';
8177
if (!gitMessage) {
82-
const msg = ((shelljs.exec(`git log --pretty=format:“%s” ${commitHash} -1`, { silent: true }) || {}).stdout || '').trim();
78+
const msg = ((execa.commandSync(`git log --pretty=format:“%s” ${commitHash} -1`, { silent: true }) || {}).stdout || '').trim();
8379
if (msg) {
8480
gitMessage = ` | ${msg}`;
8581
}
@@ -139,6 +135,10 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
139135
const logger = api.logger;
140136
const root = api.root;
141137

138+
if (!Env.hasGit()) {
139+
logger.throw('Sorry, this script requires git');
140+
}
141+
142142
const isHooks = args.hooks;
143143

144144
return Promise.all(deployConfigs.map(async (deployConfig, index) => {
@@ -178,8 +178,8 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
178178
}
179179

180180
if (!bSuccessful) {
181-
logger.error(`Fail [${index}]! Check your config, please!`);
182-
process.exitCode = 1;
181+
logger.error(`Fail${index && ` [${index}]`}! Check your config, please!`);
182+
process.exit(1);
183183
}
184184

185185
return params;

src/commands/git/utils.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
'use strict';
22

3-
const shelljs = require('shelljs');
4-
const { _ } = require('@micro-app/shared-utils');
3+
const { _, execa } = require('@micro-app/shared-utils');
4+
5+
const TIMEOUT = 1000 * 60 * 3;
56

67
module.exports = {
7-
execJS,
8+
execGit,
89
getCurrBranch,
910
getGitBranch,
1011
getGitUser,
1112
};
1213

13-
function execJS(execStr, options = {}) {
14+
function execGit(args, options = {}) {
1415
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-
}
16+
// 'pipe' | 'ignore' | 'inherit'
17+
execa('git', args, Object.assign({ stdio: 'ignore', timeout: TIMEOUT }, options)).then(({ stdout }) => {
18+
resolve(stdout);
19+
}).catch(e => {
20+
reject(e);
2121
});
2222
});
2323
}
2424

2525
function getCurrBranch() {
26-
const currBranch = ((shelljs.exec('git rev-parse --abbrev-ref HEAD', { silent: true }) || {}).stdout || '').trim();
26+
const currBranch = ((execa.commandSync('git rev-parse --abbrev-ref HEAD', { silent: true }) || {}).stdout || '').trim();
2727
return currBranch;
2828
}
2929

@@ -44,11 +44,11 @@ function getGitBranch(deployConfig) {
4444
function getGitUser(deployConfig) {
4545
let userName = deployConfig.userName;
4646
if (_.isEmpty(userName)) {
47-
userName = ((shelljs.exec('git config user.name', { silent: true }) || {}).stdout || '').trim();
47+
userName = ((execa.commandSync('git config user.name', { silent: true }) || {}).stdout || '').trim();
4848
}
4949
let userEmail = deployConfig.userEmail;
5050
if (_.isEmpty(userEmail)) {
51-
userEmail = ((shelljs.exec('git config user.email', { silent: true }) || {}).stdout || '').trim();
51+
userEmail = ((execa.commandSync('git config user.email', { silent: true }) || {}).stdout || '').trim();
5252
}
5353
return {
5454
name: userName || 'Git Deploy Anonymous',

src/methods/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ module.exports = function registerMethods(api, opts = {}) {
1515
description: '发布消息二次编辑事件',
1616
});
1717

18+
// TODO others type
1819
};

0 commit comments

Comments
 (0)