1 | process.env.DEBUG = 'app:*';
|
2 | const debug = require('debug')('app:demos');
|
3 | const commander = require('commander');
|
4 | const connect = require('connect');
|
5 | const getPort = require('get-port');
|
6 | const http = require('http');
|
7 | const open = require('open');
|
8 | const serveStatic = require('serve-static');
|
9 | const parseurl = require('parseurl');
|
10 | const assign = require('lodash').assign;
|
11 | const path = require('path');
|
12 | const resolve = path.resolve;
|
13 | const extname = path.extname;
|
14 | const basename = path.basename;
|
15 | const join = path.join;
|
16 | const fs = require('fs');
|
17 | const statSync = fs.statSync;
|
18 | const lstatSync = fs.lstatSync;
|
19 | const readdirSync = fs.readdirSync;
|
20 | const readFileSync = fs.readFileSync;
|
21 | const mkdirSync = fs.mkdirSync;
|
22 | const writeFile = fs.writeFile;
|
23 | const nunjucks = require('nunjucks');
|
24 | const renderString = nunjucks.renderString;
|
25 | const pkg = require('../package.json');
|
26 |
|
27 | function isFile(source) {
|
28 | return lstatSync(source).isFile();
|
29 | }
|
30 |
|
31 | function getFiles(source) {
|
32 | return readdirSync(source).map(function(name) {
|
33 | return join(source, name);
|
34 | }).filter(isFile);
|
35 | }
|
36 |
|
37 | const screenshotsPath = join(process.cwd(), './demos/assets/screenshots');
|
38 | try {
|
39 | statSync(screenshotsPath);
|
40 | } catch (e) {
|
41 | mkdirSync(screenshotsPath);
|
42 | }
|
43 |
|
44 | commander
|
45 | .version(pkg.version)
|
46 | .option('-w, --web')
|
47 | .option('-p, --port <port>', 'specify a port number to run on', parseInt)
|
48 | .parse(process.argv);
|
49 |
|
50 | function startService(port) {
|
51 | const server = connect();
|
52 | server.use((req, res, next) => {
|
53 | if (req.method === 'GET') {
|
54 | const pathname = parseurl(req).pathname;
|
55 | if (pathname === '/demos/index.html') {
|
56 | const demoFiles = getFiles(__dirname)
|
57 | .filter(filename => {
|
58 | return extname(filename) === '.html' && basename(filename, '.html') !== 'index';
|
59 | })
|
60 | .map(filename => {
|
61 | const bn = basename(filename, '.html');
|
62 | const file = {
|
63 | screenshot: `./assets/screenshots/${bn}.png`,
|
64 | basename: bn,
|
65 | content: readFileSync(filename),
|
66 | filename
|
67 | };
|
68 | return file;
|
69 | });
|
70 | const template = readFileSync(join(__dirname, './index.njk'), 'utf8');
|
71 | const content = renderString(template, {
|
72 | demoFiles
|
73 | });
|
74 | writeFile(join(process.cwd(), './demos/index.html'), content, 'utf8', err => {
|
75 | if (err) throw err;
|
76 | debug('The demo page has been saved!');
|
77 | });
|
78 | res.end(content);
|
79 | } else {
|
80 | next();
|
81 | }
|
82 | } else {
|
83 | next();
|
84 | }
|
85 | });
|
86 | server.use(serveStatic(process.cwd()));
|
87 | http.createServer(server).listen(port);
|
88 |
|
89 | const url = `http://127.0.0.1:${port}/demos/index.html`;
|
90 | debug(`server started, demos available! ${url}`);
|
91 |
|
92 | if (commander.web) {
|
93 | debug('running on web!');
|
94 | open(url);
|
95 | } else {
|
96 | debug('running on electron!');
|
97 | const app = require('electron').app;
|
98 | const BrowserWindow = require('electron').BrowserWindow;
|
99 | const watch = require('@lite-js/torch/lib/watch');
|
100 | const windowBoundsConfig = require('@lite-js/torch/lib/windowBoundsConfig')(
|
101 | resolve(app.getPath('userData'), './data-set-config.json')
|
102 | );
|
103 |
|
104 | let win;
|
105 | app.once('ready', () => {
|
106 | win = new BrowserWindow(assign({
|
107 |
|
108 | webPreferences: {
|
109 | nodeIntegration: false
|
110 | }
|
111 | }, windowBoundsConfig.get('demos')));
|
112 | win.loadURL(url);
|
113 | win.openDevTools();
|
114 |
|
115 | win.on('close', () => {
|
116 | windowBoundsConfig.set('demos', win.getBounds());
|
117 | });
|
118 | win.on('closed', () => {
|
119 | win = null;
|
120 | });
|
121 | watch([
|
122 | 'demos/**/*.*',
|
123 | 'src/**/*.*'
|
124 | ], () => {
|
125 | win.webContents.reloadIgnoringCache();
|
126 | });
|
127 | });
|
128 | app.on('window-all-closed', () => {
|
129 | app.quit();
|
130 | });
|
131 | }
|
132 | }
|
133 |
|
134 | if (commander.port) {
|
135 | startService(commander.port);
|
136 | } else {
|
137 | getPort().then(port => {
|
138 | startService(port);
|
139 | });
|
140 | }
|