Passed
Push — develop ( 58c45d...f6e8aa )
by Bjarn
01:47
created

SitesController.createNginxConfig   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 11
c 0
b 0
f 0
rs 9.85
cc 2
1
import {writeFileSync} from 'fs'
2
import Nginx from '../services/nginx'
3
import nginxLaravelTemplate from '../templates/nginx/apps/laravel'
4
import nginxMagento1Template from '../templates/nginx/apps/magento1'
5
import nginxMagento2Template from '../templates/nginx/apps/magento2'
6
import nginxMagento1Conf from '../templates/nginx/magento1'
7
import {ensureDirectoryExists} from '../utils/filesystem'
8
import {getConfig, jaleSitesPath} from '../utils/jale'
9
10
class SitesController {
11
12
    appTypes = ['laravel', 'magento2', 'magento1']
13
14
    executeLink = async (type: string | undefined): Promise<void> => {
15
        const config = await getConfig()
16
        let appType = config.defaultTemplate
17
18
        if (type)
19
            appType = type
20
21
        if (!this.appTypes.includes(appType)) {
22
            console.log(`Invalid app type ${appType}. Please select one of: ${this.appTypes.join(', ')}`)
23
            return
24
        }
25
26
        const domain = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
27
        const hostname = `${domain}.${config.domain}`
28
29
        await ensureDirectoryExists(jaleSitesPath)
30
31
        this.createNginxConfig(appType, hostname)
32
33
        await (new Nginx()).reload()
34
    }
35
36
    /**
37
     * Create a Nginx template for the provided hostname with a specific template.
38
     *
39
     * @param appType
40
     * @param hostname
41
     */
42
    createNginxConfig = (appType: string, hostname: string): void => {
43
        switch (appType) {
44
        case 'magento2':
45
            writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento2Template(hostname, process.cwd()))
46
            break
47
        case 'magento1':
48
            writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento1Template(hostname, process.cwd()))
49
            break
50
        default:
51
            writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxLaravelTemplate(hostname, process.cwd()))
52
            break
53
        }
54
    }
55
56
57
}
58
59
export default SitesController