forked from nocodb/nocodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgradeNocodbSdk.js
76 lines (70 loc) · 2.62 KB
/
upgradeNocodbSdk.js
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
const fs = require('fs')
const path = require('path');
const execSync = require('child_process').execSync;
// extract latest version from package.json
const nocodbSdkPackage = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'packages', 'nocodb-sdk', 'package.json'), 'utf8'))
const replacePackageName = (filePath) => {
return new Promise((resolve, reject) => {
return fs.readFile(filePath, 'utf8', function (err, data) {
if (err) return reject(err)
var result = data.replace(/nocodb-sdk/g, nocodbSdkPackage.name);
return fs.writeFile(filePath, result, 'utf8', function (err) {
if (err) return reject(err)
return resolve()
});
});
})
}
const bumbVersionAndSave = () => {
// upgrade nocodb-sdk version in nocodb
execSync(`cd packages/nocodb && npm install --save --save-exact ${nocodbSdkPackage.name}@${nocodbSdkPackage.version}`, {});
// upgrade nocodb-sdk version in nc-gui
execSync(`cd packages/nc-gui && npm install --save --save-exact ${nocodbSdkPackage.name}@${nocodbSdkPackage.version}`, {});
}
const dfs = function(dir) {
var res = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
if (['node_modules', 'build'].includes(file)) return;
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
res = res.concat(dfs(file));
} else {
const ext = path.extname(file).toLowerCase()
if (ext == '.vue' || ext == '.ts' || ext == '.js') {
res.push(file);
}
}
})
return res;
}
const searchAndReplace = (target) => {
let list = [
...dfs(path.resolve(path.join(__dirname, '..', 'packages', 'nc-gui'))),
...dfs(path.resolve(path.join(__dirname, '..', 'packages', 'nocodb'))),
path.join(__dirname, '..', 'packages', 'nc-gui', 'package.json'),
path.join(__dirname, '..', 'packages', 'nocodb', 'package.json')
]
return Promise.all(list.map(d => {
return new Promise((resolve, reject) => {
fs.readFile(d, function(err, content) {
if (err) reject(err)
if (content.indexOf(target) > -1) {
resolve(replacePackageName(d))
} else {
resolve()
}
})
})
}))
}
if (process.env.targetEnv === 'DEV') {
// replace nocodb-sdk by nocodb-sdk-daily if it is nightly build / pr build
searchAndReplace('nocodb-sdk')
.then(() => {
bumbVersionAndSave()
})
} else {
bumbVersionAndSave()
}