Passed
Push — develop ( 76f1bd...bca4a5 )
by Bjarn
01:40 queued 11s
created

src/extensions/pecl.ts   A

Complexity

Total Complexity 11
Complexity/F 2.75

Size

Lines of Code 76
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 11
mnd 7
bc 7
fnc 4
bpm 1.75
cpm 2.75
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Pecl.installExtensions 0 10 3
A Pecl.getExtensionDirectory 0 8 2
A Pecl.getPhpIni 0 15 3
A Pecl.uninstallExtensions 0 10 3
1
import execa from 'execa'
2
import {existsSync} from 'fs'
3
import {PHP_EXTENSIONS} from './extensions'
4
5
class Pecl {
6
    /**
7
     * Get the path of the PHP ini currently used by PECL.
8
     */
9
    static getPhpIni = async (): Promise<string> => {
10
        const peclIni = await execa('pecl', ['config-get', 'php_ini'])
11
        const peclIniPath = peclIni.stdout.replace('\n', '')
12
13
        if (existsSync(peclIniPath))
14
            return peclIniPath
15
16
        const phpIni = await execa('php', ['-i', '|', 'grep', 'php.ini'])
17
18
        const matches = phpIni.stdout.match(/Path => ([^\s]*)/)
19
20
        if (!matches || matches.length <= 0)
21
            throw new Error('Unable to find php.ini.')
22
23
        return `${matches[1].trim()}/php.ini`
24
    }
25
26
    /**
27
     * Get the path of the extension directory currently used by PECL.
28
     */
29
    static getExtensionDirectory = async (): Promise<string> => {
30
        const {stdout} = await execa('pecl', ['config-get', 'ext_dir'])
31
        let directory = stdout.replace('\n', '').trim()
32
33
        if (directory.indexOf('/Cellar/') !== -1)
34
            directory = directory.replace('/lib/php/', '/pecl/')
35
36
        return directory
37
    }
38
39
    /**
40
     * Install all extensions supported by Jale. Set optionals to true to also include optional extensions.
41
     *
42
     * @param optionals
43
     */
44
    static installExtensions = async (optionals: boolean = false) => {
45
        console.log('Installing PECL extensions')
46
47
        for (const extension of PHP_EXTENSIONS) {
48
            const ext = new extension
49
            if (!optionals && !ext.default)
50
                continue
51
52
            await ext.install()
53
            await ext.enable()
54
        }
55
    }
56
57
    /**
58
     * Uninstall all extensions supported by Jale. Set optionals to true to also include optional extensions.
59
     *
60
     * @param optionals
61
     */
62
    static uninstallExtensions = async (optionals: boolean = false) => {
63
        console.log('Uninstalling PECL extensions')
64
65
        for (const extension of PHP_EXTENSIONS) {
66
            const ext = new extension
67
            if (!optionals && !ext.default)
68
                continue
69
70
            await ext.uninstall()
71
            await ext.disable()
72
        }
73
    }
74
}
75
76
export default Pecl