1 | const os = require("os");
|
2 | const path = require("path");
|
3 |
|
4 | const {
|
5 | getAppPathFromProjectData,
|
6 | getAppResourcesPathFromProjectData,
|
7 | getProjectDir,
|
8 | isAndroid,
|
9 | } = require("../projectHelpers");
|
10 |
|
11 | const eventHandlers = {};
|
12 |
|
13 | function debuggingEnabled(liveSyncService, projectDir) {
|
14 | const deviceDescriptors = liveSyncService.getLiveSyncDeviceDescriptors(projectDir);
|
15 | return deviceDescriptors.some(device => device.debugggingEnabled);
|
16 | }
|
17 |
|
18 | function buildEnvData($projectData, platform, env) {
|
19 | const envData = Object.assign({},
|
20 | env,
|
21 | { [platform.toLowerCase()]: true }
|
22 | );
|
23 |
|
24 | const appPath = getAppPathFromProjectData($projectData);
|
25 | const appResourcesPath = getAppResourcesPathFromProjectData($projectData);
|
26 | Object.assign(envData,
|
27 | appPath && { appPath },
|
28 | appResourcesPath && { appResourcesPath }
|
29 | );
|
30 |
|
31 | return envData;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function getUpdatedEmittedFiles(emittedFiles) {
|
40 | let fallbackFiles = [];
|
41 | let hotHash;
|
42 | if (emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
|
43 | let result = emittedFiles.slice();
|
44 | const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
|
45 | hotUpdateScripts.forEach(hotUpdateScript => {
|
46 | const { name, hash } = parseHotUpdateChunkName(hotUpdateScript);
|
47 | hotHash = hash;
|
48 |
|
49 | result = result.filter(file => file !== `${name}.js`);
|
50 | });
|
51 |
|
52 | fallbackFiles = emittedFiles.filter(file => result.indexOf(file) === -1);
|
53 | return { emittedFiles: result, fallbackFiles, hash: hotHash };
|
54 | }
|
55 | else {
|
56 | return { emittedFiles, fallbackFiles };
|
57 | }
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function parseHotUpdateChunkName(name) {
|
66 | const matcher = /^(.+)\.(.+)\.hot-update/gm;
|
67 | const matches = matcher.exec(name);
|
68 | return {
|
69 | name: matches[1] || "",
|
70 | hash: matches[2] || "",
|
71 | };
|
72 | }
|
73 |
|
74 | function shouldSnapshot(config) {
|
75 | const platformSupportsSnapshot = isAndroid(config.platform);
|
76 | const osSupportsSnapshot = os.type() !== "Windows_NT";
|
77 |
|
78 | return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot;
|
79 | }
|
80 |
|
81 | function addListener(eventEmitter, name, handler) {
|
82 | if (!eventHandlers[name]) {
|
83 | eventEmitter.on(name, handler);
|
84 | eventHandlers[name] = handler;
|
85 | }
|
86 | }
|
87 |
|
88 | function removeListener(eventEmitter, name) {
|
89 | if (eventHandlers[name]) {
|
90 | eventEmitter.removeListener(name, eventHandlers[name]);
|
91 | delete eventHandlers[name];
|
92 | }
|
93 | }
|
94 |
|
95 | module.exports = {
|
96 | buildEnvData,
|
97 | debuggingEnabled,
|
98 | shouldSnapshot,
|
99 | getUpdatedEmittedFiles,
|
100 | parseHotUpdateChunkName,
|
101 | addListener,
|
102 | removeListener
|
103 | };
|