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): 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 domain = process.cwd().substring(process.cwd().lastIndexOf('/') + 1) |
54
|
|
|
const hostname = `${domain}.${config.tld}` |
55
|
|
|
|
56
|
|
|
info(`Linking ${domain} to ${hostname}...`) |
57
|
|
|
|
58
|
|
|
await ensureDirectoryExists(jaleSitesPath) |
59
|
|
|
|
60
|
|
|
this.createNginxConfig(appType, hostname) |
61
|
|
|
|
62
|
|
|
await (new Nginx()).reload() |
63
|
|
|
|
64
|
|
|
success(`Successfully linked ${domain}. Access it from ${url(`http://${hostname}`)}.`) |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
executeUnlink = async (): Promise<void> => { |
68
|
|
|
const config = await getConfig() |
69
|
|
|
|
70
|
|
|
const domain = process.cwd().substring(process.cwd().lastIndexOf('/') + 1) |
71
|
|
|
const hostname = `${domain}.${config.tld}` |
72
|
|
|
|
73
|
|
|
if (!existsSync(`${jaleSitesPath}/${hostname}.conf`)) { |
74
|
|
|
error(`This project doesn't seem to be linked because the configuration file can't be found: ${jaleSitesPath}/${hostname}.conf`) |
75
|
|
|
return |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
info(`Unlinking ${hostname}...`) |
79
|
|
|
|
80
|
|
|
const secureController = new SecureController |
81
|
|
|
|
82
|
|
|
if (existsSync(secureController.crtPath)) |
83
|
|
|
await secureController.executeUnsecure() |
84
|
|
|
|
85
|
|
|
unlinkSync(`${jaleSitesPath}/${hostname}.conf`) |
86
|
|
|
|
87
|
|
|
await (new Nginx()).reload() |
88
|
|
|
|
89
|
|
|
success(`Successfully unlinked ${domain}.`) |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create a Nginx template for the provided hostname with a specific template. |
94
|
|
|
* |
95
|
|
|
* @param appType |
96
|
|
|
* @param hostname |
97
|
|
|
*/ |
98
|
|
|
createNginxConfig = (appType: string, hostname: string): void => { |
99
|
|
|
switch (appType) { |
100
|
|
|
case 'magento2': |
101
|
|
|
writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento2Template(hostname, process.cwd())) |
102
|
|
|
break |
103
|
|
|
case 'magento1': |
104
|
|
|
writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento1Template(hostname, process.cwd())) |
105
|
|
|
break |
106
|
|
|
default: |
107
|
|
|
writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxLaravelTemplate(hostname, process.cwd())) |
108
|
|
|
break |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
export default SitesController |