1 | const URL = require('url')
|
2 | const path = require('path')
|
3 | const os = require('os')
|
4 | const fs = require('fs')
|
5 |
|
6 | const embeddedGit = require('./embedded-git.json')
|
7 |
|
8 | function getConfig() {
|
9 | const config = {
|
10 | outputPath: path.join(__dirname, '..', 'git'),
|
11 | source: '',
|
12 | checksum: '',
|
13 | fileName: '',
|
14 | tempFile: ''
|
15 | }
|
16 |
|
17 | const key = `${process.platform}-${os.arch()}`
|
18 |
|
19 | const entry = embeddedGit[key]
|
20 |
|
21 | if (entry != null) {
|
22 | config.checksum = entry.checksum
|
23 | config.source = entry.url
|
24 | } else {
|
25 | console.log(`No embedded Git found for ${process.platform} and architecture ${os.arch()}`)
|
26 | }
|
27 |
|
28 | if (config.source !== '') {
|
29 |
|
30 | const url = URL.parse(config.source)
|
31 | const pathName = url.pathname
|
32 | const index = pathName.lastIndexOf('/')
|
33 | config.fileName = pathName.substr(index + 1)
|
34 |
|
35 | const cacheDirEnv = process.env.DUGITE_CACHE_DIR
|
36 |
|
37 | const cacheDir = cacheDirEnv ? path.resolve(cacheDirEnv) : os.tmpdir()
|
38 |
|
39 | try {
|
40 | fs.statSync(cacheDir)
|
41 | } catch (e) {
|
42 | fs.mkdirSync(cacheDir)
|
43 | }
|
44 |
|
45 | config.tempFile = path.join(cacheDir, config.fileName)
|
46 | }
|
47 |
|
48 | return config
|
49 | }
|
50 |
|
51 | module.exports = getConfig
|