-
Notifications
You must be signed in to change notification settings - Fork 3
/
legacy.js
157 lines (143 loc) · 3.99 KB
/
legacy.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const { distros, get } = require('./lib/distro')
const { getPackageManager } = require('./lib/pkgManagers.js')
const { pathWinToLinux } = require('./lib/winVsLinux')
const name = () => get('NAME')
const v = () => get('VERSION_ID')
const isRoot = () => {
if (process.platform !== 'win32') {
return (process.getuid && process.getuid() === 0) || false
} else {
try {
require('child_process').execFileSync('net', ['session'], { stdio: 'ignore' })
return true
} catch (e) {
return false
}
}
}
const sudo = () => (isRoot() ? '' : 'sudo ')
const getPM = (operation = 'install', dist, version) =>
getPackageManager(dist ? dist : name(), operation, version ? version : v())
// Unique tags per distro and version
// eg const ts = tags();
// ['Alpine Linux3','Alpine Linux']
function getKeyByValue(object, value) {
return Object.keys(object).find((key) => object[key] === value)
}
const tags = (prefix = '') => {
if (process.platform === 'android') {
// For now support for android platforms is limited.
return ["Android", "Android", "Android"]
}
const nm = name()
const dv = v()
const k = getKeyByValue(distros, nm)
const tag2Number = parseInt(dv)
const tag3Number = parseInt(dv.replace('.', '').replace(',', ''))
return [
prefix + k,
prefix + k + tag2Number,
prefix + k + (tag2Number !== tag3Number ? tag3Number : dv.replace('.', '').replace(',', '')),
]
}
const getTags = (distroName, version, prefix = '') => {
if (!version && distroName) {
throw new Error(`You need to pass both 'name' and 'version' or none.`)
}
if (!distroName) {
distroName = name()
version = v() //convert to number
}
version += ''
const k = getKeyByValue(distros, distroName)
const result = [
prefix + k,
prefix + k + parseInt(version),
prefix + k + parseInt(version.replace('.', '').replace(',', '')),
]
const prop = (no) => ({
enumerable: false,
value: function () {
return this[no]
},
})
Object.defineProperties(result, {
first: prop(0),
second: prop(1),
third: prop(2),
})
return result
}
const replacePMByDistro = (cmd, distro, version) => {
const replacerInstall = getPM('install', distro, version)
const replacerUpdate = getPM('update', distro, version)
const replacerUninstall = getPM('uninstall', distro, version)
const replacerSearch = getPM('search', distro, version)
if (!cmd.replace) {
console.error(`@nexssp/os: There was an error. command should be string, but received: `, cmd)
process.exitCode = 1
} else {
return cmd
.replace(
new RegExp(
'(?:sudo?:(.*))?(apt -y install|apt-get -y install|apt-get install -y|apt install -y|apt install|apt-get install)',
'gs'
),
replacerInstall
)
.replace(
new RegExp(
'(?:sudo?:(.*))?(apt -y update|apt-get -y update|apt-get update -y|apt update -y|apt update|apt-get update)',
'gs'
),
replacerUpdate
)
.replace(
new RegExp(
'(?:sudo?:(.*))?(apt -y remove|apt-get -y remove|apt-get remove -y|apt remove -y|apt remove|apt-get remove)',
'gs'
),
replacerUninstall
)
.replace(
new RegExp(
'(?:sudo?:(.*))?(apt -y search|apt-get -y search|apt-get search -y|apt search -y|apt search|apt-get search)',
'gs'
),
replacerSearch
)
}
}
const getShell = (distro) => {
if (!distro) {
if (process.platform === 'freebsd') return '/bin/csh'
if (process.platform === 'darwin') return '/bin/zsh'
if (process.platform === 'win32') return true
distro = name()
}
switch (distro) {
case distros.ALPINE:
case distros.AMAZON_AMI:
return '/bin/sh'
case distros.WINDOWS:
return true
case distros.MACOS:
return '/bin/zsh'
default:
return '/bin/bash'
}
}
module.exports = {
isRoot,
sudo,
name,
v,
distros,
get,
getPM,
tags,
getTags,
replacePMByDistro,
getShell,
pathWinToLinux,
}