Skip to content

Commit 8f6cd91

Browse files
committed
feat(server): continue server refactor
1 parent 8b96ded commit 8f6cd91

File tree

2 files changed

+114
-122
lines changed

2 files changed

+114
-122
lines changed

packages/core/src/index.js

Lines changed: 9 additions & 16 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 serverModule = require('./lib/server'); // eslint-disable-line
28+
let server = require('./lib/server'); // eslint-disable-line
2929

3030
//bootstrap update notifier
3131
updateNotifier({
@@ -54,8 +54,7 @@ const getVersion = function() {
5454
const patternlab_module = function(config) {
5555
const PatternLabClass = require('./lib/patternlab');
5656
const patternlab = new PatternLabClass(config);
57-
58-
const self = this;
57+
server = server(patternlab);
5958

6059
const _api = {
6160
/**
@@ -229,23 +228,17 @@ const patternlab_module = function(config) {
229228
* @returns {Promise} a promise fulfilled when build is complete
230229
*/
231230
server: {
232-
serve: function(options) {
233-
console.log(self);
234-
options.watch = true;
235-
return self
236-
.build(options)
237-
.then(() => serverModule.serve(patternlab))
238-
.then(() => Promise.resolve())
231+
serve: options => {
232+
const _options = Object.assign({}, options, { watch: true });
233+
return _api
234+
.build(_options)
235+
.then(() => server.serve())
239236
.catch(e =>
240237
logger.error(`error inside core index.js server serve: ${e}`)
241238
);
242239
},
243-
reload: function() {
244-
return serverModule.reload(); //TODO - will this work, or does the promise need to be setup here?
245-
},
246-
refreshCSS: function() {
247-
return serverModule.refreshCSS(); //TODO - see above
248-
},
240+
reload: server.reload,
241+
refreshCSS: server.refreshCSS,
249242
},
250243

251244
events: patternlab.events,

packages/core/src/lib/server.js

Lines changed: 105 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,131 @@
11
'use strict';
2+
23
const path = require('path');
34
const liveServer = require('@pattern-lab/live-server');
45

56
const events = require('./events');
67
const logger = require('./log');
78

8-
// initialize serverReady outside of the serve method
9+
const server = patternlab => {
10+
const _module = {
11+
serve: () => {
12+
let serverReady = false;
913

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+
};
1122

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+
);
1739

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+
);
2246

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+
});
3354

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+
};
5065

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+
});
5789

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(() => {
62101
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';
64107
liveServer.refreshCSS();
65108
} else {
109+
action = 'reloaded';
66110
liveServer.reload();
67111
}
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`);
81114
}
82115
}, 1000);
116+
} catch (e) {
117+
reject(`Server reload or refresh failed: ${e}`);
83118
}
84119
});
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',
102125
});
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;
125129
};
126130

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

Comments
 (0)