Skip to content

Commit c67adf3

Browse files
committed
:test: update
1 parent 6925eac commit c67adf3

File tree

5 files changed

+108
-7
lines changed

5 files changed

+108
-7
lines changed

libs/deploy.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const shelljs = require('shelljs');
6+
const microApp = require('@micro-app/core');
7+
const chalk = require('chalk').default;
8+
const logger = microApp.logger;
9+
10+
module.exports = () => {
11+
const microAppConfig = microApp.self();
12+
if (!microAppConfig) return;
13+
14+
const deployCfg = microAppConfig.config.deploy;
15+
if (!deployCfg || typeof deployCfg !== 'object') {
16+
logger.logo(`${chalk.yellow('need "deploy: \{\}" in "micro-app.config.js"')}`);
17+
return;
18+
}
19+
20+
const gitURL = deployCfg.git || '';
21+
if (!gitURL || typeof gitURL !== 'string') {
22+
logger.logo(`${chalk.yellow('need "deploy.git: \'ssh://...\'" in "micro-app.config.js"')}`);
23+
return;
24+
}
25+
const gitPath = gitURL.replace(/^git\+/ig, '').split('#')[0];
26+
const gitBranch = deployCfg.branch || gitURL.split('#')[1] || 'master';
27+
28+
const commitHash = ((shelljs.exec('git rev-parse --verify HEAD', { silent: true }) || {}).stdout || '').trim();
29+
30+
const gitRoot = path.resolve(microAppConfig.root, '.gittest');
31+
if (fs.statSync(gitRoot).isDirectory()) {
32+
const deployDir = path.resolve(gitRoot, 'micro-deploy');
33+
if (fs.existsSync(deployDir)) {
34+
shelljs.rm('-rf', deployDir);
35+
}
36+
fs.mkdirSync(deployDir);
37+
if (fs.statSync(deployDir).isDirectory()) {
38+
const execStr = `git clone "${gitPath}" -b ${gitBranch} "${deployDir}"`;
39+
logger.logo(`Deploy: ${chalk.blueBright(gitPath)}`);
40+
logger.logo(`Branch: ${chalk.blueBright(gitBranch)}`);
41+
const result = shelljs.exec(execStr, { silent: true });
42+
if (result.code) {
43+
logger.logo(`${result.code}: ${chalk.yellow(result.stderr.trim().split('\n').reverse()[0])}`);
44+
} else {
45+
const pkg = require(path.resolve(deployDir, 'package.json')) || {};
46+
const { dependencies = {}, devDependencies = {} } = pkg;
47+
const deps = Object.assign({}, dependencies, devDependencies);
48+
49+
const MICRO_APP_CONFIG_NAME = microAppConfig.name;
50+
if (deps[MICRO_APP_CONFIG_NAME]) {
51+
const gitp = deps[MICRO_APP_CONFIG_NAME];
52+
// update
53+
const ngitp = gitp.replace(/#[-_\d\w]+$/igm, `#${commitHash}`);
54+
55+
if (gitp === ngitp) {
56+
// not change
57+
shelljs.rm('-rf', deployDir);
58+
logger.logo(chalk.yellow('NOT MODIFIED!'));
59+
return;
60+
}
61+
if (ngitp) {
62+
if (dependencies[MICRO_APP_CONFIG_NAME]) {
63+
dependencies[MICRO_APP_CONFIG_NAME] = ngitp;
64+
}
65+
if (devDependencies[MICRO_APP_CONFIG_NAME]) {
66+
devDependencies[MICRO_APP_CONFIG_NAME] = ngitp;
67+
}
68+
fs.writeFileSync(path.resolve(deployDir, 'package.json'), JSON.stringify(pkg, null, 4), 'utf8');
69+
70+
// commit + push
71+
const { code } = shelljs.exec(`git commit -a -m "auto deploy ${MICRO_APP_CONFIG_NAME}"`, { cwd: deployDir });
72+
if (code === 0) {
73+
const { code } = shelljs.exec('git push', { cwd: deployDir });
74+
if (code === 0) {
75+
shelljs.rm('-rf', deployDir);
76+
logger.logo(chalk.green('success'));
77+
return;
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
85+
if (fs.existsSync(deployDir)) {
86+
shelljs.rm('-rf', deployDir);
87+
}
88+
logger.logo(chalk.redBright('Fail! Check your config, please'));
89+
} else {
90+
logger.logo(`${chalk.yellow('not found git')}`);
91+
}
92+
};

libs/show.js

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

33
const chalk = require('chalk').default;
4-
const microApp = require('@necfe/micro-app-core');
4+
const microApp = require('@micro-app/core');
55
const logger = microApp.logger;
66

77
module.exports = name => {

libs/update.js

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

33
const shelljs = require('shelljs');
44
const chalk = require('chalk').default;
5-
const microApp = require('@necfe/micro-app-core');
5+
const microApp = require('@micro-app/core');
66
const logger = microApp.logger;
77

88
const path = require('path');
@@ -15,6 +15,10 @@ module.exports = name => {
1515
const microConfig = microApp(name);
1616
if (microConfig) {
1717
const root = microConfig.root;
18+
if (!root.startsWith(microAppConfig.nodeModules)) {
19+
// 丢弃软链接
20+
return;
21+
}
1822

1923
const pkgInfo = microAppConfig.package;
2024
const gitPath = (pkgInfo.devDependencies && pkgInfo.devDependencies[microConfig.name]) || (pkgInfo.dependencies && pkgInfo.dependencies[microConfig.name]) || false;
@@ -34,6 +38,10 @@ module.exports = name => {
3438
const microConfig = microApp(key);
3539
if (microConfig) {
3640
const root = microConfig.root;
41+
if (!root.startsWith(microAppConfig.nodeModules)) {
42+
// 丢弃软链接
43+
return false;
44+
}
3745

3846
const pkgInfo = microAppConfig.package;
3947
const gitPath = (pkgInfo.devDependencies && pkgInfo.devDependencies[microConfig.name]) || (pkgInfo.dependencies && pkgInfo.dependencies[microConfig.name]) || false;

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/cli",
3-
"version": "0.0.15",
3+
"version": "0.0.18",
44
"description": "micro app cli",
55
"bin": {
66
"micro-app": "./bin/micro-app.js",
@@ -12,7 +12,8 @@
1212
"test": "echo \"Error: no test specified\" && exit 1"
1313
},
1414
"files": [
15-
"bin"
15+
"bin",
16+
"libs"
1617
],
1718
"keywords": [
1819
"micro",
@@ -31,7 +32,7 @@
3132
"eslint-config-2o3t": "^1.1.4"
3233
},
3334
"dependencies": {
34-
"@micro-app/core": "^0.0.16",
35+
"@micro-app/core": "^0.0.17",
3536
"chalk": "^2.4.2",
3637
"commander": "^2.19.0",
3738
"opn": "^5.4.0",

test/index.js

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

3-
const update = require('../libs/update');
4-
update('a');
3+
const deploy = require('../libs/deploy');
4+
deploy();

0 commit comments

Comments
 (0)