UNPKG

3.94 kBJavaScriptView Raw
1process.env.DEBUG = 'app:*';
2const debug = require('debug')('app:demos');
3const commander = require('commander');
4const connect = require('connect');
5const getPort = require('get-port');
6const http = require('http');
7const open = require('open');
8const serveStatic = require('serve-static');
9const parseurl = require('parseurl');
10const assign = require('lodash').assign;
11const path = require('path');
12const resolve = path.resolve;
13const extname = path.extname;
14const basename = path.basename;
15const join = path.join;
16const fs = require('fs');
17const statSync = fs.statSync;
18const lstatSync = fs.lstatSync;
19const readdirSync = fs.readdirSync;
20const readFileSync = fs.readFileSync;
21const mkdirSync = fs.mkdirSync;
22const writeFile = fs.writeFile;
23const nunjucks = require('nunjucks');
24const renderString = nunjucks.renderString;
25const pkg = require('../package.json');
26
27function isFile(source) {
28 return lstatSync(source).isFile();
29}
30
31function getFiles(source) {
32 return readdirSync(source).map(function(name) {
33 return join(source, name);
34 }).filter(isFile);
35}
36
37const screenshotsPath = join(process.cwd(), './demos/assets/screenshots');
38try {
39 statSync(screenshotsPath);
40} catch (e) {
41 mkdirSync(screenshotsPath);
42}
43
44commander
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
50function 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 // transparent: true
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
134if (commander.port) {
135 startService(commander.port);
136} else {
137 getPort().then(port => {
138 startService(port);
139 });
140}