varsnap 1.6.8 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,36 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ 1.8.0 (2022-01-14)
5
+ ------------------
6
+
7
+ - Add `gitbranchenv` to make getting git branch names easier
8
+ - Add documentation of git branch usage
9
+ - Cleanup
10
+ - Update depencencies
11
+
12
+
13
+ 1.7.0 (2022-01-03)
14
+ ------------------
15
+
16
+ - Send git branch when generating trial groups
17
+ - Update dependencies
18
+ - Refactors
19
+
20
+
21
+ 1.6.10 (2022-01-01)
22
+ -------------------
23
+
24
+ - Update dependencies
25
+ - Simplify testing
26
+
27
+
28
+ 1.6.9 (2021-09-11)
29
+ ------------------
30
+
31
+ - Update dependencies
32
+
33
+
4
34
  1.6.8 (2021-07-25)
5
35
  ------------------
6
36
 
package/README.md CHANGED
@@ -17,6 +17,7 @@ The varsnap decorator needs to be configured with a `config` object with these v
17
17
 
18
18
  - `varsnap` - Should be either `true` or `false`. Varsnap will be disabled if the variable is anything other than `true`.
19
19
  - `env` - If set to `development`, the client will receive events from production. If set to `production`, the client will emit events.
20
+ - `branch` - Used for labeling test runs by (git) branch. Test runs on the `master` branch will update your varsnap badge
20
21
  - `producerToken` - Only clients with this token may emit production snapshots. Copied from https://www.varsnap.com/user/
21
22
  - `consumerToken` - Only clients with this token may emit development snapshots. Copied from https://www.varsnap.com/user/
22
23
 
@@ -38,6 +39,7 @@ var varsnap = window.varsnap;
38
39
  varsnap.updateConfig({
39
40
  varsnap: true,
40
41
  env: 'production',
42
+ branch: 'master',
41
43
  producerToken: 'producer-abcd'
42
44
  });
43
45
 
@@ -112,6 +114,17 @@ const options = {
112
114
  minify(code, options);
113
115
  ```
114
116
 
117
+ Git Branch
118
+ ----------
119
+
120
+ Providing a git branch to the client allows Varsnap to categorize test runs.
121
+ Tests on the master branch will also be reflected in your Varsnap badge image.
122
+
123
+ For builds in environments that cannot access git branch names, such as builds
124
+ in headless browsers, `varsnap/gitbranchenv.ts` will read git branch names and
125
+ set them into a `GIT_BRANCH` environment variable. This can then be copied
126
+ into the varsnap config when testing.
127
+
115
128
  Client Development
116
129
  ------------------
117
130
 
@@ -122,6 +135,5 @@ Client Development
122
135
  3. Update package.json version and run `npm install`
123
136
  4. Update `.uglify.json` and `.uglify.min.json`
124
137
  5. Run `npm run package`
125
- 6. Git commit and tag
126
- 7. `npm publish`
138
+ 6. Git commit, tag, and push
127
139
  8. Upload `dist/*` to https://github.com/albertyw/varsnap.js/releases
@@ -1,16 +1,17 @@
1
1
  /// <reference types="node" />
2
2
  import querystring = require('querystring');
3
- export declare const version = "1.6.8";
3
+ export declare const version = "1.8.0";
4
4
  export declare const clientName: string;
5
5
  export interface Config {
6
6
  varsnap: boolean;
7
7
  env: string;
8
+ branch: string;
8
9
  producerToken?: string;
9
10
  consumerToken?: string;
10
11
  logger?: Logger;
11
12
  }
12
13
  declare type ConfigKeys = keyof Config;
13
- declare type rawJSONObject = ConsumeSnapResponse | ProduceTrialResponse | TrialGroupResponse | querystring.ParsedUrlQueryInput;
14
+ declare type rawJSONObject = ConsumeSnapResponse | ProduceTrialResponse | TrialGroupRequest | TrialGroupResponse | querystring.ParsedUrlQueryInput;
14
15
  export interface Logger {
15
16
  debug(s: string): void;
16
17
  log(s: string): void;
@@ -29,6 +30,8 @@ export declare class Util {
29
30
  static getProducers(): Producer[];
30
31
  static getConsumers(): Consumer[];
31
32
  static limitString(x: TargetFunctionInput[] | TargetFunctionOutput): string;
33
+ static isNode(): boolean;
34
+ static getBranch(config: Config): Promise<string>;
32
35
  }
33
36
  export declare class Producer {
34
37
  targetFunc: TargetFunction;
@@ -84,6 +87,11 @@ export declare namespace varsnap {
84
87
  var version: string;
85
88
  var runTests: typeof import("./core").runTests;
86
89
  }
90
+ interface TrialGroupRequest {
91
+ branch: string;
92
+ consumer_token: string;
93
+ client: string;
94
+ }
87
95
  interface TrialGroupResponse {
88
96
  status: string;
89
97
  project_id: string;
@@ -3,15 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runTests = exports.varsnap = exports.Consumer = exports.Producer = exports.Util = exports.clientName = exports.version = void 0;
4
4
  const axios_1 = require("axios");
5
5
  const querystring = require("querystring");
6
+ const child_process_1 = require("child_process");
6
7
  const produceURL = 'https://www.varsnap.com/api/snap/produce/';
7
8
  const consumeURL = 'https://www.varsnap.com/api/snap/consume/';
8
9
  const produceTrialURL = 'https://www.varsnap.com/api/trial/produce/';
9
10
  const trialGroupURL = 'https://www.varsnap.com/api/trial_group/';
10
- exports.version = '1.6.8';
11
+ exports.version = '1.8.0';
11
12
  exports.clientName = 'javascript.' + exports.version;
12
13
  const defaultConfig = {
13
14
  varsnap: true,
14
15
  env: 'development',
16
+ branch: '',
15
17
  producerToken: '',
16
18
  consumerToken: '',
17
19
  logger: console, // eslint-disable-line no-console
@@ -37,7 +39,7 @@ class Util {
37
39
  }
38
40
  config[configKey] = configValue;
39
41
  }
40
- if (configKey === 'env' || configKey === 'producerToken' || configKey === 'consumerToken') {
42
+ if (configKey === 'env' || configKey === 'producerToken' || configKey === 'consumerToken' || configKey === 'branch') {
41
43
  config[configKey] = configValue;
42
44
  }
43
45
  if (configKey === 'logger') {
@@ -70,10 +72,10 @@ class Util {
70
72
  data: querystring.stringify(requestData),
71
73
  responseType: 'json',
72
74
  };
73
- if (typeof process === 'object' && typeof window === 'undefined') {
75
+ if (Util.isNode() && options.headers) {
74
76
  options.headers['User-Agent'] = 'varsnap.js';
75
77
  }
76
- const ajaxPromise = axios_1.default(options).then((response) => {
78
+ const ajaxPromise = (0, axios_1.default)(options).then((response) => {
77
79
  return response.data;
78
80
  });
79
81
  return ajaxPromise;
@@ -114,6 +116,36 @@ class Util {
114
116
  }
115
117
  return limitedString.substring(0, 30 - ellipsis.length) + ellipsis;
116
118
  }
119
+ static isNode() {
120
+ if (typeof process !== 'object') {
121
+ return false;
122
+ }
123
+ if (typeof window !== 'undefined') {
124
+ return false;
125
+ }
126
+ return true;
127
+ }
128
+ static getBranch(config) {
129
+ const configBranch = Util.getConfig(config, 'branch');
130
+ if (configBranch) {
131
+ const configPromise = new Promise((resolve) => {
132
+ return resolve(configBranch);
133
+ });
134
+ return configPromise;
135
+ }
136
+ if (!Util.isNode()) {
137
+ const defaultPromise = new Promise((resolve) => {
138
+ return resolve('');
139
+ });
140
+ return defaultPromise;
141
+ }
142
+ const command = 'git rev-parse --abbrev-ref HEAD';
143
+ return new Promise((resolve) => {
144
+ (0, child_process_1.exec)(command, (err, stdout) => {
145
+ return resolve(stdout.trim());
146
+ });
147
+ });
148
+ }
117
149
  }
118
150
  exports.Util = Util;
119
151
  class Producer {
@@ -346,11 +378,19 @@ varsnap.reset = function reset() {
346
378
  };
347
379
  varsnap.version = exports.version;
348
380
  function getTrialGroup(consumerConfig) {
349
- const data = {
350
- 'consumer_token': Util.getConfig(consumerConfig, 'consumerToken'),
351
- 'client': exports.clientName,
352
- };
353
- const ajax = Util.ajax(trialGroupURL, data);
381
+ const getBranch = Util.getBranch(consumerConfig);
382
+ const getRequestConfig = getBranch.then((branch) => {
383
+ const data = {
384
+ 'branch': branch,
385
+ 'consumer_token': Util.getConfig(consumerConfig, 'consumerToken'),
386
+ 'client': exports.clientName,
387
+ };
388
+ return data;
389
+ });
390
+ const ajax = getRequestConfig.then((data) => {
391
+ const raw = data;
392
+ return Util.ajax(trialGroupURL, raw);
393
+ });
354
394
  const trialGroup = ajax.then((rawResponse) => {
355
395
  const response = rawResponse;
356
396
  return response;
@@ -0,0 +1 @@
1
+ export declare function setGitBranchEnv(): void;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setGitBranchEnv = void 0;
4
+ const core_1 = require("./core");
5
+ /* Read git branch information from git and save it into an environment variable
6
+ * Useful when combined with envify (or loose-envify) when packaging browser apps
7
+ */
8
+ function setGitBranchEnv() {
9
+ const config = {
10
+ varsnap: true,
11
+ env: 'none',
12
+ branch: '',
13
+ };
14
+ core_1.Util.getBranch(config)
15
+ .then((branch) => {
16
+ process.env.GIT_BRANCH = branch;
17
+ });
18
+ }
19
+ exports.setGitBranchEnv = setGitBranchEnv;
20
+ setGitBranchEnv();