forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-install.ts
55 lines (53 loc) · 2.27 KB
/
check-install.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as cpy from 'cpy';
import * as path from 'path';
import { globP, spawnP, tmpDirP } from './utils';
/**
* Get the major version number of the current Node process.
*/
function getNodeMajorVersion() {
return Number(process.version.slice(1).split('.')[0]);
}
/**
* This function checks that the following two (sequential) operations succeed:
* 1. In a temporary directory, installs from the `npm pack` of this directory
* 2. Compiles a top-level file in that directory that imports this module
*/
export async function checkInstall() {
// Determine a temporary directory in which this package should be installed.
const installDir = await tmpDirP();
console.log(installDir);
// Create a tgz with package contents using npm pack
await spawnP('npm', ['pack']);
// Try to figure out the name of the tgz file that was just craeted
// This assumes that you don't already have a TGZ file
// in your current working directory.
const tgz = await globP(`${process.cwd()}/*.tgz`);
if (tgz.length !== 1) {
throw new Error(`Expected 1 tgz file in current directory, but found ${tgz.length}`);
}
// Initialize a new npm package.json in the temp directory.
await spawnP('npm', ['init', '-y'], {
cwd: installDir
});
// Install the tgz file as a package, along with necessities.
// @types/node version should match the current process version, but clamped
// at >=9 (because of http2 types) and <11 (because Node 11 doesn't yet have
// type definitions).
const nodeTypesVersion = Math.min(Math.max(getNodeMajorVersion(), 9), 10);
await spawnP('npm', ['install', 'typescript', `@types/node@${nodeTypesVersion}`, tgz[0]], {
cwd: installDir
});
// Create an entry point for the package created in the temp directory
// use-module.ts is a fixture that imports the Trace Agent
await cpy('./test/fixtures/use-module.ts', installDir, { rename: 'index.ts' });
// Compile it
await spawnP(`node_modules${path.sep}.bin${path.sep}tsc`, ['index.ts', '--lib', 'es2015'], {
cwd: installDir
});
console.log('`npm install` + `tsc` test was successful.');
// Evaluate require('..').start() in Node.
await spawnP(`node`, ['-e', `"require('@google-cloud/trace-agent').start()"`], {
cwd: installDir
});
console.log('require + start test was successful.');
}