1
+ /// <reference path="../src/compiler/sys.ts" />
2
+
3
+ /**
4
+ * A minimal description for a parsed package.json object.
5
+ */
6
+ interface PackageJson {
7
+ name : string ;
8
+ version : string ;
9
+ keywords : string [ ] ;
10
+ }
11
+
12
+ function main ( ) : void {
13
+ const sys = ts . sys ;
14
+ if ( sys . args . length < 2 ) {
15
+ sys . write ( "Usage:" + sys . newLine )
16
+ sys . write ( "\tnode configureNightly.js <package.json location> <file containing version>" + sys . newLine ) ;
17
+ return ;
18
+ }
19
+
20
+ // Acquire the version from the package.json file and modify it appropriately.
21
+ const packageJsonFilePath = ts . normalizePath ( sys . args [ 0 ] ) ;
22
+ const packageJsonContents = sys . readFile ( packageJsonFilePath ) ;
23
+ const packageJsonValue : PackageJson = JSON . parse ( packageJsonContents ) ;
24
+
25
+ const nightlyVersion = getNightlyVersionString ( packageJsonValue . version ) ;
26
+
27
+ // Modify the package.json structure
28
+ packageJsonValue . version = nightlyVersion ;
29
+
30
+ if ( packageJsonValue . name !== "typescript-nightly" ) {
31
+ packageJsonValue . name = "typescript-nightly" ;
32
+ packageJsonValue . keywords . push ( "nightly" , "alpha" , "beta" , "prerelease" ) ;
33
+ }
34
+
35
+ // Acquire and modify the source file that exposes the version string.
36
+ const tsFilePath = ts . normalizePath ( sys . args [ 1 ] ) ;
37
+ const tsFileContents = sys . readFile ( tsFilePath ) ;
38
+ const versionAssignmentRegExp = / e x p o r t \s + c o n s t \s + v e r s i o n \s + = \s + " .* " ; / ;
39
+ const modifiedTsFileContents = tsFileContents . replace ( versionAssignmentRegExp , `export const version = "${ nightlyVersion } ";` ) ;
40
+
41
+ // Ensure we are actually changing something - the user probably wants to know that the update failed.
42
+ if ( tsFileContents === modifiedTsFileContents ) {
43
+ throw `File '${ tsFilePath } ' did not contain pattern ${ versionAssignmentRegExp } ` ;
44
+ }
45
+
46
+ // Finally write the changes to disk.
47
+ sys . writeFile ( packageJsonFilePath , JSON . stringify ( packageJsonValue , /*replacer:*/ undefined , /*space:*/ 4 ) )
48
+ sys . writeFile ( tsFilePath , modifiedTsFileContents ) ;
49
+ }
50
+
51
+ function getNightlyVersionString ( versionString : string ) : string {
52
+ // If the version string already contains "-nightly",
53
+ // then get the base string and update based on that.
54
+ const dashNightlyPos = versionString . indexOf ( "-nightly" ) ;
55
+ if ( dashNightlyPos >= 0 ) {
56
+ versionString = versionString . slice ( 0 , dashNightlyPos ) ;
57
+ }
58
+
59
+ // We're going to append a representation of the current time at the end of the current version.
60
+ // String.prototype.toISOString() returns a 24-character string formatted as 'YYYY-MM-DDTHH:mm:ss.sssZ',
61
+ // but we'd prefer to just use hyphens as separators instead of 'T', ':', and '.'.
62
+ // The trailing 'Z' in this string can be removed; UTC time will always be implicit here.
63
+ const now = new Date ( ) ;
64
+ const timeStr = now . toISOString ( ) . slice ( 0 , - 1 ) . replace ( / : | T | \. / g, "-" ) ;
65
+
66
+ return `${ versionString } -nightly-${ timeStr } ` ;
67
+ }
68
+
69
+ main ( ) ;
0 commit comments