File tree Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
const execa = require ( 'execa' )
3
3
const npa = require ( 'npm-package-arg' )
4
+ const semver = require ( 'semver' )
4
5
const validateNpmPackageName = require ( 'validate-npm-package-name' )
5
6
6
7
// Remove npm env vars from the commands, this
@@ -53,6 +54,17 @@ function normalizePackageName (name, opts = {}) {
53
54
throw new Error ( `Invalid package type specifier (${ pkg . type } - ${ pkg . raw } )` )
54
55
}
55
56
57
+ if (
58
+ typeof pkg . rawSpec !== 'string' || (
59
+ pkg . rawSpec . length > 0 && (
60
+ semver . coerce ( pkg . rawSpec , { loose : true } ) == null && (
61
+ pkg . rawSpec === '*' || pkg . rawSpec . startsWith ( '<=' ) || pkg . rawSpec . startsWith ( '>=' )
62
+ ) === false )
63
+ )
64
+ ) {
65
+ throw new Error ( `Invalid package semver specifier (${ pkg . rawSpec } - ${ pkg . raw } )` )
66
+ }
67
+
56
68
switch ( pkg . type ) {
57
69
// Directory checkes for package.json and
58
70
// hosted means it looks like a remote repo or tarball
Original file line number Diff line number Diff line change 38
38
"inquirer" : " ^5.2.0" ,
39
39
"npm-package-arg" : " ^7.0.0" ,
40
40
"safe-parse-list" : " ^0.1.1" ,
41
+ "semver" : " ^7.3.5" ,
41
42
"validate-npm-package-name" : " ^3.0.0"
42
43
},
43
44
"devDependencies" : {
Original file line number Diff line number Diff line change
1
+ const assert = require ( 'assert' )
2
+ const { suite, test } = require ( 'mocha' )
3
+
4
+ const npm = require ( '../lib/npm' )
5
+
6
+ suite ( 'npm' , ( ) => {
7
+ test ( 'accept empty semver' , ( ) => {
8
+ assert . doesNotThrow ( ( ) => npm . normalizePackageName ( '@pkgjs/create' ) )
9
+ } )
10
+ test ( 'accept valid semver' , ( ) => {
11
+ assert . doesNotThrow ( ( ) => npm . normalizePackageName ( '@pkgjs/create@1.0.0' ) )
12
+ assert . doesNotThrow ( ( ) => npm . normalizePackageName ( '@pkgjs/create@<1' ) )
13
+ } )
14
+ test ( 'accept valid semver (exceptions)' , ( ) => {
15
+ assert . doesNotThrow ( ( ) => npm . normalizePackageName ( '@pkgjs/create@<=2' ) )
16
+ assert . doesNotThrow ( ( ) => npm . normalizePackageName ( '@pkgjs/create@>=2' ) )
17
+ assert . doesNotThrow ( ( ) => npm . normalizePackageName ( '@pkgjs/create@*' ) )
18
+ } )
19
+ test ( 'reject invalid semver' , ( ) => {
20
+ assert . throws ( ( ) => npm . normalizePackageName ( '@pkgjs/create@a.b.c' ) )
21
+ } )
22
+ } )
You can’t perform that action at this time.
0 commit comments