Skip to content

Commit

Permalink
initialize ...
Browse files Browse the repository at this point in the history
  • Loading branch information
thondery committed Dec 28, 2016
1 parent edbb8c7 commit f5cd8e0
Show file tree
Hide file tree
Showing 28 changed files with 1,145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/kn-react-redux.js
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict'

require('../index')
36 changes: 36 additions & 0 deletions index.js
@@ -0,0 +1,36 @@
const program = require('commander')
const _ = require('lodash')
const pkg = require('./package.json')
const init = require('./libs/init')
const create = require('./libs/create')
const version = pkg.version

program
.version(version)

program
.usage('[command] [options]')

program
.command('create [dir]')
.description('Create a project for React')
.action( function (dir, options){
const name = _.isString(program.name) ? program.name : undefined
return create(dir, name);
});

program
.command('init')
.description('Initialize a project for React')
.action( function (dir, options){
const name = _.isString(program.name) ? program.name : undefined
return init(name)
})

program
.option('-n, --name <name>', 'project name')

// Parse and fallback to help if no args
if (_.isEmpty(program.parse(process.argv).args) && process.argv.length === 2) {
program.help()
}
22 changes: 22 additions & 0 deletions libs/base.js
@@ -0,0 +1,22 @@
const fs = require('fs-extra')
const path = require('path')

const createPackage = (dir, name, author) => {
const pkg = {}
const tpl = fs.readJSONSync(path.resolve(__dirname, 'tpl/package.json'))
pkg.name = name || 'react-redux-project'
pkg.version = '1.0.0'
pkg.main = 'index.js'
pkg = Object.assign(pkg, tpl)
console.log('Creating package.json')
fs.writeJSONSync(path.resolve(dir, 'package.json'), pkg)
}

exports.createPackage = createPackage

const createApp = (dir, name) => {
console.log('Creating app')
fs.copySync(path.resolve(__dirname, 'tpl/app'), dir)
}

exports.createApp = createApp
17 changes: 17 additions & 0 deletions libs/create.js
@@ -0,0 +1,17 @@
const base = require('./base')
const path = require('path')
const _ = require('lodash')

const create = (dir, name) => {
if (!_.isString(dir) || dir.length === 0) {
console.log('Path must be completed')
return
}
const BASEDIR = /^([a-zA-Z]{1}\:\\)/.test(dir) ? dir : path.resolve(process.env.PWD, dir)
const __BASEDIR__ = BASEDIR.replace(/\s/g, '_')
console.log(__BASEDIR__)
base.createApp(__BASEDIR__, name)
base.createPackage(__BASEDIR__)
}

module.exports = create
9 changes: 9 additions & 0 deletions libs/init.js
@@ -0,0 +1,9 @@
const base = require('./base')

const init = (name) => {
const __BASEDIR__ = process.env.PWD
base.createApp(__BASEDIR__, name)
base.createPackage(__BASEDIR__)
}

module.exports = init
25 changes: 25 additions & 0 deletions libs/tpl/app/.eslintrc
@@ -0,0 +1,25 @@
{
"parser": "babel-eslint",
"extends": [
"standard",
"standard-react"
],
"plugins": [
"babel-eslint/plugin",
"babel",
"react",
"promise"
],
"env": {
"browser" : true,
"es6": true,
"node": true
},
"rules": {
"key-spacing" : 0,
"jsx-quotes" : [2, "prefer-single"],
"max-len" : [2, 120, 2],
"object-curly-spacing" : [2, "always"],
"object-shorthand" : [2, "always"]
}
}
55 changes: 55 additions & 0 deletions libs/tpl/app/bin/compile.js
@@ -0,0 +1,55 @@
const fs = require('fs-extra')
const webpack = require('webpack')
const debug = require('debug')('app:bin:compile')
const webpackConfig = require('../config/webpack.config')
const project = require('../config/project.config')

const webpackCompiler = (webpackConfig) =>
new Promise((resolve, reject) => {
const compiler = webpack(webpackConfig)

compiler.run((err, stats) => {
if (err) {
debug('Webpack compiler encountered a fatal error.', err)
return reject(err)
}

const jsonStats = stats.toJson()
debug('Webpack compile completed.')
debug(stats.toString(project.compiler_stats))

if (jsonStats.errors.length > 0) {
debug('Webpack compiler encountered errors.')
debug(jsonStats.errors.join('\n'))
return reject(new Error('Webpack compiler encountered errors'))
} else if (jsonStats.warnings.length > 0) {
debug('Webpack compiler encountered warnings.')
debug(jsonStats.warnings.join('\n'))
} else {
debug('No errors or warnings encountered.')
}
resolve(jsonStats)
})
})

const compile = () => {
debug('Starting compiler.')
return Promise.resolve()
.then(() => webpackCompiler(webpackConfig))
.then(stats => {
if (stats.warnings.length && project.compiler_fail_on_warning) {
throw new Error('Config set to fail on warning, exiting with status code "1".')
}
debug('Copying static assets to dist folder.')
fs.copySync(project.paths.public(), project.paths.dist())
})
.then(() => {
debug('Compilation completed successfully.')
})
.catch((err) => {
debug('Compiler encountered an error.', err)
process.exit(1)
})
}

compile()
6 changes: 6 additions & 0 deletions libs/tpl/app/bin/dev-server.js
@@ -0,0 +1,6 @@
const project = require('../config/project.config')
const server = require('../server/main')
const debug = require('debug')('app:bin:dev-server')

server.listen(project.server_port)
debug(`Server is now running at http://localhost:${project.server_port}.`)
29 changes: 29 additions & 0 deletions libs/tpl/app/config/environments.config.js
@@ -0,0 +1,29 @@
// Here is where you can define configuration overrides based on the execution environment.
// Supply a key to the default export matching the NODE_ENV that you wish to target, and
// the base configuration will apply your overrides before exporting itself.
module.exports = {
// ======================================================
// Overrides when NODE_ENV === 'development'
// ======================================================
// NOTE: In development, we use an explicit public path when the assets
// are served webpack by to fix this issue:
// http://stackoverflow.com/questions/34133808/webpack-ots-parsing-error-loading-fonts/34133809#34133809
development : (config) => ({
compiler_public_path : `http://${config.server_host}:${config.server_port}/`
}),

// ======================================================
// Overrides when NODE_ENV === 'production'
// ======================================================
production : (config) => ({
compiler_public_path : '/',
compiler_fail_on_warning : false,
compiler_hash_type : 'chunkhash',
compiler_devtool : null,
compiler_stats : {
chunks : true,
chunkModules : true,
colors : true
}
})
}
73 changes: 73 additions & 0 deletions libs/tpl/app/config/karma.config.js
@@ -0,0 +1,73 @@
const argv = require('yargs').argv
const project = require('./project.config')
const webpackConfig = require('./webpack.config')
const debug = require('debug')('app:config:karma')

debug('Creating configuration.')
const karmaConfig = {
basePath : '../', // project root in relation to bin/karma.js
files : [
{
pattern : `./${project.dir_test}/test-bundler.js`,
watched : false,
served : true,
included : true
}
],
singleRun : !argv.watch,
frameworks : ['mocha'],
reporters : ['mocha'],
preprocessors : {
[`${project.dir_test}/test-bundler.js`] : ['webpack']
},
browsers : ['PhantomJS'],
webpack : {
devtool : 'cheap-module-source-map',
resolve : Object.assign({}, webpackConfig.resolve, {
alias : Object.assign({}, webpackConfig.resolve.alias, {
sinon : 'sinon/pkg/sinon.js'
})
}),
plugins : webpackConfig.plugins,
module : {
noParse : [
/\/sinon\.js/
],
loaders : webpackConfig.module.loaders.concat([
{
test : /sinon(\\|\/)pkg(\\|\/)sinon\.js/,
loader : 'imports?define=>false,require=>false'
}
])
},
// Enzyme fix, see:
// https://github.com/airbnb/enzyme/issues/47
externals : Object.assign({}, webpackConfig.externals, {
'react/addons' : true,
'react/lib/ExecutionEnvironment' : true,
'react/lib/ReactContext' : 'window'
}),
sassLoader : webpackConfig.sassLoader
},
webpackMiddleware : {
noInfo : true
},
coverageReporter : {
reporters : project.coverage_reporters
}
}

if (project.globals.__COVERAGE__) {
karmaConfig.reporters.push('coverage')
karmaConfig.webpack.module.preLoaders = [{
test : /\.(js|jsx)$/,
include : new RegExp(project.dir_client),
exclude : /node_modules/,
loader : 'babel',
query : Object.assign({}, project.compiler_babel, {
plugins : (project.compiler_babel.plugins || []).concat('istanbul')
})
}]
}

module.exports = (cfg) => cfg.set(karmaConfig)

0 comments on commit f5cd8e0

Please sign in to comment.