Skip to content

Commit

Permalink
fix(docz-core): set env vars for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 20, 2018
1 parent 78b1956 commit 29f0098
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 37 deletions.
1 change: 1 addition & 0 deletions packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cpy": "^7.0.1",
"deepmerge": "^2.1.1",
"detect-port": "^1.2.3",
"env-dot-prop": "^1.0.2",
"express": "^4.16.3",
"fast-glob": "^2.2.2",
"file-loader": "^1.1.11",
Expand Down
12 changes: 6 additions & 6 deletions packages/docz-core/src/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export class Bundler<C = ConfigObj> {

public getConfig(env: Env): C {
const babelConfig = babelrc(this.args, env)
const config = this.mountConfig(this.config(babelConfig))
const config = this.mountConfig(this.config(babelConfig), env)

return this.args.modifyBundlerConfig(config, !this.isProd(), this.args)
return this.args.modifyBundlerConfig(config, !this.isProd(env), this.args)
}

public async createServer(config: C): Promise<BundlerServer> {
Expand Down Expand Up @@ -90,14 +90,14 @@ export class Bundler<C = ConfigObj> {
await this.builder(config, dist)
}

private mountConfig(config: C): any {
private mountConfig(config: C, env: Env): any {
const { plugins } = this.args
const reduce = Plugin.reduceFromPlugins<C>(plugins)

return reduce('modifyBundlerConfig', config, !this.isProd(), this.args)
return reduce('modifyBundlerConfig', config, !this.isProd(env), this.args)
}

private isProd(): boolean {
return process.env.NODE_ENV === 'production'
private isProd(env: Env): boolean {
return env === 'production'
}
}
9 changes: 2 additions & 7 deletions packages/docz-core/src/bundlers/webpack/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ import webpack, { Configuration as CFG } from 'webpack'
import FSR from 'react-dev-utils/FileSizeReporter'
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages'
import printBuildError from 'react-dev-utils/printBuildError'
import envDotProp from 'env-dot-prop'

import * as paths from '../../config/paths'

process.env.BABEL_ENV = process.env.BABEL_ENV || 'production'
process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const { measureFileSizesBeforeBuild, printFileSizesAfterBuild } = FSR
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024

const hasCiEnvVar = () =>
process.env.CI &&
(typeof process.env.CI !== 'string' ||
process.env.CI.toLowerCase() !== 'false')
const hasCiEnvVar = () => envDotProp.get('ci', false, { parse: true })

const copyPublicFolder = async (dest: string): Promise<void> => {
if (await fs.pathExists(paths.appPublic)) {
Expand Down
16 changes: 10 additions & 6 deletions packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as fs from 'fs-extra'
import humanize from 'humanize-string'
import titleize from 'titleize'
import envDotProp from 'env-dot-prop'

import { Plugin } from '../Plugin'
import { BabelRC } from '../utils/babelrc'
import * as paths from '../config/paths'

const getEnv = (val: string, defaultValue: any = null): any =>
envDotProp.get(val, defaultValue, { parse: true })

const removeScope = (name: string) => name.replace(/^@.*\//, '')
const getInitialTitle = (): string => {
const pkg = fs.readJsonSync(paths.packageJson, { throws: false })
Expand Down Expand Up @@ -103,27 +107,27 @@ export const args = (yargs: any) => {
})
yargs.positional('debug', {
type: 'boolean',
default: process.env.DEBUG || false,
default: getEnv('debug', false),
})
yargs.positional('protocol', {
type: 'string',
default: process.env.HTTPS === 'true' ? 'https' : 'http',
default: getEnv('https') ? 'https' : 'http',
})
yargs.positional('host', {
type: 'string',
default: process.env.HOST || '127.0.0.1',
default: getEnv('host', '127.0.0.1'),
})
yargs.positional('port', {
alias: 'p',
type: 'number',
default: process.env.PORT || 3000,
default: getEnv('port', 3000),
})
yargs.positional('websocketHost', {
type: 'string',
default: process.env.WEBSOCKET_HOST || '127.0.0.1',
default: getEnv('websocket.host', '127.0.0.1'),
})
yargs.positional('websocketPort', {
type: 'number',
default: process.env.WEBSOCKET_PORT || 8089,
default: getEnv('websocket.port', 8089),
})
}
9 changes: 3 additions & 6 deletions packages/docz-core/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
process.env.BABEL_ENV = 'production'
process.env.NODE_ENV = 'production'

import * as fs from 'fs-extra'
import logger from 'signale'
import envDotProp from 'env-dot-prop'

import * as paths from '../config/paths'
import { loadConfig } from '../utils/load-config'
import { webpack } from '../bundlers'
import { Entries } from '../Entries'
import { Config, Env } from './args'
import { Config } from './args'
import { Plugin } from '../Plugin'

const env = process.env.NODE_ENV as Env

export const build = async (args: Config) => {
const env = envDotProp.get('node.env')
const config = loadConfig(args)
const bundler = webpack(config, env)
const entries = new Entries(config)
Expand Down
9 changes: 3 additions & 6 deletions packages/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
process.env.BABEL_ENV = 'development'
process.env.NODE_ENV = 'development'

import * as fs from 'fs-extra'
import logger from 'signale'
import detectPort from 'detect-port'
import envDotProp from 'env-dot-prop'

import * as paths from '../config/paths'
import { Config, Env } from './args'
import { Config } from './args'
import { DataServer } from '../DataServer'
import { webpack } from '../bundlers'
import { Entries } from '../Entries'
import { loadConfig } from '../utils/load-config'

const env = process.env.NODE_ENV as Env

export const dev = async (args: Config) => {
const env = envDotProp.get('node.env')
const config = loadConfig(args)
const port = await detectPort(config.port)
const websocketPort = await detectPort(config.websocketPort)
Expand Down
1 change: 0 additions & 1 deletion packages/docz-core/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { dev } from './dev'
export { build } from './build'
export { args } from './args'
3 changes: 2 additions & 1 deletion packages/docz-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as commands from './commands'
import { args } from './commands/args'

export { commands }
export { commands, args }
export { Config } from './commands/args'
export { Plugin, createPlugin } from './Plugin'
export { BabelRC } from './utils/babelrc'
1 change: 1 addition & 0 deletions packages/docz-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare module '@mdx-js/mdx'
declare module '@sindresorhus/slugify'
declare module 'babylon'
declare module 'babel-traverse'
declare module 'env-dot-prop'
declare module 'chokidar'
declare module 'humanize-string'
declare module 'titleize'
Expand Down
22 changes: 19 additions & 3 deletions packages/docz/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#!/usr/bin/env node

const yargs = require('yargs')
const { commands } = require('docz-core')
const envDotProp = require('env-dot-prop')
const { args: defaultArgs } = require('docz-core')

const setEnv = val => {
envDotProp.set('babel.env', val)
envDotProp.set('node.env', val)
}

const execCommand = (cmd, args) => {
require('docz-core').commands[cmd](args)
}

yargs
.command('dev', 'initialize docz dev server', commands.args, commands.dev)
.command('build', 'build dir as static site', commands.args, commands.build)
.command('dev', 'initialize docz dev server', defaultArgs, args => {
setEnv('development')
execCommand('dev', args)
})
.command('build', 'build dir as static site', defaultArgs, args => {
setEnv('production')
execCommand('build', args)
})
.demandCommand()
.help()
.wrap(72)
Expand Down
1 change: 1 addition & 0 deletions packages/docz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"deepmerge": "^2.1.1",
"docz-core": "^0.6.0",
"docz-theme-default": "^0.6.1",
"env-dot-prop": "^1.0.2",
"invariant": "^2.2.4",
"loadable-components": "^2.2.2",
"pascalcase": "^0.1.1",
Expand Down
13 changes: 12 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3307,6 +3307,10 @@ circular-json@^0.3.1:
version "0.3.3"
resolved "https://registry.npmjs.org/circular-json/-/circular-json-0.3.3.tgz#815c99ea84f6809529d2f45791bdf82711352d66"

circular-json@^0.4.0:
version "0.4.0"
resolved "https://registry.npmjs.org/circular-json/-/circular-json-0.4.0.tgz#c448ea998b7fe31ecf472ec29c6b608e2e2a62fd"

class-utils@^0.3.5:
version "0.3.6"
resolved "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"
Expand Down Expand Up @@ -4508,7 +4512,7 @@ dot-prop@^3.0.0:
dependencies:
is-obj "^1.0.0"

dot-prop@^4.1.0, dot-prop@^4.1.1:
dot-prop@^4.1.0, dot-prop@^4.1.1, dot-prop@^4.2.0:
version "4.2.0"
resolved "https://registry.npmjs.org/dot-prop/-/dot-prop-4.2.0.tgz#1f19e0c2e1aa0e32797c49799f2837ac6af69c57"
dependencies:
Expand Down Expand Up @@ -4640,6 +4644,13 @@ entities@~1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0"

env-dot-prop@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/env-dot-prop/-/env-dot-prop-1.0.2.tgz#c22052199178bfc19d7364d206f0c3b461deebd1"
dependencies:
circular-json "^0.4.0"
dot-prop "^4.2.0"

errno@^0.1.1, errno@^0.1.3, errno@~0.1.7:
version "0.1.7"
resolved "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz#4684d71779ad39af177e3f007996f7c67c852618"
Expand Down

0 comments on commit 29f0098

Please sign in to comment.