|
1 | 1 | const { execSync } = require('child_process') |
2 | 2 | const fs = require("fs") |
| 3 | +let rf = require("fs") |
3 | 4 | const path = require("path") |
4 | 5 | const yaml = require('js-yaml') |
5 | 6 | let Worker |
@@ -99,27 +100,47 @@ function generateDartWithWorker(path, script) { |
99 | 100 | worker.on("message", resolve); |
100 | 101 | worker.on("error", reject); |
101 | 102 | } |
| 103 | + }).then((msg) => { |
| 104 | + if (msg.error) { |
| 105 | + console.log('filePath:' + path + '\nerror:' + msg.error) |
| 106 | + } |
| 107 | + let result = msg.result |
| 108 | + if (!result) { |
| 109 | + return |
| 110 | + } |
| 111 | + writeOutputToFileByPath(result.dartCode, msg.path) |
| 112 | + |
| 113 | + if (projectName) { |
| 114 | + result.packages.forEach(item => packageSet.add(item)) |
| 115 | + } |
102 | 116 | }); |
103 | 117 | }; |
104 | 118 |
|
| 119 | +function splitCodeFileIfNeed(separator, maxLength, path) { |
| 120 | + const content = rf.readFileSync(path, "utf-8") |
| 121 | + if (content.length > maxLength) { |
| 122 | + let components = content.split(separator) |
| 123 | + return components.map((c, i) => { |
| 124 | + if (i == components.length - 1) { |
| 125 | + return c |
| 126 | + } |
| 127 | + return c + separator |
| 128 | + }) |
| 129 | + } |
| 130 | + return [content] |
| 131 | +} |
| 132 | + |
105 | 133 | async function runWorkItems(workItems) { |
106 | 134 | const promises = Array.from(workItems.keys()).map((path) => { |
107 | 135 | let script = workItems.get(path) |
108 | | - return generateDartWithWorker(path, script).then((msg) => { |
109 | | - if (msg.error) { |
110 | | - console.log('filePath:' + msg.path + '\nerror:' + msg.error) |
111 | | - } |
112 | | - let result = msg.result |
113 | | - if (!result) { |
114 | | - return |
115 | | - } |
116 | | - writeOutputToFileByPath(result.dartCode, msg.path) |
117 | | - |
118 | | - if (projectName) { |
119 | | - result.packages.forEach(item => packageSet.add(item)) |
120 | | - } |
| 136 | + console.log('processing: ' + path) |
| 137 | + // TODO: change separator |
| 138 | + let contents = splitCodeFileIfNeed("@end\n", 5000, path) |
| 139 | + let ps = contents.map((content) => { |
| 140 | + return generateDartWithWorker(content, script) |
121 | 141 | }) |
122 | | - }) |
| 142 | + return ps |
| 143 | + }).reduce((acc, val) => acc.concat(val), []); |
123 | 144 | await Promise.all(promises) |
124 | 145 | } |
125 | 146 |
|
|
0 commit comments