@@ -2,7 +2,7 @@ const fs = require('fs')
2
2
const path = require ( 'path' )
3
3
4
4
//export 'mypkg.subpkg.name.cls' as /mypkg/subpkg/name.cls
5
- const atelier_filename = require ( './doc-to-file-as-atelier' )
5
+ const asAtelier = require ( './doc-to-file-as-atelier' )
6
6
const mkdir = require ( './mkdir-p-sync' ) // mkdir -p 'path/to/file'
7
7
8
8
// see module.exports
@@ -11,113 +11,57 @@ let api = {
11
11
getDoc : ( docname , cb ) => cb ( null , { } )
12
12
}
13
13
14
- // see module.exports
15
- let GetDoc = ( doc , fn ) => { }
16
-
17
14
// see module.exports
18
15
let log = ( ...msg ) => console . log ( 'cos.export:' , ...msg )
19
16
// export options
20
17
let root = '.'
21
18
let folder = 'src'
22
- let category = '*'
23
- let generated = 0
24
- let filter = ''
25
19
let atelier = false
26
20
let doc2file = docname => docname
27
21
28
-
29
22
// Export one document
30
- const DocExport = ( doc ) => ( ) => {
23
+ const ExportDoc = ( doc , next ) => ( { error , data } ) => {
31
24
32
- if ( ! root ) {
33
- const message = 'Open folder before export - Ctrl+K, Ctrl+O'
34
- const error = { message }
35
- return { error, data : null }
25
+ if ( error ) {
26
+ log ( `GETDOC ${ doc . name } : ${ JSON . stringify ( error ) } \n` )
27
+ return
36
28
}
29
+ const { content, status } = data . result
37
30
38
31
// atelier: 'mypkg.subpkg.myclass.cls' => 'mypkg/subpkg/myclass.cls'
39
32
const filename = doc2file ( doc . name )
40
33
const fullname = [ root , folder , doc . cat , filename ] . join ( '/' )
41
34
const folders = path . dirname ( fullname )
42
35
43
36
if ( ! fs . existsSync ( folders ) ) mkdir ( folders )
44
- fs . writeFileSync ( fullname , doc . content . join ( '\n' ) )
45
-
46
- return { error : null , data : fullname }
37
+ fs . writeFileSync ( fullname , ( content || [ ] ) . join ( '\n' ) )
38
+ log ( ` ${ doc . name } -> ${ fullname } . ${ status } ` )
39
+ next ( )
47
40
48
41
}
49
42
43
+ const doclist = ( { error, data } ) => {
50
44
51
- const Exported = ( doc ) => ( { error, data } ) => {
52
-
53
- if ( error ) {
54
- log ( '' )
55
- log ( `Load ${ doc . name } error: ${ JSON . stringify ( error ) } ` )
56
- log ( '' )
57
- return
58
- }
59
-
60
- log ( '' )
61
- log ( `${ doc . name } -> ${ data } ` )
45
+ if ( error ) return log ( `DOCLIST: ${ JSON . stringify ( error ) } ` )
46
+ const list = data . result . content
47
+ log ( `Documents on server: ${ list . length } ` )
62
48
63
- }
49
+ const docfilter = d => ( d . name . substring ( 0 , 1 ) !== '%' ) &&
50
+ ( d . cat !== 'CSP' ) &&
51
+ ( d . name . substring ( 0 , 12 ) !== 'INFORMATION.' )
64
52
65
- const Loaded = doc => ( { error, data } ) => {
53
+ const filtered = list . filter ( docfilter )
54
+ log ( `Without CSP, %* and INFORMATION.*: ${ filtered . length } ` )
66
55
67
- if ( error ) {
68
- return {
69
- error : { message : `load ${ doc . name } ERROR: ${ JSON . stringify ( err ) } ` } ,
70
- data : null
71
- }
56
+ const next = ( ) => {
57
+ let doc = filtered . shift ( )
58
+ if ( ! doc ) return
59
+ let cb = ExportDoc ( doc , next )
60
+ api . getDoc ( encodeURI ( doc . name ) , ( error , data ) => cb ( { error , data } ) )
72
61
}
62
+ next ( )
73
63
74
- return {
75
- error : null ,
76
- data : data . result
77
- }
78
-
79
- }
80
-
81
- // https://medium.com/javascript-scene/composing-software-an-introduction-27b72500d6ea
82
- const pipe = ( ...fns ) => x => fns . reduce ( ( y , f ) => f ( y ) , x ) ;
83
-
84
- const docsExport = ( docs , cb ) => {
85
-
86
- let doc , loaded , docExport , exported , getDoc , cb
87
- while ( doc = docs . shift ( ) ) {
88
-
89
- loaded = Loaded ( doc )
90
- docExport = DocExport ( doc )
91
- exported = Exported ( doc )
92
- cb = pipe ( loaded , docExport , exported )
93
- getDoc = GetDoc ( doc , cb )
94
-
95
- getDoc ( )
96
-
97
- }
98
-
99
- }
100
-
101
- const onGetDocs = ( err , json ) => {
102
-
103
- if ( err ) return log ( 'getDocs ERROR' )
104
-
105
- const list = json . result . content
106
- log ( '' )
107
- log ( 'list: ' + list . length )
108
- const docFilter = doc => {
109
- return ( doc . cat !== 'CSP' ) &&
110
- ( doc . name . substring ( 0 , 1 ) !== '%' ) &&
111
- ( doc . name . substring ( 0 , 12 ) !== 'INFORMATION.' )
112
- }
113
- const docs = list . filter ( docFilter )
114
- log ( 'without % and CSP and INFORMATION: ' + docs . length )
115
- log ( '' )
116
-
117
- docsExport ( docs , ( ) => {
118
- log ( '' )
119
- log ( 'Export completed.' )
120
- } )
64
+ log ( 'Export completed.\n' )
121
65
122
66
}
123
67
@@ -126,15 +70,27 @@ const onGetDocs = ( err, json ) => {
126
70
*/
127
71
module . exports = environment => {
128
72
129
- ( { api, log, options } = environment ) ;
130
- ( { root, folder, atelier, category, generated, filter } = options ) ;
73
+ //reassign module variables
74
+ ( { api, log, options } = environment ) ;
75
+ ( { root, folder, atelier } = options ) ;
76
+ if ( atelier ) doc2file = doc => asAtelier ( doc )
131
77
132
- GetDoc = ( doc , fn ) => api . getDoc ( encodeURI ( doc . name ) , ( error , data ) => fn ( { error, data } ) )
133
-
134
- if ( atelier ) doc2file = docname => atelier_filename ( docname )
78
+ // doclist options
79
+ const { category, generated, filter } = options ;
135
80
136
81
return ( ) => {
137
- api . getDocNames ( { category, generated, filter } , onGetDocs )
82
+
83
+ if ( ! root ) {
84
+ log ( `COS.EXPORT: Open folder before export - Ctrl+K, Ctrl+O` )
85
+ return
86
+ }
87
+
88
+ log ( 'Load list of documents...' )
89
+ api . getDocNames (
90
+ { category, generated, filter } , //list options
91
+ ( error , data ) => doclist ( { error, data } ) //callback wrapper
92
+ )
93
+
138
94
}
139
95
140
96
}
0 commit comments