Passed
Pull Request — develop (#87)
by
unknown
01:43
created

src/controllers/sitesController.ts   A

Complexity

Total Complexity 17
Complexity/F 4.25

Size

Lines of Code 126
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 101
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 17
mnd 13
bc 13
fnc 4
bpm 3.25
cpm 4.25
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
B SitesController.listLinks 0 21 8
A SitesController.executeUnlink 0 32 4
A SitesController.executeLink 0 25 3
A SitesController.createNginxConfig 0 11 2
1
import Table from 'cli-table'
2
import {existsSync, readdirSync, unlinkSync, writeFileSync} from 'fs'
3
import Nginx from '../services/nginx'
4
import nginxLaravelTemplate from '../templates/nginx/apps/laravel'
5
import nginxMagento1Template from '../templates/nginx/apps/magento1'
6
import nginxMagento2Template from '../templates/nginx/apps/magento2'
7
import {error, info, success, url} from '../utils/console'
8
import {ensureDirectoryExists} from '../utils/filesystem'
9
import {getConfig, jaleSitesPath} from '../utils/jale'
10
import SecureController from './secureController'
11
import kleur from 'kleur'
12
13
class SitesController {
14
15
    appTypes = ['laravel', 'magento2', 'magento1']
16
17
    listLinks = async (): Promise<void> => {
18
        const config = await getConfig()
19
        await ensureDirectoryExists(jaleSitesPath)
20
        const sites = readdirSync(jaleSitesPath).map(fileName => fileName.replace(`.${config.tld}.conf`, ''))
21
22
        if (sites.length) {
23
            info(`Currently there ${sites.length > 1 ? 'are' : 'is'} ${sites.length} active Nginx vhost ${sites.length > 1 ? 'configurations' : 'configuration'}\n`)
24
25
            const table = new Table({
26
                head: ['Project', 'Secure'],
27
                colors: false
28
            })
29
30
            for (const site of sites) {
31
                const secure = new SecureController(site).isSecure()
32
                table.push([`${site}.${config.tld}`, (secure ? kleur.green('Yes') : kleur.red('No'))])
33
            }
34
35
            console.log(table.toString())
36
        } else {
37
            info(`Currently there ${sites.length > 1 ? 'are' : 'is'} no active Nginx vhost ${sites.length > 1 ? 'configurations' : 'configuration'}`)
38
        }
39
    }
40
41
    executeLink = async (type: string | undefined, name: string | undefined): Promise<void> => {
42
        const config = await getConfig()
43
        let appType = config.defaultTemplate
44
45
        if (type)
46
            appType = type
47
48
        if (!this.appTypes.includes(appType)) {
49
            error(`Invalid app type ${appType}. Please select one of: ${this.appTypes.join(', ')}`)
50
            return
51
        }
52
53
        const project = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
54
        const domain = name || project
55
        const hostname = `${domain}.${config.tld}`
56
57
        info(`Linking ${project} to ${hostname}...`)
58
59
        await ensureDirectoryExists(jaleSitesPath)
60
61
        this.createNginxConfig(appType, hostname, project)
62
63
        await (new Nginx()).reload()
64
65
        success(`Successfully linked ${domain}. Access it from ${url(`http://${hostname}`)}.`)
66
    }
67
68
    executeUnlink = async (): Promise<void> => {
69
        const config = await getConfig()
70
71
        const project = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
72
73
        let filename = `${project}.${config.tld}.conf`
74
75
        readdirSync(jaleSitesPath).forEach(file => {
76
            if (file.includes(project)) {
77
                filename = file
78
            }
79
        })
80
81
        // const hostname = `${domain}.${config.tld}`
82
83
        if (!existsSync(`${jaleSitesPath}/${filename}`)) {
84
            error(`This project doesn't seem to be linked because the configuration file can't be found: ${jaleSitesPath}/${filename}`)
85
            return
86
        }
87
88
        info(`Unlinking ${project}...`)
89
90
        const secureController = new SecureController
91
92
        if (existsSync(secureController.crtPath))
93
            await secureController.executeUnsecure()
94
95
        unlinkSync(`${jaleSitesPath}/${filename}`)
96
97
        await (new Nginx()).reload()
98
99
        success(`Successfully unlinked ${project}.`)
100
    }
101
102
    /**
103
     * Create a Nginx template for the provided hostname with a specific template.
104
     *
105
     * @param appType
106
     * @param hostname
107
     * @param project
108
     */
109
    createNginxConfig = (appType: string, hostname: string, project: string): void => {
110
        switch (appType) {
111
        case 'magento2':
112
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxMagento2Template(hostname, process.cwd()))
113
            break
114
        case 'magento1':
115
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxMagento1Template(hostname, process.cwd()))
116
            break
117
        default:
118
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxLaravelTemplate(hostname, process.cwd()))
119
            break
120
        }
121
    }
122
123
124
}
125
126
export default SitesController