microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/services/validationService/util.ts

85lines · modeblame

c7856462Heniker4 years ago1// 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";
948a0d44Heniker4 years ago7import * as commandExists from "command-exists";
c7856462Heniker4 years ago8
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
948a0d44Heniker4 years ago17interface IBasicCheckResult {
18exists: boolean;
19/**
20* - 0 : within range
21* - 1 : gt range
22* - -1 : lt range*/
23versionCompare?: 0 | 1 | -1;
24}
25
26export const basicCheck = async (arg: {
27command: string;
28getVersion?: () => Promise<string | null | undefined>;
29versionRange?: semver.Range | string;
30}): Promise<IBasicCheckResult> => {
31const result = {
32exists: true,
33} as IBasicCheckResult;
34
35if (!commandExists.sync(arg.command)) {
36result.exists = false;
37return result;
38}
39
40const version = await arg.getVersion?.();
41
42if (!version) {
43return result;
44}
45
46if (!arg.versionRange) {
47result.versionCompare = 0;
48return result;
49}
50
51result.versionCompare = semver.gtr(version, arg.versionRange)
52? 1
53: semver.ltr(version, arg.versionRange)
54? -1
55: 0;
56
57return result;
58};
59
60/** Run command and parse output with regex. Get first capturing group. If command does not exist - throws an error. */
61export const parseVersion = async (
c7856462Heniker4 years ago62command: string,
948a0d44Heniker4 years ago63reg?: RegExp,
c7856462Heniker4 years ago64prop: "stdout" | "stderr" = "stdout",
65): Promise<semver.SemVer | null> => {
68c29472Heniker4 years ago66const data = await executeCommand(command).catch(() => {});
67
68if (!data) {
69return null;
70}
71
948a0d44Heniker4 years ago72const text = normizeStr(data[prop]);
73return semver.coerce(reg ? reg.exec(text)?.[1] : text);
c7856462Heniker4 years ago74};
75
76// change typescript lib to es2019 ?
77export const fromEntries = <T = any, J extends PropertyKey = PropertyKey>(
78entries: Iterable<readonly [J, T]>,
79): Record<J, T> =>
80[...entries].reduce((obj, [key, val]) => {
81obj[key] = val;
82return obj;
83}, {} as Record<J, T>);
84
85// export const flatten = (ary: any[]): unknown[] =>