Passed
Push — main ( 91e463...358ba1 )
by Bjarn
01:37 queued 10s
created

SitesController.executeUnlink   A

Complexity

Conditions 3

Size

Total Lines 23
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
dl 0
loc 23
rs 9.352
c 0
b 0
f 0
1
import {existsSync, unlinkSync, 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 {error, info, success, url} from '../utils/console'
7
import {ensureDirectoryExists} from '../utils/filesystem'
8
import {getConfig, jaleSitesPath} from '../utils/jale'
9
import SecureController from './secureController'
10
11
class SitesController {
12
13
    appTypes = ['laravel', 'magento2', 'magento1']
14
15
    executeLink = async (type: string | undefined): Promise<void> => {
16
        const config = await getConfig()
17
        let appType = config.defaultTemplate
18
19
        if (type)
20
            appType = type
21
22
        if (!this.appTypes.includes(appType)) {
23
            error(`Invalid app type ${appType}. Please select one of: ${this.appTypes.join(', ')}`)
24
            return
25
        }
26
27
        const domain = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
28
        const hostname = `${domain}.${config.tld}`
29
30
        info(`Linking ${domain} to ${hostname}...`)
31
32
        await ensureDirectoryExists(jaleSitesPath)
33
34
        this.createNginxConfig(appType, hostname)
35
36
        await (new Nginx()).reload()
37
38
        success(`Successfully linked ${domain}. Access it from ${url(`http://${hostname}`)}.`)
39
    }
40
41
    executeUnlink = async (): Promise<void> => {
42
        const config = await getConfig()
43
44
        const domain = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
45
        const hostname = `${domain}.${config.tld}`
46
47
        if (!existsSync(`${jaleSitesPath}/${hostname}.conf`)) {
48
            error(`This project doesn't seem to be linked because the configuration file can't be found: ${jaleSitesPath}/${hostname}.conf`)
49
            return
50
        }
51
52
        info(`Unlinking ${hostname}...`)
53
54
        const secureController = new SecureController
55
56
        if (existsSync(secureController.crtPath))
57
            await secureController.executeUnsecure()
58
59
        unlinkSync(`${jaleSitesPath}/${hostname}.conf`)
60
61
        await (new Nginx()).reload()
62
63
        success(`Successfully unlinked ${domain}.`)
64
    }
65
66
    /**
67
     * Create a Nginx template for the provided hostname with a specific template.
68
     *
69
     * @param appType
70
     * @param hostname
71
     */
72
    createNginxConfig = (appType: string, hostname: string): void => {
73
        switch (appType) {
74
        case 'magento2':
75
            writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento2Template(hostname, process.cwd()))
76
            break
77
        case 'magento1':
78
            writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento1Template(hostname, process.cwd()))
79
            break
80
        default:
81
            writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxLaravelTemplate(hostname, process.cwd()))
82
            break
83
        }
84
    }
85
86
87
}
88
89
export default SitesController