microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-peq/add-security-validation-tests

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/services/validationService/util.ts

82lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as cp from "child_process";
5import { promisify } from "util";
6import * as semver from "semver";
7import * as commandExists from "command-exists";
8
9export const executeCommand = promisify(cp.exec);
10export const normizeStr = (str: string): string => str.replace(/\r\n/g, "\n");
11
12export const createNotFoundMessage = (str: string): string =>
13 `Command not found. Make sure ${str} is installed`;
14export const createVersionErrorMessage = (str: string): string =>
15 `Version check failed. Make sure ${str} is working correctly`;
16
17interface IBasicCheckResult {
18 exists: boolean;
19 /**
20 * - 0 : within range
21 * - 1 : gt range
22 * - -1 : lt range*/
23 versionCompare?: 0 | 1 | -1;
24}
25
26export const basicCheck = async (arg: {
27 command: string;
28 getVersion?: () => Promise<string | semver.SemVer | null | undefined>;
29 versionRange?: semver.Range | string;
30}): Promise<IBasicCheckResult> => {
31 const result = {
32 exists: true,
33 } as IBasicCheckResult;
34
35 if (!commandExists.sync(arg.command)) {
36 result.exists = false;
37 return result;
38 }
39
40 const version = await arg.getVersion?.();
41 if (!version) {
42 return result;
43 }
44
45 if (!arg.versionRange) {
46 result.versionCompare = 0;
47 return result;
48 }
49
50 result.versionCompare = semver.gtr(version, arg.versionRange)
51 ? 1
52 : semver.ltr(version, arg.versionRange)
53 ? -1
54 : 0;
55
56 return result;
57};
58
59/** Run command and parse output with regex. Get first capturing group. If command does not exist - throws an error. */
60export const parseVersion = async (
61 command: string,
62 reg?: RegExp,
63 prop: "stdout" | "stderr" = "stdout",
64): Promise<semver.SemVer | null> => {
65 const data = await executeCommand(command).catch(() => {});
66 if (!data) {
67 return null;
68 }
69 const text = normizeStr(data[prop]);
70 return semver.coerce(reg ? reg.exec(text)?.[0] : text);
71};
72
73// change typescript lib to es2019 ?
74export const fromEntries = <T = any, J extends PropertyKey = PropertyKey>(
75 entries: Iterable<readonly [J, T]>,
76): Record<J, T> =>
77 [...entries].reduce((obj, [key, val]) => {
78 obj[key] = val;
79 return obj;
80 }, {} as Record<J, T>);
81
82// export const flatten = (ary: any[]): unknown[] =>
83