@socketsecurity/cli 0.14.40 → 0.14.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +17 -9
- package/dist/constants.d.ts.map +1 -1
- package/dist/module-sync/cli.js +105 -164
- package/dist/module-sync/npm-injection.js +238 -309
- package/dist/module-sync/npm-paths.d.ts +14 -0
- package/dist/module-sync/{path-resolve.js → npm-paths.js} +149 -21
- package/dist/module-sync/path-resolve.d.ts +4 -5
- package/dist/module-sync/settings.d.ts +6 -1
- package/dist/module-sync/shadow-bin.js +9 -13
- package/dist/module-sync/socket-url.d.ts +17 -1
- package/dist/module-sync/socket-url.js +85 -6
- package/dist/require/cli.js +105 -164
- package/dist/require/npm-paths.js +3 -0
- package/dist/require/vendor.js +4 -16
- package/package.json +29 -21
- package/dist/require/path-resolve.js +0 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare function directoryPatterns(): string[];
|
|
2
|
+
declare function getNpmBinPath(): string;
|
|
3
|
+
declare function isNpmBinPathShadowed(): boolean;
|
|
4
|
+
declare function getNpxBinPath(): string;
|
|
5
|
+
declare function isNpxBinPathShadowed(): boolean;
|
|
6
|
+
declare function getNpmPath(): string;
|
|
7
|
+
declare function getNpmNodeModulesPath(): string;
|
|
8
|
+
declare function getArboristPackagePath(): string;
|
|
9
|
+
declare function getArboristClassPath(): string;
|
|
10
|
+
declare function getArboristDepValidPath(): string;
|
|
11
|
+
declare function getArboristEdgeClassPath(): string;
|
|
12
|
+
declare function getArboristNodeClassPath(): string;
|
|
13
|
+
declare function getArboristOverrideSetClassPath(): string;
|
|
14
|
+
export { directoryPatterns, getNpmBinPath, isNpmBinPathShadowed, getNpxBinPath, isNpxBinPathShadowed, getNpmPath, getNpmNodeModulesPath, getArboristPackagePath, getArboristClassPath, getArboristDepValidPath, getArboristEdgeClassPath, getArboristNodeClassPath, getArboristOverrideSetClassPath };
|
|
@@ -12,6 +12,7 @@ function _socketInterop(e) {
|
|
|
12
12
|
var fs = require('node:fs');
|
|
13
13
|
var path = require('node:path');
|
|
14
14
|
var process = require('node:process');
|
|
15
|
+
var constants = require('./constants.js');
|
|
15
16
|
var ignore = _socketInterop(require('ignore'));
|
|
16
17
|
var micromatch = _socketInterop(require('micromatch'));
|
|
17
18
|
var tinyglobby = _socketInterop(require('tinyglobby'));
|
|
@@ -19,7 +20,6 @@ var which = _socketInterop(require('which'));
|
|
|
19
20
|
var colors = _socketInterop(require('yoctocolors-cjs'));
|
|
20
21
|
var isUnicodeSupported = require('@socketregistry/is-unicode-supported/index.cjs');
|
|
21
22
|
var spinner = require('@socketsecurity/registry/lib/spinner');
|
|
22
|
-
var constants = require('./constants.js');
|
|
23
23
|
|
|
24
24
|
const logSymbols = isUnicodeSupported() ? {
|
|
25
25
|
__proto__: null,
|
|
@@ -89,11 +89,11 @@ function directoryPatterns() {
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
const {
|
|
92
|
-
NPM,
|
|
92
|
+
NPM: NPM$1,
|
|
93
93
|
shadowBinPath
|
|
94
94
|
} = constants;
|
|
95
95
|
async function filterGlobResultToSupportedFiles(entries, supportedFiles) {
|
|
96
|
-
const patterns = ['golang', NPM, 'pypi'].reduce((r, n) => {
|
|
96
|
+
const patterns = ['golang', NPM$1, 'pypi'].reduce((r, n) => {
|
|
97
97
|
const supported = supportedFiles[n];
|
|
98
98
|
r.push(...(supported ? Object.values(supported).map(p => `**/${p.pattern}`) : []));
|
|
99
99
|
return r;
|
|
@@ -181,25 +181,12 @@ function pathsToPatterns(paths) {
|
|
|
181
181
|
// TODO: Does not support `~/` paths.
|
|
182
182
|
return paths.map(p => p === '.' ? '**/*' : p);
|
|
183
183
|
}
|
|
184
|
-
function
|
|
185
|
-
let curPath = filepath;
|
|
186
|
-
while (true) {
|
|
187
|
-
if (path.basename(curPath) === NPM) {
|
|
188
|
-
return curPath;
|
|
189
|
-
}
|
|
190
|
-
const parent = path.dirname(curPath);
|
|
191
|
-
if (parent === curPath) {
|
|
192
|
-
return undefined;
|
|
193
|
-
}
|
|
194
|
-
curPath = parent;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
async function findBinPathDetails(binName) {
|
|
184
|
+
function findBinPathDetailsSync(binName) {
|
|
198
185
|
let shadowIndex = -1;
|
|
199
|
-
const bins =
|
|
186
|
+
const bins = which.sync(binName, {
|
|
200
187
|
all: true,
|
|
201
188
|
nothrow: true
|
|
202
|
-
})
|
|
189
|
+
}) ?? [];
|
|
203
190
|
const binPath = bins.find((binPath, i) => {
|
|
204
191
|
// Skip our bin directory if it's in the front.
|
|
205
192
|
if (fs.realpathSync(path.dirname(binPath)) === shadowBinPath) {
|
|
@@ -214,6 +201,19 @@ async function findBinPathDetails(binName) {
|
|
|
214
201
|
shadowed: shadowIndex !== -1
|
|
215
202
|
};
|
|
216
203
|
}
|
|
204
|
+
function findNpmPathSync(filepath) {
|
|
205
|
+
let curPath = filepath;
|
|
206
|
+
while (true) {
|
|
207
|
+
if (path.basename(curPath) === NPM$1) {
|
|
208
|
+
return curPath;
|
|
209
|
+
}
|
|
210
|
+
const parent = path.dirname(curPath);
|
|
211
|
+
if (parent === curPath) {
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
curPath = parent;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
217
|
async function getPackageFiles(cwd, inputPaths, config, supportedFiles) {
|
|
218
218
|
debugLog(`Globbed resolving ${inputPaths.length} paths:`, inputPaths);
|
|
219
219
|
const entries = await globWithGitIgnore(pathsToPatterns(inputPaths), {
|
|
@@ -236,11 +236,139 @@ async function getPackageFilesFullScans(cwd, inputPaths, supportedFiles, debugLo
|
|
|
236
236
|
return packageFiles;
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
const {
|
|
240
|
+
NODE_MODULES,
|
|
241
|
+
NPM,
|
|
242
|
+
NPX,
|
|
243
|
+
SOCKET_CLI_ISSUES_URL
|
|
244
|
+
} = constants;
|
|
245
|
+
function exitWithBinPathError(binName) {
|
|
246
|
+
console.error(`Socket unable to locate ${binName}; ensure it is available in the PATH environment variable.`);
|
|
247
|
+
// The exit code 127 indicates that the command or binary being executed
|
|
248
|
+
// could not be found.
|
|
249
|
+
process.exit(127);
|
|
250
|
+
}
|
|
251
|
+
let _npmBinPathDetails;
|
|
252
|
+
function getNpmBinPathDetails() {
|
|
253
|
+
if (_npmBinPathDetails === undefined) {
|
|
254
|
+
_npmBinPathDetails = findBinPathDetailsSync(NPM);
|
|
255
|
+
}
|
|
256
|
+
return _npmBinPathDetails;
|
|
257
|
+
}
|
|
258
|
+
let _npxBinPathDetails;
|
|
259
|
+
function getNpxBinPathDetails() {
|
|
260
|
+
if (_npxBinPathDetails === undefined) {
|
|
261
|
+
_npxBinPathDetails = findBinPathDetailsSync(NPX);
|
|
262
|
+
}
|
|
263
|
+
return _npxBinPathDetails;
|
|
264
|
+
}
|
|
265
|
+
let _npmBinPath;
|
|
266
|
+
function getNpmBinPath() {
|
|
267
|
+
if (_npmBinPath === undefined) {
|
|
268
|
+
_npmBinPath = getNpmBinPathDetails().path;
|
|
269
|
+
if (!_npmBinPath) {
|
|
270
|
+
exitWithBinPathError(NPM);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return _npmBinPath;
|
|
274
|
+
}
|
|
275
|
+
function isNpmBinPathShadowed() {
|
|
276
|
+
return getNpmBinPathDetails().shadowed;
|
|
277
|
+
}
|
|
278
|
+
let _npxBinPath;
|
|
279
|
+
function getNpxBinPath() {
|
|
280
|
+
if (_npxBinPath === undefined) {
|
|
281
|
+
_npxBinPath = getNpxBinPathDetails().path;
|
|
282
|
+
if (!_npxBinPath) {
|
|
283
|
+
exitWithBinPathError(NPX);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return _npxBinPath;
|
|
287
|
+
}
|
|
288
|
+
function isNpxBinPathShadowed() {
|
|
289
|
+
return getNpxBinPathDetails().shadowed;
|
|
290
|
+
}
|
|
291
|
+
let _npmPath;
|
|
292
|
+
function getNpmPath() {
|
|
293
|
+
if (_npmPath === undefined) {
|
|
294
|
+
const npmEntrypoint = path.dirname(fs.realpathSync.native(getNpmBinPath()));
|
|
295
|
+
_npmPath = findNpmPathSync(npmEntrypoint);
|
|
296
|
+
if (!_npmPath) {
|
|
297
|
+
console.error(`Unable to find npm CLI install directory.
|
|
298
|
+
Searched parent directories of ${npmEntrypoint}.
|
|
299
|
+
|
|
300
|
+
This is may be a bug with socket-npm related to changes to the npm CLI.
|
|
301
|
+
Please report to ${SOCKET_CLI_ISSUES_URL}.`);
|
|
302
|
+
// The exit code 127 indicates that the command or binary being executed
|
|
303
|
+
// could not be found.
|
|
304
|
+
process.exit(127);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return _npmPath;
|
|
308
|
+
}
|
|
309
|
+
let _npmNmPath;
|
|
310
|
+
function getNpmNodeModulesPath() {
|
|
311
|
+
if (_npmNmPath === undefined) {
|
|
312
|
+
_npmNmPath = path.join(getNpmPath(), NODE_MODULES);
|
|
313
|
+
}
|
|
314
|
+
return _npmNmPath;
|
|
315
|
+
}
|
|
316
|
+
let _arboristPkgPath;
|
|
317
|
+
function getArboristPackagePath() {
|
|
318
|
+
if (_arboristPkgPath === undefined) {
|
|
319
|
+
_arboristPkgPath = path.join(getNpmNodeModulesPath(), '@npmcli/arborist');
|
|
320
|
+
}
|
|
321
|
+
return _arboristPkgPath;
|
|
322
|
+
}
|
|
323
|
+
let _arboristClassPath;
|
|
324
|
+
function getArboristClassPath() {
|
|
325
|
+
if (_arboristClassPath === undefined) {
|
|
326
|
+
_arboristClassPath = path.join(getArboristPackagePath(), 'lib/arborist/index.js');
|
|
327
|
+
}
|
|
328
|
+
return _arboristClassPath;
|
|
329
|
+
}
|
|
330
|
+
let _arboristDepValidPath;
|
|
331
|
+
function getArboristDepValidPath() {
|
|
332
|
+
if (_arboristDepValidPath === undefined) {
|
|
333
|
+
_arboristDepValidPath = path.join(getArboristPackagePath(), 'lib/dep-valid.js');
|
|
334
|
+
}
|
|
335
|
+
return _arboristDepValidPath;
|
|
336
|
+
}
|
|
337
|
+
let _arboristEdgeClassPath;
|
|
338
|
+
function getArboristEdgeClassPath() {
|
|
339
|
+
if (_arboristEdgeClassPath === undefined) {
|
|
340
|
+
_arboristEdgeClassPath = path.join(getArboristPackagePath(), 'lib/edge.js');
|
|
341
|
+
}
|
|
342
|
+
return _arboristEdgeClassPath;
|
|
343
|
+
}
|
|
344
|
+
let _arboristNodeClassPath;
|
|
345
|
+
function getArboristNodeClassPath() {
|
|
346
|
+
if (_arboristNodeClassPath === undefined) {
|
|
347
|
+
_arboristNodeClassPath = path.join(getArboristPackagePath(), 'lib/node.js');
|
|
348
|
+
}
|
|
349
|
+
return _arboristNodeClassPath;
|
|
350
|
+
}
|
|
351
|
+
let _arboristOverrideSetClassPath;
|
|
352
|
+
function getArboristOverrideSetClassPath() {
|
|
353
|
+
if (_arboristOverrideSetClassPath === undefined) {
|
|
354
|
+
_arboristOverrideSetClassPath = path.join(getArboristPackagePath(), 'lib/override-set.js');
|
|
355
|
+
}
|
|
356
|
+
return _arboristOverrideSetClassPath;
|
|
357
|
+
}
|
|
358
|
+
|
|
239
359
|
exports.debugLog = debugLog;
|
|
240
|
-
exports.
|
|
241
|
-
exports.
|
|
360
|
+
exports.getArboristClassPath = getArboristClassPath;
|
|
361
|
+
exports.getArboristDepValidPath = getArboristDepValidPath;
|
|
362
|
+
exports.getArboristEdgeClassPath = getArboristEdgeClassPath;
|
|
363
|
+
exports.getArboristNodeClassPath = getArboristNodeClassPath;
|
|
364
|
+
exports.getArboristOverrideSetClassPath = getArboristOverrideSetClassPath;
|
|
365
|
+
exports.getNpmBinPath = getNpmBinPath;
|
|
366
|
+
exports.getNpmNodeModulesPath = getNpmNodeModulesPath;
|
|
367
|
+
exports.getNpxBinPath = getNpxBinPath;
|
|
242
368
|
exports.getPackageFiles = getPackageFiles;
|
|
243
369
|
exports.getPackageFilesFullScans = getPackageFilesFullScans;
|
|
244
370
|
exports.isDebug = isDebug;
|
|
371
|
+
exports.isNpmBinPathShadowed = isNpmBinPathShadowed;
|
|
372
|
+
exports.isNpxBinPathShadowed = isNpxBinPathShadowed;
|
|
245
373
|
exports.logSymbols = logSymbols;
|
|
246
374
|
exports.logger = logger;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { SocketYml } from '@socketsecurity/config';
|
|
3
3
|
import { SocketSdkReturnType } from '@socketsecurity/sdk';
|
|
4
|
-
declare function
|
|
5
|
-
declare function findRoot(filepath: string): string | undefined;
|
|
6
|
-
declare function findBinPathDetails(binName: string): Promise<{
|
|
4
|
+
declare function findBinPathDetailsSync(binName: string): {
|
|
7
5
|
name: string;
|
|
8
6
|
path: string | undefined;
|
|
9
7
|
shadowed: boolean;
|
|
10
|
-
}
|
|
8
|
+
};
|
|
9
|
+
declare function findNpmPathSync(filepath: string): string | undefined;
|
|
11
10
|
declare function getPackageFiles(cwd: string, inputPaths: string[], config: SocketYml | undefined, supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data']): Promise<string[]>;
|
|
12
11
|
declare function getPackageFilesFullScans(cwd: string, inputPaths: string[], supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data'], debugLog?: typeof console.error): Promise<string[]>;
|
|
13
|
-
export {
|
|
12
|
+
export { findBinPathDetailsSync, findNpmPathSync, getPackageFiles, getPackageFilesFullScans };
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import config from '@socketsecurity/config';
|
|
1
2
|
interface Settings {
|
|
2
3
|
apiKey?: string | null;
|
|
3
4
|
enforcedOrgs?: string[] | null;
|
|
4
5
|
apiBaseUrl?: string | null;
|
|
5
6
|
apiProxy?: string | null;
|
|
6
7
|
}
|
|
8
|
+
declare function findSocketYmlSync(): {
|
|
9
|
+
path: string;
|
|
10
|
+
parsed: config.SocketYml;
|
|
11
|
+
} | null;
|
|
7
12
|
declare function getSetting<Key extends keyof Settings>(key: Key): Settings[Key];
|
|
8
13
|
declare function updateSetting<Key extends keyof Settings>(key: Key, value: Settings[Key]): void;
|
|
9
|
-
export { getSetting, updateSetting };
|
|
14
|
+
export { findSocketYmlSync, getSetting, updateSetting };
|
|
@@ -13,21 +13,16 @@ var path = require('node:path');
|
|
|
13
13
|
var process = require('node:process');
|
|
14
14
|
var spawn = _socketInterop(require('@npmcli/promise-spawn'));
|
|
15
15
|
var cmdShim = _socketInterop(require('cmd-shim'));
|
|
16
|
+
var npmPaths = require('./npm-paths.js');
|
|
16
17
|
var constants = require('./constants.js');
|
|
17
|
-
var pathResolve = require('./path-resolve.js');
|
|
18
18
|
|
|
19
|
+
const {
|
|
20
|
+
NPX
|
|
21
|
+
} = constants;
|
|
19
22
|
async function installLinks(realBinPath, binName) {
|
|
23
|
+
const isNpx = binName === NPX;
|
|
20
24
|
// Find package manager being shadowed by this process.
|
|
21
|
-
const
|
|
22
|
-
path: binPath,
|
|
23
|
-
shadowed
|
|
24
|
-
} = await pathResolve.findBinPathDetails(binName);
|
|
25
|
-
if (!binPath) {
|
|
26
|
-
// The exit code 127 indicates that the command or binary being executed
|
|
27
|
-
// could not be found.
|
|
28
|
-
console.error(`Socket unable to locate ${binName}; ensure it is available in the PATH environment variable.`);
|
|
29
|
-
process.exit(127);
|
|
30
|
-
}
|
|
25
|
+
const binPath = isNpx ? npmPaths.getNpxBinPath() : npmPaths.getNpmBinPath();
|
|
31
26
|
// Lazily access constants.WIN32.
|
|
32
27
|
const {
|
|
33
28
|
WIN32
|
|
@@ -36,6 +31,7 @@ async function installLinks(realBinPath, binName) {
|
|
|
36
31
|
if (WIN32 && binPath) {
|
|
37
32
|
return binPath;
|
|
38
33
|
}
|
|
34
|
+
const shadowed = isNpx ? npmPaths.isNpxBinPathShadowed() : npmPaths.isNpmBinPathShadowed();
|
|
39
35
|
// Move our bin directory to front of PATH so its found first.
|
|
40
36
|
if (!shadowed) {
|
|
41
37
|
if (WIN32) {
|
|
@@ -62,10 +58,10 @@ async function shadowBin(binName, binArgs = process.argv.slice(2)) {
|
|
|
62
58
|
// Lazily access constants.distPath.
|
|
63
59
|
path.join(constants.distPath, 'npm-injection.js'),
|
|
64
60
|
// Lazily access constants.shadowBinPath.
|
|
65
|
-
await installLinks(constants.shadowBinPath, binName), ...binArgs
|
|
61
|
+
await installLinks(constants.shadowBinPath, binName), ...(binName === NPM && binArgs.includes('install') ? [
|
|
66
62
|
// Add the `--quiet` and `--no-progress` flags to fix input being swallowed
|
|
67
63
|
// by the spinner when running the command with recent versions of npm.
|
|
68
|
-
...(
|
|
64
|
+
...binArgs.filter(a => a !== '--progress' && a !== '--no-progress'), '--no-progress', ...(binArgs.includes('-q') || binArgs.includes('--quiet') || binArgs.includes('-s') || binArgs.includes('--silent') ? [] : ['--quiet'])] : binArgs)], {
|
|
69
65
|
signal: abortSignal,
|
|
70
66
|
stdio: 'inherit'
|
|
71
67
|
});
|
|
@@ -1,9 +1,25 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { SocketSdk } from "@socketsecurity/sdk";
|
|
3
|
+
import { ObjectEncodingOptions, OpenMode, PathLike } from "node:fs";
|
|
4
|
+
import { promises as fs } from "node:fs";
|
|
5
|
+
import { readFileSync as fsReadFileSync } from "node:fs";
|
|
6
|
+
import { Abortable } from "node:events";
|
|
7
|
+
import { FileHandle } from "node:fs/promises";
|
|
2
8
|
import indentString from "@socketregistry/indent-string/index.cjs";
|
|
3
9
|
import { logSymbols } from "./logging.js";
|
|
4
10
|
declare function getDefaultToken(): string | undefined;
|
|
5
11
|
declare function getPublicToken(): string;
|
|
6
12
|
declare function setupSdk(apiToken?: string | undefined, apiBaseUrl?: string | undefined, proxy?: string | undefined): Promise<SocketSdk>;
|
|
13
|
+
declare function findUp(name: string | string[], { cwd }: {
|
|
14
|
+
cwd: string | undefined;
|
|
15
|
+
}): Promise<string | undefined>;
|
|
16
|
+
type ReadFileOptions = ObjectEncodingOptions & Abortable & {
|
|
17
|
+
flag?: OpenMode | undefined;
|
|
18
|
+
};
|
|
19
|
+
declare function readFileBinary(filepath: PathLike | FileHandle, options?: ReadFileOptions): Promise<Buffer>;
|
|
20
|
+
declare function readFileUtf8(filepath: PathLike | FileHandle, options?: ReadFileOptions): Promise<string>;
|
|
21
|
+
declare function safeReadFile(...args: Parameters<typeof fs.readFile>): ReturnType<typeof fs.readFile> | undefined;
|
|
22
|
+
declare function safeReadFileSync(...args: Parameters<typeof fsReadFileSync>): ReturnType<typeof fsReadFileSync> | undefined;
|
|
7
23
|
declare class ColorOrMarkdown {
|
|
8
24
|
useMarkdown: boolean;
|
|
9
25
|
constructor(useMarkdown: boolean);
|
|
@@ -21,4 +37,4 @@ declare class ColorOrMarkdown {
|
|
|
21
37
|
}
|
|
22
38
|
declare function getSocketDevAlertUrl(alertType: string): string;
|
|
23
39
|
declare function getSocketDevPackageOverviewUrl(eco: string, name: string, version?: string): string;
|
|
24
|
-
export { getDefaultToken, getPublicToken, setupSdk, ColorOrMarkdown, getSocketDevAlertUrl, getSocketDevPackageOverviewUrl };
|
|
40
|
+
export { getDefaultToken, getPublicToken, setupSdk, findUp, ReadFileOptions, readFileBinary, readFileUtf8, safeReadFile, safeReadFileSync, ColorOrMarkdown, getSocketDevAlertUrl, getSocketDevPackageOverviewUrl };
|
|
@@ -12,7 +12,7 @@ function _socketInterop(e) {
|
|
|
12
12
|
var terminalLink = _socketInterop(require('terminal-link'));
|
|
13
13
|
var colors = _socketInterop(require('yoctocolors-cjs'));
|
|
14
14
|
var indentString = require('@socketregistry/indent-string/index.cjs');
|
|
15
|
-
var
|
|
15
|
+
var npmPaths = require('./npm-paths.js');
|
|
16
16
|
var process = require('node:process');
|
|
17
17
|
var hpagent = _socketInterop(require('hpagent'));
|
|
18
18
|
var isInteractive = require('@socketregistry/is-interactive/index.cjs');
|
|
@@ -23,6 +23,7 @@ var sdk = require('@socketsecurity/sdk');
|
|
|
23
23
|
var fs = require('node:fs');
|
|
24
24
|
var os = require('node:os');
|
|
25
25
|
var path = require('node:path');
|
|
26
|
+
var config = require('@socketsecurity/config');
|
|
26
27
|
var constants = require('./constants.js');
|
|
27
28
|
|
|
28
29
|
class AuthError extends Error {}
|
|
@@ -81,10 +82,58 @@ class ColorOrMarkdown {
|
|
|
81
82
|
return this.useMarkdown ? `* ${indentedContent.join('\n* ')}\n` : `${indentedContent.join('\n')}\n`;
|
|
82
83
|
}
|
|
83
84
|
get logSymbols() {
|
|
84
|
-
return this.useMarkdown ? markdownLogSymbols :
|
|
85
|
+
return this.useMarkdown ? markdownLogSymbols : npmPaths.logSymbols;
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
|
|
89
|
+
async function findUp(name, {
|
|
90
|
+
cwd = process.cwd()
|
|
91
|
+
}) {
|
|
92
|
+
let dir = path.resolve(cwd);
|
|
93
|
+
const {
|
|
94
|
+
root
|
|
95
|
+
} = path.parse(dir);
|
|
96
|
+
const names = [name].flat();
|
|
97
|
+
while (dir && dir !== root) {
|
|
98
|
+
for (const name of names) {
|
|
99
|
+
const filePath = path.join(dir, name);
|
|
100
|
+
try {
|
|
101
|
+
// eslint-disable-next-line no-await-in-loop
|
|
102
|
+
const stats = await fs.promises.stat(filePath);
|
|
103
|
+
if (stats.isFile()) {
|
|
104
|
+
return filePath;
|
|
105
|
+
}
|
|
106
|
+
} catch {}
|
|
107
|
+
}
|
|
108
|
+
dir = path.dirname(dir);
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
async function readFileBinary(filepath, options) {
|
|
113
|
+
return await fs.promises.readFile(filepath, {
|
|
114
|
+
...options,
|
|
115
|
+
encoding: 'binary'
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async function readFileUtf8(filepath, options) {
|
|
119
|
+
return await fs.promises.readFile(filepath, {
|
|
120
|
+
...options,
|
|
121
|
+
encoding: 'utf8'
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function safeReadFile(...args) {
|
|
125
|
+
try {
|
|
126
|
+
return fs.promises.readFile(...args);
|
|
127
|
+
} catch {}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
function safeReadFileSync(...args) {
|
|
131
|
+
try {
|
|
132
|
+
return fs.readFileSync(...args);
|
|
133
|
+
} catch {}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
88
137
|
const LOCALAPPDATA = 'LOCALAPPDATA';
|
|
89
138
|
let _settings;
|
|
90
139
|
function getSettings() {
|
|
@@ -92,12 +141,12 @@ function getSettings() {
|
|
|
92
141
|
_settings = {};
|
|
93
142
|
const settingsPath = getSettingsPath();
|
|
94
143
|
if (settingsPath) {
|
|
95
|
-
|
|
96
|
-
|
|
144
|
+
const raw = safeReadFileSync(settingsPath, 'utf8');
|
|
145
|
+
if (raw) {
|
|
97
146
|
try {
|
|
98
147
|
Object.assign(_settings, JSON.parse(Buffer.from(raw, 'base64').toString()));
|
|
99
148
|
} catch {
|
|
100
|
-
|
|
149
|
+
npmPaths.logger.warn(`Failed to parse settings at ${settingsPath}`);
|
|
101
150
|
}
|
|
102
151
|
} else {
|
|
103
152
|
fs.mkdirSync(path.dirname(settingsPath), {
|
|
@@ -121,7 +170,7 @@ function getSettingsPath() {
|
|
|
121
170
|
if (WIN32) {
|
|
122
171
|
if (!_warnedSettingPathWin32Missing) {
|
|
123
172
|
_warnedSettingPathWin32Missing = true;
|
|
124
|
-
|
|
173
|
+
npmPaths.logger.warn(`Missing %${LOCALAPPDATA}%`);
|
|
125
174
|
}
|
|
126
175
|
} else {
|
|
127
176
|
dataHome = path.join(os.homedir(), ...(process.platform === 'darwin' ? ['Library', 'Application Support'] : ['.local', 'share']));
|
|
@@ -131,6 +180,31 @@ function getSettingsPath() {
|
|
|
131
180
|
}
|
|
132
181
|
return _settingsPath;
|
|
133
182
|
}
|
|
183
|
+
function findSocketYmlSync() {
|
|
184
|
+
let prevDir = null;
|
|
185
|
+
let dir = process.cwd();
|
|
186
|
+
while (dir !== prevDir) {
|
|
187
|
+
let ymlPath = path.join(dir, 'socket.yml');
|
|
188
|
+
let yml = safeReadFileSync(ymlPath, 'utf8');
|
|
189
|
+
if (yml === undefined) {
|
|
190
|
+
ymlPath = path.join(dir, 'socket.yaml');
|
|
191
|
+
yml = safeReadFileSync(ymlPath, 'utf8');
|
|
192
|
+
}
|
|
193
|
+
if (typeof yml === 'string') {
|
|
194
|
+
try {
|
|
195
|
+
return {
|
|
196
|
+
path: ymlPath,
|
|
197
|
+
parsed: config.parseSocketConfig(yml)
|
|
198
|
+
};
|
|
199
|
+
} catch {
|
|
200
|
+
throw new Error(`Found file but was unable to parse ${ymlPath}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
prevDir = dir;
|
|
204
|
+
dir = path.join(dir, '..');
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
134
208
|
function getSetting(key) {
|
|
135
209
|
return getSettings()[key];
|
|
136
210
|
}
|
|
@@ -212,11 +286,16 @@ function getSocketDevPackageOverviewUrl(eco, name, version) {
|
|
|
212
286
|
exports.AuthError = AuthError;
|
|
213
287
|
exports.ColorOrMarkdown = ColorOrMarkdown;
|
|
214
288
|
exports.InputError = InputError;
|
|
289
|
+
exports.findSocketYmlSync = findSocketYmlSync;
|
|
290
|
+
exports.findUp = findUp;
|
|
215
291
|
exports.getDefaultToken = getDefaultToken;
|
|
216
292
|
exports.getPublicToken = getPublicToken;
|
|
217
293
|
exports.getSetting = getSetting;
|
|
218
294
|
exports.getSocketDevAlertUrl = getSocketDevAlertUrl;
|
|
219
295
|
exports.getSocketDevPackageOverviewUrl = getSocketDevPackageOverviewUrl;
|
|
220
296
|
exports.isErrnoException = isErrnoException;
|
|
297
|
+
exports.readFileBinary = readFileBinary;
|
|
298
|
+
exports.readFileUtf8 = readFileUtf8;
|
|
299
|
+
exports.safeReadFile = safeReadFile;
|
|
221
300
|
exports.setupSdk = setupSdk;
|
|
222
301
|
exports.updateSetting = updateSetting;
|