Browse Source

npm

master
peter 2 years ago
parent
commit
db0b872435
3 changed files with 122 additions and 47 deletions
  1. 104
    32
      src/backend/Extension.ts
  2. 2
    2
      src/backend/FrontblockAdmin.ts
  3. 16
    13
      src/backend/UpdateManger.ts

+ 104
- 32
src/backend/Extension.ts View File

@@ -83,30 +83,30 @@ export type FSStatus = SharedStatus & {
83 83
 export abstract class FSExtension<Status extends FSStatus> extends Extension<Status>{
84 84
     constructor(
85 85
         name: string, 
86
-        protected readonly prefix: string){
86
+        protected readonly path: string){
87 87
             super(name)
88 88
     }
89 89
 
90 90
     public async status(): Promise<Status>{
91 91
         const shdStatus = await super.status()
92 92
         const empty = await this.isEmpty()
93
-        return <Status>{...shdStatus, prefix: this.prefix, empty: empty}
93
+        return {...shdStatus, prefix: this.path, empty: empty}
94 94
     }
95 95
 
96 96
     public async install():Promise<boolean>{
97 97
         const installed = await this.isInstalled()
98 98
         if (!installed) {
99
-            logger.info('Installing extension to', path.resolve(this.prefix, this.name))
100
-            await fs.mkdir(path.resolve(this.prefix, this.name), {recursive: true})
99
+            logger.info('Installing extension to', path.resolve(this.path, this.name))
100
+            await fs.mkdir(path.resolve(this.path, this.name), {recursive: true})
101 101
             return true
102 102
         }
103
-        logger.warn('Extension already installed at', path.resolve(this.prefix, this.name))
103
+        logger.error('Extension already installed at', path.resolve(this.path, this.name))
104 104
         return false
105 105
     }
106 106
 
107 107
     public async uninstall(): Promise<void>{
108
-        logger.info('Uninstalling extension from', path.resolve(this.prefix, this.name))
109
-        return await trash(path.resolve(this.prefix, this.name))
108
+        logger.info('Uninstalling extension from', path.resolve(this.path, this.name))
109
+        return await trash(path.resolve(this.path, this.name))
110 110
     }
111 111
 
112 112
     public async update(force?: boolean): Promise<boolean | undefined>{
@@ -117,17 +117,17 @@ export abstract class FSExtension<Status extends FSStatus> extends Extension<Sta
117 117
     public async isEmpty(): Promise<boolean | undefined>{
118 118
         if (!this.isInstalled()) return true
119 119
         try {
120
-            const files = await fs.readdir(path.resolve(this.prefix, this.name))
120
+            const files = await fs.readdir(path.resolve(this.path, this.name))
121 121
             return files.length === 0
122 122
         } catch (error) {
123
-            logger.warn('TODO')
123
+            logger.error("fs read error", error)
124 124
             return
125 125
         }
126 126
     }
127 127
 
128 128
     public async isInstalled(): Promise<boolean>{
129 129
         try {
130
-            await fs.access(path.resolve(this.prefix, this.name))
130
+            await fs.access(path.resolve(this.path, this.name))
131 131
             return true
132 132
         } catch (error) {
133 133
             return false
@@ -139,39 +139,48 @@ type NPMStatus = FSStatus & {
139 139
     latest?: string,
140 140
     current?: string,
141 141
     author?: string
142
+    license?: string,
143
+    description?: string
142 144
 }
143 145
 
144 146
 export class NPMExtension extends FSExtension<NPMStatus>{
145 147
 
146 148
     constructor(
147 149
         pkgName: string,
148
-        prefix: string){
149
-        super(pkgName, prefix)
150
+        private version: string,
151
+        private prefix: string
152
+    ){
153
+        super(pkgName, path.resolve(prefix, "node_modules"))
150 154
     }
151 155
 
152 156
     public async status(): Promise<NPMStatus> {
153 157
 
154
-        const fsStatus = await super.status()
155
-        const latest = await this.getLatestVersion()
156
-        const author = await this.getAuthor()
157
-        const current = await this.getCurrentVersion()
158
+        const fsStatus    = await super.status()
159
+        const latest      = await this.getLatestVersion()
160
+        const author      = await this.getAuthor()
161
+        const current     = await this.getCurrentVersion()
162
+        const license     = await this.getLicense()
163
+        const description = await this.getDescription()
164
+
158 165
         return {  
159 166
             ...fsStatus, 
160 167
             latest: latest,
161 168
             author: author,
162
-            current: current
169
+            current: current,
170
+            license: license,
171
+            description: description
163 172
         }
164 173
     }
165 174
 
166 175
     public async install(): Promise<boolean> {
167 176
         super.install()
168 177
         logger.info("npm install", this.name, 'to', path.resolve(this.prefix, this.name))
169
-        const { error, stdout, stderr } = await exec('npm install --prefix '+ this.prefix + ' ' + this.name)
170
-        if (error !== null) {
171
-            logger.warn('npm install', this.name, 'from prefix', this.prefix, 'stderr:', stderr)
178
+        const { error, stdout, stderr } = await exec('npm install --prefix '+ this.prefix + ' ' + this.name+"@"+this.version)
179
+        if (error) {
180
+            logger.error('npm install', this.name, "@", this.version, ' from prefix', this.prefix, 'stderr:', stderr)
172 181
             return false
173 182
         } else {
174
-            logger.info('npm install', this.name, 'from prefix', this.prefix, 'stdout:', stdout)
183
+            logger.info('npm install', this.name, "@", this.version, 'from prefix', this.prefix, 'stdout:', stdout)
175 184
             return true
176 185
         }
177 186
     }
@@ -181,7 +190,7 @@ export class NPMExtension extends FSExtension<NPMStatus>{
181 190
         logger.info("npm uninstall", this.name, 'from', path.resolve(this.prefix, this.name))
182 191
         const { error, stdout, stderr } = await exec('npm uninstall --prefix '+ this.prefix + ' ' + this.name)
183 192
         if (error) {
184
-            logger.warn('npm uninstall', this.name, 'from prefix', this.prefix, 'stderr:', stderr)
193
+            logger.error('npm uninstall', this.name, 'from prefix', this.prefix, 'stderr:', stderr)
185 194
         } else {
186 195
             logger.info('npm uninstall', this.name, 'from prefix', this.prefix, 'stdout:', stdout)
187 196
         }
@@ -192,7 +201,7 @@ export class NPMExtension extends FSExtension<NPMStatus>{
192 201
         logger.info("npm update", path.resolve(this.prefix, this.name))
193 202
         const { error, stdout, stderr } = await exec('npm update --prefix '+ this.prefix + ' ' + this.name)
194 203
         if (error) {
195
-            logger.warn('npm update', this.name, 'from prefix', this.prefix, 'stderr:', stderr)
204
+            logger.error('npm update', this.name, 'from prefix', this.prefix, 'stderr:', stderr)
196 205
             return false
197 206
         } else {
198 207
             logger.info('npm update', this.name, 'from prefix', this.prefix, 'stdout:', stdout)
@@ -210,7 +219,7 @@ export class NPMExtension extends FSExtension<NPMStatus>{
210 219
             if (Object.keys(JSON.parse(stdout)).length > 0) return true
211 220
             else return false
212 221
         } catch (error) {
213
-            logger.warn('npm outdated', this.name, 'from prefix', this.prefix, 'stderr:', error)
222
+            logger.error('npm outdated', this.name, 'from prefix', this.prefix, 'stderr:', error)
214 223
             return
215 224
         }
216 225
     }
@@ -222,7 +231,7 @@ export class NPMExtension extends FSExtension<NPMStatus>{
222 231
             stderr } = await exec('npm author ls --prefix ' + this.prefix + ' ' + this.name)
223 232
 
224 233
         if(error){
225
-            logger.warn(stderr)
234
+            logger.error(stderr)
226 235
             return
227 236
         } else {
228 237
             logger.info(stdout)
@@ -231,22 +240,84 @@ export class NPMExtension extends FSExtension<NPMStatus>{
231 240
     }
232 241
 
233 242
     private async getLatestVersion(): Promise<string | undefined>{
234
-        const { 
243
+        try{
244
+            const { 
235 245
             error, 
236 246
             stdout, 
237 247
             stderr } = await exec('npm view --prefix ' + this.prefix + ' ' + this.name + " version")
238 248
 
239
-        if(error){
240
-            logger.warn(stderr)
249
+            if(error){
250
+                logger.error(stderr)
251
+                return
252
+            } else {
253
+                logger.info(stdout)
254
+                return stdout
255
+            }
256
+        }catch(error){
257
+            logger.error(error)
241 258
             return
242
-        } else {
243
-            logger.info(stdout)
244
-            return stdout
245 259
         }
246 260
     }
247 261
 
248 262
     private async getCurrentVersion(): Promise<string | undefined>{
249
-        return //TODO
263
+        try{
264
+            const { 
265
+            error, 
266
+            stdout, 
267
+            stderr } = await exec('npm list --json --prefix ' + this.prefix + ' ' + this.name)
268
+
269
+            if(error){
270
+                logger.error(stderr)
271
+                return 
272
+            } else {
273
+                logger.info(stdout)
274
+                return JSON.parse(stdout)['dependencies'][this.name]['version']
275
+            }
276
+        }catch(error){
277
+            logger.error(error)
278
+            return
279
+        }
280
+    }
281
+
282
+    private async getDescription(): Promise<string | undefined>{
283
+        try{
284
+            const { 
285
+            error, 
286
+            stdout, 
287
+            stderr } = await exec('npm show --prefix ' + this.prefix + ' ' + this.name+" description")
288
+
289
+            if(error){
290
+                logger.error(stderr)
291
+                return 
292
+            } else {
293
+                logger.info(stdout)
294
+                return stdout
295
+            }
296
+        }catch(error){
297
+            logger.error(error)
298
+            return
299
+        }
300
+    }
301
+
302
+    private async getLicense(): Promise<string | undefined>{
303
+        try{
304
+
305
+        const { 
306
+            error, 
307
+            stdout, 
308
+            stderr } = await exec('npm show --prefix ' + this.prefix + ' ' + this.name+" license")
309
+
310
+            if(error){
311
+                logger.error(stderr)
312
+                return 
313
+            } else {
314
+                logger.info(stdout)
315
+                return stdout
316
+            }
317
+        }catch(error){
318
+            logger.error(error)
319
+            return
320
+        }
250 321
     }
251 322
 }
252 323
 
@@ -288,6 +359,7 @@ export class GitExtension<Status extends GitStatus> extends FSExtension<Status>{
288 359
         } else {
289 360
 
290 361
         }
362
+        return false;
291 363
     }
292 364
     
293 365
 

+ 2
- 2
src/backend/FrontblockAdmin.ts View File

@@ -2,7 +2,7 @@
2 2
 
3 3
 import * as Logger from 'log4js'
4 4
 import * as Knex from 'knex'
5
-import { FrontblockApiClient } from 'frontblock';
5
+import { FrontblockApiClient, FrontblockApiConf } from 'frontblock';
6 6
 import { socketioRPC, rpcHooker, ExtendedRpcInfo } from 'frontblock-generic/RPC';
7 7
 import { ApiClientBase, AdminBase } from 'frontblock-generic/Admin';
8 8
 import express = require('express');
@@ -11,7 +11,7 @@ import bsock = require('bsock');
11 11
 import { promises as fs } from "fs"
12 12
 import * as path from "path"
13 13
 import { UpdateManager } from './UpdateManger';
14
-import { FrontblockApiConf, FrontblockApi } from 'frontblock-generic/Api';
14
+import { FrontblockApi } from 'frontblock-generic/Api';
15 15
 import { Plugin } from 'frontblock-generic/Plugin';
16 16
 var exec = require('child-process-promise').exec;
17 17
 

+ 16
- 13
src/backend/UpdateManger.ts View File

@@ -3,15 +3,9 @@ import { socketioRPC } from "frontblock-generic/RPC";
3 3
 import { GitUpdater, RepoFolderStatus } from "./GitUpdater";
4 4
 import { FrontblockCherryPicker } from "git-cherrypicker";
5 5
 import * as Logger from 'log4js'
6
-import * as path from "path";
7
-import * as rmrf from "rimraf"
8 6
 import { FrontblockAdmin } from "./FrontblockAdmin";
7
+import { NPMExtension } from "./Extension";
9 8
 import { TableDefiniton } from "frontblock-generic/Admin";
10
-import { promises as fs, mkdirSync as mkdir } from "fs"
11
-import { version } from "punycode";
12
-import * as git from "simple-git/promise"
13
-import * as trash from "trash"
14
-import { type } from "os";
15 9
 
16 10
 var exec = require('child-process-promise').exec;
17 11
 
@@ -30,6 +24,7 @@ const logger = Logger.getLogger("admin/updatemanager")
30 24
 
31 25
 export class UpdateManager extends Plugin{
32 26
     
27
+    
33 28
     private loadedPlugins:{[name in string]:Plugin} = {}
34 29
     private dashboardUpdater:GitUpdater
35 30
     private adminUpdater:GitUpdater
@@ -46,7 +41,7 @@ export class UpdateManager extends Plugin{
46 41
     exportRPCs(): socketioRPC[] {
47 42
         return [{
48 43
             name: 'installPlugin',
49
-            func: async (name:string, force = false) => {return await this.installPlugin(name, force)},
44
+            func: async (name:string, force = false) => { },
50 45
             type: 'call',
51 46
             visibility: 'private'
52 47
         },{
@@ -80,7 +75,7 @@ export class UpdateManager extends Plugin{
80 75
     getTableDefinitions(): TableDefiniton[] {
81 76
         return []
82 77
     }
83
-
78
+ 
84 79
     async updatePlatformDependencies(){
85 80
 
86 81
         logger.info("installing npm stuff")
@@ -90,10 +85,18 @@ export class UpdateManager extends Plugin{
90 85
         logger.warn(res.stderr)
91 86
         logger.info(res.stdout)
92 87
 */      
93
-        await Promise.all([
94
-            new NPMExtension("knex@0.19.2", "./plugins").install(),
95
-            new NPMExtension("sqlite3@4.1.0", "./plugins").install()
96
-        ])
88
+
89
+        const extensions = [
90
+            new NPMExtension("knex", "0.19.2", "./plugins"),
91
+            new NPMExtension("sqlite3", "4.1.0", "./plugins"),
92
+            new NPMExtension("frontblock", "latest", "./plugins")
93
+        ]
94
+
95
+
96
+
97
+        await Promise.all(extensions.map(e => e.install()))
98
+        const status = await Promise.all(extensions.map(e => e.status()))
99
+        console.log(status)
97 100
     }
98 101
 
99 102
     async updateAdmin(force:boolean = false){

Loading…
Cancel
Save