|
4 | 4 |
|
5 | 5 | const path = require('path');
|
6 | 6 | const childProcess = require('child_process');
|
| 7 | +const assert = require('assert'); |
| 8 | +const fs = require('fs'); |
| 9 | + |
| 10 | +const glob = require('glob'); |
| 11 | +const semver = require('semver'); |
7 | 12 |
|
8 | 13 | const { dependencies } = require('./package.json');
|
9 | 14 |
|
10 | 15 | const tsVersions = Object.keys(dependencies)
|
11 | 16 | .filter((pkg) => pkg.startsWith('typescript-'))
|
12 | 17 | .sort((a, b) => b.localeCompare(a));
|
13 | 18 |
|
| 19 | +// To allow omitting certain code from older versions of TypeScript, we have a |
| 20 | +// "magic" comment syntax. We precede a block of code with: |
| 21 | +// |
| 22 | +// /*! SEMVER_RANGE !*/ |
| 23 | +// |
| 24 | +// replacing SEMVER_RANGE with a semver range spec, such as '>=3.2'; we |
| 25 | +// terminate the block of code with: |
| 26 | +// |
| 27 | +// /*!!*/ |
| 28 | +// |
| 29 | +// We will only include the code between these comments if the TypeScript |
| 30 | +// version being tested satisfies the semver range that was specified. NOTE: We |
| 31 | +// currently do not allow nesting of these blocks. |
| 32 | +const templates = glob.sync('./*.ts.template').map((filename) => { |
| 33 | + const content = fs.readFileSync(filename, 'utf8'); |
| 34 | + const targetFilename = filename.replace(/\.template$/, ''); |
| 35 | + assert.notEqual(filename, targetFilename); |
| 36 | + const writeFileSync = (version) => { |
| 37 | + // Captures our magic comment syntax: `/*!(CAPTURE1)!*/(CAPTURE 2)/*!!*/` |
| 38 | + const regex = /\/\*!([^!]+)!\*\/([\s\S]*?)\/\*!!\*\//g; |
| 39 | + const finalContent = content.replace(regex, (_, versionRange, payload) => { |
| 40 | + if (semver.satisfies(version, versionRange)) { |
| 41 | + return payload; |
| 42 | + } |
| 43 | + return ''; |
| 44 | + }); |
| 45 | + fs.writeFileSync(targetFilename, finalContent); |
| 46 | + }; |
| 47 | + return { writeFileSync }; |
| 48 | +}); |
| 49 | + |
14 | 50 | for (const version of tsVersions) {
|
15 | 51 | console.log(`Testing on ${version} ...`);
|
16 | 52 |
|
| 53 | + for (const template of templates) { |
| 54 | + template.writeFileSync(version); |
| 55 | + } |
| 56 | + |
17 | 57 | const tscPath = path.join(__dirname, 'node_modules', version, 'bin/tsc');
|
18 | 58 | childProcess.execSync(tscPath, { stdio: 'inherit' });
|
19 | 59 | }
|
0 commit comments