forked from CodecrumbsIO/codecrumbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (135 loc) · 3.74 KB
/
index.js
File metadata and controls
153 lines (135 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const path = require('path');
const logger = require('../utils/logger');
const codeParser = require('../code-parse/index');
const { getProjectFiles } = require('./file-system');
const { createWatcher } = require('./watcher');
const {
addFileFlowsToCodeCrumbedFlows,
mergeFileIntoNode,
resetCodeCrumbedFlowsByFile
} = require('./utils');
const { search: traversalSearch } = require('../utils/traversal');
const parseProjectSourceFiles = ({
filesMap,
projectDir,
entryPoint,
webpackConfigPath,
language
}) =>
Promise.all(
Object.keys(filesMap).map(itemPath =>
codeParser
.parseFile(itemPath, projectDir, {
parseCodeCrumbs: true,
parseImports: true,
parseDependencies: itemPath === entryPoint,
attachCode: itemPath === entryPoint,
language
})
.then(item =>
Object.keys(item).forEach(key => {
filesMap[itemPath][key] = item[key];
})
)
)
);
const watchers = [];
const closePreviousSubscription = id => {
const watcherIndex = watchers.findIndex(({ namespace }) => namespace === id);
if (watcherIndex !== -1) {
watchers[watcherIndex].watcher.close();
watchers.splice(watcherIndex, 1);
}
};
const subscribeOnChange = (
namespace,
{ projectName, projectDir, entryPoint, webpackConfigPath, language, fileExtensions },
{ onInit, onChange }
) => {
logger.info(`> source watcher subscribing for changes`);
// TODO: refactor function, too long
closePreviousSubscription(namespace);
logger.log(`- getting: project files`);
const fs = getProjectFiles(projectDir, { extensions: fileExtensions });
logger.info(
`+ got: project files.`,
` Number of files: ${Object.keys(fs.filesMap).length},`,
` Number of folders: ${Object.keys(fs.foldersMap).length}.`
);
const codeCrumbs = {
flows: {}
};
logger.log(`- parsing: project files`);
parseProjectSourceFiles({
filesMap: fs.filesMap,
projectDir,
entryPoint,
webpackConfigPath,
language
})
.then(() => {
logger.info(`+ parsed: project files.`);
Object.entries(fs.filesMap).forEach(([path, file]) => {
if (file.hasCodecrumbs) {
addFileFlowsToCodeCrumbedFlows(codeCrumbs.flows, file);
}
});
return onInit({
namespace,
projectName,
language,
platformPathSeparator: path.sep,
sourceTree: fs.sourceTree,
filesMap: fs.filesMap,
foldersMap: fs.foldersMap,
codeCrumbedFlowsMap: codeCrumbs.flows,
dependenciesEntryName: entryPoint
});
})
.catch(e => {
logger.error(`! parsed: project files. Error: ${logger.getText(e)}`);
});
const watcher = createWatcher(projectDir, path => {
const file = fs.filesMap[path];
if (!file) {
return;
}
if (file.hasCodecrumbs) {
resetCodeCrumbedFlowsByFile(codeCrumbs.flows, file);
}
parseProjectSourceFiles({
filesMap: { [path]: file },
projectDir,
entryPoint: path,
webpackConfigPath,
language
}).then(() => {
traversalSearch(fs.sourceTree, node => {
if (node.path === path) {
mergeFileIntoNode(file, node);
return true;
}
});
if (file.hasCodecrumbs) {
addFileFlowsToCodeCrumbedFlows(codeCrumbs.flows, file);
}
logger.info('> code state change was parsed and sent to client.');
return onChange({
sourceTree: fs.sourceTree,
foldersMap: fs.foldersMap,
filesMap: {
...fs.filesMap,
[file.path]: fs.filesMap[file.path]
},
codeCrumbedFlowsMap: codeCrumbs.flows
});
});
});
watchers.push({
watcher,
namespace
});
};
module.exports = {
subscribeOnChange
};