Passed
Push — main ( 7025c8...8c0a07 )
by Bjarn
03:07 queued 01:44
created

src/controllers/sitesController.ts   A

Complexity

Total Complexity 5
Complexity/F 2.5

Size

Lines of Code 58
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 5
mnd 3
bc 3
fnc 2
bpm 1.5
cpm 2.5
noi 0

2 Functions

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