Skip to content

Commit 413dc91

Browse files
Add cli
1 parent 184578f commit 413dc91

6 files changed

Lines changed: 98 additions & 40 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ Leave "breadcrumbs" in source code via comments to find your way out from code m
1616
- leave bread crumbs in a code maze
1717
- cut off paths leading nowhere
1818
- see entire maze from a “bird’s-eye” view
19+
20+
21+
Your project setup:
22+
```javascript
23+
{
24+
"dependencies": {
25+
"codecrumbs": "1.0.0"
26+
},
27+
"scripts": {
28+
"start": "codecrumbs -e src/index.js -d src"
29+
}
30+
}
31+
```

cli/index.cli.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env node
2+
3+
const program = require('commander');
4+
const colors = require('colors');
5+
6+
const httpServer = require('http-server');
7+
const apiServer = require('../src/server');
8+
9+
program
10+
.option('-e, --entry [entryFile]', 'Specify path to entry point file. E.g. `./src/app.js`')
11+
.option('-d, --dir [projectDir]', 'Specify path to project source code directory. E.g. `./src`')
12+
.option(
13+
'-w, --webpack [webpackConfigFile]',
14+
'Specify path to webpack config file. E.g. ./webpack.config.js'
15+
)
16+
.option('-p, --port [defaultPort]', 'Specify port for Codecrumbs client. E.g. 3333', 2018)
17+
.parse(process.argv);
18+
19+
if (!program.entry && !program.dir) {
20+
console.log(
21+
colors.magenta(
22+
'Please specify `entry` and `dir` params. E.g. `codecrumbs -e src/app.js -d src`'
23+
)
24+
);
25+
process.exit();
26+
}
27+
28+
apiServer.run({
29+
entryFile: program.entry,
30+
projectDir: program.dir,
31+
webpackConfigFile: program.webpack
32+
});
33+
34+
httpServer.createServer({ root: './src/public/dist' }).listen(program.port, '127.0.0.1', () => {
35+
console.log(`Go to "Codecrumbs" client http://localhost:${program.port} `);
36+
});

index.dev.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const httpServer = require('http-server');
2+
const apiServer = require('./src/server');
3+
4+
const projectDir = `example-project/src`;
5+
const entryFile = `example-project/src/index.js`;
6+
const webpackConfigFile = `example-project/webpack.config.js`;
7+
8+
apiServer.run({ projectDir, entryFile, webpackConfigFile });
9+
httpServer.createServer({ root: './src/public/dist', cache: -1 }).listen(2018, '127.0.0.1', () => {
10+
console.log('"Codecrumbs" is served on port:2018. Go to http://localhost:2018. Enjoy ;)');
11+
});

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
{
22
"name": "codecrumbs",
3-
"version": "1.0.1-alpha",
3+
"version": "1.0.2-alpha",
44
"author": "Bohdan Liashenko",
55
"license": "MIT",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/Bogdan-Lyashenko/codecrumbs.git"
99
},
1010
"scripts": {
11-
"start": "yarn client-dev & yarn server-api-dev & yarn server-static",
12-
"start:demo": "yarn server-api & yarn server-static",
11+
"start": "yarn client-dev & yarn server-dev",
12+
"start:demo": "node ./index.dev.js",
1313
"client-dev": "cd src/public && webpack --progress --colors --watch --env dev",
14-
"server-api-dev": "nodemon ./src/server/index.js",
15-
"server-api-debug": "nodemon --inspect ./src/server/index.js",
16-
"server-static": "cd src/public/dist && http-server ./ -c-1 -p 2018",
17-
"server-api": "node ./src/server/index.js",
14+
"server-dev": "nodemon ./index.dev.js",
15+
"server-debug": "nodemon --inspect ./index.dev.js",
1816
"pretty": "prettier --write \"./src/public/js/**/*.js\""
1917
},
2018
"bin": {
@@ -27,6 +25,8 @@
2725
"antd": "^3.9.2",
2826
"chokidar": "^2.0.3",
2927
"classnames": "^2.2.6",
28+
"colors": "^1.3.2",
29+
"commander": "^2.19.0",
3030
"copy-text-to-clipboard": "^1.0.4",
3131
"d3-flextree": "^2.1.1",
3232
"directory-tree": "^2.1.0",
@@ -52,11 +52,11 @@
5252
"@babel/preset-react": "^7.0.0",
5353
"babel-loader": "^8.0.4",
5454
"babel-plugin-import": "^1.9.1",
55+
"css-loader": "^0.28.11",
5556
"node-sass": "^4.9.3",
5657
"nodemon": "^1.18.7",
5758
"prettier": "^1.14.0",
5859
"sass-loader": "^7.1.0",
59-
"css-loader": "^0.28.11",
6060
"style-loader": "^0.21.0",
6161
"webpack": "^4.20.2",
6262
"webpack-cli": "^3.1.2"

src/server/api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const url = require('url');
22
const codeParser = require('../code-parse');
33

44
const handleRequests = projectDir => (request, response) => {
5-
// TODO: move to config
5+
// TODO: move PORT to config
66
response.setHeader('Access-Control-Allow-Origin', 'http://localhost:2018');
77

88
if (request.method === 'GET') {

src/server/index.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,35 @@ const api = require('./api/');
44
const projectSourceWatcher = require('./project-source/');
55
const { SERVER_PORT, SOCKET_EVENT_TYPE } = require('../shared/constants');
66

7-
// TODO: should come as params
8-
const ROOT_DIR = 'example-project';
9-
const PROJECT_DIR = `${ROOT_DIR}/src`; //, src/public/js, example-project, get as param to server script
10-
const ENTRY = `${PROJECT_DIR}/index.js`; //, index.js, get as param to server script
11-
const WEBPACK_CONFIG_FILE_PATH = `${ROOT_DIR}/webpack.config.js`;
12-
// -- end params --
13-
14-
const httpServer = http.createServer(api.handleRequests(PROJECT_DIR));
15-
httpServer.listen(SERVER_PORT, () => {
16-
console.log(new Date() + `Server is listening localhost: ${SERVER_PORT}.`);
17-
});
7+
const run = ({ projectDir, entryFile, webpackConfigFile }) => {
8+
const httpServer = http.createServer(api.handleRequests(projectDir));
9+
httpServer.listen(SERVER_PORT, () => {
10+
console.log(new Date() + `API server is listening: ${SERVER_PORT}.`);
11+
});
1812

19-
const webSocketServer = new WebSocketServer({ httpServer });
20-
webSocketServer.on('request', request => {
21-
const connection = request.accept(null, request.origin);
13+
const webSocketServer = new WebSocketServer({ httpServer });
14+
webSocketServer.on('request', request => {
15+
const connection = request.accept(null, request.origin);
2216

23-
projectSourceWatcher.subscribeOnChange(PROJECT_DIR, ENTRY, WEBPACK_CONFIG_FILE_PATH, {
24-
onInit: data =>
25-
connection.sendUTF(
26-
JSON.stringify({
27-
type: SOCKET_EVENT_TYPE.INIT_SOURCE_FILES_SYNC,
28-
data
29-
})
30-
),
31-
onChange: data =>
32-
connection.sendUTF(
33-
JSON.stringify({
34-
type: SOCKET_EVENT_TYPE.UPDATE_SOURCE_FILE_SYNC,
35-
data
36-
})
37-
)
17+
projectSourceWatcher.subscribeOnChange(projectDir, entryFile, webpackConfigFile, {
18+
onInit: data =>
19+
connection.sendUTF(
20+
JSON.stringify({
21+
type: SOCKET_EVENT_TYPE.INIT_SOURCE_FILES_SYNC,
22+
data
23+
})
24+
),
25+
onChange: data =>
26+
connection.sendUTF(
27+
JSON.stringify({
28+
type: SOCKET_EVENT_TYPE.UPDATE_SOURCE_FILE_SYNC,
29+
data
30+
})
31+
)
32+
});
3833
});
39-
});
34+
};
35+
36+
module.exports = {
37+
run
38+
};

0 commit comments

Comments
 (0)