@@ -40,6 +40,11 @@ var opts = require('nomnom')
40
40
abbr : 'm' ,
41
41
full : 'only-members' ,
42
42
flag : true
43
+ } ,
44
+ append : {
45
+ help : 'Append tasks when exists the same project' ,
46
+ abbr : 'a' ,
47
+ flag : true
43
48
}
44
49
} )
45
50
. parse ( ) ;
@@ -80,10 +85,10 @@ var getUniqueName = function getUniqueName(name, haystack) {
80
85
}
81
86
} ;
82
87
83
- var convertMap = function convertToMap ( data , map ) {
88
+ var convertMap = function convertMap ( data , map ) {
84
89
if ( _ . isArray ( data ) ) {
85
90
return _ . compact ( _ . map ( data , id => {
86
- return convertToMap ( id , map ) ;
91
+ return convertMap ( id , map ) ;
87
92
} ) ) ;
88
93
}
89
94
@@ -120,7 +125,9 @@ fs.readJson(opts.config).then(function (config) {
120
125
var asanaData = {
121
126
projects : [ ] ,
122
127
tags : [ ] ,
123
- users : [ ]
128
+ users : [ ] ,
129
+ tasks : [ ] ,
130
+ sections : [ ]
124
131
} ;
125
132
126
133
var uploadImageToAsana = function ( taskId , file , filename ) {
@@ -224,6 +231,7 @@ fs.readJson(opts.config).then(function (config) {
224
231
let labelToTagMap = { } ;
225
232
let checklistMap = { } ;
226
233
let userMap = { } ;
234
+ let promise ;
227
235
228
236
_ . each ( file . checklists , checklist => {
229
237
checklistMap [ checklist . id ] = checklist ;
@@ -233,26 +241,57 @@ fs.readJson(opts.config).then(function (config) {
233
241
userMap [ user . id ] = user . name ;
234
242
} ) ;
235
243
236
- // Creates a Project
237
- return client . projects . createInTeam ( config . asana . team , {
238
- name : getUniqueName ( file . name , _ . pluck ( asanaData . projects , 'name' ) ) ,
239
- notes : file . desc ,
240
- layout : 'board'
241
- } ) . then ( result => {
242
- console . log ( `Created ${ result . name } project in your team.` ) ;
243
- projectData = result ;
244
- asanaData . projects . push ( result ) ;
244
+ // Append tasks
245
+ if ( opts . append && _ . contains ( _ . pluck ( asanaData . projects , 'name' ) , file . name ) ) {
246
+ projectData = _ . find ( asanaData . projects , project => {
247
+ return project . name === file . name ;
248
+ } ) ;
249
+
250
+ promise = client . tasks . findByProject ( projectData . id ) . then ( fetch ) . then ( tasks => {
251
+ console . log ( `Loaded exists ${ tasks . length } tasks.` ) ;
252
+ asanaData . tasks = tasks ;
253
+
254
+ return client . sections . findByProject ( projectData . id ) ;
255
+ } ) . then ( sections => {
256
+ console . log ( `Loaded exists ${ sections . length } sections.` ) ;
257
+ asanaData . sections = sections ;
258
+ } ) ;
259
+ } else {
260
+ promise = client . projects . createInTeam ( config . asana . team , {
261
+ name : getUniqueName ( file . name , _ . pluck ( asanaData . projects , 'name' ) ) ,
262
+ notes : file . desc ,
263
+ layout : 'board'
264
+ } ) . then ( result => {
265
+ console . log ( `Created ${ result . name } project in your team.` ) ;
266
+ projectData = result ;
267
+ asanaData . projects . push ( result ) ;
268
+ } ) ;
269
+ }
270
+
271
+ return promise . then ( function ( ) {
272
+ var filteredList = _ . filter ( file . lists , list => {
273
+ var matchedSection = _ . find ( asanaData . sections , section => {
274
+ return section . name === list . name ;
275
+ } ) ;
276
+
277
+ if ( matchedSection ) {
278
+ listToSectionMap [ list . id ] = matchedSection . id ;
279
+ return false ;
280
+ } else {
281
+ return true ;
282
+ }
283
+ } ) ;
245
284
246
285
// Creates sections in order
247
- return Promise . mapSeries ( file . lists , list => {
286
+ return Promise . mapSeries ( filteredList , list => {
248
287
return client . sections . createInProject ( projectData . id , {
249
288
name : list . name
250
289
} ) . then ( result => {
251
290
listToSectionMap [ list . id ] = result . id ;
252
291
console . log ( `Created ${ list . name } section.` ) ;
253
292
} ) ;
254
293
} ) ;
255
- } ) . then ( ( ) => {
294
+ } ) . then ( function ( ) {
256
295
// Filter exists tags same with label
257
296
var labels = _ . filter ( file . labels , label => {
258
297
var matchedTag = _ . find ( asanaData . tags , tag => {
@@ -283,11 +322,24 @@ fs.readJson(opts.config).then(function (config) {
283
322
} , {
284
323
concurrency : 3
285
324
} ) . then ( function ( ) {
286
- console . log ( `Creating ${ file . cards . length } tasks...` ) ;
287
325
let countTask = 0 ;
326
+ var filteredCards = _ . filter ( file . cards , card => {
327
+ var matchedTask = _ . find ( asanaData . tasks , task => {
328
+ return task . name === card . name ;
329
+ } ) ;
330
+
331
+ if ( matchedTask ) {
332
+ cardToTaskMap [ card . id ] = matchedTask . id
333
+ return false ;
334
+ } else {
335
+ return true ;
336
+ }
337
+ } ) ;
338
+
339
+ console . log ( `Creating ${ filteredCards . length } of ${ file . cards . length } tasks...` ) ;
288
340
289
341
// Creates tasks
290
- return Promise . mapSeries ( file . cards , card => {
342
+ return Promise . mapSeries ( filteredCards , card => {
291
343
return client . tasks . create ( {
292
344
assignee : card . idMembers . length ? convertMap ( _ . first ( card . idMembers ) , config . member ) : null ,
293
345
due_at : card . due ,
0 commit comments