• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / boxcli / 18978219136

31 Oct 2025 04:05PM UTC coverage: 85.581% (+0.08%) from 85.497%
18978219136

push

github

web-flow
chore: setup `prettier` for code formatting and `eslint` (#605)

1261 of 1668 branches covered (75.6%)

Branch coverage included in aggregate %.

983 of 1133 new or added lines in 188 files covered. (86.76%)

16 existing lines in 9 files now uncovered.

4609 of 5191 relevant lines covered (88.79%)

631.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.15
/src/token-cache.js
1
'use strict';
2

3
/* eslint-disable promise/catch-or-return,promise/no-callback-in-promise */
4

5
const os = require('node:os');
9✔
6
const path = require('node:path');
9✔
7
const BoxCLIError = require('./cli-error');
9✔
8
const utilities = require('./util');
9✔
9

10
/**
11
 * Cache interface used by the Node SDK to cache tokens to disk in the user's home directory
12
 */
13
class CLITokenCache {
14
        /**
15
         * @constructor
16
         * @param {string} environmentName The name of the active CLI environment
17
         */
18
        constructor(environmentName) {
NEW
19
                this.filePath = path.join(
×
20
                        os.homedir(),
21
                        '.box',
22
                        `${environmentName}_token_cache.json`
23
                );
24
        }
25

26
        /**
27
         * Read tokens from disk
28
         * @param {Function} callback The callback to pass resulting token info to
29
         * @returns {void}
30
         */
31
        read(callback) {
NEW
32
                utilities
×
33
                        .readFileAsync(this.filePath, 'utf8')
NEW
34
                        .then((json) => JSON.parse(json))
×
35
                        // If file is not present or not valid JSON, treat that as empty (but available) cache
36
                        .catch(() => ({}))
×
NEW
37
                        .then((tokenInfo) => callback(null, tokenInfo));
×
38
        }
39

40
        /**
41
         * Write tokens to disk
42
         * @param {Object} tokenInfo The token object to write
43
         * @param {Function} callback The callback to pass results to
44
         * @returns {void}
45
         */
46
        write(tokenInfo, callback) {
UNCOV
47
                let output = JSON.stringify(tokenInfo, null, 4);
×
NEW
48
                utilities
×
49
                        .writeFileAsync(this.filePath, output, 'utf8')
50
                        // Pass success or error to the callback
51
                        .then(callback)
52
                        .catch((error) =>
NEW
53
                                callback(
×
54
                                        new BoxCLIError('Failed to write to token cache', error)
55
                                )
56
                        );
57
        }
58

59
        /**
60
         * Delete the cache file from disk
61
         * @param {Function} callback The callback to pass results to
62
         * @returns {void}
63
         */
64
        clear(callback) {
NEW
65
                utilities
×
66
                        .unlinkAsync(this.filePath)
67
                        // Pass success or error to the callback
68
                        .then(callback)
69
                        .catch((error) =>
NEW
70
                                callback(new BoxCLIError('Failed to delete token cache', error))
×
71
                        );
72
        }
73

74
        /**
75
         * Write the token to disk, complatible with TS SDK
76
         * @param {AccessToken} token The token to write
77
         * @returns {Promise<undefined>} A promise resolving to undefined
78
         */
79
        store(token) {
UNCOV
80
                return new Promise((resolve, reject) => {
×
NEW
81
                        const accquiredAtMS = Date.now();
×
82
                        const tokenInfo = {
×
83
                                accessToken: token.accessToken,
84
                                accessTokenTTLMS: token.expiresIn * 1000,
85
                                refreshToken: token.refreshToken,
86
                                acquiredAtMS: accquiredAtMS,
87
                        };
NEW
88
                        this.write(tokenInfo, (error) => {
×
NEW
89
                                if (error) {
×
NEW
90
                                        reject(error);
×
91
                                } else {
92
                                        resolve();
×
93
                                }
94
                        });
95
                });
96
        }
97

98
        /**
99
         * Read the token from disk, compatible with TS SDK
100
         * @returns {Promise<undefined | AccessToken>} A promise resolving to the token
101
         */
102
        get() {
UNCOV
103
                return new Promise((resolve, reject) => {
×
NEW
104
                        this.read((error, tokenInfo) => {
×
NEW
105
                                if (error) {
×
NEW
106
                                        reject(error);
×
107
                                } else {
108
                                        resolve(tokenInfo.accessToken ? tokenInfo : undefined);
×
109
                                }
110
                        });
111
                });
112
        }
113
}
114

115
module.exports = CLITokenCache;
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc