UNPKG

3.27 kBJavaScriptView Raw
1/*
2 * Copyright 2014-2016 Guy Bedford (http://guybedford.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/*
17 Parse a package name into registry:package@version
18
19 name: 'github:jquery/jquery',
20 exactName: 'github:jquery/jquery@2.0.3',
21 exactPackage: 'jquery/jquery@2.0.3',
22
23 registry: 'github',
24 package: 'jquery/jquery',
25 version: '2.0.3'
26*/
27module.exports = Package;
28
29var path = require('path');
30
31function Package(name, escaped) {
32 // (never detect :// protocol specifier as a registry to allow protocol maps)
33 if (name.indexOf(':') == -1 || name.split(':')[1].substr(0, 2) == '//') {
34 this.registry = '';
35 this.name = name;
36 return;
37 }
38
39 this.registry = name.split(':')[0];
40
41 var pkg = name.substr(this.registry.length + 1);
42
43 var versionIndex = pkg.lastIndexOf('@');
44 var version = '';
45
46 if (versionIndex !== -1 && versionIndex !== 0) {
47 version = pkg.substr(versionIndex + 1);
48 pkg = pkg.substr(0, versionIndex);
49 }
50
51 this.package = pkg;
52
53 // if escaped and version contains / then this is a custom map into a package subpath
54 if (escaped && version.indexOf('/') != -1) {
55 this.registry = '';
56 this.name = name;
57 return;
58 }
59
60 if (escaped)
61 version = version.replace(/\%25|\%2F/g, function(encoding) {
62 if (encoding == '%25')
63 return '%';
64 if (encoding == '%2F')
65 return '/';
66 });
67
68 this.setVersion(version);
69}
70
71// sets name, exactName, exactPackage from registry, package, version
72function setDerivedProperties(pkg) {
73 var v = pkg.version ? '@' + pkg.version : '';
74 pkg.name = (pkg.registry ? pkg.registry + ':' : '') + pkg.package;
75 pkg.exactPackage = pkg.package + v;
76 pkg.exactName = pkg.name + v;
77 pkg.exactNameEncoded = pkg.name + pkg.getEncodedVersion();
78}
79
80Package.prototype.setVersion = function(version) {
81 this.version = version == '*' ? '' : version;
82 setDerivedProperties(this);
83 return this;
84};
85
86Package.prototype.setRegistry = function(registry) {
87 this.registry = registry;
88 setDerivedProperties(this);
89 return this;
90};
91
92Package.prototype.setPackage = function(name) {
93 this.package = name;
94 setDerivedProperties(this);
95 return this;
96};
97
98Package.prototype.copy = function() {
99 return new Package(this.exactName);
100};
101
102Package.prototype.getPath = function(packagesFolder) {
103 var config = require('./config');
104 return path.resolve(packagesFolder || config.pjson.packages, this.registry, this.package + this.getEncodedVersion());
105};
106
107Package.prototype.getEncodedVersion = function() {
108 if (!this.version)
109 return '';
110 return '@' + this.version.replace(/[\/%]/g, function(symbol) {
111 return encodeURIComponent(symbol);
112 });
113};
114
115Package.prototype.toString = function() {
116 return this.name + this.getEncodedVersion();
117};