Skip to content

Commit b74b686

Browse files
author
Nick Pape
committed
Make the shrinkwrap be switchable
1 parent 7702db7 commit b74b686

3 files changed

Lines changed: 223 additions & 98 deletions

File tree

‎apps/rush/src/utilities/ShrinkwrapFile.ts‎

Lines changed: 27 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,112 +2,36 @@
22
// See LICENSE in the project root for license information.
33

44
import * as colors from 'colors';
5-
import * as fsx from 'fs-extra';
6-
import * as os from 'os';
75
import * as semver from 'semver';
86
import npmPackageArg = require('npm-package-arg');
97

108
import { Utilities, RushConstants } from '@microsoft/rush-lib';
119

12-
interface IShrinkwrapDependencyJson {
13-
version: string;
14-
from: string;
15-
resolved: string;
16-
dependencies: { [dependency: string]: IShrinkwrapDependencyJson };
17-
}
18-
19-
interface IShrinkwrapJson {
20-
name: string;
21-
version: string;
22-
dependencies: { [dependency: string]: IShrinkwrapDependencyJson };
23-
}
24-
2510
/**
26-
* This class is a parser for NPM's npm-shrinkwrap.json file format.
11+
* This class is a parser for both NPM's npm-shrinkwrap.json and PNPM's shrinkwrap.yaml file formats.
2712
*/
28-
export default class ShrinkwrapFile {
29-
private _shrinkwrapJson: IShrinkwrapJson;
30-
private _alreadyWarnedSpecs: Set<string> = new Set<string>();
31-
32-
public static loadFromFile(shrinkwrapJsonFilename: string): ShrinkwrapFile | undefined {
33-
let data: string | undefined = undefined;
34-
try {
35-
if (!fsx.existsSync(shrinkwrapJsonFilename)) {
36-
return undefined; // file does not exist
37-
}
13+
export default abstract class ShrinkwrapFile {
14+
protected _alreadyWarnedSpecs: Set<string> = new Set<string>();
3815

39-
// We don't use JsonFile/jju here because shrinkwrap.json is a special NPM file format
40-
// and typically very large, so we want to load it the same way that NPM does.
41-
data = fsx.readFileSync(shrinkwrapJsonFilename).toString();
42-
if (data.charCodeAt(0) === 0xFEFF) { // strip BOM
43-
data = data.slice(1);
44-
}
45-
46-
return new ShrinkwrapFile(JSON.parse(data));
47-
} catch (error) {
48-
throw new Error(`Error reading "${shrinkwrapJsonFilename}":` + os.EOL + ` ${error.message}`);
49-
}
16+
public static loadFromFile(shrinkwrapFilename: string): ShrinkwrapFile | undefined {
17+
return require('./npm/NpmShrinkwrapFile').default.loadFromFile(shrinkwrapFilename)
18+
|| require('./npm/PnpmShrinkwrapFile').default.loadFromFile(shrinkwrapFilename);
5019
}
5120

52-
private static tryGetValue<T>(dictionary: { [key2: string]: T }, key: string): T | undefined {
21+
protected static tryGetValue<T>(dictionary: { [key2: string]: T }, key: string): T | undefined {
5322
if (dictionary.hasOwnProperty(key)) {
5423
return dictionary[key];
5524
}
5625
return undefined;
5726
}
5827

59-
/**
60-
* Returns the list of temp projects defined in this file.
61-
* Example: [ '@rush-temp/project1', '@rush-temp/project2' ]
62-
*/
63-
public getTempProjectNames(): ReadonlyArray<string> {
64-
const result: string[] = [];
65-
for (const key of Object.keys(this._shrinkwrapJson.dependencies)) {
66-
// If it starts with @rush-temp, then include it:
67-
if (Utilities.parseScopedPackageName(key).scope === RushConstants.rushTempNpmScope) {
68-
result.push(key);
69-
}
70-
}
71-
result.sort(); // make the result deterministic
72-
return result;
73-
}
74-
7528
/**
7629
* Returns true if the shrinkwrap file includes a package that would satisfiying the specified
77-
* package name and SemVer version range. By default, the dependencies are resolved by looking
78-
* at the root of the node_modules folder described by the shrinkwrap file. However, if
79-
* tempProjectName is specified, then the resolution will start in that subfolder.
80-
*
81-
* Consider this example:
82-
*
83-
* - node_modules\
84-
* - temp-project\
85-
* - lib-a@1.2.3
86-
* - lib-b@1.0.0
87-
* - lib-b@2.0.0
88-
*
89-
* In this example, hasCompatibleDependency("lib-b", ">= 1.1.0", "temp-project") would fail
90-
* because it finds lib-b@1.0.0 which does not satisfy the pattern ">= 1.1.0".
30+
* package name and SemVer version range.
9131
*/
9232
public hasCompatibleDependency(dependencyName: string, versionRange: string, tempProjectName?: string): boolean {
93-
94-
// First, check under tempProjectName, as this is the first place "rush link" looks.
95-
let dependencyJson: IShrinkwrapDependencyJson | undefined = undefined;
96-
97-
if (tempProjectName) {
98-
const tempDependency: IShrinkwrapDependencyJson | undefined = ShrinkwrapFile.tryGetValue(
99-
this._shrinkwrapJson.dependencies, tempProjectName);
100-
if (tempDependency && tempDependency.dependencies) {
101-
dependencyJson = ShrinkwrapFile.tryGetValue(tempDependency.dependencies, dependencyName);
102-
}
103-
}
104-
105-
// Otherwise look at the root of the shrinkwrap file
106-
if (!dependencyJson) {
107-
dependencyJson = ShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
108-
}
109-
110-
if (!dependencyJson) {
33+
const dependencyVersion: string | undefined = this.getDependencyVersion(dependencyName, tempProjectName);
34+
if (!dependencyVersion) {
11135
return false;
11236
}
11337

@@ -116,7 +40,7 @@ export default class ShrinkwrapFile {
11640
case 'version':
11741
case 'range':
11842
// If it's a SemVer pattern, then require that the shrinkwrapped version must be compatible
119-
return semver.satisfies(dependencyJson.version, versionRange);
43+
return semver.satisfies(dependencyVersion, versionRange);
12044
default:
12145
// Only warn once for each spec
12246
if (!this._alreadyWarnedSpecs.has(result.rawSpec)) {
@@ -127,18 +51,23 @@ export default class ShrinkwrapFile {
12751
}
12852
}
12953

130-
private constructor(shrinkwrapJson: IShrinkwrapJson) {
131-
this._shrinkwrapJson = shrinkwrapJson;
54+
/**
55+
* Returns the list of temp projects defined in this file.
56+
* Example: [ '@rush-temp/project1', '@rush-temp/project2' ]
57+
*/
58+
public abstract getTempProjectNames(): ReadonlyArray<string>;
59+
60+
protected abstract getDependencyVersion(dependencyName: string, tempProjectName?: string): string | undefined;
13261

133-
// Normalize the data
134-
if (!this._shrinkwrapJson.version) {
135-
this._shrinkwrapJson.version = '';
136-
}
137-
if (!this._shrinkwrapJson.name) {
138-
this._shrinkwrapJson.name = '';
139-
}
140-
if (!this._shrinkwrapJson.dependencies) {
141-
this._shrinkwrapJson.dependencies = { };
62+
protected _getTempProjectNames(dependencies: { [key: string]: {} } ): ReadonlyArray<string> {
63+
const result: string[] = [];
64+
for (const key of Object.keys(dependencies)) {
65+
// If it starts with @rush-temp, then include it:
66+
if (Utilities.parseScopedPackageName(key).scope === RushConstants.rushTempNpmScope) {
67+
result.push(key);
68+
}
14269
}
70+
result.sort(); // make the result deterministic
71+
return result;
14372
}
14473
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import * as fsx from 'fs-extra';
2+
import * as os from 'os';
3+
4+
import ShrinkwrapFile from '../ShrinkwrapFile';
5+
6+
interface IShrinkwrapDependencyJson {
7+
version: string;
8+
from: string;
9+
resolved: string;
10+
dependencies: { [dependency: string]: IShrinkwrapDependencyJson };
11+
}
12+
13+
interface IShrinkwrapJson {
14+
name: string;
15+
version: string;
16+
dependencies: { [dependency: string]: IShrinkwrapDependencyJson };
17+
}
18+
19+
export default class NpmShrinkwrapFile extends ShrinkwrapFile {
20+
private _shrinkwrapJson: IShrinkwrapJson;
21+
22+
public static loadFromFile(shrinkwrapJsonFilename: string): NpmShrinkwrapFile | undefined {
23+
let data: string | undefined = undefined;
24+
try {
25+
if (!fsx.existsSync(shrinkwrapJsonFilename)) {
26+
return undefined; // file does not exist
27+
}
28+
29+
// We don't use JsonFile/jju here because shrinkwrap.json is a special NPM file format
30+
// and typically very large, so we want to load it the same way that NPM does.
31+
data = fsx.readFileSync(shrinkwrapJsonFilename).toString();
32+
if (data.charCodeAt(0) === 0xFEFF) { // strip BOM
33+
data = data.slice(1);
34+
}
35+
36+
return new NpmShrinkwrapFile(JSON.parse(data));
37+
} catch (error) {
38+
throw new Error(`Error reading "${shrinkwrapJsonFilename}":` + os.EOL + ` ${error.message}`);
39+
}
40+
}
41+
42+
public getTempProjectNames(): ReadonlyArray<string> {
43+
return this._getTempProjectNames(this._shrinkwrapJson.dependencies);
44+
}
45+
46+
/**
47+
* Returns true if the shrinkwrap file includes a package that would satisfiying the specified
48+
* package name and SemVer version range. By default, the dependencies are resolved by looking
49+
* at the root of the node_modules folder described by the shrinkwrap file. However, if
50+
* tempProjectName is specified, then the resolution will start in that subfolder.
51+
*
52+
* Consider this example:
53+
*
54+
* - node_modules\
55+
* - temp-project\
56+
* - lib-a@1.2.3
57+
* - lib-b@1.0.0
58+
* - lib-b@2.0.0
59+
*
60+
* In this example, hasCompatibleDependency("lib-b", ">= 1.1.0", "temp-project") would fail
61+
* because it finds lib-b@1.0.0 which does not satisfy the pattern ">= 1.1.0".
62+
*/
63+
protected getDependencyVersion(dependencyName: string, tempProjectName?: string): string | undefined {
64+
65+
// First, check under tempProjectName, as this is the first place "rush link" looks.
66+
let dependencyJson: IShrinkwrapDependencyJson | undefined = undefined;
67+
68+
if (tempProjectName) {
69+
const tempDependency: IShrinkwrapDependencyJson | undefined = ShrinkwrapFile.tryGetValue(
70+
this._shrinkwrapJson.dependencies, tempProjectName);
71+
if (tempDependency && tempDependency.dependencies) {
72+
dependencyJson = ShrinkwrapFile.tryGetValue(tempDependency.dependencies, dependencyName);
73+
}
74+
}
75+
76+
// Otherwise look at the root of the shrinkwrap file
77+
if (!dependencyJson) {
78+
dependencyJson = ShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
79+
}
80+
81+
if (!dependencyJson) {
82+
return undefined;
83+
}
84+
85+
return dependencyJson.version;
86+
}
87+
88+
private constructor(shrinkwrapJson: IShrinkwrapJson) {
89+
super();
90+
this._shrinkwrapJson = shrinkwrapJson;
91+
92+
// Normalize the data
93+
if (!this._shrinkwrapJson.version) {
94+
this._shrinkwrapJson.version = '';
95+
}
96+
if (!this._shrinkwrapJson.name) {
97+
this._shrinkwrapJson.name = '';
98+
}
99+
if (!this._shrinkwrapJson.dependencies) {
100+
this._shrinkwrapJson.dependencies = { };
101+
}
102+
}
103+
104+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as fsx from 'fs-extra';
2+
import * as yaml from 'js-yaml';
3+
import * as os from 'os';
4+
5+
import ShrinkwrapFile from '../ShrinkwrapFile';
6+
7+
interface IShrinkwrapDependencyJson {
8+
resolution: {
9+
integrity: string;
10+
tarball?: string;
11+
};
12+
dependencies: { [dependency: string]: string };
13+
}
14+
15+
interface IShrinkwrapJson {
16+
/** The list of resolved direct dependencies */
17+
dependencies: { [dependency: string]: string };
18+
/** The description of the solved DAG */
19+
packages: { [dependencyVersion: string]: IShrinkwrapDependencyJson };
20+
/** URL of the registry */
21+
registry: string;
22+
shrinkwrapVersion: number;
23+
/** The list of specifiers used to resolve direct dependency versions */
24+
specifiers: { [dependency: string]: string };
25+
}
26+
27+
export default class PnpmShrinkwrapFile extends ShrinkwrapFile {
28+
private _shrinkwrapJson: IShrinkwrapJson;
29+
30+
public static loadFromFile(shrinkwrapYamlFilename: string): ShrinkwrapFile | undefined {
31+
try {
32+
if (!fsx.existsSync(shrinkwrapYamlFilename)) {
33+
return undefined; // file does not exist
34+
}
35+
36+
// We don't use JsonFile/jju here because shrinkwrap.json is a special NPM file format
37+
// and typically very large, so we want to load it the same way that NPM does.
38+
const parsedData: IShrinkwrapJson = yaml.safeLoad(fsx.readFileSync(shrinkwrapYamlFilename).toString());
39+
40+
return new PnpmShrinkwrapFile(parsedData);
41+
} catch (error) {
42+
throw new Error(`Error reading "${shrinkwrapYamlFilename}":` + os.EOL + ` ${error.message}`);
43+
}
44+
}
45+
46+
public getTempProjectNames(): ReadonlyArray<string> {
47+
return this._getTempProjectNames(this._shrinkwrapJson.dependencies);
48+
}
49+
50+
// @todo this still has problems!
51+
protected getDependencyVersion(dependencyName: string): string | undefined {
52+
let dependencyVersion: string | undefined = undefined;
53+
54+
if (!dependencyVersion) {
55+
dependencyVersion = ShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
56+
}
57+
58+
if (!dependencyVersion) {
59+
return undefined;
60+
}
61+
62+
// dependency version can also be a pnpm path such as "/gulp-karma/0.0.5/karma@0.13.22"
63+
// in this case we want the first version number that appears. it will be in 3rd spot
64+
if (dependencyVersion[0] === '/') {
65+
dependencyVersion = dependencyVersion.split('/')[2];
66+
}
67+
68+
return dependencyVersion;
69+
}
70+
71+
private constructor(shrinkwrapJson: IShrinkwrapJson) {
72+
super();
73+
this._shrinkwrapJson = shrinkwrapJson;
74+
75+
// Normalize the data
76+
if (!this._shrinkwrapJson.registry) {
77+
this._shrinkwrapJson.registry = '';
78+
}
79+
if (!this._shrinkwrapJson.shrinkwrapVersion) {
80+
this._shrinkwrapJson.shrinkwrapVersion = 3; // 3 is the current version for pnpm
81+
}
82+
if (!this._shrinkwrapJson.dependencies) {
83+
this._shrinkwrapJson.dependencies = { };
84+
}
85+
if (!this._shrinkwrapJson.specifiers) {
86+
this._shrinkwrapJson.specifiers = { };
87+
}
88+
if (!this._shrinkwrapJson.packages) {
89+
this._shrinkwrapJson.packages = { };
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)