Browse Source

isOutdated in NPMExtension

master
Daniel Hübleitner 2 years ago
parent
commit
4a568728c0
2 changed files with 58 additions and 41 deletions
  1. 7
    1
      package.json
  2. 51
    40
      src/backend/UpdateManger.ts

+ 7
- 1
package.json View File

@@ -51,5 +51,11 @@
51 51
   },
52 52
   "files": [
53 53
     "lib/**/*"
54
-  ]
54
+  ],
55
+  "main": "index.js",
56
+  "directories": {
57
+    "lib": "lib",
58
+    "test": "test"
59
+  },
60
+  "keywords": []
55 61
 }

+ 51
- 40
src/backend/UpdateManger.ts View File

@@ -4,10 +4,11 @@ import { GitUpdater, RepoFolderStatus } from "./GitUpdater";
4 4
 import { FrontblockCherryPicker } from "git-cherrypicker";
5 5
 import * as Logger from 'log4js'
6 6
 import * as path from "path";
7
-import * as rimraf from "rimraf"
7
+import * as rmrf from "rimraf"
8 8
 import { FrontblockAdmin } from "./FrontblockAdmin";
9 9
 import { TableDefiniton } from "frontblock-generic/Admin";
10 10
 import { promises as fs, mkdirSync as mkdir } from "fs"
11
+import { version } from "punycode";
11 12
 
12 13
 var exec = require('child-process-promise').exec;
13 14
 
@@ -45,7 +46,7 @@ const SharedStatus = <Status>
45 46
 type SharedStatus = {
46 47
     name: string
47 48
     installed: boolean
48
-    outdated?: boolean
49
+    outdated: boolean
49 50
 }
50 51
 
51 52
 abstract class Extension<Status extends SharedStatus>{
@@ -108,17 +109,19 @@ export abstract class FSExtension<Status extends FSStatus> extends Extension<FSS
108 109
 
109 110
     public async status(): Promise<Status | FSStatus>{
110 111
         const installed = await this.isInstalled()
112
+        const outdated = await this.isOutdated()
111 113
         const empty = await this.isEmpty()
112 114
         return {
113 115
             name: this.name,
114 116
             prefix: this.prefix,
115 117
             installed: installed,
118
+            outdated: outdated,
116 119
             empty: empty
117 120
         }
118 121
     }
119 122
 
120 123
     public async uninstall(): Promise<boolean>{
121
-        return await rimraf(path.resolve(this.prefix, this.name))
124
+        return await rmrf(path.resolve(this.prefix, this.name))
122 125
     }
123 126
 
124 127
     public async isEmpty(): Promise<boolean>{
@@ -140,9 +143,7 @@ export abstract class FSExtension<Status extends FSStatus> extends Extension<FSS
140 143
 }
141 144
 
142 145
 type NPMStatus = FSStatus & {
143
-    current?: string
144
-    wanted?: string
145
-    latest?: string
146
+    version?: string
146 147
 }
147 148
 
148 149
 export class NPMExtension extends FSExtension<NPMStatus>{
@@ -154,31 +155,19 @@ export class NPMExtension extends FSExtension<NPMStatus>{
154 155
     }
155 156
 
156 157
     public async status(): Promise<NPMStatus> {
157
-        const empty = await this.isEmpty()
158
-        const installed = await this.isInstalled()
159 158
 
160
-        const { _error, _stdout, _stderr } = await exec('npm outdated --prefix ' + this.prefix + " --json" + ' ' + this.name)
161
-        const { error, stdout, stderr } = await exec('npm info --prefix ' + this.prefix + ' ' + this.name + " version")
162
-        
163
-        const status = {
164
-            empty: empty,
165
-            installed: installed,
166
-            name: this.name,
167
-            prefix: this.prefix
168
-        }
159
+        const fsStatus = await super.status()
160
+        const { error, 
161
+                stdout, 
162
+                stderr } = await exec('npm info --prefix ' + this.prefix + ' ' + this.name + " version")
169 163
 
170
-        if(_error | error){
171
-            logger.warn( _stderr, stderr)
172
-            return status
164
+        if(error){
165
+            logger.warn(stderr)
166
+            return fsStatus
173 167
         }
174
-
175
-        const statusJson:{latest:string, location: string, wanted:string} = JSON.parse(_stdout)
176 168
         return {  
177
-            ...status, 
178
-            current: stdout, 
179
-            latest: statusJson.latest, 
180
-            wanted: statusJson.wanted,
181
-            outdated: statusJson.wanted !== stdout
169
+            ...fsStatus, 
170
+            version: stdout
182 171
         }
183 172
     }
184 173
 
@@ -218,16 +207,37 @@ export class NPMExtension extends FSExtension<NPMStatus>{
218 207
         }
219 208
     }
220 209
 
221
-    public async isOutdated(): Promise<boolean> { //TODO
222
-        const status = await this.status()
223
-        return !((status.wanted && status.current) && status.wanted === status.current) 
210
+    public async isOutdated(): Promise<boolean> {
211
+        try {
212
+            const { 
213
+                error, 
214
+                stdout, 
215
+                stderr } = await exec('npm outdated --prefix ' + this.prefix + " --json" + ' ' + this.name)
216
+            if (error){
217
+                logger.warn(stderr)
218
+                return false
219
+            }
220
+            const status = JSON.parse(stdout)
221
+            if (status){
222
+                const outdated = (Object.keys(status).length > 0)
223
+                if (outdated){
224
+                    return true
225
+                }
226
+            }
227
+            return false
228
+        } catch (error) {
229
+            return false
230
+        }
224 231
     }
225 232
 }
226 233
 
227
-export type GitRef = string 
228
-export type GitExtensionState = UnknownState | GitRef
234
+export type GitStatus = FSStatus & {
235
+    current?: string
236
+    wanted?: string
237
+    latest?: string
238
+}
229 239
 
230
-export class GitExtension extends Extension<GitExtensionState>{
240
+export class GitExtension extends FSExtension<GitStatus>{
231 241
     constructor(
232 242
         name:string,
233 243
         version = "latest", 
@@ -236,16 +246,17 @@ export class GitExtension extends Extension<GitExtensionState>{
236 246
         super(name, version)
237 247
     }
238 248
 
239
-    public async install(): Promise<GitExtensionState> {
240
-        return 'unknown'
249
+    public async status(): Promise<GitStatus>{
250
+
241 251
     }
242
-    
243
-    public async uninstall(): Promise<GitExtensionState> {
244
-        return 'unknown'
252
+
253
+    public async install(): Promise<boolean> {
254
+        return true
245 255
     }
256
+    
246 257
 
247
-    public async update(): Promise<GitExtensionState> {
248
-        return 'unknown'
258
+    public async update(): Promise<boolean> {
259
+        return true
249 260
     }
250 261
 
251 262
     public async isOutdated(): Promise<boolean> {

Loading…
Cancel
Save