|
1 | 1 | 'use strict';
|
| 2 | + |
2 | 3 | const path = require('path');
|
3 | 4 | const liveServer = require('@pattern-lab/live-server');
|
4 | 5 |
|
5 | 6 | const events = require('./events');
|
6 | 7 | const logger = require('./log');
|
7 | 8 |
|
8 |
| -// initialize serverReady outside of the serve method |
| 9 | +const server = patternlab => { |
| 10 | + const _module = { |
| 11 | + serve: () => { |
| 12 | + let serverReady = false; |
9 | 13 |
|
10 |
| -let serverReady = false; |
| 14 | + // our default liveserver config |
| 15 | + const defaults = { |
| 16 | + open: true, |
| 17 | + file: 'index.html', |
| 18 | + logLevel: 0, // errors only |
| 19 | + wait: 1000, |
| 20 | + port: 3000, |
| 21 | + }; |
11 | 22 |
|
12 |
| -// this would be a private init to manage stuff for all exposed module methods |
13 |
| -const getServerReady = () => serverReady; |
14 |
| -const setServerReady = bool => { |
15 |
| - serverReady = bool; |
16 |
| -}; |
| 23 | + const servers = Object.keys(patternlab.uikits).map(kit => { |
| 24 | + const uikit = patternlab.uikits[kit]; |
| 25 | + defaults.root = path.resolve( |
| 26 | + path.join( |
| 27 | + process.cwd(), |
| 28 | + uikit.outputDir, |
| 29 | + patternlab.config.paths.public.root |
| 30 | + ) |
| 31 | + ); |
| 32 | + defaults.ignore = path.resolve( |
| 33 | + path.join( |
| 34 | + process.cwd(), |
| 35 | + uikit.outputDir, |
| 36 | + patternlab.config.paths.public.root |
| 37 | + ) |
| 38 | + ); |
17 | 39 |
|
18 |
| -const serve = patternlab => { |
19 |
| - //externalize the serverReady flag |
20 |
| - //let serverReady = false; |
21 |
| - setServerReady(false); |
| 40 | + // allow for overrides should they exist inside patternlab-config.json |
| 41 | + const liveServerConfig = Object.assign( |
| 42 | + {}, |
| 43 | + defaults, |
| 44 | + patternlab.config.serverOptions |
| 45 | + ); |
22 | 46 |
|
23 |
| - // our default liveserver config |
24 |
| - const defaults = { |
25 |
| - root: patternlab.config.paths.public.root, |
26 |
| - open: true, |
27 |
| - ignore: path.join(path.resolve(patternlab.config.paths.public.root)), |
28 |
| - file: 'index.html', |
29 |
| - logLevel: 0, // errors only |
30 |
| - wait: 1000, |
31 |
| - port: 3000, |
32 |
| - }; |
| 47 | + const setupEventWatchers = () => { |
| 48 | + // watch for asset changes, and reload appropriately |
| 49 | + patternlab.events.on(events.PATTERNLAB_PATTERN_ASSET_CHANGE, data => { |
| 50 | + if (serverReady) { |
| 51 | + _module.reload(data); |
| 52 | + } |
| 53 | + }); |
33 | 54 |
|
34 |
| - return Promise.all( |
35 |
| - _.map(patternlab.uikits, uikit => { |
36 |
| - defaults.root = path.resolve( |
37 |
| - path.join( |
38 |
| - process.cwd(), |
39 |
| - uikit.outputDir, |
40 |
| - patternlab.config.paths.public.root |
41 |
| - ) |
42 |
| - ); |
43 |
| - defaults.ignore = path.resolve( |
44 |
| - path.join( |
45 |
| - process.cwd(), |
46 |
| - uikit.outputDir, |
47 |
| - patternlab.config.paths.public.root |
48 |
| - ) |
49 |
| - ); |
| 55 | + //watch for pattern changes, and reload |
| 56 | + patternlab.events.on(events.PATTERNLAB_PATTERN_CHANGE, () => { |
| 57 | + if (serverReady) { |
| 58 | + _module.reload({ |
| 59 | + file: '', |
| 60 | + action: 'reload', |
| 61 | + }); |
| 62 | + } |
| 63 | + }); |
| 64 | + }; |
50 | 65 |
|
51 |
| - // allow for overrides should they exist inside patternlab-config.json |
52 |
| - const liveServerConfig = Object.assign( |
53 |
| - {}, |
54 |
| - defaults, |
55 |
| - patternlab.config.serverOptions |
56 |
| - ); |
| 66 | + //start! |
| 67 | + //There is a new server instance for each uikit |
| 68 | + const serveKit = new Promise((resolve, reject) => { |
| 69 | + let resolveMsg = ''; |
| 70 | + setTimeout(() => { |
| 71 | + try { |
| 72 | + liveServer.start(liveServerConfig); |
| 73 | + resolveMsg = `Pattern Lab is being served from http://127.0.0.1:${ |
| 74 | + liveServerConfig.port |
| 75 | + }`; |
| 76 | + logger.info(resolveMsg); |
| 77 | + } catch (e) { |
| 78 | + const err = `Pattern Lab serve failed to start: ${e}`; |
| 79 | + logger.error(`Pattern Lab serve failed to start: ${e}`); |
| 80 | + reject(err); |
| 81 | + } |
| 82 | + setupEventWatchers(); |
| 83 | + serverReady = true; |
| 84 | + resolve(resolveMsg); |
| 85 | + }, liveServerConfig.wait); |
| 86 | + }); |
| 87 | + return serveKit; |
| 88 | + }); |
57 | 89 |
|
58 |
| - // watch for asset changes, and reload appropriately |
59 |
| - patternlab.events.on(events.PATTERNLAB_PATTERN_ASSET_CHANGE, data => { |
60 |
| - if (getServerReady()) { |
61 |
| - const reload = setInterval(() => { |
| 90 | + return Promise.all(servers); |
| 91 | + }, |
| 92 | + reload: data => { |
| 93 | + const _data = data || { |
| 94 | + file: '', |
| 95 | + action: '', |
| 96 | + }; |
| 97 | + return new Promise((resolve, reject) => { |
| 98 | + let action; |
| 99 | + try { |
| 100 | + const reloadInterval = setInterval(() => { |
62 | 101 | if (!patternlab.isBusy) {
|
63 |
| - if (data.file.indexOf('css') > -1) { |
| 102 | + if ( |
| 103 | + _data.file.indexOf('css') > -1 || |
| 104 | + _data.action === 'refresh' |
| 105 | + ) { |
| 106 | + action = 'refreshed CSS'; |
64 | 107 | liveServer.refreshCSS();
|
65 | 108 | } else {
|
| 109 | + action = 'reloaded'; |
66 | 110 | liveServer.reload();
|
67 | 111 | }
|
68 |
| - clearInterval(reload); |
69 |
| - } |
70 |
| - }, 1000); |
71 |
| - } |
72 |
| - }); |
73 |
| - |
74 |
| - //watch for pattern changes, and reload |
75 |
| - patternlab.events.on(events.PATTERNLAB_PATTERN_CHANGE, () => { |
76 |
| - if (getServerReady()) { |
77 |
| - const reload = setInterval(() => { |
78 |
| - if (!patternlab.isBusy) { |
79 |
| - liveServer.reload(); |
80 |
| - clearInterval(reload); |
| 112 | + clearInterval(reloadInterval); |
| 113 | + resolve(`Server ${action} successfully`); |
81 | 114 | }
|
82 | 115 | }, 1000);
|
| 116 | + } catch (e) { |
| 117 | + reject(`Server reload or refresh failed: ${e}`); |
83 | 118 | }
|
84 | 119 | });
|
85 |
| - |
86 |
| - return new Promise((resolve, reject) => { |
87 |
| - //start! |
88 |
| - setTimeout(() => { |
89 |
| - try { |
90 |
| - liveServer.start(liveServerConfig); |
91 |
| - logger.info( |
92 |
| - `Pattern Lab is being served from http://127.0.0.1:${ |
93 |
| - liveServerConfig.port |
94 |
| - }` |
95 |
| - ); |
96 |
| - setServerReady(true); |
97 |
| - resolve('Server started!'); |
98 |
| - } catch (e) { |
99 |
| - reject(e); |
100 |
| - } |
101 |
| - }, liveServerConfig.wait); |
| 120 | + }, |
| 121 | + refreshCSS: () => { |
| 122 | + return _module.reload({ |
| 123 | + file: '', |
| 124 | + action: 'refresh', |
102 | 125 | });
|
103 |
| - }) |
104 |
| - ); |
105 |
| -}; |
106 |
| - |
107 |
| -const reload = () => { |
108 |
| - return new Promise((resolve, reject) => { |
109 |
| - if (!getServerReady()) { |
110 |
| - reject('Cannot reload because server is not ready'); |
111 |
| - } |
112 |
| - liveServer.reload(); |
113 |
| - resolve('Server reloaded'); |
114 |
| - }); |
115 |
| -}; |
116 |
| - |
117 |
| -const refreshCSS = () => { |
118 |
| - return new Promise((resolve, reject) => { |
119 |
| - if (!getServerReady()) { |
120 |
| - reject('Cannot reload because server is not ready'); |
121 |
| - } |
122 |
| - liveServer.refreshCSS(); |
123 |
| - resolve('CSS refreshed'); |
124 |
| - }); |
| 126 | + }, |
| 127 | + }; |
| 128 | + return _module; |
125 | 129 | };
|
126 | 130 |
|
127 |
| -//expose as 'server' module with methods serve, reload, and refreshCSS |
128 |
| -module.exports = { |
129 |
| - serve, |
130 |
| - reload, |
131 |
| - refreshCSS, |
132 |
| -}; |
| 131 | +module.exports = server; |
0 commit comments