@@ -19,55 +19,65 @@ function main(): void {
1919
2020 // Acquire the version from the package.json file and modify it appropriately.
2121 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 ) ) ;
2423
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 ) ;
2926
3027 // Acquire and modify the source file that exposes the version string.
3128 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 ) ;
3531
3632 // Ensure we are actually changing something - the user probably wants to know that the update failed.
3733 if ( tsFileContents === modifiedTsFileContents ) {
3834 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 } "'.` ;
4736 throw err + "\n" ;
4837 }
4938
5039 // Finally write the changes to disk.
40+ // Modify the package.json structure
41+ packageJsonValue . version = `${ majorMinor } .${ nightlyPatch } ` ;
5142 sys . writeFile ( packageJsonFilePath , JSON . stringify ( packageJsonValue , /*replacer:*/ undefined , /*space:*/ 4 ) )
5243 sys . writeFile ( tsFilePath , modifiedTsFileContents ) ;
5344}
5445
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 } '` ) ;
6159 }
6260
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 {
6373 // We're going to append a representation of the current time at the end of the current version.
6474 // String.prototype.toISOString() returns a 24-character string formatted as 'YYYY-MM-DDTHH:mm:ss.sssZ',
6575 // but we'd prefer to just remove separators and limit ourselves to YYYYMMDD.
6676 // UTC time will always be implicit here.
6777 const now = new Date ( ) ;
6878 const timeStr = now . toISOString ( ) . replace ( / : | T | \. | - / g, "" ) . slice ( 0 , 8 ) ;
6979
70- return `${ versionString } -dev.${ timeStr } ` ;
80+ return `${ plainPatch } -dev.${ timeStr } ` ;
7181}
7282
7383main ( ) ;
0 commit comments