-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (97 loc) · 2.1 KB
/
index.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
/*
*
* MIT License
*
* Copyright (c) Marc Espín Sanz
*
* Full license on License.md
*
*/
function isInstalled({ alias = 'flutter' } = {}){
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
exec(alias, out => {
resolve( !out )
});
})
}
function getDevices({ alias = 'flutter' } = {}){
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
const process = exec(`${alias} devices`);
parsingDevices( process ).then( devices => {
resolve({
msg: devices.length == 0 ? 'No devices found.' : 'Found devices:',
devices: devices
})
})
});
}
function app({ path, deviceId, alias = 'flutter' }){
let execProcess;
return {
run({ onData = ()=>{}, onExit = ()=>{}, onClose = ()=>{} } = {}) {
const { exec } = require("child_process");
execProcess = exec(`cd ${path} && ${alias} run -d ${deviceId}`);
execProcess.stdout.on('data', data => {
onData(data)
})
execProcess.stdout.on('exit', data => {
onExit(data)
})
execProcess.stdout.on('close', data => {
onClose(data)
})
},
reload: () => {
execProcess.stdin.write('r')
},
close: () => {
execProcess.stdin.write('q')
}
}
}
function parsingDevices(process){
let devices = []
let devicesCount = 0
return new Promise( (resolve, reject) => {
process.stdout.on('data', data => {
const device = getDevice(data,devicesCount)
if( device.totalDevices ){
devicesCount = device.totalDevices
}else if( device[0] ){
devices.push({
name: device[0].trim(),
id: device[1].trim(),
arch: device[2].trim(),
api: device[3].trim(),
})
}
if( devicesCount == devices.length && device.parsed !== false ){
resolve(devices)
}
});
})
}
function getDevice(data,count){
const splittedData = data.split('•')
if( !splittedData[0].match('^Waiting') ){
if( count == 0 ){
const char = splittedData[0][0]
return {
totalDevices : char.match('N') ? 0 : Number(char)
}
}else{
return splittedData
}
}else{
return {
parsed: false
};
}
}
module.exports = {
isInstalled,
getDevices,
app
}