forked from svt/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-common.js
80 lines (69 loc) · 1.94 KB
/
init-common.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
// SPDX-FileCopyrightText: 2024 Sveriges Television AB
//
// SPDX-License-Identifier: MIT
const fs = require('node:fs')
const path = require('node:path')
const assert = require('node:assert')
const paths = require('./paths')
const config = require('./config')
const Logger = require('./Logger')
const logger = new Logger({ name: 'init-common' })
const UserDefaults = require('./UserDefaults')
require('./api')
const DEFAULT_HTTP_PORT = config.defaults.HTTP_PORT
/**
* Verify that an assets file is
* created before running the app,
* hashes are used in order to eliminate
* caching issues
*/
;(function () {
const assetsExist = fs.existsSync(path.join(__dirname, '../assets.json'))
assert(
assetsExist,
'No assets file found, the project must be built before it\'s run: \'npm build\''
)
})()
/**
* Create the plugin directories
* if they don't already exist
*/
;(function () {
logger.debug('Creating plugin directory')
fs.promises.mkdir(paths.plugins, { recursive: true })
})()
/**
* Remove and recreate the temporary directory
* in order to make sure that it's cleared and
* exists
*/
;(function () {
logger.debug('Recreating temporary directory')
try {
fs.rmSync(paths.temp, { force: true, recursive: true })
} catch (err) {
logger.warn('Failed to remove temporary files directory', err)
}
fs.promises.mkdir(paths.temp, { recursive: true })
})()
/**
* Restore user defaults into
* the user defaults-state
*/
;(async function () {
logger.debug('Restoring user deafults', paths.userDefaults)
let json
try {
const data = fs.readFileSync(paths.userDefaults, { encoding: 'utf8' })
json = JSON.parse(data || '{}')
UserDefaults.apply({
...json
})
} catch (err) {
logger.warn('Failed to restore user defaults, maybe it\'s the first time the application is running', err)
} finally {
UserDefaults.apply({
httpPort: process.env.PORT || json?.httpPort || DEFAULT_HTTP_PORT
})
}
})()