Skip to content

Commit e74ce10

Browse files
committed
resolve issues
1 parent af7d187 commit e74ce10

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

lib/commands/login/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import meow from 'meow'
33
import ora from 'ora'
44
import prompts from 'prompts'
55

6+
import { ChalkOrMarkdown } from '../../utils/chalk-markdown.js'
67
import { AuthError, InputError } from '../../utils/errors.js'
78
import { setupSdk } from '../../utils/sdk.js'
89
import { getSetting, updateSetting } from '../../utils/settings.js'
@@ -33,10 +34,14 @@ export const login = {
3334
if (!isInteractive()) {
3435
throw new InputError('cannot prompt for credentials in a non-interactive shell')
3536
}
37+
const format = new ChalkOrMarkdown(false)
3638
const { apiKey } = await prompts({
3739
type: 'password',
3840
name: 'apiKey',
39-
message: 'Enter your Socket.dev API key',
41+
message: `Enter your ${format.hyperlink(
42+
'Socket.dev API key',
43+
'https://docs.socket.dev/docs/api-keys'
44+
)}`,
4045
})
4146

4247
if (!apiKey) {

lib/shadow/npm-injection.cjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const oraPromise = import('ora')
1212
const isInteractivePromise = import('is-interactive')
1313
const chalkPromise = import('chalk')
1414
const chalkMarkdownPromise = import('../utils/chalk-markdown.js')
15+
const settingsPromise = import('../utils/settings.js')
1516
const ipc_version = require('../../package.json').version
1617

1718
try {
@@ -32,7 +33,9 @@ try {
3233
* @typedef {import('stream').Writable} Writable
3334
*/
3435

35-
const pubToken = 'sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api'
36+
const pubTokenPromise = settingsPromise.then(({ getSetting }) =>
37+
getSetting('apiKey') || 'sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api'
38+
);
3639

3740
// shadow `npm` and `npx` to mitigate subshells
3841
require('./link.cjs')(fs.realpathSync(path.join(__dirname, 'bin')), 'npm')
@@ -64,6 +67,7 @@ const pkgidParts = (pkgid) => {
6467
async function * batchScan (
6568
pkgids
6669
) {
70+
const pubToken = await pubTokenPromise
6771
const query = {
6872
packages: pkgids.map(pkgid => {
6973
const { name, version } = pkgidParts(pkgid)

lib/utils/sdk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function setupSdk () {
2424
const input = await prompts({
2525
type: 'password',
2626
name: 'apiKey',
27-
message: 'Enter your Socket.dev API key',
27+
message: 'Enter your Socket.dev API key (not saved)',
2828
})
2929

3030
apiKey = sessionAPIKey = input.apiKey

lib/utils/settings.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'path'
44

55
// @ts-ignore no types for ascii85
66
import ascii85 from 'ascii85'
7+
import ora from 'ora'
78

89
let dataHome = process.platform === 'win32'
910
? process.env['LOCALAPPDATA']
@@ -20,31 +21,33 @@ if (!dataHome) {
2021

2122
const settingsPath = path.join(dataHome, 'socket', 'settings')
2223

23-
/** @type {any} */
24+
/** @type {{apiKey?: string | null}} */
2425
let settings = {}
2526

2627
if (fs.existsSync(settingsPath)) {
2728
const raw = fs.readFileSync(settingsPath)
2829
try {
2930
settings = JSON.parse(ascii85.decode(raw).toString())
3031
} catch (e) {
31-
// failed decode - don't load
32+
ora(`Failed to parse settings at ${settingsPath}`).warn()
3233
}
3334
} else {
3435
fs.mkdirSync(path.dirname(settingsPath), { recursive: true })
3536
}
3637

3738
/**
38-
* @param {string} key
39-
* @returns {any}
39+
* @template {keyof typeof settings} Key
40+
* @param {Key} key
41+
* @returns {typeof settings[Key]}
4042
*/
4143
export function getSetting (key) {
4244
return settings[key]
4345
}
4446

4547
/**
46-
* @param {string} key
47-
* @param {any} value
48+
* @template {keyof typeof settings} Key
49+
* @param {Key} key
50+
* @param {typeof settings[Key]} value
4851
* @returns {void}
4952
*/
5053
export function updateSetting (key, value) {

0 commit comments

Comments
 (0)