File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,12 @@ const readOrDefault = filename => fs.existsSync(filename)
4141 ? fs . readJsonSync ( filename )
4242 : [ ] ;
4343
44+ /**
45+ * @typedef InstallPackageOptions
46+ * @param {boolean } system
47+ * @param {object } [auth]
48+ */
49+
4450/**
4551 * OS.js Package Management
4652 */
@@ -133,6 +139,36 @@ class Packages {
133139 } , 500 ) ;
134140 }
135141
142+ /**
143+ * Installs a package from given url
144+ * @param {string } url
145+ * @param {InstallPackageOptions } options
146+ */
147+ async installPackage ( url , options ) {
148+ throw new Error ( 'Not implemented yet' ) ;
149+ }
150+
151+ /**
152+ * Reads package manifests
153+ * @return {Package[] } List of packages
154+ */
155+ async readPackageManifests ( user ) {
156+ const { realpath} = this . core . make ( 'osjs/vfs' ) ;
157+ const { manifestFile} = this . options ;
158+ const homePath = await realpath ( 'home:/.packages/metadata.json' , user ) ;
159+
160+ const systemManifest = await readOrDefault ( manifestFile ) ;
161+ const userManifest = await readOrDefault ( homePath ) ;
162+
163+ return [
164+ ...systemManifest ,
165+ ...userManifest . map ( pkg => Object . assign ( { } , pkg , {
166+ _user : true ,
167+ server : null
168+ } ) )
169+ ] ;
170+ }
171+
136172 /**
137173 * Loads package data
138174 * @param {string } filename Filename
Original file line number Diff line number Diff line change @@ -54,15 +54,35 @@ class PackageServiceProvider extends ServiceProvider {
5454 } ) ;
5555 }
5656
57+ depends ( ) {
58+ return [
59+ 'osjs/express'
60+ ] ;
61+ }
62+
5763 provides ( ) {
5864 return [
5965 'osjs/packages'
6066 ] ;
6167 }
6268
6369 init ( ) {
70+ const { routeAuthenticated} = this . core . make ( 'osjs/express' ) ;
71+
6472 this . core . singleton ( 'osjs/packages' , ( ) => this . packages ) ;
6573
74+ routeAuthenticated ( 'GET' , '/api/packages/manifest' , ( req , res ) => {
75+ this . packages . readPackageManifests ( req . session . user )
76+ . then ( json => res . json ( json ) )
77+ . catch ( error => res . status ( 400 ) . json ( { error} ) ) ;
78+ } ) ;
79+
80+ routeAuthenticated ( 'POST' , '/api/packages/install' , ( req , res ) => {
81+ this . packages . installPackage ( req . body . url , req . body . options )
82+ . then ( ( ) => res . json ( { success : true } ) )
83+ . catch ( error => res . status ( 400 ) . json ( { error} ) ) ;
84+ } ) ;
85+
6686 return this . packages . init ( ) ;
6787 }
6888
You can’t perform that action at this time.
0 commit comments