forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ts
88 lines (85 loc) · 3.12 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as cpy from 'cpy';
import {describe, it} from 'mocha';
import * as path from 'path';
import {globP, spawnP, tmpDirP} from '../scripts/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
*/
describe('install', () => {
it('should be able to pack and install', async () => {
// 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.');
});
});