|
| 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 | +}; |
0 commit comments