12
12
console . log ( 'NodeJS version is too low. Running on slow mode.' )
13
13
}
14
14
15
-
15
+ let dnPartExtension = 'dnpart'
16
16
var outputDir
17
17
var projectName
18
18
var packageSet = new Set ( )
@@ -58,9 +58,40 @@ function recFindByExt(base, ext, files, result) {
58
58
59
59
function writeOutputToFileByPath ( result , srcPath ) {
60
60
var srcFile = srcPath . substr ( srcPath . lastIndexOf ( '/' ) + 1 )
61
- var dartFile = srcFile . substring ( 0 , srcFile . indexOf ( '.' ) ) . toLowerCase ( ) + '.dart'
61
+ var components
62
+ if ( srcFile . endsWith ( dnPartExtension ) ) {
63
+ partExtension = srcFile . split ( '.' ) . slice ( - 2 ) . join ( '.' )
64
+ components = srcFile . split ( '.' ) . slice ( 0 , - 3 )
65
+ components . push ( 'dart' )
66
+ components . push ( partExtension )
67
+ } else {
68
+ components = srcFile . split ( '.' ) . slice ( 0 , - 1 )
69
+ components . push ( 'dart' )
70
+ }
71
+
72
+ let dartFile = components . join ( '.' ) . toLowerCase ( )
73
+
62
74
var outputFile = outputDir ? path . join ( outputDir , dartFile ) : dartFile
63
75
fs . writeFileSync ( outputFile , result )
76
+ return outputFile
77
+ }
78
+
79
+ function mergeFiles ( filePaths ) {
80
+ if ( ! filePaths || filePaths . length <= 1 ) {
81
+ return
82
+ }
83
+
84
+ let result = filePaths . sort ( ) . map ( filePath => {
85
+ if ( ! filePath ) {
86
+ return ''
87
+ }
88
+ return fs . readFileSync ( filePath , { encoding : 'utf8' } )
89
+ } ) . join ( '\n' )
90
+ let mergedFilePath = filePaths [ 0 ] . split ( '.' ) . slice ( 0 , - 2 ) . join ( '.' )
91
+ fs . writeFileSync ( mergedFilePath , result )
92
+ filePaths . forEach ( ( filePath ) => {
93
+ fs . unlinkSync ( filePath )
94
+ } )
64
95
}
65
96
66
97
function formatDartFile ( dartPath ) {
@@ -74,7 +105,7 @@ function createFlutterPackage(template, projectName) {
74
105
}
75
106
76
107
function writeDependencyToPubSpec ( filePath ) {
77
- var doc = yaml . safeLoad ( fs . readFileSync ( filePath , 'utf8' ) ) ;
108
+ var doc = yaml . safeLoad ( fs . readFileSync ( filePath , 'utf8' ) )
78
109
packageSet . forEach ( item => {
79
110
if ( typeof ( item ) == "undefined" ) {
80
111
return
@@ -85,20 +116,20 @@ function writeDependencyToPubSpec(filePath) {
85
116
fs . writeFileSync ( filePath , yaml . safeDump ( doc ) . replace ( / n u l l / g, '' ) )
86
117
}
87
118
88
- function generateDartWithWorker ( path , script ) {
119
+ function generateDartWithWorker ( content , path , script ) {
89
120
return new Promise ( ( resolve , reject ) => {
90
121
if ( this . isMainThread ) {
91
122
const convert = require ( script ) . convert
92
- convert ( path , ( result , path , error ) => {
93
- resolve ( { result : result , path : path , error : error } )
123
+ convert ( content , ( result , error ) => {
124
+ resolve ( { result : result , error : error } )
94
125
} )
95
126
} else {
96
127
const worker = new Worker ( script , {
97
- workerData : { path : path } ,
128
+ workerData : { content : content } ,
98
129
resourceLimits : { maxOldGenerationSizeMb : 8 * 1024 }
99
- } ) ;
100
- worker . on ( "message" , resolve ) ;
101
- worker . on ( "error" , reject ) ;
130
+ } )
131
+ worker . on ( "message" , resolve )
132
+ worker . on ( "error" , reject )
102
133
}
103
134
} ) . then ( ( msg ) => {
104
135
if ( msg . error ) {
@@ -108,13 +139,15 @@ function generateDartWithWorker(path, script) {
108
139
if ( ! result ) {
109
140
return
110
141
}
111
- writeOutputToFileByPath ( result . dartCode , msg . path )
142
+
143
+ let dartFilePath = writeOutputToFileByPath ( result . dartCode , path )
112
144
113
145
if ( projectName ) {
114
146
result . packages . forEach ( item => packageSet . add ( item ) )
115
147
}
116
- } ) ;
117
- } ;
148
+ return dartFilePath
149
+ } )
150
+ }
118
151
119
152
function splitCodeFileIfNeed ( separator , maxLength , path ) {
120
153
const content = rf . readFileSync ( path , "utf-8" )
@@ -131,16 +164,23 @@ function splitCodeFileIfNeed(separator, maxLength, path) {
131
164
}
132
165
133
166
async function runWorkItems ( workItems ) {
134
- const promises = Array . from ( workItems . keys ( ) ) . map ( ( path ) => {
135
- let script = workItems . get ( path )
136
- console . log ( 'processing: ' + path )
167
+ const promises = Array . from ( workItems . keys ( ) ) . map ( ( filePath ) => {
168
+ let script = workItems . get ( filePath )
169
+ console . log ( 'processing: ' + filePath )
137
170
// TODO: change separator
138
- let contents = splitCodeFileIfNeed ( "@end\n" , 5000 , path )
139
- let ps = contents . map ( ( content ) => {
140
- return generateDartWithWorker ( content , script )
171
+ let contents = splitCodeFileIfNeed ( "@end\n" , 5000 , filePath )
172
+ let ps = contents . map ( ( content , index ) => {
173
+ let p = filePath
174
+ if ( contents . length > 1 ) {
175
+ p = `${ filePath } .${ index } .${ dnPartExtension } `
176
+ }
177
+ return generateDartWithWorker ( content , p , script )
141
178
} )
142
- return ps
143
- } ) . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ;
179
+ return Promise . all ( ps ) . then ( ( results ) => {
180
+ // merge files
181
+ mergeFiles ( results )
182
+ } )
183
+ } )
144
184
await Promise . all ( promises )
145
185
}
146
186
0 commit comments