Skip to content

Commit

Permalink
Allows env specific kyt.configs for build command (#70)
Browse files Browse the repository at this point in the history
* saving for now. not working

* turning kytconfig into function. still not working yet

* works now

* moving kytConfig to utils

* saving for now. not working

* turning kytconfig into function. still not working yet

* works now

* moving kytConfig to utils

* updating everything except test.

* cleanup

* Adding path info

* moving loadConfigAndDo inline

* removing program param
  • Loading branch information
ccpricenytimes authored and delambo committed Aug 29, 2016
1 parent 51d3b99 commit 13c84b4
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 75 deletions.
2 changes: 2 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ You can change ports in the [kyt config](/config/kytConfig.md).
### build
The `build` command takes the entry index.js in client and server, compiles them, and saves them to a build folder. This is a production build and includes minification and tree shaking (with Webpack 2).

Build uses option `-c`(`--config`) to denote a path to a different [kyt.config.js](/config/kytConfig.md) file

### run
The `run` command takes the compiled code from the production build and runs a node server at the specified port.
You can change ports in the kyt config.
Expand Down
2 changes: 1 addition & 1 deletion cli/actions/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const printAssets = require('../../utils/printAssets');
const buildConfigs = require('../../utils/buildConfigs');
const webpackCompiler = require('../../utils/webpackCompiler');

module.exports = () => {
module.exports = (program) => {
logger.start('Starting production build...');

let serverCompiler;
Expand Down
3 changes: 1 addition & 2 deletions cli/actions/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const SingleChild = require('single-child');
const logger = require('./../logger');
const kytConfig = require('./../../config/kyt.config');
const ifPortIsFreeDo = require('../../utils/ifPortIsFreeDo');
const buildConfigs = require('../../utils/buildConfigs');
const webpackCompiler = require('../../utils/webpackCompiler');

module.exports = () => {
logger.start('Starting development build...');
// Clean the build directory.
const buildPath = path.resolve(kytConfig.userRootPath, './build');
const buildPath = path.resolve(global.config.userRootPath, './build');

if (shell.test('-d', buildPath) && shell.rm('-rf', buildPath).code === 0) {
logger.task('Cleaned ./build');
Expand Down
6 changes: 4 additions & 2 deletions cli/actions/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const path = require('path');
const CLIEngine = require('eslint').CLIEngine;
const shell = require('shelljs');
const logger = require('./../logger');
const kytConfig = require('./../../config/kyt.config');

module.exports = () => {
const userRootPath = global.config.userRootPath;

// http://eslint.org/docs/developer-guide/nodejs-api
const eslintCLI = {
envs: ['browser', 'mocha'],
Expand All @@ -24,7 +25,8 @@ module.exports = () => {
logger.log(formatter(report.results));
};

const esLintPath = path.join(kytConfig.userRootPath, './.eslintrc');

const esLintPath = path.join(userRootPath, './.eslintrc');

// Check to see if eslint file exists
if (!shell.test('-f', esLintPath)) {
Expand Down
3 changes: 1 addition & 2 deletions cli/actions/lintStyle.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

const stylelint = require('stylelint');
const logger = require('./../logger');
const kytConfig = require('./../../config/kyt.config');

module.exports = () => {
const userRootPath = kytConfig.userRootPath;
const userRootPath = global.config.userRootPath;

stylelint.lint({
files: [`${userRootPath}/src/**/*.css`, `${userRootPath}/src/**/*.scss`],
Expand Down
10 changes: 6 additions & 4 deletions cli/actions/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ const path = require('path');
const shell = require('shelljs');
const logger = require('./../logger');
const ifPortIsFreeDo = require('./../../utils/ifPortIsFreeDo');
const kytConfig = require('./../../config/kyt.config');
const baseConfig = require('./../../config/webpack.base');
const protoConfig = require('./../../config/webpack.proto');

module.exports = () => {
const port = kytConfig.prototypePort;
const userRootPath = kytConfig.userRootPath;
const port = global.config.prototypePort;
const userRootPath = global.config.userRootPath;
const options = {
type: 'prototype',
environment: 'prototype',
port,
publicDir: path.join(userRootPath, 'src/public'),
userRootPath,
buildPath: path.join(userRootPath, 'build'),
clientAssetsFile: 'publicAssets.json',
};

const startPrototype = () => {
// Build webpack config
let webpackConfig = merge.smart(baseConfig(options), protoConfig(options));
webpackConfig = kytConfig.modifyWebpackConfig(webpackConfig, options);
webpackConfig = global.config.modifyWebpackConfig(webpackConfig, options);

// Preparing prototype compiler
let compiler = null;
Expand Down
3 changes: 1 addition & 2 deletions cli/actions/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

const shell = require('shelljs');
const logger = require('./../logger');
const kytConfig = require('./../../config/kyt.config');

module.exports = () => {
logger.start('Starting production...');
shell.exec('node build/server/main.js', { async: true });
logger.end(`Server running at http://localhost:${kytConfig.serverPort}`);
logger.end(`Server running at http://localhost:${global.config.serverPort}`);
};
3 changes: 1 addition & 2 deletions cli/actions/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ const fs = require('fs');
const shell = require('shelljs');
const simpleGit = require('simple-git')();
const logger = require('./../logger');
const kytConfig = require('../../config/kyt.config');

module.exports = (program) => {
const args = program.args[0];

const userRootPath = kytConfig.userRootPath;
const userRootPath = global.config.userRootPath;
const userSrc = path.join(userRootPath, 'src');
const packageJSONPath = path.join(userRootPath, 'package.json');
const nodeModulesPath = path.join(userRootPath, 'node_modules');
Expand Down
11 changes: 5 additions & 6 deletions cli/actions/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ const glob = require('glob');
const logger = require('./../logger');
const shell = require('shelljs');
const merge = require('webpack-merge');
const kytConfig = require('./../../config/kyt.config');
let testConfig = require('../../config/webpack.test');
const baseConfig = require('../../config/webpack.base');
const webpackCompiler = require('../../utils/webpackCompiler');


module.exports = () => {
// Comment the following to see verbose shell ouput.
shell.config.silent = true;

const userRootPath = kytConfig.userRootPath;
const userRootPath = global.config.userRootPath;
const userSrc = path.join(userRootPath, 'src');
const userBuild = path.join(userRootPath, 'build/test');
const avaCLI = path.resolve(userRootPath, './node_modules/ava/cli.js');
const npath = path.resolve(userRootPath, './node_modules');
Expand Down Expand Up @@ -73,7 +72,7 @@ module.exports = () => {
const babelLoader = base.module.loaders.find(loader => loader.loader === 'babel-loader');
babelLoader.compact = true;
webpackConfig = merge.smart(base, testConfig(options));
webpackConfig = kytConfig.modifyWebpackConfig(webpackConfig, options);
webpackConfig = global.config.modifyWebpackConfig(webpackConfig, options);
} catch (error) {
logger.error('Error Loading the Test Webpack Config', error);
process.exit();
Expand All @@ -90,12 +89,12 @@ module.exports = () => {
logger.info('Starting test...');

let command = `NODE_PATH=$NODE_PATH:${npath} node ${avaCLI} ${userRootPath}/build/test/*.js`;
if (kytConfig.debug) command += ' --verbose';

if (global.config.debug) command += ' --verbose';
shell.config.silent = false;
shell.exec(command);
});


logger.info('Compiling...');
compiler.run(() => undefined);
};
28 changes: 20 additions & 8 deletions cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path');

// define user root
process.env.USER_ROOT = path.resolve(process.cwd());

const exitIfOldNodeVersion = require('./../utils/exitIfOldNodeVersion');
const program = require('commander');
const devAction = require('./actions/dev');
Expand All @@ -21,9 +22,16 @@ const runAction = require('./actions/run');
const protoAction = require('./actions/proto');
const setupAction = require('./actions/setup');
const lintStyleAction = require('./actions/lintStyle');
const kytConfigFn = require('./../utils/kytConfig');

exitIfOldNodeVersion();

const loadConfigAndDo = (callback, optionalConfig) => {
kytConfigFn(optionalConfig);
callback(program);
};


program
.command('lint')
.description(`lint .js and .jsx files in the ./src directory.
Expand All @@ -33,43 +41,47 @@ program
If you want to lint your own, add a comma-delimited list.
kyt lint -d src/,test/
`)
.action(() => lintAction(program));
.action(() => loadConfigAndDo(lintAction));

program
.command('dev')
.description('Start an express server for development')
.action(() => devAction(program));
.action(() => loadConfigAndDo(devAction));

program
.command('build')
.option('-C, --config <path>', 'config path')
.description('Create a production build')
.action(() => buildAction(program));
.action(() => {
let config = program.args[0].config ? program.args[0].config: null;
loadConfigAndDo(buildAction, config);
});

program
.command('run')
.description('Run the production build')
.action(() => runAction(program));
.action(() => loadConfigAndDo(runAction));

program
.command('setup')
.description('Generate a project from a github url to get started.')
.option('-r, --repository [address]', 'Github repository address')
.action(() => setupAction(program));
.action(() => loadConfigAndDo(setupAction));

program
.command('test')
.description('Run test files with Ava.')
.action(() => testAction(program));
.action(() => loadConfigAndDo(testAction));

program
.command('lint-style')
.description('')
.action(() => lintStyleAction(program));
.action(() => loadConfigAndDo(lintStyleAction));

program
.command('proto')
.description('Start a prorotype dev server.')
.action(() => protoAction(program));
.action(() => loadConfigAndDo(protoAction));


program.parse(process.argv);
4 changes: 1 addition & 3 deletions cli/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

const kytConfig = require('../config/kyt.config');

const logger = console;
const write = (status, text, verbose) => {
let textToLog = '';
Expand Down Expand Up @@ -56,7 +54,7 @@ const info = (text) => {
// Verbose output
// takes optional data
const debug = (text, data) => {
if (kytConfig.debug) {
if (global.config.debug) {
write('debug', text, data);
}
};
Expand Down
38 changes: 0 additions & 38 deletions config/kyt.config.js

This file was deleted.

8 changes: 8 additions & 0 deletions config/kytConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ Define the function in your `kyt.config.js` and it will be called as each webpac

Dev Tip:
[webpack-merge](https://github.com/survivejs/webpack-merge) is a helpful tool for changing and combining Webpack configs.


## Creating env specific kyt configs
kyt allows developers to specify a different kyt config in `build` command for the purpose of creating environment specific configurations.
```
kyt build -c kyt.stage.js
```
option `-c` or `--config` takes a configuration path from the root of your project.
10 changes: 5 additions & 5 deletions utils/buildConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const merge = require('webpack-merge');
const logger = require('../cli/logger');
const clone = require('ramda').clone;
// base configs
const kytConfig = require('../config/kyt.config');
const baseConfig = require('../config/webpack.base');
// dev configs
const devClientConfig = require('../config/webpack.dev.client');
Expand All @@ -17,7 +16,8 @@ const prodClientConfig = require('../config/webpack.prod.client');
const prodServerConfig = require('../config/webpack.prod.server');

module.exports = (environment = 'development') => {
const { clientPort, serverPort, userRootPath, reactHotLoader } = kytConfig;

const { clientPort, serverPort, userRootPath, reactHotLoader } = global.config;
const buildPath = path.join(userRootPath, 'build');

let clientConfig = devClientConfig;
Expand All @@ -42,7 +42,7 @@ module.exports = (environment = 'development') => {
serverConfig = prodServerConfig;
clientOptions = merge(clientOptions, {
clientPort: undefined,
publicPath: kytConfig.productionPublicPath,
publicPath: global.config.productionPublicPath,
// In production, we use the relative path
// from build/client/*.js or build/server/*.js.
publicDir: '../public',
Expand All @@ -61,8 +61,8 @@ module.exports = (environment = 'development') => {

// Modify via userland config
try {
clientConfig = kytConfig.modifyWebpackConfig(clone(clientConfig), clientOptions);
serverConfig = kytConfig.modifyWebpackConfig(clone(serverConfig), serverOptions);
clientConfig = global.config.modifyWebpackConfig(clone(clientConfig), clientOptions);
serverConfig = global.config.modifyWebpackConfig(clone(serverConfig), serverOptions);
} catch (error) {
logger.error('Error in your kyt.config.js modifyWebpackConfig():', error);
process.exit(1);
Expand Down
43 changes: 43 additions & 0 deletions utils/kytConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

// Merges base and user kyt config

const path = require('path');
const shell = require('shelljs');
const baseConfig = require('./../config/kyt.base.config');
const merge = require('ramda').merge;

module.exports = (optionalConfig) => {
if (global.config) return;

const userConfigPath = optionalConfig ?
path.join(process.env.USER_ROOT, optionalConfig) :
path.join(process.env.USER_ROOT, './kyt.config.js');
let config;
const logger = console;

// Add base config option for productionPublicPath
baseConfig.productionPublicPath = '/assets/';
// Find user config
if (shell.test('-f', userConfigPath)) {
try {
logger.info(`Using kyt config at ${userConfigPath}`);
config = require(userConfigPath); // eslint-disable-line global-require
} catch (error) {
logger.error('Error loading your kyt.config.js:', error);
process.exit();
}
}

config = merge(baseConfig, config);
// add userRootPath to Config
config.userRootPath = process.env.USER_ROOT;

// Create default modify function
if (typeof config.modifyWebpackConfig !== 'function') {
config.modifyWebpackConfig = (webpackConfig) => webpackConfig;
}

// In case `reactHotLoader` is undefined, make it a boolean
config.reactHotLoader = !!config.reactHotLoader;
global.config = Object.freeze(config);
};

0 comments on commit 13c84b4

Please sign in to comment.