@@ -25,8 +25,8 @@ const tuf_js_1 = require("tuf-js");
2525const target_1 = require ( "./target" ) ;
2626class TUFClient {
2727 constructor ( options ) {
28- initTufCache ( options . cachePath , options . rootPath ) ;
29- const remote = initRemoteConfig ( options . cachePath , options . mirrorURL ) ;
28+ initTufCache ( options ) ;
29+ const remote = initRemoteConfig ( options ) ;
3030 this . updater = initClient ( options . cachePath , remote , options ) ;
3131 }
3232 async refresh ( ) {
@@ -42,7 +42,7 @@ exports.TUFClient = TUFClient;
4242// created. If the targets directory does not exist, it will be created.
4343// If the root.json file does not exist, it will be copied from the
4444// rootPath argument.
45- function initTufCache ( cachePath , tufRootPath ) {
45+ function initTufCache ( { cachePath, rootPath : tufRootPath , force , } ) {
4646 const targetsPath = path_1 . default . join ( cachePath , 'targets' ) ;
4747 const cachedRootPath = path_1 . default . join ( cachePath , 'root.json' ) ;
4848 if ( ! fs_1 . default . existsSync ( cachePath ) ) {
@@ -51,22 +51,28 @@ function initTufCache(cachePath, tufRootPath) {
5151 if ( ! fs_1 . default . existsSync ( targetsPath ) ) {
5252 fs_1 . default . mkdirSync ( targetsPath ) ;
5353 }
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 ) {
5557 fs_1 . default . copyFileSync ( tufRootPath , cachedRootPath ) ;
5658 }
5759 return cachePath ;
5860}
5961// Initializes the remote.json file, which contains the URL of the TUF
6062// repository. If the file does not exist, it will be created. If the file
6163// exists, it will be parsed and returned.
62- function initRemoteConfig ( rootDir , mirrorURL ) {
64+ function initRemoteConfig ( { cachePath , mirrorURL, force , } ) {
6365 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 ) ) {
6670 const data = fs_1 . default . readFileSync ( remoteConfigPath , 'utf-8' ) ;
6771 remoteConfig = JSON . parse ( data ) ;
6872 }
69- if ( ! remoteConfig ) {
73+ // If the remote config file does not exist (or we're forcing initialization),
74+ // create it
75+ if ( ! remoteConfig || force ) {
7076 remoteConfig = { mirror : mirrorURL } ;
7177 fs_1 . default . writeFileSync ( remoteConfigPath , JSON . stringify ( remoteConfig ) ) ;
7278 }
0 commit comments