-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.js
More file actions
63 lines (52 loc) · 2.47 KB
/
util.js
File metadata and controls
63 lines (52 loc) · 2.47 KB
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
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
function getFolderHash(folder) {
const files = getAllFiles(folder)
const hashes = []
files.forEach(file => {
hashes.push(getFileHash(file))
})
// Combine all hashes to generate a unique hash for the folder
const combinedHash = crypto.createHash('md5').update(hashes.join()).digest('hex')
return combinedHash
}
function getFileHash(file){
const data = fs.readFileSync(file, 'utf8')
const normalizedData = data.replace(/\r\n/g, '\n') // Normalize line endings
const hash = crypto.createHash('md5').update(normalizedData).digest('hex')
return hash
}
function getAllFiles(folder) {
let files = []
fs.readdirSync(folder).forEach(file => {
const fullPath = path.join(folder, file)
if (fs.statSync(fullPath).isDirectory()) {
files = files.concat(getAllFiles(fullPath))
} else {
files.push(fullPath)
}
})
return files
}
function updateDependency(projectFolder) {
// write dependency in package.json
const packageJSONPath = path.join(projectFolder, 'package.json')
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf8'))
packageJSON.devDependencies["@cap-js/cap-operator-plugin"] = "file:../../../../"
fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 4))
}
function setupHack(projectFolder) {
// Required to get the plugin called during npm link. With other approaches(eg: npm pack), we don't get the coverage.
const cdsPlugin = fs.readFileSync(path.join(projectFolder, 'node_modules/@cap-js/cap-operator-plugin/cds-plugin.js'), 'utf8')
// Replace requires from cds to cds-dk
const updatedData = cdsPlugin.replace(/^const cds = require\('@sap\/cds'\)/m, "const cds = require('@sap/cds-dk')");
fs.writeFileSync(path.join(projectFolder, 'node_modules/@cap-js/cap-operator-plugin/cds-plugin.js'), updatedData)
}
function undoSetupHack(projectFolder) {
const cdsPlugin = fs.readFileSync(path.join(projectFolder, 'node_modules/@cap-js/cap-operator-plugin/cds-plugin.js'), 'utf8')
// Replace back to cds
const updatedData = cdsPlugin.replace(/^const cds = require\('@sap\/cds-dk'\)/m, "const cds = require('@sap/cds')");
fs.writeFileSync(path.join(projectFolder, 'node_modules/@cap-js/cap-operator-plugin/cds-plugin.js'), updatedData)
}
module.exports = { getFolderHash, getFileHash, updateDependency, setupHack, undoSetupHack }