-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·130 lines (104 loc) · 3.79 KB
/
index.ts
File metadata and controls
executable file
·130 lines (104 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import {execSync} from "child_process";
import chalk from "chalk";
const checkSudo = () => {
const sudo_euid = 0;
const euid = process.geteuid();
if (euid != sudo_euid) {
console.log("Re-run with sudo, please.");
process.exit(1);
}
};
const checkArgs = () => {
if (process.argv.length != 3) {
printHelp();
process.exit(1);
}
};
const printHelp = () => {
let text = `Usage:\n`;
text += `sphp X.Y switch php to version X.Y\n`;
text += `sphp --list print versions available`;
console.log(text);
};
type TVersionSupportMap = {[version: string]: {cli: boolean; a2: boolean}};
const printVersionList = (versionAvailableMap: TVersionSupportMap) => {
const yesMark = chalk.green("yes");
const noMark = chalk.red("no");
let text = "";
text += `${chalk.bold("version")} ${chalk.bold("cli")} ${chalk.bold("a2")}\n`;
const versions = Object.keys(versionAvailableMap);
versions.forEach((version, idx) => {
text += `${version} ${versionAvailableMap[version].cli ? yesMark : noMark} ${versionAvailableMap[version].a2 ? yesMark : noMark}`;
if (idx < versions.length - 1) {
text += `\n`;
}
});
console.log(text);
};
const printUnsupportedVersion = (targetVersion: string) => {
let text = chalk.red(`error: unsupported version '${targetVersion}' (check with 'sphp --list')`);
console.log(text);
};
const getPhpVersionsInstalled: () => TVersionSupportMap = () => {
const cmd_php_installed = `dpkg --list | grep php`;
const out_php_installed = execSync(cmd_php_installed).toString();
const phpCliRegex = /^ii\s+php\d\.\d\s+/gm;
const phpA2Regex = /^ii\s+libapache2-mod-php\d\.\d\s+/gm;
const cliVersions = out_php_installed.match(phpCliRegex).map((match) => match.match(/\d\.\d/)[0]);
const a2Versions = out_php_installed.match(phpA2Regex).map((match) => match.match(/\d\.\d/)[0]);
//get array of all versions installed - both cli and a2 - concatenate and uniquize
const allVersions = Array.from(new Set([...cliVersions, ...a2Versions]));
//now build a map: keys are php versions, values are cli/a2 support flags
const versionSupportMap = allVersions.reduce((acc, version) => {
return {
...acc,
[version]: {
cli: cliVersions.includes(version),
a2: a2Versions.includes(version)
}
};
}, {});
return versionSupportMap;
};
const switchPhpCLI = (phpTarget) => {
process.stdout.write(`switching CLI version.. `);
execSync(`sudo update-alternatives --set php /usr/bin/php${phpTarget}`);
process.stdout.write(`${chalk.green(`done`)}\n`);
};
const switchPhpA2 = (phpTarget) => {
process.stdout.write(`switching A2 version.. `);
const cmd_php_enabled = `sudo a2query -m`;
const out_php_enabled = execSync(cmd_php_enabled).toString();
const phpEnabledRegex = /^php\d\.\d/gm;
const phpEnabledVersions = out_php_enabled.match(phpEnabledRegex) || [];
const cmd_a2_disable_php_versions = phpEnabledVersions.map((v) => `sudo a2dismod ${v} && `).join("");
execSync(`${cmd_a2_disable_php_versions} sudo a2enmod php${phpTarget} && sudo service apache2 restart`);
process.stdout.write(`${chalk.green(`done`)}\n`);
};
const main = () => {
//check prerequisities
checkSudo();
checkArgs();
//get php versions available
const phpInstalled = getPhpVersionsInstalled();
if (process.argv[2] == "--list") {
printVersionList(phpInstalled);
process.exit(0);
} else if (process.argv[2] == "--help") {
printHelp();
process.exit(0);
} else if (process.argv[2].match(/\d\.\d/gm) == null) {
printHelp();
process.exit(0);
}
const phpTarget = process.argv[2];
const phpTargetSupport = phpInstalled[phpTarget];
if (phpTargetSupport == undefined || phpTargetSupport.cli == false || phpTargetSupport.a2 == false) {
//version has to be supported by both, cli and a2
printUnsupportedVersion(phpTarget);
process.exit(1);
}
switchPhpCLI(phpTarget);
switchPhpA2(phpTarget);
};
main();