Skip to content

Commit 1354610

Browse files
committed
refactoring
1 parent 6564c46 commit 1354610

File tree

1 file changed

+43
-87
lines changed

1 file changed

+43
-87
lines changed

commands/export/index.js

Lines changed: 43 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33

44
//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')
66
const mkdir = require('./mkdir-p-sync') // mkdir -p 'path/to/file'
77

88
// see module.exports
@@ -11,113 +11,57 @@ let api = {
1111
getDoc: ( docname, cb ) => cb( null, {} )
1212
}
1313

14-
// see module.exports
15-
let GetDoc = ( doc, fn ) => {}
16-
1714
// see module.exports
1815
let log = (...msg) => console.log('cos.export:', ...msg )
1916
// export options
2017
let root = '.'
2118
let folder = 'src'
22-
let category = '*'
23-
let generated = 0
24-
let filter = ''
2519
let atelier = false
2620
let doc2file = docname => docname
2721

28-
2922
// Export one document
30-
const DocExport = ( doc ) => () => {
23+
const ExportDoc = ( doc, next ) => ({ error, data }) => {
3124

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
3628
}
29+
const { content, status } = data.result
3730

3831
// atelier: 'mypkg.subpkg.myclass.cls' => 'mypkg/subpkg/myclass.cls'
3932
const filename = doc2file( doc.name )
4033
const fullname = [ root, folder, doc.cat, filename ].join('/')
4134
const folders = path.dirname( fullname )
4235

4336
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()
4740

4841
}
4942

43+
const doclist = ( { error, data } ) => {
5044

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 }` )
6248

63-
}
49+
const docfilter = d => ( d.name.substring( 0, 1 ) !== '%' ) &&
50+
( d.cat !== 'CSP' ) &&
51+
( d.name.substring( 0, 12 ) !== 'INFORMATION.' )
6452

65-
const Loaded = doc => ( { error, data } ) => {
53+
const filtered = list.filter( docfilter )
54+
log( `Without CSP, %* and INFORMATION.*: ${ filtered.length}` )
6655

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 } ) )
7261
}
62+
next()
7363

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' )
12165

12266
}
12367

@@ -126,15 +70,27 @@ const onGetDocs = ( err, json ) => {
12670
*/
12771
module.exports = environment => {
12872

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 )
13177

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;
13580

13681
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+
13894
}
13995

14096
}

0 commit comments

Comments
 (0)