fix(push): 支持提交被忽略的 Java 模板产物
- 强制暂存 templates 下当前应用的构建产物 - 限制差异检测和提交范围,避免影响 Java 仓库其他文件 - 增加 Java 模板目标路径测试和使用说明master
parent
240caf3c66
commit
8d92f20aa0
|
|
@ -68,6 +68,8 @@ start/src/main/resources/templates/<appIdentifier>.html
|
|||
start/src/main/resources/templates/<appIdentifier>/
|
||||
```
|
||||
|
||||
工具只暂存和提交当前应用在 `templates` 下的目录及 HTML,不会提交 Java 仓库其他路径的文件。
|
||||
|
||||
## 发布到 npm Registry
|
||||
|
||||
登录 Registry:
|
||||
|
|
|
|||
36
src/push.js
36
src/push.js
|
|
@ -7,8 +7,9 @@ const { ensureYarnAvailable, runYarn } = require('./package-manager');
|
|||
|
||||
const DEFAULT_BRANCH = 'master';
|
||||
const CONFIG_FILE_NAME = 'jjb.config.js';
|
||||
const JAVA_TEMPLATE_PATH_PARTS = ['start', 'src', 'main', 'resources', 'templates'];
|
||||
// 前端构建产物在 Java 仓库中的固定部署目录。
|
||||
const JAVA_TEMPLATE_PATH = path.join('start', 'src', 'main', 'resources', 'templates');
|
||||
const JAVA_TEMPLATE_PATH = path.join(...JAVA_TEMPLATE_PATH_PARTS);
|
||||
|
||||
function run(command, args, cwd) {
|
||||
// Git 命令同步执行并继承终端输出,失败时由 execFileSync 直接抛出异常。
|
||||
|
|
@ -94,6 +95,13 @@ function resolveBuildScript(buildScripts, environment) {
|
|||
return buildScript;
|
||||
}
|
||||
|
||||
function getJavaTargetPaths(appIdentifier) {
|
||||
return [
|
||||
path.posix.join(...JAVA_TEMPLATE_PATH_PARTS, appIdentifier),
|
||||
path.posix.join(...JAVA_TEMPLATE_PATH_PARTS, `${appIdentifier}.html`),
|
||||
];
|
||||
}
|
||||
|
||||
function ensureDependencies(projectRoot) {
|
||||
// 业务项目没有 node_modules 时才安装依赖,并且始终使用 Yarn。
|
||||
if (fs.existsSync(path.join(projectRoot, 'node_modules'))) {
|
||||
|
|
@ -190,9 +198,17 @@ async function pushJava({ environment: environmentArgument } = {}) {
|
|||
// 第 6 步:把前端 dist 产物复制到 Java 仓库的 resources/templates 目录。
|
||||
copyBuildOutput(projectRoot, javaRepository, config.appIdentifier);
|
||||
|
||||
// 第 7 步:下面所有 Git 命令都在临时 Java 仓库中执行。
|
||||
run('git', ['add', '.'], javaRepository);
|
||||
const changedFiles = childProcess.execFileSync('git', ['status', '--porcelain'], {
|
||||
// 第 7 步:-f 允许暂存被 Java 仓库 .gitignore 忽略的模板,-A 同时记录新增、修改和删除。
|
||||
const javaTargetPaths = getJavaTargetPaths(config.appIdentifier);
|
||||
run('git', ['add', '-A', '-f', '--', ...javaTargetPaths], javaRepository);
|
||||
// 只检查当前应用在 templates 下的暂存内容,不处理 Java 仓库其他路径。
|
||||
const changedFiles = childProcess.execFileSync('git', [
|
||||
'diff',
|
||||
'--cached',
|
||||
'--name-only',
|
||||
'--',
|
||||
...javaTargetPaths,
|
||||
], {
|
||||
cwd: javaRepository,
|
||||
encoding: 'utf8',
|
||||
}).trim();
|
||||
|
|
@ -206,8 +222,17 @@ async function pushJava({ environment: environmentArgument } = {}) {
|
|||
const defaultMessage = `zy-gbs-cmd: update ${config.appIdentifier}`;
|
||||
const message = (await input.question(`请输入提交信息 (${defaultMessage}): `)).trim()
|
||||
|| defaultMessage;
|
||||
// --only 再次限制提交范围,确保 Java 仓库其他路径不会进入本次提交。
|
||||
// Git 认证和提交作者均由本机 Git 配置提供,工具本身不保存凭据或写死 author。
|
||||
run('git', ['commit', '-m', message, '--no-verify'], javaRepository);
|
||||
run('git', [
|
||||
'commit',
|
||||
'-m',
|
||||
message,
|
||||
'--no-verify',
|
||||
'--only',
|
||||
'--',
|
||||
...javaTargetPaths,
|
||||
], javaRepository);
|
||||
run('git', ['push'], javaRepository);
|
||||
console.log('代码推送完成。');
|
||||
} finally {
|
||||
|
|
@ -225,3 +250,4 @@ async function pushJava({ environment: environmentArgument } = {}) {
|
|||
|
||||
module.exports = pushJava;
|
||||
module.exports.resolveBuildScript = resolveBuildScript;
|
||||
module.exports.getJavaTargetPaths = getJavaTargetPaths;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
const assert = require('assert');
|
||||
const { resolveBuildScript } = require('../src/push');
|
||||
const { getJavaTargetPaths, resolveBuildScript } = require('../src/push');
|
||||
|
||||
const buildScripts = ['build', 'build:development', 'build:production'];
|
||||
|
||||
|
|
@ -9,3 +9,8 @@ assert.throws(
|
|||
() => resolveBuildScript(buildScripts, 'test'),
|
||||
/package.json 中没有构建脚本 build:test/
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(getJavaTargetPaths('basicInfo'), [
|
||||
'start/src/main/resources/templates/basicInfo',
|
||||
'start/src/main/resources/templates/basicInfo.html',
|
||||
]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue