very-express 0.5.26 → 0.6.0-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +36 -37
- package/.gitattributes +2 -2
- package/.github/copilot-instructions.md +107 -84
- package/.github/workflows/npm-start.yml +23 -23
- package/LICENSE +21 -21
- package/README.md +75 -75
- package/dist/index.js +1040 -1149
- package/dist/templates/_controllers/_ControllerFactory.ts +7 -3
- package/dist/templates/_middlewares/Authentication.ts +1 -5
- package/dist/templates/_middlewares/tsoaAuthentication.ts +24 -0
- package/dist/templates/_projectSettings/env +25 -24
- package/dist/templates/_projectSettings/package.json +50 -54
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +48 -0
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +59 -0
- package/dist/templates/_services/VexDb.ts +14 -0
- package/dist/templates/_services/VexDbConnector.ts +108 -163
- package/dist/templates/_services/VexSystem.ts +25 -42
- package/dist/templates/_services/auth/JWTService.ts +26 -13
- package/dist/templates/_services/oauth/OAuthProfileMap.ts +6 -14
- package/dist/templates/_services/oauth/OAuthStrategyService.ts +70 -51
- package/dist/templates/_types/IVexRepository.ts +18 -0
- package/dist/templates/_types/VexRequest.ts +11 -0
- package/dist/templates/_types/VexResponse.ts +25 -0
- package/dist/templates/_types/VexResponseError.ts +4 -3
- package/dist/templates/_types/responseObject.ts +1 -1
- package/dist/templates/_utils/common.ts +50 -16
- package/dist/templates/_utils/hash.ts +6 -5
- package/dist/templates/_utils/index.ts +2 -1
- package/dist/templates/_utils/logger.ts +15 -14
- package/dist/templates/_utils/processTimer.ts +1 -0
- package/dist/templates/_utils/response.ts +4 -3
- package/dist/templates/_utils/validator.ts +1 -0
- package/dist/templates/jsonSchema/Session.json +34 -31
- package/dist/templates/jsonSchema/User.json +50 -85
- package/dist/templates/jsonSchema/UserAuthProfiles.json +48 -0
- package/dist/templates/root/Dockerfile +19 -19
- package/dist/templates/root/README.md +38 -38
- package/dist/templates/root/public/css/style.css +7 -7
- package/dist/templates/root/public/js/login.js +58 -58
- package/dist/templates/root/public/js/logincallback.js +18 -18
- package/dist/templates/root/public/js/mytokens.js +10 -10
- package/dist/templates/root/public/js/refreshtokens.js +22 -22
- package/dist/templates/root/scripts/build.js +38 -38
- package/dist/templates/root/tsconfig.json +29 -27
- package/docs/ForeignKey.md +43 -43
- package/docs/appGenerated/auth.md +22 -22
- package/docs/developmentNote.md +22 -22
- package/docs/releaseNote/v0-5-0.md +17 -17
- package/docs/roadMap/futurePlan.md +9 -9
- package/docs/roadMap/released.md +49 -49
- package/docs/roadMap/v0-6-0.md +15 -15
- package/docs/vexJsonSchema.md +55 -55
- package/package.json +59 -58
- package/scripts/build.js +13 -13
- package/scripts/bump-version.js +86 -86
- package/scripts/copyTsTemplates.js +30 -30
- package/scripts/merge.js +124 -124
- package/tsconfig.json +121 -121
- package/vex.config.json +37 -36
- package/dist/templates/_types/VexResponsePayloadError.ts +0 -20
package/scripts/build.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
const esbuild = require('esbuild');
|
|
2
|
-
|
|
3
|
-
esbuild.build({
|
|
4
|
-
entryPoints: ['./src/cli.ts'], // Your main file
|
|
5
|
-
bundle: true, // Bundle all dependencies
|
|
6
|
-
platform: 'node', // Specify the platform
|
|
7
|
-
target: 'es2016', // Target Node version, adjust as needed
|
|
8
|
-
minify: true, // Minify the output
|
|
9
|
-
tsconfig: './tsconfig.json', // Your tsconfig file
|
|
10
|
-
outfile: './dist/index.js', // Output file
|
|
11
|
-
}).catch((e) => {
|
|
12
|
-
console.error(e);
|
|
13
|
-
process.exit(1);
|
|
1
|
+
const esbuild = require('esbuild');
|
|
2
|
+
|
|
3
|
+
esbuild.build({
|
|
4
|
+
entryPoints: ['./src/cli.ts'], // Your main file
|
|
5
|
+
bundle: true, // Bundle all dependencies
|
|
6
|
+
platform: 'node', // Specify the platform
|
|
7
|
+
target: 'es2016', // Target Node version, adjust as needed
|
|
8
|
+
minify: true, // Minify the output
|
|
9
|
+
tsconfig: './tsconfig.json', // Your tsconfig file
|
|
10
|
+
outfile: './dist/index.js', // Output file
|
|
11
|
+
}).catch((e) => {
|
|
12
|
+
console.error(e);
|
|
13
|
+
process.exit(1);
|
|
14
14
|
});
|
package/scripts/bump-version.js
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
-
import { execSync } from 'child_process';
|
|
5
|
-
|
|
6
|
-
function bumpVersion(mode, devNum, major, minor, patch) {
|
|
7
|
-
let newVersion;
|
|
8
|
-
if (mode === 'dev') {
|
|
9
|
-
if (devNum) {
|
|
10
|
-
newVersion = `${major}.${minor}.${patch}-dev${parseInt(devNum) + 1}`;
|
|
11
|
-
} else {
|
|
12
|
-
newVersion = `${major}.${minor}.${patch}-dev1`;
|
|
13
|
-
}
|
|
14
|
-
} else if (mode === 'patch') {
|
|
15
|
-
patch += 1;
|
|
16
|
-
newVersion = `${major}.${minor}.${patch}`;
|
|
17
|
-
} else if (mode === 'minor') {
|
|
18
|
-
minor += 1;
|
|
19
|
-
patch = 0;
|
|
20
|
-
newVersion = `${major}.${minor}.${patch}`;
|
|
21
|
-
} else if (mode === 'major') {
|
|
22
|
-
major += 1;
|
|
23
|
-
minor = 0;
|
|
24
|
-
patch = 0;
|
|
25
|
-
newVersion = `${major}.${minor}.${patch}`;
|
|
26
|
-
} else {
|
|
27
|
-
console.error('\x1b[31m', `Invalid version mode "${mode}", no version applied`);
|
|
28
|
-
process.exit(0);
|
|
29
|
-
};
|
|
30
|
-
return newVersion;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function main() {
|
|
34
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
35
|
-
const pkgPath = path.resolve(__dirname, '../package.json');
|
|
36
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
37
|
-
|
|
38
|
-
const mode = process.env.npm_config_version_type || process.argv[2] || 'none';
|
|
39
|
-
|
|
40
|
-
const versionMatch = pkg.version.match(/^(\d+)\.(\d+)\.(\d+)(-dev(\d+))?$/);
|
|
41
|
-
if (!versionMatch) {
|
|
42
|
-
console.error('\x1b[31m', `Invalid version "${pkg.version}", Version must be in the form x.y.z or x.y.z-devN, no version applied`);
|
|
43
|
-
process.exit(0);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
let [_, major, minor, patch, devFull, devNum] = versionMatch;
|
|
47
|
-
major = parseInt(major);
|
|
48
|
-
minor = parseInt(minor);
|
|
49
|
-
patch = parseInt(patch);
|
|
50
|
-
|
|
51
|
-
// Run preversion
|
|
52
|
-
if (pkg.scripts && pkg.scripts.preversion) {
|
|
53
|
-
try {
|
|
54
|
-
execSync(pkg.scripts.preversion, { stdio: 'inherit' });
|
|
55
|
-
} catch (e) {
|
|
56
|
-
console.warn('preversion script failed:', e.message);
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// Bump version
|
|
62
|
-
const newVersion = bumpVersion(mode, devNum, major, minor, patch);
|
|
63
|
-
pkg.version = newVersion;
|
|
64
|
-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4) + '\n');
|
|
65
|
-
console.log(`Bumped version (${mode}) to`, newVersion);
|
|
66
|
-
|
|
67
|
-
// Run postversion
|
|
68
|
-
if (pkg.scripts && pkg.scripts.postversion) {
|
|
69
|
-
try {
|
|
70
|
-
execSync(pkg.scripts.postversion, { stdio: 'inherit' });
|
|
71
|
-
} catch (e) {
|
|
72
|
-
console.warn('postversion script failed:', e.message);
|
|
73
|
-
process.exit(1);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// Add git tag for versioning
|
|
78
|
-
try {
|
|
79
|
-
execSync(`git commit -am "v${newVersion}"`);
|
|
80
|
-
execSync(`git tag v${newVersion}`);
|
|
81
|
-
console.log(`Created git tag v${newVersion}`);
|
|
82
|
-
} catch (e) {
|
|
83
|
-
console.warn('Failed to create git tag:', e.message);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { execSync } from 'child_process';
|
|
5
|
+
|
|
6
|
+
function bumpVersion(mode, devNum, major, minor, patch) {
|
|
7
|
+
let newVersion;
|
|
8
|
+
if (mode === 'dev') {
|
|
9
|
+
if (devNum) {
|
|
10
|
+
newVersion = `${major}.${minor}.${patch}-dev${parseInt(devNum) + 1}`;
|
|
11
|
+
} else {
|
|
12
|
+
newVersion = `${major}.${minor}.${patch}-dev1`;
|
|
13
|
+
}
|
|
14
|
+
} else if (mode === 'patch') {
|
|
15
|
+
patch += 1;
|
|
16
|
+
newVersion = `${major}.${minor}.${patch}`;
|
|
17
|
+
} else if (mode === 'minor') {
|
|
18
|
+
minor += 1;
|
|
19
|
+
patch = 0;
|
|
20
|
+
newVersion = `${major}.${minor}.${patch}`;
|
|
21
|
+
} else if (mode === 'major') {
|
|
22
|
+
major += 1;
|
|
23
|
+
minor = 0;
|
|
24
|
+
patch = 0;
|
|
25
|
+
newVersion = `${major}.${minor}.${patch}`;
|
|
26
|
+
} else {
|
|
27
|
+
console.error('\x1b[31m', `Invalid version mode "${mode}", no version applied`);
|
|
28
|
+
process.exit(0);
|
|
29
|
+
};
|
|
30
|
+
return newVersion;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function main() {
|
|
34
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
35
|
+
const pkgPath = path.resolve(__dirname, '../package.json');
|
|
36
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
37
|
+
|
|
38
|
+
const mode = process.env.npm_config_version_type || process.argv[2] || 'none';
|
|
39
|
+
|
|
40
|
+
const versionMatch = pkg.version.match(/^(\d+)\.(\d+)\.(\d+)(-dev(\d+))?$/);
|
|
41
|
+
if (!versionMatch) {
|
|
42
|
+
console.error('\x1b[31m', `Invalid version "${pkg.version}", Version must be in the form x.y.z or x.y.z-devN, no version applied`);
|
|
43
|
+
process.exit(0);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
let [_, major, minor, patch, devFull, devNum] = versionMatch;
|
|
47
|
+
major = parseInt(major);
|
|
48
|
+
minor = parseInt(minor);
|
|
49
|
+
patch = parseInt(patch);
|
|
50
|
+
|
|
51
|
+
// Run preversion
|
|
52
|
+
if (pkg.scripts && pkg.scripts.preversion) {
|
|
53
|
+
try {
|
|
54
|
+
execSync(pkg.scripts.preversion, { stdio: 'inherit' });
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.warn('preversion script failed:', e.message);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Bump version
|
|
62
|
+
const newVersion = bumpVersion(mode, devNum, major, minor, patch);
|
|
63
|
+
pkg.version = newVersion;
|
|
64
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4) + '\n');
|
|
65
|
+
console.log(`Bumped version (${mode}) to`, newVersion);
|
|
66
|
+
|
|
67
|
+
// Run postversion
|
|
68
|
+
if (pkg.scripts && pkg.scripts.postversion) {
|
|
69
|
+
try {
|
|
70
|
+
execSync(pkg.scripts.postversion, { stdio: 'inherit' });
|
|
71
|
+
} catch (e) {
|
|
72
|
+
console.warn('postversion script failed:', e.message);
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// Add git tag for versioning
|
|
78
|
+
try {
|
|
79
|
+
execSync(`git commit -am "v${newVersion}"`);
|
|
80
|
+
execSync(`git tag v${newVersion}`);
|
|
81
|
+
console.log(`Created git tag v${newVersion}`);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
console.warn('Failed to create git tag:', e.message);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
87
|
main();
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
// copy dir src/templates/utils to dist/templates/utils
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* to copy template files into tsc output dir (dist)
|
|
7
|
-
*/
|
|
8
|
-
function copyDir (src, destination) {
|
|
9
|
-
|
|
10
|
-
if (!fs.existsSync(destination)) {
|
|
11
|
-
fs.mkdirSync(destination);
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
fs.rmSync(destination, { recursive: true });
|
|
15
|
-
fs.mkdirSync(destination);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
fs.readdirSync(src).forEach((file) => {
|
|
19
|
-
const srcPath = path.join(src, file);
|
|
20
|
-
const destPath = path.join(destination, file);
|
|
21
|
-
const stat = fs.statSync(srcPath);
|
|
22
|
-
if (stat.isFile()) {
|
|
23
|
-
fs.writeFileSync(destPath, fs.readFileSync(srcPath));
|
|
24
|
-
} else if (stat.isDirectory()) {
|
|
25
|
-
copyDir(srcPath, destPath);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
copyDir('src/templates', 'dist/templates');
|
|
1
|
+
// copy dir src/templates/utils to dist/templates/utils
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* to copy template files into tsc output dir (dist)
|
|
7
|
+
*/
|
|
8
|
+
function copyDir (src, destination) {
|
|
9
|
+
|
|
10
|
+
if (!fs.existsSync(destination)) {
|
|
11
|
+
fs.mkdirSync(destination);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
fs.rmSync(destination, { recursive: true });
|
|
15
|
+
fs.mkdirSync(destination);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fs.readdirSync(src).forEach((file) => {
|
|
19
|
+
const srcPath = path.join(src, file);
|
|
20
|
+
const destPath = path.join(destination, file);
|
|
21
|
+
const stat = fs.statSync(srcPath);
|
|
22
|
+
if (stat.isFile()) {
|
|
23
|
+
fs.writeFileSync(destPath, fs.readFileSync(srcPath));
|
|
24
|
+
} else if (stat.isDirectory()) {
|
|
25
|
+
copyDir(srcPath, destPath);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
copyDir('src/templates', 'dist/templates');
|
package/scripts/merge.js
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const DiffMatchPatch = require('diff-match-patch');
|
|
3
|
-
const dmp = new DiffMatchPatch();
|
|
4
|
-
|
|
5
|
-
async function merge(currentFilePath, incomingNewFilePath, incomingOldFilePath, outFilePath) {
|
|
6
|
-
const current = fs.readFileSync(currentFilePath, 'utf8');
|
|
7
|
-
const incomingOld = fs.readFileSync(incomingOldFilePath, 'utf8');
|
|
8
|
-
const incomingNew = fs.readFileSync(incomingNewFilePath, 'utf8');
|
|
9
|
-
|
|
10
|
-
// diff incoming old and current
|
|
11
|
-
// find manual changes in current file
|
|
12
|
-
let diffOld = dmp.diff_main(incomingOld, current);
|
|
13
|
-
dmp.diff_cleanupSemantic(diffOld);
|
|
14
|
-
diffOld = diff_LineMode(diffOld);
|
|
15
|
-
// console.log("\n===== diffOld =====", diffOld)
|
|
16
|
-
|
|
17
|
-
// diff incoming new and current
|
|
18
|
-
// find new update in incomming file
|
|
19
|
-
let diffNew = dmp.diff_main(current, incomingNew);
|
|
20
|
-
dmp.diff_cleanupSemantic(diffNew);
|
|
21
|
-
diffNew = diff_LineMode(diffNew);
|
|
22
|
-
console.log("\n===== diffNew =====", diffNew);
|
|
23
|
-
|
|
24
|
-
// console.log("duck")
|
|
25
|
-
// loop diffNew if diff is found in diffOld then pop diff
|
|
26
|
-
for (let i = 0; i < diffNew.length; i++) {
|
|
27
|
-
const newDiff = diffNew[i];
|
|
28
|
-
|
|
29
|
-
let found = false;
|
|
30
|
-
|
|
31
|
-
for (let j = 0; j < diffOld.length; j++) {
|
|
32
|
-
const oldDiff = diffOld[j];
|
|
33
|
-
|
|
34
|
-
if (newDiff[0] == 0) {
|
|
35
|
-
found = true;
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
// else {
|
|
39
|
-
// console.log([i,j], newDiff, oldDiff)
|
|
40
|
-
// };
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
((newDiff[0] == -1 && oldDiff[0] == 1) || (newDiff[0] == 1 && oldDiff[0] == -1)) &&
|
|
44
|
-
compareWithoutSymbol(newDiff[1], oldDiff[1])
|
|
45
|
-
) {
|
|
46
|
-
found = true;
|
|
47
|
-
if (newDiff[0] == -1) {
|
|
48
|
-
diffNew[i - 1][1] += newDiff[1];
|
|
49
|
-
// console.log("> added to previous diff", diffNew[i - 1][1]);
|
|
50
|
-
};
|
|
51
|
-
// console.log("> deleted", newDiff[1]);
|
|
52
|
-
diffNew.splice(i, 1);
|
|
53
|
-
diffOld.splice(j, 1);
|
|
54
|
-
i -= 1;
|
|
55
|
-
break;
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// if(!found){
|
|
60
|
-
// console.log("> not found", newDiff);
|
|
61
|
-
// newDiff[1] = `\n<<<<<<< v1\n=======\n${newDiff[1]}\n>>>>>>> v2`;
|
|
62
|
-
// };
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// log updated diffNew
|
|
67
|
-
console.log("\n===== updated diffNew =====", diffNew);
|
|
68
|
-
diffNew.forEach((diff, index) => {
|
|
69
|
-
const color = diff[0] == -1 ? '\x1b[31m' : (diff[0] == 1 ? '\x1b[32m' : '\x1b[0m');
|
|
70
|
-
console.log(color, diff[1]);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// apply diffNew
|
|
75
|
-
const patches = dmp.patch_make(diffNew);
|
|
76
|
-
const [patchedText, results] = dmp.patch_apply(patches, current);
|
|
77
|
-
// console.log("\n===== patches =====", patchedText)
|
|
78
|
-
|
|
79
|
-
// Save the merged file
|
|
80
|
-
fs.writeFileSync(outFilePath, patchedText, 'utf8');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function diff_LineMode(diffs = [[0,''],[0,'']]) {
|
|
84
|
-
for (let i = 0; i < diffs.length; i++) {
|
|
85
|
-
let diff = diffs[i];
|
|
86
|
-
const [operation, text] = diff;
|
|
87
|
-
|
|
88
|
-
const split = /\n/gm.test(text);
|
|
89
|
-
if(split){
|
|
90
|
-
// const lines = text.split('\n');
|
|
91
|
-
const lines = text.split(/\n/gm);
|
|
92
|
-
console.log(">>lines", lines);
|
|
93
|
-
|
|
94
|
-
const currentLines = [];
|
|
95
|
-
lines.forEach((line, index) => {
|
|
96
|
-
|
|
97
|
-
if(index !== lines.length - 1) {
|
|
98
|
-
line += '\n';
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
currentLines.push([operation, line]);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
diffs = [ ...diffs.slice(0, i), ...currentLines, ...diffs.slice(i+1)];
|
|
105
|
-
// console.log(">>>>>", diffs);
|
|
106
|
-
i += currentLines.length;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return diffs;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function compareWithoutSymbol(text1, text2){
|
|
114
|
-
let t1 = text1;
|
|
115
|
-
t1 = text1.replaceAll(/\r/g, '');
|
|
116
|
-
t1 = text1.replaceAll(/\n/g, '');
|
|
117
|
-
|
|
118
|
-
let t2 = text2;
|
|
119
|
-
t2 = text2.replaceAll(/\r/g, '');
|
|
120
|
-
t2 = text2.replaceAll(/\n/g, '');
|
|
121
|
-
|
|
122
|
-
return t1 == t2;
|
|
123
|
-
}
|
|
124
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const DiffMatchPatch = require('diff-match-patch');
|
|
3
|
+
const dmp = new DiffMatchPatch();
|
|
4
|
+
|
|
5
|
+
async function merge(currentFilePath, incomingNewFilePath, incomingOldFilePath, outFilePath) {
|
|
6
|
+
const current = fs.readFileSync(currentFilePath, 'utf8');
|
|
7
|
+
const incomingOld = fs.readFileSync(incomingOldFilePath, 'utf8');
|
|
8
|
+
const incomingNew = fs.readFileSync(incomingNewFilePath, 'utf8');
|
|
9
|
+
|
|
10
|
+
// diff incoming old and current
|
|
11
|
+
// find manual changes in current file
|
|
12
|
+
let diffOld = dmp.diff_main(incomingOld, current);
|
|
13
|
+
dmp.diff_cleanupSemantic(diffOld);
|
|
14
|
+
diffOld = diff_LineMode(diffOld);
|
|
15
|
+
// console.log("\n===== diffOld =====", diffOld)
|
|
16
|
+
|
|
17
|
+
// diff incoming new and current
|
|
18
|
+
// find new update in incomming file
|
|
19
|
+
let diffNew = dmp.diff_main(current, incomingNew);
|
|
20
|
+
dmp.diff_cleanupSemantic(diffNew);
|
|
21
|
+
diffNew = diff_LineMode(diffNew);
|
|
22
|
+
console.log("\n===== diffNew =====", diffNew);
|
|
23
|
+
|
|
24
|
+
// console.log("duck")
|
|
25
|
+
// loop diffNew if diff is found in diffOld then pop diff
|
|
26
|
+
for (let i = 0; i < diffNew.length; i++) {
|
|
27
|
+
const newDiff = diffNew[i];
|
|
28
|
+
|
|
29
|
+
let found = false;
|
|
30
|
+
|
|
31
|
+
for (let j = 0; j < diffOld.length; j++) {
|
|
32
|
+
const oldDiff = diffOld[j];
|
|
33
|
+
|
|
34
|
+
if (newDiff[0] == 0) {
|
|
35
|
+
found = true;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
// else {
|
|
39
|
+
// console.log([i,j], newDiff, oldDiff)
|
|
40
|
+
// };
|
|
41
|
+
|
|
42
|
+
if (
|
|
43
|
+
((newDiff[0] == -1 && oldDiff[0] == 1) || (newDiff[0] == 1 && oldDiff[0] == -1)) &&
|
|
44
|
+
compareWithoutSymbol(newDiff[1], oldDiff[1])
|
|
45
|
+
) {
|
|
46
|
+
found = true;
|
|
47
|
+
if (newDiff[0] == -1) {
|
|
48
|
+
diffNew[i - 1][1] += newDiff[1];
|
|
49
|
+
// console.log("> added to previous diff", diffNew[i - 1][1]);
|
|
50
|
+
};
|
|
51
|
+
// console.log("> deleted", newDiff[1]);
|
|
52
|
+
diffNew.splice(i, 1);
|
|
53
|
+
diffOld.splice(j, 1);
|
|
54
|
+
i -= 1;
|
|
55
|
+
break;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// if(!found){
|
|
60
|
+
// console.log("> not found", newDiff);
|
|
61
|
+
// newDiff[1] = `\n<<<<<<< v1\n=======\n${newDiff[1]}\n>>>>>>> v2`;
|
|
62
|
+
// };
|
|
63
|
+
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// log updated diffNew
|
|
67
|
+
console.log("\n===== updated diffNew =====", diffNew);
|
|
68
|
+
diffNew.forEach((diff, index) => {
|
|
69
|
+
const color = diff[0] == -1 ? '\x1b[31m' : (diff[0] == 1 ? '\x1b[32m' : '\x1b[0m');
|
|
70
|
+
console.log(color, diff[1]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
// apply diffNew
|
|
75
|
+
const patches = dmp.patch_make(diffNew);
|
|
76
|
+
const [patchedText, results] = dmp.patch_apply(patches, current);
|
|
77
|
+
// console.log("\n===== patches =====", patchedText)
|
|
78
|
+
|
|
79
|
+
// Save the merged file
|
|
80
|
+
fs.writeFileSync(outFilePath, patchedText, 'utf8');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function diff_LineMode(diffs = [[0,''],[0,'']]) {
|
|
84
|
+
for (let i = 0; i < diffs.length; i++) {
|
|
85
|
+
let diff = diffs[i];
|
|
86
|
+
const [operation, text] = diff;
|
|
87
|
+
|
|
88
|
+
const split = /\n/gm.test(text);
|
|
89
|
+
if(split){
|
|
90
|
+
// const lines = text.split('\n');
|
|
91
|
+
const lines = text.split(/\n/gm);
|
|
92
|
+
console.log(">>lines", lines);
|
|
93
|
+
|
|
94
|
+
const currentLines = [];
|
|
95
|
+
lines.forEach((line, index) => {
|
|
96
|
+
|
|
97
|
+
if(index !== lines.length - 1) {
|
|
98
|
+
line += '\n';
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
currentLines.push([operation, line]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
diffs = [ ...diffs.slice(0, i), ...currentLines, ...diffs.slice(i+1)];
|
|
105
|
+
// console.log(">>>>>", diffs);
|
|
106
|
+
i += currentLines.length;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return diffs;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function compareWithoutSymbol(text1, text2){
|
|
114
|
+
let t1 = text1;
|
|
115
|
+
t1 = text1.replaceAll(/\r/g, '');
|
|
116
|
+
t1 = text1.replaceAll(/\n/g, '');
|
|
117
|
+
|
|
118
|
+
let t2 = text2;
|
|
119
|
+
t2 = text2.replaceAll(/\r/g, '');
|
|
120
|
+
t2 = text2.replaceAll(/\n/g, '');
|
|
121
|
+
|
|
122
|
+
return t1 == t2;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
125
|
merge('Current.ts', 'IncomingNew.ts', 'IncomingOld.ts', 'Merged.ts');
|