@@ -16,61 +16,116 @@ exports.is_special_installname = function (pkg_name = '') {
16
16
return false ;
17
17
}
18
18
19
- const parse_pkg_installname = exports . parse_pkg_installname = function ( input_uri = '' , version = '' ) {
20
- /**
21
- * when providing version but not semantic version, consider it as special installation name
22
- */
23
- if ( version && exports . is_special_installname ( version ) ) {
24
- input_uri = version
25
- }
26
-
27
- const Is = {
28
- http : false ,
29
- github : false ,
30
- registry : false ,
31
- file : false ,
32
- }
33
- const uriObj = url . parse ( input_uri )
34
- Is . http = uriObj . protocol === 'http:' || uriObj . protocol === 'https:'
35
-
36
- const origin = ! Is . http ? '' : `${ uriObj . protocol } //${ uriObj . host } ` ;
37
-
38
-
39
- let pkg_pattern = input_uri ;
40
- if ( Is . http ) pkg_pattern = `${ uriObj . pathname } ${ uriObj . hash } ` ;
41
-
42
- pkg_pattern = helpers_string . ensure_unprefix ( pkg_pattern ) ;
43
-
44
- let [ scope_str , pkg ] = ( pkg_pattern || '' ) . split ( '/' )
45
-
46
- if ( ! pkg ) {
47
- pkg = scope_str
48
- scope_str = ''
19
+ const GIT_PATTERN = / ^ ( [ a - z A - Z 0 - 9 _ . ~ - ] + \/ ( [ a - z A - Z 0 - 9 _ . ~ - ] + ) ( \. g i t ) ? ) ( \# ( .+ ) ) ? $ /
20
+ const MODULE_NAME_PATTERN = / ^ [ a - z ] [ a - z 0 - 9 _ . ~ - ] * $ / ;
21
+
22
+ const parse_pkg_installname = exports . parse_pkg_installname = function ( pkg_name , input_uri ) {
23
+ if ( ! input_uri || input_uri === '*' )
24
+ input_uri = pkg_name ;
25
+
26
+ input_uri = input_uri . replace ( / ^ n p m : / , '' ) ;
27
+ input_uri = input_uri . replace ( / ^ h t t p s ? : \/ \/ [ ^ \/ ] + \/ / , '' ) ;
28
+
29
+ const registry_part = input_uri . split ( '@' ) ;
30
+ if ( registry_part . length == 2 && registry_part [ 0 ] !== '' )
31
+ return {
32
+ type : 'registry' ,
33
+ package_name : registry_part [ 0 ] ,
34
+ registry_pkg_path : registry_part [ 0 ] ,
35
+ registry_semver : registry_part [ 1 ] ,
36
+ from_http : false ,
37
+ git_origin : '' ,
38
+ git_basename : null ,
39
+ git_path : null ,
40
+ git_reference : null ,
41
+ }
42
+
43
+ if ( registry_part . length == 2 && registry_part [ 0 ] === '' )
44
+ return {
45
+ type : 'registry' ,
46
+ package_name : input_uri ,
47
+ registry_pkg_path : input_uri ,
48
+ registry_semver : "*" ,
49
+ from_http : false ,
50
+ git_origin : '' ,
51
+ git_basename : null ,
52
+ git_path : null ,
53
+ git_reference : null ,
54
+ }
55
+
56
+ if ( registry_part . length == 3 ) {
57
+ if ( registry_part [ 0 ] !== '' )
58
+ throw new Error ( `[parse_pkg_installname] invalid input_uri: ${ input_uri } ` ) ;
59
+
60
+ const pkg_name = '@' + registry_part [ 1 ] ;
61
+ return {
62
+ type : 'registry' ,
63
+ package_name : pkg_name ,
64
+ registry_pkg_path : pkg_name ,
65
+ registry_semver : registry_part [ 2 ] ,
66
+ from_http : false ,
67
+ git_origin : '' ,
68
+ git_basename : null ,
69
+ git_path : null ,
70
+ git_reference : null ,
71
+ }
49
72
}
50
73
51
- Is . github = scope_str && / [ a - z A - Z ] / . test ( scope_str [ 0 ] )
52
- Is . registry = ! scope_str || scope_str [ 0 ] === '@'
53
-
54
- let github_org_str = ''
55
- if ( Is . github ) github_org_str = scope_str
56
-
57
- const [ name1 , registry_semver = null ] = ( pkg || '' ) . split ( '@' )
58
-
59
- const [ name0 , git_reference = null ] = ( name1 || '' ) . split ( '#' )
60
-
61
- const installation_name = Is . github ? null : ( scope_str ? `${ scope_str } /${ name0 } ` : name0 )
74
+ if ( registry_part . length > 1 )
75
+ throw new Error ( `[parse_pkg_installname] invalid input_uri: ${ input_uri } ` ) ;
76
+
77
+ const git_part = input_uri . match ( GIT_PATTERN ) ;
78
+ if ( git_part )
79
+ return {
80
+ type : 'git' ,
81
+ package_name : null ,
82
+ registry_pkg_path : null ,
83
+ registry_semver : null ,
84
+ from_http : false ,
85
+ git_origin : helpers_string . ensure_suffx ( CST . DEFAULT_GIT_ORIGIN ) ,
86
+ git_basename : git_part [ 2 ] ,
87
+ git_path : git_part [ 1 ] ,
88
+ git_reference : git_part [ 5 ] || 'master' ,
89
+ }
90
+
91
+ const git_part1 = pkg_name . match ( GIT_PATTERN ) ;
92
+ if ( git_part1 )
93
+ return {
94
+ type : 'git' ,
95
+ package_name : null ,
96
+ registry_pkg_path : null ,
97
+ registry_semver : null ,
98
+ from_http : false ,
99
+ git_origin : helpers_string . ensure_suffx ( CST . DEFAULT_GIT_ORIGIN ) ,
100
+ git_basename : git_part1 [ 2 ] ,
101
+ git_path : git_part1 [ 1 ] ,
102
+ git_reference : input_uri || git_part1 [ 5 ] || 'master' ,
103
+ }
104
+
105
+ if ( MODULE_NAME_PATTERN . test ( input_uri ) )
106
+ return {
107
+ type : 'registry' ,
108
+ package_name : input_uri ,
109
+ registry_pkg_path : input_uri ,
110
+ registry_semver : "*" ,
111
+ from_http : false ,
112
+ git_origin : '' ,
113
+ git_basename : null ,
114
+ git_path : null ,
115
+ git_reference : null ,
116
+ } ;
62
117
63
118
return {
64
- type : Is . github ? 'git' : 'registry' ,
65
- package_name : installation_name ,
66
- registry_pkg_path : ! Is . registry ? null : installation_name ,
67
- registry_semver : registry_semver ,
68
- from_http : Is . http ,
69
- git_origin : Is . github ? ( null || helpers_string. ensure_suffx ( CST . DEFAULT_GIT_ORIGIN ) ) : origin ,
70
- git_basename : ! Is . github ? null : name0 ,
71
- git_path : ! Is . github ? null : ` ${ github_org_str } / ${ name0 } ` ,
72
- git_reference : ! Is . github ? null : ( git_reference || 'master' ) ,
73
- }
119
+ type : 'registry' ,
120
+ package_name : pkg_name ,
121
+ registry_pkg_path : pkg_name ,
122
+ registry_semver : input_uri ,
123
+ from_http : false ,
124
+ git_origin : '' ,
125
+ git_basename : null ,
126
+ git_path : null ,
127
+ git_reference : null ,
128
+ } ;
74
129
}
75
130
76
131
/**
0 commit comments