@@ -2,94 +2,87 @@ var fs = require('fs'),
22 path = require ( 'path' ) ,
33 hbs = require ( 'handlebars' ) ,
44 helpers = require ( '../lib/helpers' ) ( hbs ) ,
5+ Promise = require ( 'promise' ) ,
56 admzip = require ( 'adm-zip' ) ,
67 temp = require ( 'temp' ) . track ( ) ,
78 sqlite3 = require ( 'sqlite3' ) . verbose ( ) ;
89
910module . exports . buildDashDocSet = function ( input ) {
1011
11- var zip = new admzip ( ) ,
12- tempdb = temp . openSync ( 'temp.sqlite' ) ,
13- db = new sqlite3 . Database ( tempdb . path ) ;
12+ return new Promise ( function ( resolve ) {
1413
15- input . uid = module . exports . formatStringForUID ( input . title ) ;
14+ var zip = new admzip ( ) ,
15+ tempdb = temp . openSync ( 'temp.sqlite' ) ,
16+ db = new sqlite3 . Database ( tempdb . path ) ;
1617
17- zip . addFile (
18- input . title + '.docset/Contents/Resources/Documents/index.html' ,
19- require ( '../templates/dash/index.hbs' ) ( input )
20- ) ;
18+ input . uid = module . exports . formatStringForUID ( input . title ) ;
2119
22- zip . addFile (
23- input . title + '.docset/Contents/Info.plist' ,
24- require ( '../templates/dash/plist.hbs' ) ( input )
25- ) ;
26-
27- [
28- 'bootstrap/bootstrap.min.css' ,
29- 'highlight.js/github.min.css'
30- ] . forEach ( function ( resource ) {
31-
32- zip . addLocalFile (
33- path . join ( __dirname , '../templates/dash/resources/' + resource ) ,
34- input . title + '.docset/Contents/Resources/Documents/resources/' + path . dirname ( resource )
20+ zip . addFile (
21+ input . title + '.docset/Contents/Resources/Documents/index.html' ,
22+ require ( '../templates/dash/index.hbs' ) ( input )
3523 ) ;
3624
37- } ) ;
25+ zip . addFile (
26+ input . title + '.docset/Contents/Info.plist' ,
27+ require ( '../templates/dash/plist.hbs' ) ( input )
28+ ) ;
3829
39- db . serialize ( function ( ) {
30+ [
31+ 'bootstrap/bootstrap.min.css' ,
32+ 'highlight.js/github.min.css'
33+ ] . forEach ( function ( resource ) {
4034
41- db . run ( 'CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);' ) ;
42- db . run ( 'CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);' ) ;
35+ zip . addLocalFile (
36+ path . join ( __dirname , '../templates/dash/resources/' + resource ) ,
37+ input . title + '.docset/Contents/Resources/Documents/resources/' + path . dirname ( resource )
38+ ) ;
4339
44- input . files . forEach ( function ( file ) {
40+ } ) ;
4541
46- file . methods . forEach ( function ( method ) {
42+ db . serialize ( function ( ) {
4743
48- if ( ! method . ignore && method . ctx ) {
44+ db . run ( 'CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);' ) ;
45+ db . run ( 'CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);' ) ;
4946
50- db . run ( 'INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ($name, $type, $path);' , {
51- $name : hbs . helpers . formatName ( method . ctx . string ) ,
52- $type : method . ctx . type . replace ( / ^ [ a - z ] / , function ( match ) { return match . toUpperCase ( ) ; } ) ,
53- $path : 'index.html#' + method . ctx . uid
54- } ) ;
47+ input . files . forEach ( function ( file ) {
5548
56- }
49+ file . methods . forEach ( function ( method ) {
5750
58- } ) ;
51+ if ( ! method . ignore && method . ctx ) {
5952
60- } ) ;
53+ db . run ( 'INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ($name, $type, $path);' , {
54+ $name : hbs . helpers . formatName ( method . ctx . string ) ,
55+ $type : method . ctx . type . replace ( / ^ [ a - z ] / , function ( match ) { return match . toUpperCase ( ) ; } ) ,
56+ $path : 'index.html#' + method . ctx . uid
57+ } ) ;
6158
62- } ) ;
59+ }
6360
64- db . close ( function ( ) {
61+ } ) ;
6562
66- zip . addFile (
67- input . title + '.docset/Contents/Resources/docSet.dsidx' ,
68- fs . readFileSync ( tempdb . path )
69- ) ;
70-
71- if ( input . output ) {
72-
73- fs . writeFileSync ( input . output , zip . toBuffer ( ) , 'utf8' ) ;
63+ } ) ;
7464
75- }
65+ } ) ;
7666
77- } ) ;
67+ db . close ( function ( ) {
7868
79- if ( ! input . output ) {
69+ zip . addFile (
70+ input . title + '.docset/Contents/Resources/docSet.dsidx' ,
71+ fs . readFileSync ( tempdb . path )
72+ ) ;
8073
81- console . error ( 'Not avalible through stdout. Please use the --output flag instead.' ) ;
74+ resolve ( zip . toBuffer ( ) ) ;
8275
83- }
76+ } ) ;
8477
85- return false ;
78+ } ) ;
8679
8780} ;
8881
8982module . exports . findPackage = function ( input ) {
9083
91- var stat ,
92- pkg ;
84+ var pkg ,
85+ stat ;
9386
9487 if ( fs . existsSync ( input ) ) {
9588
@@ -160,25 +153,25 @@ module.exports.parseData = function (data, file) {
160153
161154} ;
162155
163- module . exports . walk = function ( dir , opts ) {
156+ module . exports . walk = function ( dir , options ) {
164157
165158 var files = [ ] ;
166159
167- if ( ! opts ) {
160+ if ( ! options ) {
168161
169- opts = { } ;
162+ options = { } ;
170163
171164 }
172165
173- if ( ! opts . match ) {
166+ if ( ! options . match ) {
174167
175- opts . match = / \. j s $ / ;
168+ options . match = / \. j s $ / ;
176169
177170 }
178171
179- if ( ! opts . exception ) {
172+ if ( ! options . exception ) {
180173
181- opts . exception = / \. g i t | \. m i n | n o d e _ m o d u l e s | b o w e r _ c o m p o n e n t s | t e s t | g r u n t f i l e | g u l p f i l e / i;
174+ options . exception = / \. g i t | \. m i n | n o d e _ m o d u l e s | b o w e r _ c o m p o n e n t s | t e s t | g r u n t f i l e | g u l p f i l e / i;
182175
183176 }
184177
@@ -192,11 +185,11 @@ module.exports.walk = function (dir, opts) {
192185
193186 stat = fs . statSync ( file ) ;
194187
195- if ( stat . isDirectory ( ) && ! file . match ( opts . exception ) ) {
188+ if ( stat . isDirectory ( ) && ! file . match ( options . exception ) ) {
196189
197- files = files . concat ( module . exports . walk ( file , opts ) ) ;
190+ files = files . concat ( module . exports . walk ( file , options ) ) ;
198191
199- } else if ( stat . isFile ( ) && file . match ( opts . match ) && ! file . match ( opts . exception ) ) {
192+ } else if ( stat . isFile ( ) && file . match ( options . match ) && ! file . match ( options . exception ) ) {
200193
201194 files . push ( file ) ;
202195
0 commit comments