Skip to content

Commit f37d906

Browse files
author
Andy
authored
Fix configure-nightly script to match new contents of core.ts (microsoft#17014)
* Fix configureNightly script to match new contents of core.ts * Use ts.Debug.assert * Use a regexp for parsePackageJsonVersion
1 parent d996946 commit f37d906

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

scripts/configureNightly.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /export\s+const\s+version\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 = /export const versionMajorMinor = "(\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 = /export const version = `\$\{versionMajorMinor\}\.(\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

7383
main();

src/compiler/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/// <reference path="performance.ts" />
33

44
namespace ts {
5+
// WARNING: The script `configureNightly.ts` uses a regexp to parse out these values.
6+
// If changing the text in this section, be sure to test `configureNightly` too.
57
export const versionMajorMinor = "2.5";
68
/** The version of the TypeScript compiler release */
79
export const version = `${versionMajorMinor}.0`;

0 commit comments

Comments
 (0)