@@ -19,55 +19,65 @@ function main(): void {
19
19
20
20
// Acquire the version from the package.json file and modify it appropriately.
21
21
const packageJsonFilePath = ts . normalizePath ( sys . args [ 0 ] ) ;
22
- const packageJsonContents = sys . readFile ( packageJsonFilePath ) ;
23
- const packageJsonValue : PackageJson = JSON . parse ( packageJsonContents ) ;
22
+ const packageJsonValue : PackageJson = JSON . parse ( sys . readFile ( packageJsonFilePath ) ) ;
24
23
25
- const nightlyVersion = getNightlyVersionString ( packageJsonValue . version ) ;
26
-
27
- // Modify the package.json structure
28
- packageJsonValue . version = nightlyVersion ;
24
+ const { majorMinor, patch } = parsePackageJsonVersion ( packageJsonValue . version ) ;
25
+ const nightlyPatch = getNightlyPatch ( patch ) ;
29
26
30
27
// Acquire and modify the source file that exposes the version string.
31
28
const tsFilePath = ts . normalizePath ( sys . args [ 1 ] ) ;
32
- const tsFileContents = sys . readFile ( tsFilePath ) ;
33
- const versionAssignmentRegExp = / e x p o r t \s + c o n s t \s + v e r s i o n \s + = \s + " .* " ; / ;
34
- const modifiedTsFileContents = tsFileContents . replace ( versionAssignmentRegExp , `export const version = "${ nightlyVersion } ";` ) ;
29
+ const tsFileContents = ts . sys . readFile ( tsFilePath ) ;
30
+ const modifiedTsFileContents = updateTsFile ( tsFilePath , tsFileContents , majorMinor , patch , nightlyPatch ) ;
35
31
36
32
// Ensure we are actually changing something - the user probably wants to know that the update failed.
37
33
if ( tsFileContents === modifiedTsFileContents ) {
38
34
let err = `\n '${ tsFilePath } ' was not updated while configuring for a nightly publish.\n ` ;
39
-
40
- if ( tsFileContents . match ( versionAssignmentRegExp ) ) {
41
- err += `Ensure that you have not already run this script; otherwise, erase your changes using 'git checkout -- "${ tsFilePath } "'.` ;
42
- }
43
- else {
44
- err += `The file seems to no longer have a string matching '${ versionAssignmentRegExp } '.` ;
45
- }
46
-
35
+ err += `Ensure that you have not already run this script; otherwise, erase your changes using 'git checkout -- "${ tsFilePath } "'.` ;
47
36
throw err + "\n" ;
48
37
}
49
38
50
39
// Finally write the changes to disk.
40
+ // Modify the package.json structure
41
+ packageJsonValue . version = `${ majorMinor } .${ nightlyPatch } ` ;
51
42
sys . writeFile ( packageJsonFilePath , JSON . stringify ( packageJsonValue , /*replacer:*/ undefined , /*space:*/ 4 ) )
52
43
sys . writeFile ( tsFilePath , modifiedTsFileContents ) ;
53
44
}
54
45
55
- function getNightlyVersionString ( versionString : string ) : string {
56
- // If the version string already contains "-nightly",
57
- // then get the base string and update based on that.
58
- const dashNightlyPos = versionString . indexOf ( "-dev" ) ;
59
- if ( dashNightlyPos >= 0 ) {
60
- versionString = versionString . slice ( 0 , dashNightlyPos ) ;
46
+ function updateTsFile ( tsFilePath : string , tsFileContents : string , majorMinor : string , patch : string , nightlyPatch : string ) : string {
47
+ const majorMinorRgx = / e x p o r t c o n s t v e r s i o n M a j o r M i n o r = " ( \d + \. \d + ) " / ;
48
+ const majorMinorMatch = majorMinorRgx . exec ( tsFileContents ) ;
49
+ ts . Debug . assert ( majorMinorMatch !== null , "" , ( ) => `The file seems to no longer have a string matching '${ majorMinorRgx } '.` ) ;
50
+ const parsedMajorMinor = majorMinorMatch [ 1 ] ;
51
+ ts . Debug . assert ( parsedMajorMinor === majorMinor , "versionMajorMinor does not match." , ( ) => `${ tsFilePath } : '${ parsedMajorMinor } '; package.json: '${ majorMinor } '` ) ;
52
+
53
+ const versionRgx = / e x p o r t c o n s t v e r s i o n = ` \$ \{ v e r s i o n M a j o r M i n o r \} \. ( \d ) ` ; / ;
54
+ const patchMatch = versionRgx . exec ( tsFileContents ) ;
55
+ ts . Debug . assert ( patchMatch !== null , "The file seems to no longer have a string matching" , ( ) => versionRgx . toString ( ) ) ;
56
+ const parsedPatch = patchMatch [ 1 ] ;
57
+ if ( parsedPatch !== patch ) {
58
+ throw new Error ( `patch does not match. ${ tsFilePath } : '${ parsedPatch } ; package.json: '${ patch } '` ) ;
61
59
}
62
60
61
+ return tsFileContents . replace ( versionRgx , `export const version = \`\${versionMajorMinor}.${ nightlyPatch } \`;` ) ;
62
+ }
63
+
64
+ function parsePackageJsonVersion ( versionString : string ) : { majorMinor : string , patch : string } {
65
+ const versionRgx = / ( \d + \. \d + ) \. ( \d + ) ( $ | \- ) / ;
66
+ const match = versionString . match ( versionRgx ) ;
67
+ ts . Debug . assert ( match !== null , "package.json 'version' should match" , ( ) => versionRgx . toString ( ) ) ;
68
+ return { majorMinor : match [ 1 ] , patch : match [ 2 ] } ;
69
+ }
70
+
71
+ /** e.g. 0-dev.20170707 */
72
+ function getNightlyPatch ( plainPatch : string ) : string {
63
73
// We're going to append a representation of the current time at the end of the current version.
64
74
// String.prototype.toISOString() returns a 24-character string formatted as 'YYYY-MM-DDTHH:mm:ss.sssZ',
65
75
// but we'd prefer to just remove separators and limit ourselves to YYYYMMDD.
66
76
// UTC time will always be implicit here.
67
77
const now = new Date ( ) ;
68
78
const timeStr = now . toISOString ( ) . replace ( / : | T | \. | - / g, "" ) . slice ( 0 , 8 ) ;
69
79
70
- return `${ versionString } -dev.${ timeStr } ` ;
80
+ return `${ plainPatch } -dev.${ timeStr } ` ;
71
81
}
72
82
73
83
main ( ) ;
0 commit comments