UNPKG

2.27 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.processTemplateName = processTemplateName;
7
8function _path() {
9 const data = _interopRequireDefault(require("path"));
10
11 _path = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _url() {
19 const data = require("url");
20
21 _url = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
30const FILE_PROTOCOL = /file:/;
31const TARBALL = /\.tgz$/;
32const VERSION_POSTFIX = /(.*)(-\d+\.\d+\.\d+)/;
33const VERSIONED_PACKAGE = /(@?.+)(@)(.+)/;
34
35function handleFileProtocol(filePath) {
36 let uri = new (_url().URL)(filePath).pathname;
37
38 if (process.platform === 'win32') {
39 // On Windows, the pathname has an extra leading / so remove that
40 uri = uri.substring(1);
41 }
42
43 return {
44 uri,
45 name: require(_path().default.join(uri, 'package.json')).name
46 };
47}
48
49function handleTarball(filePath) {
50 const nameWithVersion = _path().default.parse(_path().default.basename(filePath)).name;
51
52 const tarballVersionMatch = nameWithVersion.match(VERSION_POSTFIX);
53
54 if (!tarballVersionMatch) {
55 throw new Error(`Failed to retrieve tarball name. We expect the tarball to include package name and version, e.g.: "template-name-1.2.3-rc.0.tgz", but received: "${nameWithVersion}".`);
56 }
57
58 return {
59 uri: filePath,
60 name: tarballVersionMatch[1]
61 };
62}
63
64function handleVersionedPackage(versionedPackage) {
65 const versionedPackageMatch = versionedPackage.match(VERSIONED_PACKAGE);
66
67 if (!versionedPackageMatch) {
68 throw new Error(`Failed to retrieve package name. We expect the package to include name and version, e.g.: "template-name@1.2.3-rc.0", but received: "${versionedPackage}".`);
69 }
70
71 return {
72 uri: versionedPackage,
73 name: versionedPackageMatch[1]
74 };
75}
76
77async function processTemplateName(templateName) {
78 if (templateName.match(TARBALL)) {
79 return handleTarball(templateName);
80 }
81
82 if (templateName.match(FILE_PROTOCOL)) {
83 return handleFileProtocol(templateName);
84 }
85
86 if (templateName.match(VERSIONED_PACKAGE)) {
87 return handleVersionedPackage(templateName);
88 }
89
90 return {
91 uri: templateName,
92 name: templateName
93 };
94}
\No newline at end of file