Skip to content

Commit 78da657

Browse files
feat: add parsr fallbacl for js
1 parent 8155ed3 commit 78da657

5 files changed

Lines changed: 31 additions & 13 deletions

File tree

cli/index.cli.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
#!/usr/bin/env node
22

3+
const path = require('path');
34
const program = require('commander');
45
const colors = require('colors');
56

67
const server = require('../src/server');
78

89
program
910
.option('-e, --entry [entryFile]', 'Specify path to entry point file. E.g. `./src/app.js`')
10-
.option('-d, --dir [projectDir]', 'Specify path to project source code directory. E.g. `./src`')
11+
.option(
12+
'-d, --dir [projectDir]',
13+
'Specify path to project source code directory. E.g. `./src`',
14+
''
15+
)
1116
.option(
1217
'-w, --webpack [webpackConfigFile]',
1318
'Specify path to webpack config file. E.g. ./webpack.config.js'
1419
)
1520
.option('-p, --port [defaultPort]', 'Specify port for Codecrumbs client. E.g. 3333', 2018)
21+
.option('-f, --parserFallback [astParserFallback]', 'Use AST parser fallback')
22+
.option('-n, --projectName [projectNameAlias]', 'Project name alias')
1623
.parse(process.argv);
1724

18-
if (!program.entry && !program.dir) {
25+
if (!program.entry || !program.dir) {
1926
console.log(
2027
colors.magenta(
2128
'Please specify `entry` and `dir` params. E.g. `codecrumbs -e src/app.js -d src`'
@@ -26,11 +33,12 @@ if (!program.entry && !program.dir) {
2633

2734
server.setup(
2835
{
29-
projectNameAlias: undefined, // TODO: add param for this
36+
projectNameAlias: program.projectName,
3037
entryPoint: program.entry,
31-
projectDir: program.dir,
38+
projectDir: program.dir.replace(new RegExp(`${path.sep}$`), ''),
3239
webpackConfigPath: program.webpack,
33-
clientPort: program.port
40+
clientPort: program.port,
41+
astParserFallback: program.parserFallback
3442
},
3543
false
3644
);

src/index.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const namespaceOne = {
55
projectDir: `example-project/src-client`,
66
entryPoint: `example-project/src-client/index.js`,
77
webpackConfigPath: `example-project/webpack.config.js`,
8-
clientPort: 2018
8+
clientPort: 2018,
9+
astParserFallback: false
910
};
1011

1112
const namespaceTwo = {

src/server/code-parse/language/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// https://github.com/blakeembrey/language-map/blob/master/languages.json
33
// copy(`/\.(${extensions.map(i=>i.slice(1)).join('|')})$/`)
44
module.exports = {
5-
detectLanguage: entryPoint => {
5+
detectLanguage: (entryPoint, astParserFallback) => {
66
const list = [
77
{
88
language: 'cpp',
@@ -27,10 +27,18 @@ module.exports = {
2727
{
2828
language: 'typescript',
2929
extensions: /.(ts|tsx)$/
30-
},
30+
}
3131
];
3232

3333
const detection = list.find(item => item.extensions.test(entryPoint));
34+
// only case for JavaScript for now, because AST parser is used and can fail
35+
if (detection && astParserFallback) {
36+
return {
37+
...detection,
38+
language: 'default'
39+
};
40+
}
41+
3442
return detection ? detection : { language: 'default', extensions: /(.*?)/ };
3543
},
3644
getLanguageParsers: (language = 'default') => {

src/server/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const mediator = require('./mediator');
88
const sourceWatcher = require('./source-watcher');
99

1010
const setup = (
11-
{ projectNameAlias, projectDir, entryPoint, webpackConfigPath, clientPort },
11+
{ projectNameAlias, projectDir, entryPoint, webpackConfigPath, clientPort, astParserFallback },
1212
isDev
1313
) => {
1414
const PORT_IN_USE = 'open';
@@ -46,12 +46,13 @@ const setup = (
4646
{
4747
mediatorEndPoint: `${HOST}:${SERVER_PORT}`,
4848
namespace: `source-project-${Date.now()}`,
49-
projectName: `project-${projectNameAlias || projectDir}`
49+
projectName: `project:${projectNameAlias || projectDir}`
5050
},
5151
{
5252
projectDir,
5353
entryPoint,
54-
webpackConfigPath
54+
webpackConfigPath,
55+
astParserFallback
5556
}
5657
);
5758
});

src/server/source-watcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const { detectLanguage } = require('./code-parse/language');
99

1010
const run = (
1111
{ mediatorEndPoint, namespace, projectName },
12-
{ projectDir, entryPoint, webpackConfigPath }
12+
{ projectDir, entryPoint, webpackConfigPath, astParserFallback }
1313
) => {
14-
const { language, extensions: fileExtensions } = detectLanguage(entryPoint);
14+
const { language, extensions: fileExtensions } = detectLanguage(entryPoint, astParserFallback);
1515

1616
const webSocketClient = new WebSocketClient();
1717

0 commit comments

Comments
 (0)