UNPKG

3.26 kBJavaScriptView Raw
1const os = require("os");
2const path = require("path");
3
4const {
5 getAppPathFromProjectData,
6 getAppResourcesPathFromProjectData,
7 getProjectDir,
8 isAndroid,
9} = require("../projectHelpers");
10
11const eventHandlers = {};
12
13function debuggingEnabled(liveSyncService, projectDir) {
14 const deviceDescriptors = liveSyncService.getLiveSyncDeviceDescriptors(projectDir);
15 return deviceDescriptors.some(device => device.debugggingEnabled);
16}
17
18function 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 * Checks if there's a file in the following pattern 5e0326f3bb50f9f26cf0.hot-update.json
36 * if yes this is a HMR update and remove all bundle files as we don't need them to be synced,
37 * but only the update chunks
38 */
39function 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 // remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
49 result = result.filter(file => file !== `${name}.js`);
50 });
51 //if applying of hot update fails, we must fallback to the full files
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 * Parse the filename of the hot update chunk.
62 * @param name bundle.deccb264c01d6d42416c.hot-update.js
63 * @returns { name: string, hash: string } { name: 'bundle', hash: 'deccb264c01d6d42416c' }
64 */
65function 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
74function 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
81function addListener(eventEmitter, name, handler) {
82 if (!eventHandlers[name]) {
83 eventEmitter.on(name, handler);
84 eventHandlers[name] = handler;
85 }
86}
87
88function removeListener(eventEmitter, name) {
89 if (eventHandlers[name]) {
90 eventEmitter.removeListener(name, eventHandlers[name]);
91 delete eventHandlers[name];
92 }
93}
94
95module.exports = {
96 buildEnvData,
97 debuggingEnabled,
98 shouldSnapshot,
99 getUpdatedEmittedFiles,
100 parseHotUpdateChunkName,
101 addListener,
102 removeListener
103};