|
| 1 | +const {relative, sep, join} = require('path'); |
| 2 | +const {readdirSync, readFileSync, existsSync} = require('fs'); |
| 3 | +const {set, ln, rm, mkdir} = require('shelljs'); |
| 4 | +const {fork} = require('child_process'); |
| 5 | +const runfiles = require(process.env.BAZEL_NODE_RUNFILES_HELPER); |
| 6 | + |
| 7 | +// Exit if any command fails. |
| 8 | +set('-e'); |
| 9 | + |
| 10 | +// List of NPM packages that have been built for the current test target. |
| 11 | +const npmPackages = getNpmPackagesFromRunfiles(); |
| 12 | +// Path to the node modules of the workspace. |
| 13 | +const nodeModulesDir = runfiles.resolve('npm/node_modules'); |
| 14 | +// Path to the generated file that imports all entry-points. |
| 15 | +const testFilePath = require.resolve('./import-all-entry-points.ts'); |
| 16 | + |
| 17 | +/** |
| 18 | + * Runs the TypeScript compatibility test with the specified tsc binary. The |
| 19 | + * compatibility test, links the built release packages into `node_modules` and |
| 20 | + * compiles a test file using the specified tsc binary which imports all entry-points. |
| 21 | + */ |
| 22 | +exports.runTypeScriptCompatibilityTest = async (tscBinPath) => { |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + const angularDir = join(nodeModulesDir, '@angular/'); |
| 25 | + |
| 26 | + // Create the `node_modules/@angular` directory in case it's not present. |
| 27 | + mkdir('-p', angularDir); |
| 28 | + |
| 29 | + // Symlink npm packages into `node_modules/` so that the project can |
| 30 | + // be compiled without path mappings (simulating a real project). |
| 31 | + for (const {name, pkgPath} of npmPackages) { |
| 32 | + console.info(`Linking "@angular/${name}" into node modules..`); |
| 33 | + ln('-s', pkgPath, join(angularDir, name)); |
| 34 | + } |
| 35 | + |
| 36 | + const tscArgs = [ |
| 37 | + '--strict', |
| 38 | + '--lib', 'es2015,dom', |
| 39 | + // Ensures that `node_modules` can be resolved. By default, in sandbox environments the |
| 40 | + // node modules cannot be resolved because they are wrapped in the `npm/node_modules` folder |
| 41 | + '--baseUrl', nodeModulesDir, |
| 42 | + testFilePath |
| 43 | + ]; |
| 44 | + // Run `tsc` to compile the project. The stdout/stderr output is inherited, so that |
| 45 | + // warnings and errors are printed to the console. |
| 46 | + const tscProcess = fork(tscBinPath, tscArgs, {stdio: 'inherit'}); |
| 47 | + |
| 48 | + tscProcess.on('exit', (exitCode) => { |
| 49 | + // Remove symlinks to keep a clean repository state. |
| 50 | + for (const {name} of npmPackages) { |
| 51 | + console.info(`Removing link for "@angular/${name}"..`); |
| 52 | + rm(join(angularDir, name)); |
| 53 | + } |
| 54 | + exitCode === 0 ? resolve() : reject(); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * Gets all built Angular NPM package artifacts by querying the Bazel runfiles. |
| 61 | + * In case there is a runfiles manifest (e.g. on Windows), the packages are resolved |
| 62 | + * through the manifest because the runfiles are not symlinked and cannot be searched |
| 63 | + * within the real filesystem. TODO: Remove if Bazel on Windows uses runfile symlinking. |
| 64 | + */ |
| 65 | +function getNpmPackagesFromRunfiles() { |
| 66 | + // Path to the Bazel runfiles manifest if present. This file is present if runfiles are |
| 67 | + // not symlinked into the runfiles directory. |
| 68 | + const runfilesManifestPath = process.env.RUNFILES_MANIFEST_FILE; |
| 69 | + const workspacePath = 'angular_material/src'; |
| 70 | + if (!runfilesManifestPath) { |
| 71 | + const packageRunfilesDir = join(process.env.RUNFILES, workspacePath); |
| 72 | + return readdirSync(packageRunfilesDir) |
| 73 | + .map(name => ({name, pkgPath: join(packageRunfilesDir, name, 'npm_package/')})) |
| 74 | + .filter(({pkgPath}) => existsSync(pkgPath)); |
| 75 | + } |
| 76 | + const workspaceManifestPathRegex = new RegExp(`^${workspacePath}/[\\w-]+/npm_package$`); |
| 77 | + return readFileSync(runfilesManifestPath, 'utf8') |
| 78 | + .split('\n') |
| 79 | + .map(mapping => mapping.split(' ')) |
| 80 | + .filter(([runfilePath]) => runfilePath.match(workspaceManifestPathRegex)) |
| 81 | + .map(([runfilePath, realPath]) => ({ |
| 82 | + name: relative(workspacePath, runfilePath).split(sep)[0], |
| 83 | + pkgPath: realPath, |
| 84 | + })); |
| 85 | +} |
0 commit comments