forked from raphamorim/react-tv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
183 lines (150 loc) · 4.63 KB
/
run.js
File metadata and controls
183 lines (150 loc) · 4.63 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
const execSync = require('child_process').execSync;
function defaultCLIEnv() {
//return '/opt/tizen/tools/ide/bin';
return 'E:/Ferramentas/tizen/tools/ide/bin';
}
function getPackageId(root) {
const appinfo = path.resolve(root, 'react-tv/tizen/config.xml');
const content = fs.readFileSync(appinfo, {encoding: 'utf-8'});
const re = new RegExp(/tizen:application id="(.*?)"/);
const matches = content.match(re);
if (!matches) {
return null;
}
return matches[1];
}
function isReactTVTizenProject(root) {
const appinfo = path.resolve(root, 'react-tv/tizen/config.xml');
if (fs.existsSync(appinfo)) {
return true;
}
return false;
}
function run(root) {
let tizen_CLI_ENV = process.env['TIZEN_CLI'] || false;
if (!tizen_CLI_ENV) {
tizen_CLI_ENV = defaultCLIEnv();
}
process.env['PATH'] = `${tizen_CLI_ENV}:${process.env['PATH']}`;
if (!isReactTVTizenProject(root)) {
const msg = `This project isn\'t a React-TV Tizen Project:
Just run "react-tv init"`;
return console.log(chalk.dim('[react-tv]'), msg);
}
const packageJson = require(path.resolve(root, 'package.json'));
const ReactTVConfig = packageJson['react-tv'];
if (!ReactTVConfig) {
return console.log(
chalk.dim('[react-tv]'),
'You should set react-tv properties on package.json'
);
}
if (!ReactTVConfig.files || !ReactTVConfig.files.length) {
return console.log(chalk.dim('[react-tv]'), 'You should add files');
}
// TODO: option to create/select profiles?
const securityProfiles = execSync(
`${tizen_CLI_ENV}/tizen security-profiles list`
)
.toString()
.trim()
.split('\n');
if (!securityProfiles) {
return console.log(
chalk.dim('[react-tv]'),
'No tizen security profiles found'
);
}
// Select the last profile
const selectedProfile = securityProfiles[securityProfiles.length - 1]
.split(' ', 1)[0]
.trim();
const tizenPath = path.resolve(root, 'react-tv/tizen');
process.on('exit', cleanup);
process.on('SIGINT', cleanup);
process.on('SIGUSR1', cleanup);
process.on('SIGUSR2', cleanup);
process.on('uncaughtException', cleanup);
function cleanup() {
fs.removeSync(`${tizenPath}/icon.png`);
ReactTVConfig.files.forEach(file => {
fs.removeSync(`${tizenPath}/${file}`);
});
}
try {
cleanup();
fs.copySync(`${root}/react-tv/icon-large.png`, `${tizenPath}/icon.png`);
ReactTVConfig.files.forEach(file => {
const filePath = path.resolve(root, file);
const toFile = path.resolve(tizenPath, file);
fs.ensureDirSync(path.dirname(toFile));
fs.copySync(`${filePath}`, `${toFile}`);
});
} catch (e) {
return console.log('FAIL TO MOUNT', e.toString());
}
console.log('');
console.log(chalk.dim('Setting up Emulator...'));
const vms = execSync(
`${tizen_CLI_ENV}/../../emulator/bin/em-cli list-vm`
).toString();
if (vms.indexOf('react-tv-tizen') < 0) {
execSync(
`${
tizen_CLI_ENV
}/../../emulator/bin/em-cli create -n react-tv-tizen -p tv-samsung-3.0-x86`
);
}
const runningVms = execSync(`${tizen_CLI_ENV}/../../sdb devices`).toString();
if (runningVms.indexOf('react-tv-tizen') < 0) {
console.log(chalk.dim('Running Emulator...'));
execSync(
`${tizen_CLI_ENV}/../../emulator/bin/em-cli launch -n react-tv-tizen`
);
console.log(chalk.yellow(' Tizen Emulator successful running'));
} else {
console.log(chalk.yellow(' already running.'));
}
console.log(chalk.dim('Packing...'));
execSync(
`cd ${tizenPath} && ${tizen_CLI_ENV}/tizen package -t wgt -s ${
selectedProfile
}`
);
console.log(chalk.yellow(` succefull pack from ${root}`));
console.log(chalk.dim('Running App...'));
let attemps = 0;
const task = setInterval(function() {
if (attemps > 15) {
console.log('FAILED TO UP Tizen emulator');
clearInterval(task);
}
try {
execSync(`${tizen_CLI_ENV}/../../sdb devices`).toString();
execSync(
`cd ${tizenPath} && ${tizen_CLI_ENV}/tizen install -n ${
packageJson['name']
}.wgt -t react-tv-tizen`
);
} catch (error) {
if (error.stdout.toString().indexOf('install completed') < 0) {
attemps += 1;
return false;
}
}
clearInterval(task);
const packageId = getPackageId(root);
if (!packageId) {
return console.log('Invalid package id!');
}
execSync(
`cd ${tizenPath} && ${tizen_CLI_ENV}/tizen run -p ${
packageId
} -t react-tv-tizen`
);
}, 500);
}
module.exports = run;