云
10 小时以前 1f6a660180d4e6fccd1ec83a27b79ea93a121817
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '../..');
const front = path.join(root, 'mom-pro2-before');
const ts = require(path.join(front, 'node_modules/typescript'));
const changes = [];
const priorChanges = JSON.parse(fs.readFileSync(path.join(__dirname,'changes.json'),'utf8'));
function walk(dir) {
  return fs.readdirSync(dir, {withFileTypes:true}).flatMap(e => e.isDirectory() ? walk(path.join(dir,e.name)) : [path.join(dir,e.name)]);
}
function save(file, before, after, removed) {
  if (before === after) return;
  changes.push({file:path.relative(root,file), removed, before, after});
  const current=fs.readFileSync(file,'utf8');
  const prior=priorChanges.find(x=>x.file===path.relative(root,file));
  if (current!==before && current!==after && (!prior||current!==prior.after)) throw new Error('File changed concurrently: '+file);
  if (current!==after) fs.writeFileSync(file,after);
}
for (const file of walk(path.join(front,'src')).filter(f => f.endsWith('.ts'))) {
  const initial = fs.readFileSync(path.join(__dirname,'changes.json'),'utf8');
  const prior = JSON.parse(initial).find(x=>x.file===path.relative(root,file));
  const current = fs.readFileSync(file,'utf8');
  const before = prior ? prior.before : current;
  if (!/clientCode|MD_CLIENT_CODE|客户编码/.test(before)) continue;
  const source = ts.createSourceFile(file,before,ts.ScriptTarget.Latest,true);
  const spans=[];
  function remove(node) {
    let start=node.getStart(source), end=node.end;
    const lineStart=before.lastIndexOf('\n',start-1)+1;
    if (!before.slice(lineStart,start).trim()) start=lineStart;
    if (ts.isPropertySignature(node)||ts.isPropertyAssignment(node)) {
      const lineEnd=before.indexOf('\n',end);
      if (lineEnd!==-1) end=lineEnd+1;
    } else {
      if (before[end]===',') end++;
      if (/^[ \t\r]*\n/.test(before.slice(end))) end+=before.slice(end).match(/^[ \t\r]*\n/)[0].length;
    }
    spans.push([start,end]);
  }
  function visit(node) {
    if (ts.isObjectLiteralExpression(node)) {
      const props = new Map(node.properties.filter(ts.isPropertyAssignment).map(p=>[p.name.getText(source).replace(/['"]/g,''),p.initializer]));
      const value = key => {const n=props.get(key);return n && ts.isStringLiteral(n) ? n.text : null;};
      if (value('field')==='clientCode' || value('fieldName')==='clientCode' ||
          ((value('field')==='code'||value('fieldName')==='code') && (value('title')==='客户编码'||value('label')==='客户编码'))) {
        remove(node);return;
      }
    }
    if (ts.isPropertySignature(node) && node.name.getText(source)==='clientCode') {remove(node);return;}
    if (ts.isPropertyAssignment(node) && node.name.getText(source)==='MD_CLIENT_CODE') {remove(node);return;}
    if (file.endsWith(path.join('md','client','index.ts'))) {
      if (ts.isPropertySignature(node) && node.name.getText(source)==='code') {remove(node);return;}
    }
    ts.forEachChild(node,visit);
  }
  visit(source);
  let after=before;
  for (const [start,end] of spans.sort((a,b)=>b[0]-a[0])) after=after.slice(0,start)+after.slice(end);
  if (file.endsWith(path.join('md','client','index.ts'))) after=after.replace(/createCodes/g,'createCustomerNames').replace(/updateCodes/g,'updateCustomerNames').replace(/failureCodes/g,'failureCustomerNames').replace(/客户编码/g,'客户名称');
  if (ts.createSourceFile(file,after,ts.ScriptTarget.Latest,true).parseDiagnostics.length) throw new Error('Invalid TypeScript: '+file);
  save(file,before,after,spans.length);
}
const selectFile=path.join(front,'src/views/mes/md/client/components/select.vue');
const selectPrior=JSON.parse(fs.readFileSync(path.join(__dirname,'changes.json'),'utf8')).find(x=>x.file===path.relative(root,selectFile));
const selectBefore=selectPrior ? selectPrior.before : fs.readFileSync(selectFile,'utf8');
const selectAfter=selectBefore.replace(/^\s*<div>编码:\{\{ selectedItem\.code \|\| '-' \}\}<\/div>\r?\n/m,'');
if (selectBefore===selectAfter) throw new Error('Missing customer selector tooltip');
save(selectFile,selectBefore,selectAfter,1);
const javaRoot=path.join(root,'yudao-module-mes/src/main/java');
for (const file of walk(javaRoot).filter(f=>f.endsWith('.java'))) {
  const prior=JSON.parse(fs.readFileSync(path.join(__dirname,'changes.json'),'utf8')).find(x=>x.file===path.relative(root,file));
  const current=fs.readFileSync(file,'utf8');
  const before=prior ? prior.before : current;
  if (!before.includes('clientCode')&&!before.includes('MD_CLIENT_CODE')) continue;
  const property=/(?:    \/\*\*(?:(?!\*\/)[\s\S])*\*\/\r?\n)?(?:    @(?:Schema|ExcelProperty)[^\r\n]*\r?\n)*    private String clientCode;\r?\n/g;
  let after=before.replace(property,'');
  after=after.replace(/^    MD_CLIENT_CODE\("MD_CLIENT_CODE", "客户编码"\),\r?\n/m,'');
  after=after.replace(/^    ErrorCode MD_CLIENT_CODE_DUPLICATE = [^\r\n]*\r?\n/m,'');
  if (after.includes('clientCode')) throw new Error('Unremoved reference: '+file);
  save(file,before,after,1);
}
fs.writeFileSync(path.join(__dirname,'changes.json'),JSON.stringify(changes,null,2));
console.log(JSON.stringify({changedFiles:changes.length,files:changes.map(x=>x.file)},null,2));