@@ -12,7 +12,6 @@ import Alamofire
1212let VERSION_URL = " https://raw.githubusercontent.com/learning/node-box/master/data.json "
1313
1414class Store {
15- static private var directory : URL ? = FileManager . default. urls ( for: . applicationSupportDirectory, in: . userDomainMask) . first? . appendingPathComponent ( Bundle . main. bundleIdentifier!, isDirectory: true )
1615 static private var store : Store ? = nil ;
1716
1817 var branches : Array < Branch > ;
@@ -21,6 +20,18 @@ class Store {
2120 init ( data: Dictionary < String , Any > ) {
2221 self . branches = ( data [ " branches " ] as! Array < Dictionary < String , String > > ) . map { Branch ( data: $0) }
2322 self . versions = ( data [ " versions " ] as! Array < Dictionary < String , Any > > ) . map { Version ( data: $0) }
23+ do {
24+ let files = try FileManager . default. contentsOfDirectory ( at: Utils . directory!, includingPropertiesForKeys: nil )
25+ let dirs = files. filter { $0. hasDirectoryPath }
26+ . map { $0. path. components ( separatedBy: " / " ) . last }
27+ dirs. forEach { dir in
28+ versions. first ( where: { $0. filename == dir } ) ? . isDownloaded = true
29+ }
30+ } catch {
31+ NotificationCenter . default. post ( name: Notification . Name ( " alert " ) , object: " Error: list node.js downloaded versions " )
32+ }
33+ let ver = Utils . runAndGetOutput ( " readlink " , " current " ) ? . trimmingCharacters ( in: NSCharacterSet . newlines)
34+ versions. first ( where: { $0. filename == ver } ) ? . isActive = true
2435 }
2536
2637 static public func getStore( onSuccess success: @escaping ( Store ) -> Void ) {
@@ -60,7 +71,7 @@ class Store {
6071 * - name: The file's name
6172 */
6273 static private func getFile( name: String ) -> URL ? {
63- if let pathComponent = directory? . appendingPathComponent ( name) {
74+ if let pathComponent = Utils . directory? . appendingPathComponent ( name) {
6475 let filePath = pathComponent. path
6576 let fileManager = FileManager . default
6677 if fileManager. fileExists ( atPath: filePath) {
@@ -82,15 +93,15 @@ class Store {
8293 */
8394 static private func writeFile( name: String , content: String ) -> Bool {
8495 // If directory not exists, create it
85- if !FileManager. default. fileExists ( atPath: ( directory? . path) !) {
96+ if !FileManager. default. fileExists ( atPath: ( Utils . directory? . path) !) {
8697 do {
87- try FileManager . default. createDirectory ( at: directory!, withIntermediateDirectories: false , attributes: nil )
98+ try FileManager . default. createDirectory ( at: Utils . directory!, withIntermediateDirectories: false , attributes: nil )
8899 } catch {
89- print ( " Create directory \( directory!. path) failed " )
100+ print ( " Create directory \( Utils . directory!. path) failed " )
90101 return false
91102 }
92103 }
93- let file : URL ? = directory? . appendingPathComponent ( name)
104+ let file : URL ? = Utils . directory? . appendingPathComponent ( name)
94105 if file != nil {
95106 do {
96107 try content. write ( to: file!, atomically: false , encoding: String . Encoding. utf8)
@@ -122,10 +133,10 @@ class Store {
122133 if let data = response. data, let utf8Text = String ( data: data, encoding: . utf8) {
123134 // Write json to file
124135 if writeFile ( name: " data.json " , content: utf8Text) {
125- print ( " Wrote to \( directory!. path) success! " )
136+ print ( " Wrote to \( Utils . directory!. path) success! " )
126137 success ( )
127138 } else {
128- NotificationCenter . default. post ( name: Notification . Name ( " alert " ) , object: " wrote to \( directory!. path) failed " )
139+ NotificationCenter . default. post ( name: Notification . Name ( " alert " ) , object: " wrote to \( Utils . directory!. path) failed " )
129140 }
130141 }
131142 }
0 commit comments