|
1 | 1 | import * as path from 'path'; |
2 | 2 | import { globP, ncpP, spawnP, tmpDirP } from './utils'; |
3 | 3 |
|
| 4 | +/** |
| 5 | + * Get the major version number of the current Node process. |
| 6 | + */ |
| 7 | +function getNodeMajorVersion() { |
| 8 | + return Number(process.version.slice(1).split('.')[0]); |
| 9 | +} |
| 10 | + |
4 | 11 | /** |
5 | 12 | * This function checks that the following two (sequential) operations succeed: |
6 | 13 | * 1. In a temporary directory, installs from the `npm pack` of this directory |
7 | 14 | * 2. Compiles a top-level file in that directory that imports this module |
8 | 15 | */ |
9 | 16 | export async function checkInstall() { |
10 | | - // This script assumes that you don't already have a TGZ file |
11 | | - // in your current working directory. |
| 17 | + // Determine a temporary directory in which this package should be installed. |
12 | 18 | const installDir = await tmpDirP(); |
13 | 19 | console.log(installDir); |
| 20 | + // Create a tgz with package contents using npm pack |
14 | 21 | await spawnP('npm', ['pack']); |
| 22 | + // Try to figure out the name of the tgz file that was just craeted |
| 23 | + // This assumes that you don't already have a TGZ file |
| 24 | + // in your current working directory. |
15 | 25 | const tgz = await globP(`${process.cwd()}/*.tgz`); |
16 | 26 | if (tgz.length !== 1) { |
17 | 27 | throw new Error(`Expected 1 tgz file in current directory, but found ${tgz.length}`); |
18 | 28 | } |
| 29 | + // Initialize a new npm package.json in the temp directory. |
19 | 30 | await spawnP('npm', ['init', '-y'], { |
20 | 31 | cwd: installDir |
21 | 32 | }); |
22 | | - await spawnP('npm', ['install', 'typescript', '@types/node', tgz[0]], { |
| 33 | + // Install the tgz file as a package, along with necessities. |
| 34 | + // @types/node version should match the current process version, but clamped |
| 35 | + // at >=9 (because of http2 types). |
| 36 | + const nodeTypesVersion = Math.max(getNodeMajorVersion(), 9); |
| 37 | + await spawnP('npm', ['install', 'typescript', `@types/node@${nodeTypesVersion}`, tgz[0]], { |
23 | 38 | cwd: installDir |
24 | 39 | }); |
| 40 | + // Create an entry point for the package created in the temp directory |
25 | 41 | // use-module.ts is a fixture that imports the Trace Agent |
26 | 42 | await ncpP('./test/fixtures/use-module.ts', `${installDir}/index.ts`); |
| 43 | + // Compile it |
27 | 44 | await spawnP(`node_modules${path.sep}.bin${path.sep}tsc`, ['index.ts'], { |
28 | 45 | cwd: installDir |
29 | 46 | }); |
|
0 commit comments