-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathNodeDetector.ts
More file actions
187 lines (160 loc) · 7.26 KB
/
NodeDetector.ts
File metadata and controls
187 lines (160 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// adapted from Oryx's C# implementation
// https://github.com/microsoft/Oryx/blob/main/src/Detector/Node/NodeDetector.cs
import { AzExtFsExtra, parseError } from "@microsoft/vscode-azext-utils";
import { Uri } from 'vscode';
import { parse } from 'yaml';
import { NodeConstants } from "./nodeConstants";
type PackageJson = {
engines?: {
node?: string
},
devDependencies?: {
[key: string]: string
},
dependencies?: {
[key: string]: string
}
}
type FrameworkInfo = {
framework: string,
version: string
}
export type DetectorResults = {
platform: string | undefined,
platformVersion: string | undefined,
appDirectory: string | undefined,
frameworks: FrameworkInfo[],
hasLernaJsonFile: boolean,
hasLageConfigJSFile: boolean,
lernaNpmClient: string | undefined,
hasYarnrcYmlFile: boolean,
isYarnLockFileValidYamlFormat: boolean,
}
export class NodeDetector {
public async detect(uri: Uri): Promise<DetectorResults | undefined> {
let isNodeApp = false;
let hasLernaJsonFile = false;
let hasLageConfigJSFile = false;
let hasYarnrcYmlFile = false;
let isYarnLockFileValidYamlFormat = false;
const appDirectory = '';
let lernaNpmClient = '';
isNodeApp = (await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.PackageJsonFileName)) ||
await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.PackageLockJsonFileName)) ||
await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.YarnLockFileName)));
hasYarnrcYmlFile = await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.YarnrcYmlName));
const yarnLockUri: Uri = Uri.joinPath(uri, NodeConstants.YarnLockFileName);
isYarnLockFileValidYamlFormat = await AzExtFsExtra.pathExists(yarnLockUri) && await this.isYarnLockFileYamlFile(yarnLockUri);
if (await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.LernaJsonFileName))) {
hasLernaJsonFile = true;
lernaNpmClient = await this.getLearnJsonNpmClient(uri);
}
hasLageConfigJSFile = await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.LageConfigJSFileName));
// Copying the logic currently running in Kudu:
if (!isNodeApp) {
let mightBeNode = false;
for (const nodeFile of NodeConstants.TypicalNodeDetectionFiles) {
if (await AzExtFsExtra.pathExists(Uri.joinPath(uri, nodeFile))) {
mightBeNode = true;
break;
}
}
// Check if any of the known iis start pages exist
// If so, then it is not a node.js web site otherwise it is
if (mightBeNode) {
for (const iisStartupFile of NodeConstants.IisStartupFiles) {
if (await AzExtFsExtra.pathExists(Uri.joinPath(uri, iisStartupFile))) {
// "App in repo is not a Node.js app as it has the file {iisStartupFile}"
return undefined;
}
}
isNodeApp = true;
} else {
// "Could not find typical Node.js files in repo"
}
}
if (!isNodeApp) {
return undefined;
}
const version = await this.getVersionFromPackageJson(uri);
const detectedFrameworkInfos = await this.detectFrameworkInfos(uri);
return {
platform: NodeConstants.PlatformName,
platformVersion: version,
appDirectory,
frameworks: detectedFrameworkInfos,
hasLernaJsonFile,
hasLageConfigJSFile,
lernaNpmClient,
hasYarnrcYmlFile,
isYarnLockFileValidYamlFormat,
};
}
private async isYarnLockFileYamlFile(yamlFileUri: Uri): Promise<boolean> {
try {
const yamlFile = await AzExtFsExtra.readFile(yamlFileUri);
parse(yamlFile);
return true;
} catch (err) {
return false;
}
}
private async getVersionFromPackageJson(uri: Uri): Promise<string | undefined> {
const packageJson = await this.getPackageJsonObject(uri);
return packageJson?.engines?.node;
}
private async getPackageJsonObject(uri: Uri): Promise<PackageJson | undefined> {
try {
return <{ engines?: { node?: string } }>JSON.parse((await AzExtFsExtra.readFile(Uri.joinPath(uri, NodeConstants.PackageJsonFileName))));
} catch (err) {
console.error(parseError(err).message);
return undefined;
}
}
private async detectFrameworkInfos(uri: Uri): Promise<FrameworkInfo[]> {
const detectedFrameworkResult: FrameworkInfo[] = [];
const packageJson = await this.getPackageJsonObject(uri);
if (packageJson?.devDependencies !== undefined) {
const devDependencies = packageJson.devDependencies;
for (const framework of Object.keys(NodeConstants.DevDependencyFrameworkKeyWordToName)) {
if (devDependencies[framework]) {
detectedFrameworkResult.push({ framework: NodeConstants.DevDependencyFrameworkKeyWordToName[framework as keyof typeof NodeConstants.DevDependencyFrameworkKeyWordToName], version: devDependencies[framework] });
}
}
}
if (packageJson?.dependencies !== undefined) {
const dependencies = packageJson.dependencies;
for (const framework of Object.keys(NodeConstants.DependencyFrameworkKeyWordToName)) {
if (dependencies[framework]) {
detectedFrameworkResult.push({ framework: NodeConstants.DependencyFrameworkKeyWordToName[framework as keyof typeof NodeConstants.DependencyFrameworkKeyWordToName], version: dependencies[framework] });
}
}
}
if (await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.FlutterYamlFileName))) {
detectedFrameworkResult.push({ framework: NodeConstants.FlutterFrameworkeName, version: '' });
}
return detectedFrameworkResult;
}
private async getLearnJsonNpmClient(uri: Uri) {
let npmClientName: string = '';
if (!await AzExtFsExtra.pathExists(Uri.joinPath(uri, NodeConstants.LernaJsonFileName))) {
return npmClientName;
}
try {
const learnJson = <{ npmClient?: string }>JSON.parse((await AzExtFsExtra.readFile(Uri.joinPath(uri, NodeConstants.LernaJsonFileName))));
if (learnJson?.npmClient) {
npmClientName = learnJson.npmClient;
} else {
//Default Client for Lerna is npm.
npmClientName = NodeConstants.NpmToolName;
}
} catch (err) {
console.error(`Exception caught while trying to deserialize ${NodeConstants.LernaJsonFileName}`);
}
return npmClientName;
}
}