forked from tanshuai/alphabiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
138 lines (132 loc) · 5.57 KB
/
make.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const { exec, execSync } = require('child_process')
const { existsSync, copyFileSync, mkdirSync, unlinkSync, readFileSync, writeFileSync } = require('fs')
const { resolve } = require('path')
const { productName, description } = require('./package.json')
const publicVersion = require('./public/version.json').version
const versionHeader = publicVersion.match(/\d+\.\d+\.\d+/gm)
const pkgVersion = require('./public/version.json').packageVer
const readline = require('readline')
const { copySync } = require('fs-extra')
const version = publicVersion || pkgVersion
console.log(`version: ${version}`)
const { platform, arch } = process
const symlinkDir = require('symlink-dir')
const doMake = async () => {
const arg = platform === 'darwin'
? 'make:dmg'
: platform === 'win32'
? 'make:win'
: 'make:deb'
// console.log('make:win')
console.log(`Make for \x1b[36m${platform}-${arch}\x1b[0m`)
const packageDir = resolve(__dirname, `build/electron/${productName}-${platform}-${arch}`)
if (!existsSync(packageDir)) {
console.error('\x1b[41m Error \x1b[0m Cannot find packaged app. Run \x1b[90myarn build\x1b[0m before make.')
console.log(` Ensure that the build result contains a folder of\x1b[33m dist/electron/${productName}-${platform}-${arch}\x1b[0m`)
process.exit(1)
}
const destDir = resolve(__dirname, `out/${productName}-${platform}-${arch}`)
if (platform === 'linux') {
/**
* The @electron-forge/maker-deb use symlink linking files to /tmp,
* so we cannot use symlink for it. Since `fs` does not have an
* recursive copy method, we just exec copy here
*/
// execSync(`cp -r "${packageDir}/" "${resolve(__dirname, 'out')}/"`)
copySync(packageDir, destDir, { recursive: true })
} else copySync(packageDir, destDir, { recursive: true })
// } else await symlinkDir(packageDir, destDir)
console.log(`Executing: \x1b[32myarn ${arg}\x1b[0m`)
const prefix = `\x1b[32m * make \x1b[0m`
const res = exec(`yarn ${arg}`)
res.stdout.on('data', d => {
// process.stdout.clearLine()
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0, null)
process.stdout.write(prefix + d.toString().trim())
})
// res.stdout.pipe(process.stdout)
res.stderr.on('data', e => {
// process.stderr.clearLine()
readline.clearLine(process.stderr, 0)
readline.cursorTo(process.stderr, 0, null)
process.stderr.write(prefix + e.toString().trim())
})
// res.stderr.pipe(process.stderr)
res.on('exit', code => {
// process.stdout.clearLine()
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0, null)
process.stdout.write(prefix + 'Exit.\n')
process.exit(code)
})
}
const doPostmake = () => {
const outDir = resolve(__dirname, 'out/make')
const destDir = resolve(__dirname, `out/installers/${version}`)
mkdirSync(destDir, { recursive: true })
/** @type { Array<[string, string]> } */
const toMoves = []
if (platform === 'win32') {
const wixDir = resolve(outDir, `wix/${arch}`)
toMoves.push([resolve(wixDir, `${productName}.msi`), resolve(destDir, `${productName.toLowerCase()}-${version}.msi`)])
const squirrelDir = resolve(outDir, `squirrel.windows/${arch}`)
const appxDir = resolve(outDir, `appx/${arch}`)
toMoves.push([resolve(appxDir, `${productName}.appx`), resolve(destDir, `${productName.toLowerCase()}-${version}.appx`)])
const files = [
`${productName}-${versionHeader[0]} Setup.exe`,
`${productName}-${versionHeader[0]}-full.nupkg`,
`${productName}-${versionHeader[0]}-delta.nupkg`,
'RELEASES'
]
files.forEach(file => {
toMoves.push([resolve(squirrelDir, file), resolve(destDir, file.replace(' Setup', '').replace(`${productName}`, `${productName.toLowerCase()}`).replace('-full', '').replace('-delta', '').replace(versionHeader[0], version))])
})
} else if (platform === 'darwin') {
// move darwin installers
toMoves.push([
resolve(outDir, `${productName}.dmg`),
resolve(destDir, `${productName.toLowerCase()}-${version}.dmg`)
])
} else if (platform === 'linux') {
// move linux installers
toMoves.push([
resolve(outDir, `deb/${arch}/${productName.toLowerCase()}_${versionHeader[0]}_${arch === 'x64' ? 'amd64' : arch}.deb`),
resolve(destDir, `${productName.toLowerCase()}-${version}.deb`)
])
}
for (const [src, tar] of toMoves) {
if (existsSync(src)) {
console.log(`Asset => \x1b[36m${tar}\x1b[0m`)
copyFileSync(src, tar)
}
}
}
if (process.argv.includes('--make')) {
// modify package.json version for Update the installation package version
const packagePath = resolve(__dirname, './package.json')
const packageObj = readFileSync(packagePath)
const pkg = JSON.parse(packageObj)
pkg.version = versionHeader[0]
writeFileSync(packagePath, JSON.stringify(pkg, null, 2))
process.on('exit', () => {
writeFileSync(packagePath, packageObj)
console.log('Restored package.json before exit')
})
// if windows modify appxManifest
if (platform === 'win32') {
const xmlFilePath = resolve(__dirname, 'appx/template.xml')
const appxTemplate = readFileSync(resolve(__dirname, 'appx/template.xml'), 'utf-8')
writeFileSync(xmlFilePath, appxTemplate.replace('{{pkgVersion}}', versionHeader[0] + '.0').replace('{{description}}', description))
process.on('exit', () => {
writeFileSync(xmlFilePath, appxTemplate)
console.log('Restored appx/template.xml before exit')
})
}
doMake()
} else if (process.argv.includes('--postmake')) {
doPostmake()
} else {
console.warn('Require passing --make or --postmake')
process.exit(1)
}