From 9917425cc872c333c3a69e0f6b5fc7ef325c4a07 Mon Sep 17 00:00:00 2001 From: Bogdan Lyashenko Date: Tue, 6 Aug 2019 20:07:45 +0200 Subject: [PATCH] fix: config file support --- README.md | 23 ++++++++++++----------- cli/index.cli.js | 24 +++++++++++++----------- example-project/codecrumbs.config.js | 13 +++++-------- src/public/js/core/store/index.js | 2 +- src/server/index.js | 7 +++++-- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 51ed63d..2b3376e 100644 --- a/README.md +++ b/README.md @@ -40,18 +40,19 @@ Check out the example of [**standalone version running here**](https://codecrumb 2) Run ```codecrumbs -d project-src-dir -e project-src-dir/index.js```. Change parameters to match your project:```-d``` is *directory with source code*, ```-e``` is *entry point file* . 3) Go to [http://localhost:2018](http://localhost:2018/#) in the browser to check it out. -### CLI -Parameter | Description | Example ---- | --- | --- -```-d```, ```--dir``` | Relative path to project source code directory | ```-d src``` -```-e```, ```--entry``` | Relative path to project source entry point file (must be inside ```dir```) | ```-e src/app.js``` -```-x```, ```--excludeDir``` | Relative path(or paths separated by ```,```) to directories for exclusion | ```-x src/doc,src/thirdparty``` -```-i```, ```--ideCmd``` | command to open file in IDE from bash (default 'webstorm') | ```-i code``` -```-p```, ```--port``` | Port for Codecrumbs client (optional, default *2018*) | ```-p 2019``` -```-n```, ```--projectName``` | Project name alias (optional, default same as ```-d``` value) | ```-n my-hello-world``` -```-D```, ```--debugModeEnabled``` | Enable debug mode for logs (optional, default is ```false```) | ```-D``` +### Configuration +Run codecrumbs with CLI params or specify static config file `codecrumbs.config.js` (see example [here](/example-project/codecrumbs.config.js)) -Parameters could be specified in `codecrumbs.config.js` +CLI | Config file | Description | Example +--- | --- | --- | --- +```-d``` | ```projectDir``` | Relative path to project source code directory | ```-d src``` +```-e``` | ```entryPoint``` | Relative path to project source entry point file (must be inside ```dir```) | ```-e src/app.js``` +```-x``` | ```excludeDir``` | Relative path(or paths separated by ```,```) to directories for exclusion | ```-x src/doc,src/thirdparty``` +```-i``` | ```ideCmd``` | command to open file in IDE from bash (default 'webstorm') | ```-i code``` +```-p``` | ```clientPort``` | Port for Codecrumbs client (optional, default *2018*) | ```-p 2019``` +```-n``` | ```projectNameAlias``` | Project name alias (optional, default same as ```-d``` value) | ```-n my-hello-world``` +```-C``` | - | Path to codecrumbs.config.js (optional, by default will try to find the file in PWD) | ```-C config/codecrumbs.config.js``` +```-D``` | ```debugModeEnabled``` | Enable debug mode for logs (optional, default is ```false```) | ```-D``` ## Features ### Breadcrumbs and trails diff --git a/cli/index.cli.js b/cli/index.cli.js index d55db5d..95f1c46 100755 --- a/cli/index.cli.js +++ b/cli/index.cli.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +const path = require('path'); const program = require('commander'); const colors = require('colors'); const _ = require('lodash'); @@ -7,8 +8,10 @@ const _ = require('lodash'); const showUpdatesInfo = require('./updatesInfo'); const server = require('../src/server'); +showUpdatesInfo(); + program - .option('-e, --entry [entryFile]', 'Specify path to entry point file. E.g. `src/app.js`') + .option('-e, --entry [entryPoint]', 'Specify path to entry point file. E.g. `src/app.js`') .option('-d, --dir [projectDir]', 'Specify path to project source code directory. E.g. `src`', '') .option( '-w, --webpack [webpackConfigFile]', @@ -22,22 +25,24 @@ program .option('-i, --ideCmd [ideCmd]', 'IDE command to open file') .option('-x, --excludeDir [excludeDirectories]', 'Exclude directories') .option('-n, --projectName [projectNameAlias]', 'Project name alias') + .option('-C, --configFile [pathToConfigFile]', 'Path to codecrumbs.config.js') .option('-D, --debugModeEnabled [debugModeEnabled]', 'Enable debug mode for logs.') .parse(process.argv); -if (!program.entry || !program.dir) { +const pathToConfigFile = program.configFile || 'codecrumbs.config.js'; +const configFileExists = server.checkIfPathExists(pathToConfigFile); +if ((!program.entry || !program.dir) && !configFileExists) { console.log( colors.magenta( - 'Please specify `entry` and `dir` params. E.g. `codecrumbs -e src/app.js -d src`' + 'Please specify `entryPoint` and `projectDir` params (e.g. `codecrumbs -e src/app.js -d src`). Or use `-C codecrumbs.config.js` instead.' ) ); process.exit(); } -showUpdatesInfo(); +const configFromFile = configFileExists ? require(path.resolve(pathToConfigFile)) : {}; -const config = require('codecrumbs.config.js'); -const environmentConfig = { +const configFromCLI = { projectNameAlias: program.projectName, entryPoint: program.entry, projectDir: program.dir, @@ -47,9 +52,6 @@ const environmentConfig = { excludeDir: program.excludeDir, ideCmd: program.ideCmd, debugModeEnabled: program.debugModeEnabled -} +}; -server.setup( - _.merge(config, environmentConfig), - { isDev: false } -); +server.setup(_.merge(configFromCLI, configFromFile), { isDev: false }); diff --git a/example-project/codecrumbs.config.js b/example-project/codecrumbs.config.js index 9404ad8..c940c70 100644 --- a/example-project/codecrumbs.config.js +++ b/example-project/codecrumbs.config.js @@ -1,10 +1,7 @@ module.exports = { - "entryPoint": 'src/index.js', - "projectDir": 'src', - "webpackConfigFile": 'webpack.config.js', - "tsConfigFile": 'tsconfig.js', - "defaultPort": 1234, - "ideCmd": 'webstorm', - "excludeDirectories": ['build'], - "projectNameAlias": 'my-hello-world' + entryPoint: 'example-project/src-client/index.js', + projectDir: 'example-project/src-client', + clientPort: 1234, + projectNameAlias: 'example-project-for-client', + debugModeEnabled: true }; diff --git a/src/public/js/core/store/index.js b/src/public/js/core/store/index.js index c966a69..b7b385b 100644 --- a/src/public/js/core/store/index.js +++ b/src/public/js/core/store/index.js @@ -9,7 +9,7 @@ import dataBus from '../dataBus/reducer'; import namespaceIntegration from '../namespaceIntegration/reducer'; import rootSaga from './sagas'; -export default ({ extraReducers, extraPersistWhiteList } = {}) => { +export default ({ extraReducers = {}, extraPersistWhiteList = [] } = {}) => { const sagaMiddleware = createSagaMiddleware(); const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; diff --git a/src/server/index.js b/src/server/index.js index f748e55..d33758d 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -97,11 +97,11 @@ const alignPlatformPath = (p = '') => const validateProjectPath = (pDir, ePoint) => { const paths = []; - if (!fs.existsSync(alignPlatformPath(pDir))) { + if (!checkIfPathExists(pDir)) { paths.push(pDir); } - if (!fs.existsSync(alignPlatformPath(ePoint))) { + if (!checkIfPathExists(ePoint)) { paths.push(ePoint); } @@ -113,6 +113,9 @@ const validateProjectPath = (pDir, ePoint) => { } }; +const checkIfPathExists = path => fs.existsSync(alignPlatformPath(path)); + module.exports = { + checkIfPathExists, setup };