1
1
/* eslint-disable no-param-reassign */
2
2
/* eslint-disable no-await-in-loop */
3
- import fs from 'fs-extra' ;
3
+ import fs from 'fs/promises' ;
4
+ import fsSync from 'fs' ;
4
5
import path from 'path' ;
5
6
import semver from 'semver' ;
7
+ import { fileURLToPath } from 'url' ;
6
8
import { logger } from '../Logger.js' ;
9
+ import * as FsUtils from '../lib/FsUtils.js' ;
7
10
8
11
/** @typedef {import('@advanced-rest-client/events').Theme.ArcThemeStore } ArcThemeStore */
9
12
@@ -19,6 +22,11 @@ import { logger } from '../Logger.js';
19
22
* A class that is responsible for setting up theme defaults.
20
23
*/
21
24
export class ThemeDefaults {
25
+ constructor ( ) {
26
+ const base = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
27
+ this . appPath = path . join ( base , '..' , '..' , '..' ) ;
28
+ }
29
+
22
30
/**
23
31
* Sets defaults if the defaults are not yet set.
24
32
* It copies anypoint and default theme to theme location
@@ -41,8 +49,8 @@ export class ThemeDefaults {
41
49
*/
42
50
async _readDefaultThemesPackages ( ) {
43
51
logger . silly ( 'Reading local (app) themes info file...' ) ;
44
- const source = path . join ( __dirname , '..' , '..' , '..' , 'appresources' , 'themes' ) ;
45
- logger . silly ( ' Searching for default themes...' ) ;
52
+ const source = path . join ( this . appPath , 'appresources' , 'themes' ) ;
53
+ logger . silly ( ` Searching for default themes in ${ source } ...` ) ;
46
54
const themes = await this . _listThemePackages ( source ) ;
47
55
if ( themes ) {
48
56
logger . silly ( `Found ${ themes . length } default themes.` ) ;
@@ -56,13 +64,16 @@ export class ThemeDefaults {
56
64
* @returns {Promise<DefaultThemeInfo[]|undefined> }
57
65
*/
58
66
async _listThemePackages ( themePath , parent ) {
67
+ /** @type string[]|undefined */
59
68
let items ;
60
69
try {
61
- items = await fs . readdir ( themePath , 'utf8' ) ;
70
+ fsSync . readdirSync ( themePath ) ;
71
+ items = await fs . readdir ( themePath ) ;
62
72
} catch ( e ) {
63
- logger . error ( `Unable to read themes path ${ themePath } . Skipping themes initialization.` ) ;
64
- logger . error ( e ) ;
65
- logger . error ( e . stack ) ;
73
+ logger . error ( `Unable to read themes path ${ themePath } . Skipping themes initialization.\n${ e . message } ` ) ;
74
+ if ( e . stack ) {
75
+ logger . error ( e . stack ) ;
76
+ }
66
77
return undefined ;
67
78
}
68
79
let themePaths = [ ] ;
@@ -71,9 +82,9 @@ export class ThemeDefaults {
71
82
const stats = await fs . stat ( loc ) ;
72
83
if ( stats . isDirectory ( ) ) {
73
84
const pkgFile = path . join ( loc , 'package.json' ) ;
74
- const hasPackage = await fs . pathExists ( pkgFile ) ;
85
+ const hasPackage = await FsUtils . canRead ( pkgFile ) ;
75
86
if ( hasPackage ) {
76
- const pkgContent = await fs . readJSON ( pkgFile , { throws : false } ) ;
87
+ const pkgContent = /** @type DefaultThemeInfo */ ( await FsUtils . readJson ( pkgFile , { throws : false } ) ) ;
77
88
const main = this . _readMainFile ( pkgContent , name ) ;
78
89
if ( parent ) {
79
90
name = path . join ( parent , name ) ;
@@ -146,15 +157,15 @@ export class ThemeDefaults {
146
157
*/
147
158
async _ensureTheme ( info ) {
148
159
const file = path . join ( process . env . ARC_THEMES , info . name , info . main ) ;
149
- const exists = await fs . pathExists ( file ) ;
160
+ const exists = await FsUtils . canRead ( file ) ;
150
161
if ( ! exists ) {
151
162
await this . _copyThemeFiles ( info ) ;
152
163
return ;
153
164
}
154
165
const localPkgFile = path . join ( info . location , 'package.json' ) ;
155
- const localPkg = await fs . readJson ( localPkgFile ) ;
166
+ const localPkg = /** @type any */ ( await FsUtils . readJson ( localPkgFile ) ) ;
156
167
const installedPkgFile = path . join ( process . env . ARC_THEMES , info . name , 'package.json' ) ;
157
- const installedPkg = await fs . readJson ( installedPkgFile ) ;
168
+ const installedPkg = /** @type any */ ( await FsUtils . readJson ( installedPkgFile ) ) ;
158
169
const localVersion = localPkg . version ;
159
170
const installedVersion = installedPkg . version ;
160
171
if ( semver . gt ( localVersion , installedVersion ) ) {
@@ -173,8 +184,8 @@ export class ThemeDefaults {
173
184
logger . debug ( `Creating ${ info . name } theme...` ) ;
174
185
const dest = path . join ( process . env . ARC_THEMES , info . name ) ;
175
186
try {
176
- await fs . emptyDir ( dest ) ;
177
- await fs . copy ( info . location , dest ) ;
187
+ await FsUtils . emptyDir ( dest ) ;
188
+ await FsUtils . copy ( info . location , dest ) ;
178
189
await this . _updateThemeVersion ( info ) ;
179
190
} catch ( cause ) {
180
191
logger . error ( `Unable to copy default theme from ${ info . location } to ${ dest } ` ) ;
@@ -188,7 +199,7 @@ export class ThemeDefaults {
188
199
*/
189
200
async _setThemeInfo ( ) {
190
201
const file = path . join ( process . env . ARC_THEMES , 'themes-info.json' ) ;
191
- const exists = await fs . pathExists ( file ) ;
202
+ const exists = await FsUtils . canRead ( file ) ;
192
203
if ( exists ) {
193
204
logger . debug ( `${ file } exists. Ensuring info scheme.` ) ;
194
205
await this . _ensureThemesInfoVersion ( file ) ;
@@ -203,7 +214,7 @@ export class ThemeDefaults {
203
214
* @returns {Promise<void> }
204
215
*/
205
216
async _ensureThemesInfoVersion ( file ) {
206
- const data = await fs . readJson ( file , { throws : false } ) ;
217
+ const data = /** @type any */ ( await FsUtils . readJson ( file , { throws : false } ) ) ;
207
218
if ( ! data ) {
208
219
return this . _copyInfoFile ( ) ;
209
220
}
@@ -229,7 +240,7 @@ export class ThemeDefaults {
229
240
* @return {string } Location of theme info file in local resources.
230
241
*/
231
242
get localThemeInfoFile ( ) {
232
- return path . join ( __dirname , '..' , '..' , '..' , 'appresources' , 'themes' , 'themes-info.json' ) ;
243
+ return path . join ( this . appPath , 'appresources' , 'themes' , 'themes-info.json' ) ;
233
244
}
234
245
235
246
/**
@@ -238,10 +249,10 @@ export class ThemeDefaults {
238
249
*/
239
250
async _copyInfoFile ( ) {
240
251
const dest = process . env . ARC_THEMES_SETTINGS ;
241
- await fs . ensureDir ( process . env . ARC_THEMES ) ;
242
- let info = await fs . readJson ( this . localThemeInfoFile , { throws : false } )
252
+ await FsUtils . ensureDir ( process . env . ARC_THEMES ) ;
253
+ let info = await FsUtils . readJson ( this . localThemeInfoFile , { throws : false } )
243
254
info = info || { } ;
244
- await fs . writeJson ( dest , info ) ;
255
+ await FsUtils . writeJson ( dest , info ) ;
245
256
}
246
257
247
258
/**
@@ -255,7 +266,7 @@ export class ThemeDefaults {
255
266
* @returns {Promise<void> }
256
267
*/
257
268
async _upgradeInfoFile ( file , installed ) {
258
- let info = await fs . readJson ( this . localThemeInfoFile , { throws : false } ) ;
269
+ let info = /** @type any */ ( await FsUtils . readJson ( this . localThemeInfoFile , { throws : false } ) ) ;
259
270
if ( ! info || ! info . themes ) {
260
271
info = { themes : [ ] } ;
261
272
}
@@ -266,7 +277,7 @@ export class ThemeDefaults {
266
277
info . themes . push ( item ) ;
267
278
} ) ;
268
279
info . systemPreferred = false ;
269
- await fs . writeJson ( file , info ) ;
280
+ await FsUtils . writeJson ( file , info ) ;
270
281
}
271
282
272
283
/**
@@ -275,16 +286,16 @@ export class ThemeDefaults {
275
286
*/
276
287
async _updateThemeVersion ( info ) {
277
288
const dbFile = process . env . ARC_THEMES_SETTINGS ;
278
- const db = /** @type ArcThemeStore */ ( await fs . readJson ( dbFile ) ) ;
289
+ const db = /** @type ArcThemeStore */ ( await FsUtils . readJson ( dbFile ) ) ;
279
290
// name contains path separator that is different on different platforms.
280
291
const normalizedName = info . name . replace ( / [ \\ / ] / g, '' ) ;
281
292
const theme = db . themes . find ( ( i ) => i . name . replace ( / [ \\ / ] / g, '' ) === normalizedName ) ;
282
293
if ( ! theme ) {
283
294
return ;
284
295
}
285
296
const localPkgFile = path . join ( info . location , 'package.json' ) ;
286
- const localPkg = await fs . readJson ( localPkgFile ) ;
297
+ const localPkg = /** @type any */ ( await FsUtils . readJson ( localPkgFile ) ) ;
287
298
theme . version = localPkg . version ;
288
- await fs . writeJson ( dbFile , db ) ;
299
+ await FsUtils . writeJson ( dbFile , db ) ;
289
300
}
290
301
}
0 commit comments