forked from tanshuai/alphabiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-patch.js
113 lines (105 loc) · 4.4 KB
/
copy-patch.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// UnPackaged files uploaded by private repository, webTorrent module without patch after 'YARN' installation.
// So you need to run this file, the root directory of modified webtorrent copied to dist/electron/UnPackaged/node_modules,
// then run the playwright test, effective download function
const fs = require('fs')
const path = require('path')
const modulesAppConfigPath = path.resolve(__dirname, 'dist/electron/UnPackaged/node_modules/developer')
const unPackagedAppConfigPath = path.resolve(__dirname, 'dist/electron/UnPackaged/developer')
const { getPackageDetailsFromPatchFilename } = require('patch-package/dist/PackageDetails')
const patches = fs.readdirSync(path.resolve(__dirname, 'patches'))
.map(getPackageDetailsFromPatchFilename)
.filter(i => i && !i.isDevOnly)
.map(i => i.name)
const copyModule = async () => {
[
...patches,
'torrent-discovery', // this builds with self-dep bittorrent-tracker
'@videojs'
].forEach(dep => {
const src = path.resolve(__dirname, 'node_modules', dep)
const dest = path.resolve(__dirname, 'dist/electron/UnPackaged/node_modules', dep)
if (!fs.existsSync(src)) return
if (fs.existsSync(dest)) fs.rmSync(dest, { recursive: true })
const copyRecursive = (src, dest) => {
if (fs.statSync(src).isDirectory()) {
fs.readdirSync(src).forEach(dir => {
copyRecursive(path.resolve(src, dir), path.resolve(dest, dir))
})
} else {
// ensure directory exists
if (!fs.existsSync(path.dirname(dest))) {
fs.mkdirSync(path.dirname(dest), { recursive: true })
}
fs.copyFileSync(src, dest)
}
}
copyRecursive(src, dest)
})
}
const copyDeveloper = async () => {
const src = path.resolve(__dirname, 'developer')
// console.log('src:' + src)
// console.log('dest:' + dest)
if (!fs.existsSync(src)) return
if (fs.existsSync(modulesAppConfigPath)) fs.rmSync(modulesAppConfigPath, { recursive: true })
if (fs.existsSync(unPackagedAppConfigPath)) fs.rmSync(unPackagedAppConfigPath, { recursive: true })
const copyRecursive = (src, dest) => {
if (fs.statSync(src).isDirectory()) {
fs.readdirSync(src).forEach(dir => {
copyRecursive(path.resolve(src, dir), path.resolve(dest, dir))
})
} else {
// ensure directory exists
if (!fs.existsSync(path.dirname(dest))) {
fs.mkdirSync(path.dirname(dest), { recursive: true })
}
fs.copyFileSync(src, dest)
}
}
copyRecursive(src, modulesAppConfigPath)
copyRecursive(src, unPackagedAppConfigPath)
resetRegisterMode(modulesAppConfigPath)
resetRegisterMode(unPackagedAppConfigPath)
}
const copyVersionJSON = async () => {
const src = path.resolve(__dirname, 'public/version.json')
const dest = path.resolve(__dirname, 'node_modules/@zeeis/velectron/dist/resources/version.json')
// console.log('src:' + src)
// console.log('dest:' + dest)
fs.copyFileSync(src, dest)
}
const deleteVersionJSON = async () => {
const dest = path.resolve(__dirname, 'node_modules/@zeeis/velectron/dist/resources/version.json')
if (!fs.existsSync(dest)) return
fs.unlink(dest, (err) => {
if (err) throw err;
console.log('version.json was deleted');
})
}
// any one can register when run e2e test
const resetRegisterMode = (folder) => {
const appConfigPath = path.resolve(folder, './app.js')
// console.log(appConfigPath)
const appConfigContent = fs.readFileSync(appConfigPath, 'utf-8')
const registerMode = appConfigContent.substring(appConfigContent.indexOf('register: {'), appConfigContent.indexOf('Country code list'))
const newRegisterMode = registerMode.replace(/mode:\s\'.*\'\,/gm, 'mode: \'none\',')
const newAppConfigContent = appConfigContent.replace(registerMode, newRegisterMode)
fs.writeFileSync(appConfigPath, newAppConfigContent)
}
if (process.argv.includes('--pre')) {
console.log('run copy-patch.js --pre')
if (process.platform !== 'darwin') copyVersionJSON()
copyModule()
copyDeveloper()
} else if (process.argv.includes('--post')) {
console.log('run copy-patch.js --post')
if (process.platform === 'darwin') deleteVersionJSON()
if (fs.existsSync(modulesAppConfigPath)) {
console.log(modulesAppConfigPath)
fs.rmSync(modulesAppConfigPath, { recursive: true })
}
if (fs.existsSync(unPackagedAppConfigPath)) {
console.log(unPackagedAppConfigPath)
fs.rmSync(unPackagedAppConfigPath, { recursive: true })
}
}