forked from boltpkg/bolt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPackage.js
182 lines (157 loc) · 4.48 KB
/
Package.js
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
// @flow
import * as path from 'path';
import Config from './Config';
import { DEPENDENCY_TYPES } from './constants';
import * as processes from './utils/processes';
import * as semver from 'semver';
import * as logger from './utils/logger';
import * as messages from './utils/messages';
import sortObject from 'sort-object';
import { BoltError } from './utils/errors';
export default class Package {
filePath: string;
dir: string;
nodeModules: string;
nodeModulesBin: string;
contents: string;
config: Config;
indent: string;
constructor(filePath: string, config: Config) {
this.filePath = filePath;
this.dir = path.dirname(filePath);
this.nodeModules = path.join(this.dir, 'node_modules');
this.nodeModulesBin = path.join(this.nodeModules, '.bin');
this.config = config;
}
static async init(filePath: string) {
let config = await Config.init(filePath);
if (!config) {
throw new BoltError(`Could not init config for "${filePath}"`);
}
return new Package(filePath, config);
}
static async closest(filePath: string) {
let pkgPath = await Config.findConfigFile(filePath);
if (!pkgPath) {
throw new BoltError(`Could not find package.json from "${filePath}"`);
}
return await Package.init(pkgPath);
}
getWorkspacesConfig(): Array<string> {
return this.config.getWorkspaces() || [];
}
getAllDependencies() {
let allDependencies = new Map();
for (let type of DEPENDENCY_TYPES) {
let deps = this.config.getDeps(type);
if (!deps) continue;
for (let name of Object.keys(deps)) {
allDependencies.set(name, deps[name]);
}
}
return allDependencies;
}
async setDependencyVersionRange(
depName: string,
depType: string,
versionRange: string | null
) {
let prevDeps = this.config.getDeps(depType);
let prevVersionRange = (prevDeps && prevDeps[depName]) || null;
if (prevVersionRange === versionRange) return;
await this._updateDependencies(depType, {
...prevDeps,
[depName]: versionRange
});
let pkgName = this.config.getDescriptor();
if (versionRange === null) {
logger.info(messages.removedPackageDependency(pkgName, depName));
} else if (prevVersionRange === null) {
logger.info(
messages.addedPackageDependency(pkgName, depName, versionRange)
);
} else {
logger.info(
messages.updatedPackageDependency(
pkgName,
depName,
versionRange,
prevVersionRange
)
);
}
}
async _updateDependencies(
depType: string,
dependencies: { [key: string]: string | null }
) {
let cleaned = {};
for (let depName of Object.keys(dependencies)) {
let versionRange = dependencies[depName];
if (typeof versionRange === 'string') {
cleaned[depName] = versionRange;
}
}
await this.config.write({
...this.config.getConfig(),
[depType]: sortObject(cleaned)
});
}
getDependencyTypes(depName: string): Array<string> {
let matchedTypes = [];
for (let depType of DEPENDENCY_TYPES) {
let deps = this.config.getDeps(depType);
if (deps && deps[depName]) {
matchedTypes.push(depType);
}
}
return matchedTypes;
}
getDependencyVersionRange(depName: string) {
for (let depType of DEPENDENCY_TYPES) {
let deps = this.config.getDeps(depType);
if (deps && deps[depName]) {
return deps[depName];
}
}
return null;
}
// async maybeUpdateDependencyVersionRange(depName: string, current: string, version: string) {
// let versionRange = '^' + version;
// let updated = false;
// if (semver.satisfies(version, current)) {
// await this.updateDependencyVersionRange(depName, versionRange);
// updated = true;
// }
// return updated;
// }
isSamePackage(pkg: Package) {
return pkg.dir === this.dir;
}
getName() {
return this.config.getName();
}
getVersion() {
return this.config.getVersion();
}
getBins(): Array<{ name: string, filePath: string }> {
let bin = this.config.getBin();
if (typeof bin === 'undefined') {
return [];
} else if (typeof bin === 'string') {
return [
{
name: this.config.getName(),
filePath: path.join(this.dir, bin)
}
];
} else {
return Object.keys(bin).map(key => {
return {
name: key,
filePath: path.join(this.dir, bin[key])
};
});
}
}
}