-
Notifications
You must be signed in to change notification settings - Fork 585
/
ShrinkwrapFile.ts
144 lines (126 loc) · 4.98 KB
/
ShrinkwrapFile.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as colors from 'colors';
import * as fsx from 'fs-extra';
import * as os from 'os';
import * as semver from 'semver';
import npmPackageArg = require('npm-package-arg');
import { Utilities, RushConstants } from '@microsoft/rush-lib';
interface IShrinkwrapDependencyJson {
version: string;
from: string;
resolved: string;
dependencies: { [dependency: string]: IShrinkwrapDependencyJson };
}
interface IShrinkwrapJson {
name: string;
version: string;
dependencies: { [dependency: string]: IShrinkwrapDependencyJson };
}
/**
* This class is a parser for NPM's npm-shrinkwrap.json file format.
*/
export default class ShrinkwrapFile {
private _shrinkwrapJson: IShrinkwrapJson;
private _alreadyWarnedSpecs: Set<string> = new Set<string>();
public static loadFromFile(shrinkwrapJsonFilename: string): ShrinkwrapFile | undefined {
let data: string = undefined;
try {
if (!fsx.existsSync(shrinkwrapJsonFilename)) {
return undefined; // file does not exist
}
// We don't use JsonFile/jju here because shrinkwrap.json is a special NPM file format
// and typically very large, so we want to load it the same way that NPM does.
data = fsx.readFileSync(shrinkwrapJsonFilename).toString();
if (data.charCodeAt(0) === 0xFEFF) { // strip BOM
data = data.slice(1);
}
return new ShrinkwrapFile(JSON.parse(data));
} catch (error) {
throw new Error(`Error reading "${shrinkwrapJsonFilename}":` + os.EOL + ` ${error.message}`);
}
}
private static tryGetValue<T>(dictionary: { [key2: string]: T }, key: string): T | undefined {
if (dictionary.hasOwnProperty(key)) {
return dictionary[key];
}
return undefined;
}
private constructor(shrinkwrapJson: IShrinkwrapJson) {
this._shrinkwrapJson = shrinkwrapJson;
// Normalize the data
if (!this._shrinkwrapJson.version) {
this._shrinkwrapJson.version = '';
}
if (!this._shrinkwrapJson.name) {
this._shrinkwrapJson.name = '';
}
if (!this._shrinkwrapJson.dependencies) {
this._shrinkwrapJson.dependencies = { };
}
}
/**
* Returns the list of temp projects defined in this file.
* Example: [ '@rush-temp/project1', '@rush-temp/project2' ]
*/
public getTempProjectNames(): ReadonlyArray<string> {
const result: string[] = [];
for (const key of Object.keys(this._shrinkwrapJson.dependencies)) {
// If it starts with @rush-temp, then include it:
if (Utilities.parseScopedPackageName(key).scope === RushConstants.rushTempNpmScope) {
result.push(key);
}
}
result.sort(); // make the result deterministic
return result;
}
/**
* Returns true if the shrinkwrap file includes a package that would satisfiying the specified
* package name and SemVer version range. By default, the dependencies are resolved by looking
* at the root of the node_modules folder described by the shrinkwrap file. However, if
* tempProjectName is specified, then the resolution will start in that subfolder.
*
* Consider this example:
*
* - node_modules\
* - temp-project\
* - lib-a@1.2.3
* - lib-b@1.0.0
* - lib-b@2.0.0
*
* In this example, hasCompatibleDependency("lib-b", ">= 1.1.0", "temp-project") would fail
* because it finds lib-b@1.0.0 which does not satisfy the pattern ">= 1.1.0".
*/
public hasCompatibleDependency(dependencyName: string, versionRange: string, tempProjectName?: string): boolean {
// First, check under tempProjectName, as this is the first place "rush link" looks.
let dependencyJson: IShrinkwrapDependencyJson = undefined;
if (tempProjectName) {
const tempDependency: IShrinkwrapDependencyJson = ShrinkwrapFile.tryGetValue(
this._shrinkwrapJson.dependencies, tempProjectName);
if (tempDependency && tempDependency.dependencies) {
dependencyJson = ShrinkwrapFile.tryGetValue(tempDependency.dependencies, dependencyName);
}
}
// Otherwise look at the root of the shrinkwrap file
if (!dependencyJson) {
dependencyJson = ShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
}
if (!dependencyJson) {
return false;
}
const result: npmPackageArg.IResult = npmPackageArg.resolve(dependencyName, versionRange);
switch (result.type) {
case 'version':
case 'range':
// If it's a SemVer pattern, then require that the shrinkwrapped version must be compatible
return semver.satisfies(dependencyJson.version, versionRange);
default:
// Only warn once for each spec
if (!this._alreadyWarnedSpecs.has(result.rawSpec)) {
this._alreadyWarnedSpecs.add(result.rawSpec);
console.log(colors.yellow(`WARNING: Not validating ${result.type}-based specifier: "${result.rawSpec}"`));
}
return true;
}
}
}