Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Serve and open built files #219

Merged
merged 30 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7bc54be
Allowing mechanism for serving built files
Jul 13, 2017
a88ab54
Fixed merge conflicts from master
Jul 13, 2017
afae356
Cleaned up bad merge
Jul 13, 2017
c60e9ae
Fixed linting errors
Jul 13, 2017
8a8067a
Merge branch 'master' into serve-build
Jul 13, 2017
e7101f1
Merge branch 'master' into serve-build
Jul 14, 2017
047b223
Returning from build. Removing Protractor warning
Jul 14, 2017
a21343d
Merge branch 'serve-build' of github.com:blackbaud/skyux-builder into…
Jul 14, 2017
d1a54ea
Moved files. Fixed bug causing test failure
Jul 14, 2017
8a0808c
Manually setting name if launching after build
Jul 14, 2017
8947d70
Using different server to allow for better redirects
Jul 14, 2017
fb4f88e
Merged conflicts
Blackbaud-SteveBrush Aug 2, 2017
8513ffd
Merge branch 'master' into serve-build
Aug 16, 2017
8c48663
Cleanup
Aug 16, 2017
7b11e75
Fixed conflicts
Aug 16, 2017
8fdf1a6
Cleaned up linting
Aug 16, 2017
a7a7781
Fixed mock
Aug 16, 2017
66f72e9
Merge branch 'master' into serve-build
Aug 16, 2017
2d3cb65
Cleaned up static directory
Aug 17, 2017
c0e743b
Merge branch 'serve-build' of github.com:blackbaud/skyux-builder into…
Aug 18, 2017
53dfb1c
Merged master
Aug 18, 2017
503c7d6
Merge branch 'master' into serve-build
Aug 18, 2017
7d418a5
Merge branch 'master' into serve-build
Blackbaud-SteveBrush Aug 18, 2017
ced2832
Removed unnecessary forward slash. Fixed logging.
Aug 18, 2017
fd07c81
Merge branch 'master' into serve-build
Aug 18, 2017
dc29453
Separated launch from serve functionality. Defaulting launch to host
Aug 18, 2017
70e7479
Merge branch 'serve-build' of github.com:blackbaud/skyux-builder into…
Aug 18, 2017
2085ebb
Refactored to use --serve and correctly set assets
Aug 18, 2017
7c6c298
Always returning promise (if lint passes) from build. Fixed e2e branc…
Aug 21, 2017
bdc59f5
Cleaned up promises
Aug 21, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 45 additions & 20 deletions cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

const fs = require('fs-extra');
const merge = require('merge');

const skyPagesConfigUtil = require('../config/sky-pages/sky-pages.config');
const generator = require('../lib/sky-pages-module-generator');
const assetsProcessor = require('../lib/assets-processor');
const pluginFileProcessor = require('../lib/plugin-file-processor');

const server = require('./utils/server');
const browser = require('./utils/browser');
const runCompiler = require('./utils/run-compiler');
const tsLinter = require('./utils/ts-linter');

Expand Down Expand Up @@ -114,45 +118,66 @@ function cleanupAot() {
fs.removeSync(skyPagesConfigUtil.spaPathTemp());
}

/**
* Executes the build command.
* @name build
*/
function build(argv, skyPagesConfig, webpack) {
const compileModeIsAoT = skyPagesConfig &&
skyPagesConfig.skyux &&
skyPagesConfig.skyux.compileMode === 'aot';

let buildConfig;
function buildServe(argv, skyPagesConfig, webpack, isAot) {
const base = skyPagesConfigUtil.getAppBase(skyPagesConfig);
return server
.start(base)
.then(port => {
argv.assets = argv.assets || `https://localhost:${port}`;
return buildCompiler(argv, skyPagesConfig, webpack, isAot)
.then(stats => {
browser(argv, skyPagesConfig, stats, port);
return stats;
});
});
}

function buildCompiler(argv, skyPagesConfig, webpack, isAot) {
const assetsBaseUrl = argv.assets || '';
const assetsRel = argv.assetsrel;

const lintResult = tsLinter.lintSync();
if (lintResult.exitCode > 0) {
process.exit(lintResult.exitCode);
return;
}
let buildConfig;

if (compileModeIsAoT) {
if (isAot) {
stageAot(skyPagesConfig, assetsBaseUrl, assetsRel);
buildConfig = require('../config/webpack/build-aot.webpack.config');
} else {
buildConfig = require('../config/webpack/build.webpack.config');
}

const config = buildConfig.getWebpackConfig(skyPagesConfig);

assetsProcessor.setSkyAssetsLoaderUrl(config, skyPagesConfig, assetsBaseUrl, assetsRel);

return runCompiler(webpack, config)
return runCompiler(webpack, config, isAot)
.then(stats => {
if (compileModeIsAoT) {
if (isAot) {
cleanupAot();
}

return Promise.resolve(stats);
return stats;
});
}

/**
* Executes the build command.
* @name build
* @param {*} skyPagesConfig
* @param {*} webpack
* @param {*} isAot
*/
function build(argv, skyPagesConfig, webpack) {

const lintResult = tsLinter.lintSync();
const isAot = skyPagesConfig &&
skyPagesConfig.skyux &&
skyPagesConfig.skyux.compileMode === 'aot';

if (lintResult.exitCode > 0) {
process.exit(lintResult.exitCode);
} else {
const name = argv.serve ? buildServe : buildCompiler;
return name(argv, skyPagesConfig, webpack, isAot);
}
}

module.exports = build;
53 changes: 6 additions & 47 deletions cli/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
const glob = require('glob');
const path = require('path');
const spawn = require('cross-spawn');
const logger = require('../utils/logger');
const portfinder = require('portfinder');
const HttpServer = require('http-server');
const selenium = require('selenium-standalone');

const build = require('./build');
const server = require('./utils/server');
const logger = require('../utils/logger');

// Disable this to quiet the output
const spawnOptions = { stdio: 'inherit' };

let httpServer;
let seleniumServer;
let start;

Expand Down Expand Up @@ -45,18 +44,13 @@ function killServers(exitCode) {
seleniumServer = null;
}

if (httpServer) {
logger.info('Closing http server');
httpServer.close();
httpServer = null;
}

// Catch protractor's "Kitchen Sink" error.
if (exitCode === 199) {
logger.warn('Supressing protractor\'s "kitchen sink" error 199');
exitCode = 0;
}

server.stop();
logger.info(`Execution Time: ${(new Date().getTime() - start) / 1000} seconds`);
logger.info(`Exiting process with ${exitCode}`);
process.exit(exitCode || 0);
Expand Down Expand Up @@ -91,6 +85,7 @@ function spawnProtractor(chunks, port, skyPagesConfig) {
protractorPath,
[
getProtractorConfigPath(),
`--disableChecks`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, adding this check to solve the "unknown arguments" warning (soon to be error) that's shown. https://github.com/angular/protractor/blob/master/CHANGELOG.md#features-3

`--baseUrl ${skyPagesConfig.skyux.host.url}`,
`--params.localUrl=https://localhost:${port}`,
`--params.chunks=${JSON.stringify(chunks)}`,
Expand Down Expand Up @@ -150,42 +145,6 @@ function spawnSelenium() {
});
}

/**
* Spawns the httpServer
*/
function spawnServer() {
return new Promise((resolve, reject) => {
logger.info('Requesting open port...');

httpServer = HttpServer.createServer({
root: 'dist/',
cors: true,
https: {
cert: path.resolve(__dirname, '../', 'ssl', 'server.crt'),
key: path.resolve(__dirname, '../', 'ssl', 'server.key')
},
logFn: (req, res, err) => {
if (err) {
reject(err);
return;
}
}
});

portfinder
.getPortPromise()
.then(port => {
logger.info(`Open port found: ${port}`);
logger.info('Starting web server...');
httpServer.listen(port, 'localhost', () => {
logger.info('Web server running.');
resolve(port);
});
})
.catch(reject);
});
}

/**
* Spawns the build process. Captures the config used.
*/
Expand Down Expand Up @@ -218,7 +177,7 @@ function e2e(argv, skyPagesConfig, webpack) {
return killServers(0);
}

spawnServer()
server.start()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much cleaner; love it.

.then((port) => {
argv.assets = 'https://localhost:' + port;

Expand Down
78 changes: 78 additions & 0 deletions cli/utils/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*jslint node: true */
'use strict';

const util = require('util');
const open = require('open');
const logger = require('winston');
const hostUtils = require('../../utils/host-utils');
const skyPagesConfigUtil = require('../../config/sky-pages/sky-pages.config');

/**
* Returns the querystring base for parameters allowed to be passed through.
* PLEASE NOTE: The method is nearly duplicated in `runtime/params.ts`.
* @name getQueryStringFromArgv
* @param {Object} argv
* @param {SkyPagesConfig} skyPagesConfig
* @returns {string}
*/
function getQueryStringFromArgv(argv, skyPagesConfig) {

let found = [];
skyPagesConfig.skyux.params.forEach(param => {
if (argv[param]) {
found.push(`${param}=${encodeURIComponent(argv[param])}`);
}
});

if (found.length) {
return `?${found.join('&')}`;
}

return '';
}

function browser(argv, skyPagesConfig, stats, port) {

const queryStringBase = getQueryStringFromArgv(argv, skyPagesConfig);
let localUrl = util.format(
'https://localhost:%s%s',
port,
skyPagesConfigUtil.getAppBase(skyPagesConfig)
);

let hostUrl = hostUtils.resolve(
queryStringBase,
localUrl,
stats.toJson().chunks,
skyPagesConfig
);

// Edge uses a different technique (protocol vs executable)
if (argv.browser === 'edge') {
const edge = 'microsoft-edge:';
argv.browser = undefined;
hostUrl = edge + hostUrl;
localUrl = edge + localUrl;
}

// Browser defaults to launching host
argv.launch = argv.launch || 'host';

switch (argv.launch) {
case 'local':

// Only adding queryStringBase to the message + local url opened,
// Meaning doesn't need those to communicate back to localhost
localUrl += queryStringBase;

logger.info(`Launching Local URL: ${localUrl}`);
open(localUrl, argv.browser);
break;
case 'host':
logger.info(`Launching Host URL: ${hostUrl}`);
open(hostUrl, argv.browser);
break;
}
}

module.exports = browser;
73 changes: 73 additions & 0 deletions cli/utils/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*jslint node: true */
'use strict';

const fs = require('fs');
const path = require('path');
const logger = require('winston');
const portfinder = require('portfinder');
const express = require('express');
const https = require('https');
const cors = require('cors');

const app = express();

let server;

/**
* Starts the httpServer
* @name start
*/
function start(root) {
return new Promise((resolve, reject) => {

const dist = path.resolve(process.cwd(), 'dist');

logger.info('Creating web server');
app.use(cors());

logger.info(`Exposing static directory: ${dist}`);
app.use(express.static(dist));
if (root) {
logger.info(`Mapping server requests from ${root} to ${dist}`);
app.use(root, express.static(dist));
}

const options = {
cert: fs.readFileSync(path.resolve(__dirname, '../../ssl/server.crt')),
key: fs.readFileSync(path.resolve(__dirname, '../../ssl/server.key'))
};

server = https.createServer(options, app);
server.on('error', reject);

logger.info('Requesting open port...');
portfinder
.getPortPromise()
.then(port => {
logger.info(`Open port found: ${port}`);
logger.info('Starting web server...');
server.listen(port, 'localhost', () => {
logger.info('Web server running.');
resolve(port);
});
})
.catch(reject);
});
}

/**
* Kills the server if it exists
* @name kill
*/
function stop() {
if (server) {
logger.info('Stopping http server');
server.close();
server = null;
}
}

module.exports = {
start: start,
stop: stop
};
4 changes: 2 additions & 2 deletions cli/utils/stage-library-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ function getFileContents(filePath) {
switch (path.extname(filePath)) {
case '.scss':
contents = compileSass(filePath);
break;
break;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear how this got in to start with, but the linter complained to be about the spacing.

case '.html':
contents = getHtmlContents(filePath);
break;
break;
}

contents = contents
Expand Down
Loading