Skip to content

Commit f65bb0e

Browse files
authored
feat(installMethod): add installMethod to inline config (#82)
Allows better triage of issues that may arise within this library fix #81
1 parent 0859acb commit f65bb0e

File tree

7 files changed

+21
-14
lines changed

7 files changed

+21
-14
lines changed

src/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Can create iopipe handler file 1`] = `
4-
"const iopipe = require('iopipe')({\\"token\\":\\"TEST_TOKEN\\"});
4+
"const iopipe = require('iopipe')({\\"token\\":\\"TEST_TOKEN\\",\\"installMethod\\":\\"[email protected]\\"});
55
66
let handler, handlerError;
77
// The following is an automatically generated require statement by the plugin,

src/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs-extra';
55
import debugLib from 'debug';
66
import cosmiconfig from 'cosmiconfig';
77

8+
import thisPkg from '../package';
89
import { getVisitor, track } from './util/track';
910
import hrMillis from './util/hrMillis';
1011
import handlerCode from './handlerCode';
@@ -351,7 +352,7 @@ class ServerlessIOpipePlugin {
351352
throw new Error(err);
352353
}
353354
}
354-
getConfigFromCosmi() {
355+
getConfig() {
355356
const { token } = this.getOptions();
356357
const { config: cosmi = {} } =
357358
cosmiconfig('iopipe', {
@@ -364,10 +365,13 @@ class ServerlessIOpipePlugin {
364365
.join('')
365366
.defaultTo('')
366367
.value();
367-
const inlineConfigObject = _.pickBy(_.assign({}, cosmi, { token }));
368-
const inlineConfig = _.isEmpty(inlineConfigObject)
369-
? ''
370-
: JSON.stringify(inlineConfigObject);
368+
const inlineConfigObject = _.pickBy(
369+
_.assign({}, cosmi, {
370+
token,
371+
installMethod: `${thisPkg.name}@${thisPkg.version}`
372+
})
373+
);
374+
const inlineConfig = JSON.stringify(inlineConfigObject);
371375
return {
372376
requireLines,
373377
inlineConfig
@@ -376,7 +380,7 @@ class ServerlessIOpipePlugin {
376380
createFiles() {
377381
const debug = createDebugger('createFiles');
378382
debug('Creating file');
379-
const { inlineConfig, requireLines } = this.getConfigFromCosmi();
383+
const { inlineConfig, requireLines } = this.getConfig();
380384
const { handlerDir } = this.getOptions();
381385
const iopipeInclude = `${requireLines}const iopipe = require('${this.getInstalledPackageName()}')(${inlineConfig});`;
382386
this.funcs.forEach((func, index) => {

src/index.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,17 @@ test('Can create iopipe handler file', () => {
237237
expect(file).toMatchSnapshot();
238238
});
239239

240-
test('Agent instantiation is blank if no iopipeToken in custom section of serverless.yml', () => {
240+
test('Agent instantiation only includes installMethod if no iopipeToken in custom section of serverless.yml', () => {
241241
Plugin.getOptions({ token: '' });
242242
Plugin.createFiles();
243243
const file = readFileSync(
244244
path.join(prefix, 'iopipe_handlers/simple-0-iopipe.js'),
245245
'utf8'
246246
);
247247
expect(file).toBeDefined();
248-
expect(file.split('\n')[0]).toEqual("const iopipe = require('iopipe')();");
248+
expect(file.split('\n')[0]).toEqual(
249+
'const iopipe = require(\'iopipe\')({"installMethod":"[email protected]"});'
250+
);
249251
});
250252

251253
test('Cleans up', () => {

testProjects/cosmi/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`Generated files requires plugin and includes plugin inline 1`] = `
44
"require('@iopipe/event-info');
55
require('@iopipe/trace');
6-
const iopipe = require('@iopipe/core')({\\"plugins\\":[\\"@iopipe/event-info\\",\\"@iopipe/trace\\"],\\"token\\":\\"test-token\\"});
6+
const iopipe = require('@iopipe/core')({\\"plugins\\":[\\"@iopipe/event-info\\",\\"@iopipe/trace\\"],\\"token\\":\\"test-token\\",\\"installMethod\\":\\"[email protected]\\"});
77
88
let handler, handlerError;
99
// The following is an automatically generated require statement by the plugin,

testProjects/default/__snapshots__/index.test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Generated files require plugin, include plugin inline, and export original handler 1`] = `
4-
"const iopipe = require('iopipe')({\\"token\\":\\"test-token\\"});
4+
"const iopipe = require('iopipe')({\\"token\\":\\"test-token\\",\\"installMethod\\":\\"[email protected]\\"});
55
66
let handler, handlerError;
77
// The following is an automatically generated require statement by the plugin,
@@ -29,7 +29,7 @@ exports['simple'] = function attemptSimple(event, context, callback) {
2929
`;
3030

3131
exports[`Generated files require plugin, include plugin inline, and export original handler 2`] = `
32-
"const iopipe = require('iopipe')({\\"token\\":\\"test-token\\"});
32+
"const iopipe = require('iopipe')({\\"token\\":\\"test-token\\",\\"installMethod\\":\\"[email protected]\\"});
3333
3434
let handler, handlerError;
3535
// The following is an automatically generated require statement by the plugin,
@@ -57,7 +57,7 @@ exports['nameMismatch'] = function attemptNameMismatch(event, context, callback)
5757
`;
5858

5959
exports[`Generated files require plugin, include plugin inline, and export original handler 3`] = `
60-
"const iopipe = require('iopipe')({\\"token\\":\\"test-token\\"});
60+
"const iopipe = require('iopipe')({\\"token\\":\\"test-token\\",\\"installMethod\\":\\"[email protected]\\"});
6161
6262
let handler, handlerError;
6363
// The following is an automatically generated require statement by the plugin,

testProjects/offline/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ functions:
1919
custom:
2020
serverless-offline:
2121
port: 4982
22+
host: 127.0.0.1
2223
iopipeHandlerDir: iopipe_handlers

testProjects/offline/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const gotLib = require('got');
22
const spawn = require('cross-spawn');
33

44
const got = str => gotLib(str, { json: true });
5-
const BASE = `http://localhost:4982`;
5+
const BASE = `http://127.0.0.1:4982`;
66

77
async function run() {
88
// make sure that the error state works too

0 commit comments

Comments
 (0)