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
126 lines (110 loc) · 3.06 KB
/
Copy pathindex.js
File metadata and controls
126 lines (110 loc) · 3.06 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
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 }) =>
Promise.all(
Object.keys(filesMap).map(itemPath =>
codeParser
.parseFile(itemPath, projectDir, {
parseCodeCrumbs: true,
parseImports: true,
parseDependencies: itemPath === entryPoint,
attachCode: itemPath === entryPoint
})
.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 },
{ onInit, onChange }
) => {
// TODO: refactor function, too long
closePreviousSubscription(namespace);
const fs = getProjectFiles(projectDir);
const codeCrumbs = {
flows: {}
};
parseProjectSourceFiles({
filesMap: fs.filesMap,
projectDir,
entryPoint,
webpackConfigPath
}).then(() => {
Object.entries(fs.filesMap).forEach(([path, file]) => {
if (file.hasCodecrumbs) {
addFileFlowsToCodeCrumbedFlows(codeCrumbs.flows, file);
}
});
return onInit({
namespace,
projectName,
sourceTree: fs.sourceTree,
filesMap: fs.filesMap,
foldersMap: fs.foldersMap,
codeCrumbedFlowsMap: codeCrumbs.flows,
dependenciesEntryName: entryPoint
});
});
// TODO: path can be multiple if not save after fist change, fix
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
}).then(() => {
traversalSearch(fs.sourceTree, node => {
if (node.path === path) {
mergeFileIntoNode(file, node);
return true;
}
});
if (file.hasCodecrumbs) {
addFileFlowsToCodeCrumbedFlows(codeCrumbs.flows, file);
}
console.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
};