Skip to content

Commit a3d65c3

Browse files
committed
feat(server): beginning of refator
1 parent 9a04ed3 commit a3d65c3

File tree

3 files changed

+164
-97
lines changed

3 files changed

+164
-97
lines changed

packages/core/src/index.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let fs = require('fs-extra'); // eslint-disable-line
2525
let ui_builder = require('./lib/ui_builder'); // eslint-disable-line
2626
let copier = require('./lib/copier'); // eslint-disable-line
2727
let pattern_exporter = new pe(); // eslint-disable-line
28-
let serve = require('./lib/serve'); // eslint-disable-line
28+
let serverModule = require('./lib/server'); // eslint-disable-line
2929

3030
//bootstrap update notifier
3131
updateNotifier({
@@ -46,7 +46,9 @@ const patternlab_module = function(config) {
4646
const PatternLabClass = require('./lib/patternlab');
4747
const patternlab = new PatternLabClass(config);
4848

49-
return {
49+
const self = this;
50+
51+
const _api = {
5052
/**
5153
* Returns current version
5254
*
@@ -90,10 +92,15 @@ const patternlab_module = function(config) {
9092
.then(() => {
9193
patternlab.isBusy = false;
9294
// only wire up this listener and the one inside serve.js
95+
// figure out how to detect if serve was called. we should not assume it was
9396
if (
94-
this.events.listenerCount(
95-
events.PATTERNLAB_PATTERN_CHANGE
96-
) === 1
97+
patternlab.serverReady //check for server presence
98+
? this.events.listenerCount(
99+
events.PATTERNLAB_PATTERN_CHANGE
100+
) === 1 //if the server is started, it has already setup one listener
101+
: !this.events.listenerCount(
102+
events.PATTERNLAB_PATTERN_CHANGE
103+
) // else, check for the presnce of none
97104
) {
98105
this.events.on(events.PATTERNLAB_PATTERN_CHANGE, () => {
99106
if (!patternlab.isBusy) {
@@ -212,16 +219,30 @@ const patternlab_module = function(config) {
212219
* @param {bool} options.watch **ALWAYS OVERRIDDEN to `true`** whether or not Pattern Lab should watch configured `source/` directories for changes to rebuild
213220
* @returns {Promise} a promise fulfilled when build is complete
214221
*/
215-
serve: function(options) {
216-
options.watch = true;
217-
return this.build(options).then(function() {
218-
serve(patternlab);
219-
return Promise.resolve();
220-
});
222+
server: {
223+
serve: function(options) {
224+
console.log(self);
225+
options.watch = true;
226+
return self
227+
.build(options)
228+
.then(() => serverModule.serve(patternlab))
229+
.then(() => Promise.resolve())
230+
.catch(e =>
231+
logger.error(`error inside core index.js server serve: ${e}`)
232+
);
233+
},
234+
reload: function() {
235+
return serverModule.reload(); //TODO - will this work, or does the promise need to be setup here?
236+
},
237+
refreshCSS: function() {
238+
return serverModule.refreshCSS(); //TODO - see above
239+
},
221240
},
222241

223242
events: patternlab.events,
224243
};
244+
245+
return _api;
225246
};
226247

227248
patternlab_module.getDefaultConfig = getDefaultConfig;

packages/core/src/lib/serve.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

packages/core/src/lib/server.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
'use strict';
2+
const path = require('path');
3+
const liveServer = require('@pattern-lab/live-server');
4+
5+
const events = require('./events');
6+
const logger = require('./log');
7+
8+
// initialize serverReady outside of the serve method
9+
10+
let serverReady = false;
11+
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+
};
17+
18+
const serve = patternlab => {
19+
//externalize the serverReady flag
20+
//let serverReady = false;
21+
setServerReady(false);
22+
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+
};
33+
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+
);
50+
51+
// allow for overrides should they exist inside patternlab-config.json
52+
const liveServerConfig = Object.assign(
53+
{},
54+
defaults,
55+
patternlab.config.serverOptions
56+
);
57+
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(() => {
62+
if (!patternlab.isBusy) {
63+
if (data.file.indexOf('css') > -1) {
64+
liveServer.refreshCSS();
65+
} else {
66+
liveServer.reload();
67+
}
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);
81+
}
82+
}, 1000);
83+
}
84+
});
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);
102+
});
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+
});
125+
};
126+
127+
//expose as 'server' module with methods serve, reload, and refreshCSS
128+
module.exports = {
129+
serve,
130+
reload,
131+
refreshCSS,
132+
};

0 commit comments

Comments
 (0)