@@ -25,8 +25,8 @@ const tuf_js_1 = require("tuf-js");
25
25
const target_1 = require ( "./target" ) ;
26
26
class TUFClient {
27
27
constructor ( options ) {
28
- initTufCache ( options . cachePath , options . rootPath ) ;
29
- const remote = initRemoteConfig ( options . cachePath , options . mirrorURL ) ;
28
+ initTufCache ( options ) ;
29
+ const remote = initRemoteConfig ( options ) ;
30
30
this . updater = initClient ( options . cachePath , remote , options ) ;
31
31
}
32
32
async refresh ( ) {
@@ -42,7 +42,7 @@ exports.TUFClient = TUFClient;
42
42
// created. If the targets directory does not exist, it will be created.
43
43
// If the root.json file does not exist, it will be copied from the
44
44
// rootPath argument.
45
- function initTufCache ( cachePath , tufRootPath ) {
45
+ function initTufCache ( { cachePath, rootPath : tufRootPath , force , } ) {
46
46
const targetsPath = path_1 . default . join ( cachePath , 'targets' ) ;
47
47
const cachedRootPath = path_1 . default . join ( cachePath , 'root.json' ) ;
48
48
if ( ! fs_1 . default . existsSync ( cachePath ) ) {
@@ -51,22 +51,28 @@ function initTufCache(cachePath, tufRootPath) {
51
51
if ( ! fs_1 . default . existsSync ( targetsPath ) ) {
52
52
fs_1 . default . mkdirSync ( targetsPath ) ;
53
53
}
54
- if ( ! fs_1 . default . existsSync ( cachedRootPath ) ) {
54
+ // If the root.json file does not exist (or we're forcing re-initialization),
55
+ // copy it from the rootPath argument
56
+ if ( ! fs_1 . default . existsSync ( cachedRootPath ) || force ) {
55
57
fs_1 . default . copyFileSync ( tufRootPath , cachedRootPath ) ;
56
58
}
57
59
return cachePath ;
58
60
}
59
61
// Initializes the remote.json file, which contains the URL of the TUF
60
62
// repository. If the file does not exist, it will be created. If the file
61
63
// exists, it will be parsed and returned.
62
- function initRemoteConfig ( rootDir , mirrorURL ) {
64
+ function initRemoteConfig ( { cachePath , mirrorURL, force , } ) {
63
65
let remoteConfig ;
64
- const remoteConfigPath = path_1 . default . join ( rootDir , 'remote.json' ) ;
65
- if ( fs_1 . default . existsSync ( remoteConfigPath ) ) {
66
+ const remoteConfigPath = path_1 . default . join ( cachePath , 'remote.json' ) ;
67
+ // If the remote config file exists, read it and parse it (skip if force is
68
+ // true)
69
+ if ( ! force && fs_1 . default . existsSync ( remoteConfigPath ) ) {
66
70
const data = fs_1 . default . readFileSync ( remoteConfigPath , 'utf-8' ) ;
67
71
remoteConfig = JSON . parse ( data ) ;
68
72
}
69
- if ( ! remoteConfig ) {
73
+ // If the remote config file does not exist (or we're forcing initialization),
74
+ // create it
75
+ if ( ! remoteConfig || force ) {
70
76
remoteConfig = { mirror : mirrorURL } ;
71
77
fs_1 . default . writeFileSync ( remoteConfigPath , JSON . stringify ( remoteConfig ) ) ;
72
78
}
0 commit comments