@@ -18,6 +18,36 @@ import * as semver from 'semver';
18
18
import { PostUpdateSchema } from './schema' ;
19
19
20
20
21
+ /**
22
+ * Cleans up "short" version numbers so they become valid semver. For example;
23
+ * 1 => 1.0.0
24
+ * 1.2 => 1.2.0
25
+ * 1-beta => 1.0.0-beta
26
+ *
27
+ * Exported for testing only.
28
+ * @internal
29
+ */
30
+ export function _coerceVersionNumber ( version : string ) : string | null {
31
+ if ( ! version . match ( / ^ \d { 1 , 30 } \. \d { 1 , 30 } \. \d { 1 , 30 } / ) ) {
32
+ const match = version . match ( / ^ \d { 1 , 30 } ( \. \d { 1 , 30 } ) * / ) ;
33
+
34
+ if ( ! match ) {
35
+ return null ;
36
+ }
37
+
38
+ if ( ! match [ 1 ] ) {
39
+ version = version . substr ( 0 , match [ 0 ] . length ) + '.0.0' + version . substr ( match [ 0 ] . length ) ;
40
+ } else if ( ! match [ 2 ] ) {
41
+ version = version . substr ( 0 , match [ 0 ] . length ) + '.0' + version . substr ( match [ 0 ] . length ) ;
42
+ } else {
43
+ return null ;
44
+ }
45
+ }
46
+
47
+ return semver . valid ( version ) ;
48
+ }
49
+
50
+
21
51
export default function ( options : PostUpdateSchema ) : Rule {
22
52
return ( tree : Tree , context : SchematicContext ) => {
23
53
const schematicsToRun : { name : string ; version : string ; } [ ] = [ ] ;
@@ -28,16 +58,12 @@ export default function(options: PostUpdateSchema): Rule {
28
58
const schematic = collection . createSchematic ( name , true ) ;
29
59
30
60
const description : JsonObject = schematic . description as JsonObject ;
61
+ let version = description [ 'version' ] ;
31
62
32
- if ( typeof description [ 'version' ] == 'string' ) {
33
- let version = description [ 'version' ] as string ;
34
- if ( ! version . match ( / ^ \d { 1 , 30 } \. \d { 1 , 30 } \. \d { 1 , 30 } $ / ) ) {
35
- version += '.0' ;
36
- }
37
- if ( ! version . match ( / ^ \d { 1 , 30 } \. \d { 1 , 30 } \. \d { 1 , 30 } $ / ) ) {
38
- version += '.0' ;
39
- }
40
- if ( ! semver . valid ( version ) ) {
63
+ if ( typeof version == 'string' ) {
64
+ version = _coerceVersionNumber ( version ) ;
65
+
66
+ if ( ! version ) {
41
67
throw new SchematicsException (
42
68
`Invalid migration version: ${ JSON . stringify ( description [ 'version' ] ) } ` ,
43
69
) ;
0 commit comments