@percy/env 1.0.0-beta.7 → 1.0.0-beta.73

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/dist/dotenv.js CHANGED
@@ -3,25 +3,75 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.config = config;
6
+ exports.load = load;
7
7
 
8
- const dotenv = require('dotenv'); // mimic dotenv-rails file hierarchy
9
- // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
8
+ var _fs = _interopRequireDefault(require("fs"));
10
9
 
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
12
- function config() {
12
+ // Heavily inspired by dotenv-rails
13
+ // https://github.com/bkeepers/dotenv
14
+ // matches each valid line of a dotenv file
15
+ const LINE_REG = new RegExp([// key with optional export
16
+ '^\\s*(?:export\\s+)?(?<key>[\\w.]+)', // separator
17
+ '(?:\\s*=\\s*?|:\\s+?)(?:', // single quoted value or
18
+ '\\s*(?<squote>\')(?<sval>(?:\\\\\'|[^\'])*)\'|', // double quoted value or
19
+ '\\s*(?<dquote>")(?<dval>(?:\\\\"|[^"])*)"|', // unquoted value
20
+ '(?<uval>[^#\\r\\n]+))?', // optional comment
21
+ '\\s*(?:#.*)?$'].join(''), 'gm'); // interpolate variable substitutions
22
+
23
+ const INTERPOLATE_REG = /(.?)(\${?([a-zA-Z0-9_]+)?}?)/g; // expand newlines
24
+
25
+ const EXPAND_CRLF_REG = /\\(?:(r)|n)/g; // unescape characters
26
+
27
+ const UNESC_CHAR_REG = /\\([^$])/g;
28
+
29
+ function load() {
30
+ // don't load dotenv files when disabled
31
+ if (process.env.PERCY_DISABLE_DOTENV) return;
13
32
  let {
14
- NODE_ENV: env,
15
- PERCY_DISABLE_DOTENV: disable
16
- } = process.env; // don't load dotenv files when disabled
33
+ NODE_ENV
34
+ } = process.env; // dotenv filepaths ordered by priority
17
35
 
18
- if (disable) return;
19
- let paths = [env && `.env.${env}.local`, // .env.local is not loaded in test environments
20
- env === 'test' ? null : '.env.local', env && `.env.${env}`, '.env'].filter(Boolean);
36
+ let paths = [NODE_ENV && `.env.${NODE_ENV}.local`, NODE_ENV !== 'test' && '.env.local', NODE_ENV && `.env.${NODE_ENV}`, '.env']; // load each dotenv file synchronously
21
37
 
22
38
  for (let path of paths) {
23
- dotenv.config({
24
- path
25
- });
39
+ try {
40
+ let src = _fs.default.readFileSync(path, {
41
+ encoding: 'utf-8'
42
+ }); // iterate over each matching line
43
+
44
+
45
+ for (let {
46
+ groups: match
47
+ } of src.matchAll(LINE_REG)) {
48
+ var _ref, _ref2, _match$sval;
49
+
50
+ let value = (_ref = (_ref2 = (_match$sval = match.sval) !== null && _match$sval !== void 0 ? _match$sval : match.dval) !== null && _ref2 !== void 0 ? _ref2 : match.uval) !== null && _ref !== void 0 ? _ref : ''; // if double quoted, expand newlines
51
+
52
+ if (match.dquote) {
53
+ value = value.replace(EXPAND_CRLF_REG, (_, r) => r ? '\r' : '\n');
54
+ } // unescape characters
55
+
56
+
57
+ value = value.replace(UNESC_CHAR_REG, '$1'); // if not single quoted, interpolate substitutions
58
+
59
+ if (!match.squote) {
60
+ value = value.replace(INTERPOLATE_REG, (_, pre, ref, key) => {
61
+ var _process$env$key;
62
+
63
+ if (pre === '\\') return ref; // escaped reference
64
+
65
+ return pre + ((_process$env$key = process.env[key]) !== null && _process$env$key !== void 0 ? _process$env$key : '');
66
+ });
67
+ } // set process.env if not already
68
+
69
+
70
+ if (!Object.prototype.hasOwnProperty.call(process.env, match.key)) {
71
+ process.env[match.key] = value;
72
+ }
73
+ }
74
+ } catch (e) {// silent error
75
+ }
26
76
  }
27
77
  }
@@ -3,11 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.PercyEnv = void 0;
7
7
 
8
- var _git = require("./git");
8
+ var _utils = require("./utils");
9
9
 
10
- class PercyEnvironment {
10
+ class PercyEnv {
11
11
  constructor(vars = process.env) {
12
12
  this.vars = vars;
13
13
  } // used for getter switch statements
@@ -55,11 +55,9 @@ class PercyEnvironment {
55
55
 
56
56
 
57
57
  get info() {
58
- var _this$vars$PERCY_GITH;
59
-
60
58
  switch (this.ci) {
61
59
  case 'github':
62
- return `github/${(_this$vars$PERCY_GITH = this.vars.PERCY_GITHUB_ACTION) !== null && _this$vars$PERCY_GITH !== void 0 ? _this$vars$PERCY_GITH : 'unknown'}`;
60
+ return this.vars.PERCY_GITHUB_ACTION ? `github/${this.vars.PERCY_GITHUB_ACTION}` : this.ci;
63
61
 
64
62
  case 'gitlab':
65
63
  return `gitlab/${this.vars.CI_SERVER_VERSION}`;
@@ -79,6 +77,8 @@ class PercyEnvironment {
79
77
  }
80
78
 
81
79
  let commit = (() => {
80
+ var _github$pull_request;
81
+
82
82
  switch (this.ci) {
83
83
  case 'travis':
84
84
  return this.vars.TRAVIS_COMMIT;
@@ -87,7 +87,7 @@ class PercyEnvironment {
87
87
  return this.vars.ghprbActualCommit || this.vars.GIT_COMMIT;
88
88
 
89
89
  case 'jenkins':
90
- return (0, _git.getJenkinsSha)() || this.vars.GIT_COMMIT;
90
+ return (0, _utils.getJenkinsSha)() || this.vars.GIT_COMMIT;
91
91
 
92
92
  case 'circle':
93
93
  return this.vars.CIRCLE_SHA1;
@@ -124,7 +124,7 @@ class PercyEnvironment {
124
124
  return this.vars.BITBUCKET_COMMIT;
125
125
 
126
126
  case 'github':
127
- return this.vars.GITHUB_SHA;
127
+ return ((_github$pull_request = (0, _utils.github)(this.vars).pull_request) === null || _github$pull_request === void 0 ? void 0 : _github$pull_request.head.sha) || this.vars.GITHUB_SHA;
128
128
  }
129
129
  })();
130
130
 
@@ -138,7 +138,7 @@ class PercyEnvironment {
138
138
  }
139
139
 
140
140
  let branch = (() => {
141
- var _this$vars$GITHUB_REF;
141
+ var _github$pull_request2;
142
142
 
143
143
  switch (this.ci) {
144
144
  case 'travis':
@@ -184,14 +184,14 @@ class PercyEnvironment {
184
184
  return this.vars.BITBUCKET_BRANCH;
185
185
 
186
186
  case 'github':
187
- return ((_this$vars$GITHUB_REF = this.vars.GITHUB_REF) === null || _this$vars$GITHUB_REF === void 0 ? void 0 : _this$vars$GITHUB_REF.match(/^refs\//)) ? this.vars.GITHUB_REF.replace(/^refs\/\w+?\//, '') : this.vars.GITHUB_REF;
187
+ return ((_github$pull_request2 = (0, _utils.github)(this.vars).pull_request) === null || _github$pull_request2 === void 0 ? void 0 : _github$pull_request2.head.ref) || this.vars.GITHUB_REF;
188
188
 
189
189
  case 'netlify':
190
190
  return this.vars.HEAD;
191
191
  }
192
192
  })();
193
193
 
194
- return branch || null;
194
+ return (branch === null || branch === void 0 ? void 0 : branch.replace(/^refs\/\w+?\//, '')) || null;
195
195
  } // pull request number
196
196
 
197
197
 
@@ -201,7 +201,7 @@ class PercyEnvironment {
201
201
  }
202
202
 
203
203
  let pr = (() => {
204
- var _this$vars$CI_PULL_RE, _this$vars$PULL_REQUE;
204
+ var _this$vars$CI_PULL_RE, _this$vars$PULL_REQUE, _github$pull_request3;
205
205
 
206
206
  switch (this.ci) {
207
207
  case 'travis':
@@ -225,11 +225,14 @@ class PercyEnvironment {
225
225
  case 'buildkite':
226
226
  return this.vars.BUILDKITE_PULL_REQUEST !== 'false' && this.vars.BUILDKITE_PULL_REQUEST;
227
227
 
228
+ case 'heroku':
229
+ return this.vars.HEROKU_PR_NUMBER;
230
+
228
231
  case 'gitlab':
229
232
  return this.vars.CI_MERGE_REQUEST_IID;
230
233
 
231
234
  case 'azure':
232
- return this.vars.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER;
235
+ return this.vars.SYSTEM_PULLREQUEST_PULLREQUESTID || this.vars.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER;
233
236
 
234
237
  case 'appveyor':
235
238
  return this.vars.APPVEYOR_PULL_REQUEST_NUMBER;
@@ -242,15 +245,21 @@ class PercyEnvironment {
242
245
 
243
246
  case 'netlify':
244
247
  return this.vars.PULL_REQUEST !== 'false' && this.vars.REVIEW_ID;
248
+
249
+ case 'github':
250
+ return (_github$pull_request3 = (0, _utils.github)(this.vars).pull_request) === null || _github$pull_request3 === void 0 ? void 0 : _github$pull_request3.number;
245
251
  }
246
252
  })();
247
253
 
248
254
  return pr || null;
249
- } // parallel nonce & total
255
+ } // parallel total & nonce
250
256
 
251
257
 
252
258
  get parallel() {
253
- let nonce = (() => {
259
+ let total = parseInt(this.vars.PERCY_PARALLEL_TOTAL, 10);
260
+ if (!Number.isInteger(total)) total = null; // no nonce if no total
261
+
262
+ let nonce = total && (() => {
254
263
  var _this$vars$BUILD_TAG;
255
264
 
256
265
  if (this.vars.PERCY_PARALLEL_NONCE) {
@@ -299,49 +308,21 @@ class PercyEnvironment {
299
308
 
300
309
  case 'bitbucket':
301
310
  return this.vars.BITBUCKET_BUILD_NUMBER;
302
- }
303
- })();
304
-
305
- let total = (() => {
306
- if (this.vars.PERCY_PARALLEL_TOTAL) {
307
- return this.vars.PERCY_PARALLEL_TOTAL;
308
- }
309
-
310
- switch (this.ci) {
311
- case 'travis':
312
- return this.vars.CI_NODE_TOTAL;
313
-
314
- case 'circle':
315
- return this.vars.CIRCLE_NODE_TOTAL;
316
-
317
- case 'codeship':
318
- return this.vars.CI_NODE_TOTAL;
319
-
320
- case 'semaphore':
321
- return this.vars.SEMAPHORE_THREAD_COUNT;
322
311
 
323
- case 'buildkite':
324
- return this.vars.BUILDKITE_PARALLEL_JOB_COUNT;
325
-
326
- case 'heroku':
327
- return this.vars.CI_NODE_TOTAL;
328
-
329
- case 'azure':
330
- // SYSTEM_TOTALJOBSINPHASE is set for parallel builds and non-parallel matrix builds, so
331
- // check build strategy is parallel by ensuring SYSTEM_PARALLELEXECUTIONTYPE == MultiMachine
332
- return this.vars.SYSTEM_PARALLELEXECUTIONTYPE === 'MultiMachine' && this.vars.SYSTEM_TOTALJOBSINPHASE;
312
+ case 'github':
313
+ return this.vars.GITHUB_RUN_ID;
333
314
  }
334
315
  })();
335
316
 
336
317
  return {
337
- nonce: nonce || null,
338
- total: total ? parseInt(total) : null
318
+ total: total || null,
319
+ nonce: nonce || null
339
320
  };
340
321
  } // git information for the current commit
341
322
 
342
323
 
343
324
  get git() {
344
- return (0, _git.getCommitData)(this.commit, this.branch, this.vars);
325
+ return (0, _utils.getCommitData)(this.commit, this.branch, this.vars);
345
326
  } // manually set build commit and branch targets
346
327
 
347
328
 
@@ -366,8 +347,8 @@ class PercyEnvironment {
366
347
  } // cache getters on initial call so subsequent calls are not re-computed
367
348
 
368
349
 
369
- exports.default = PercyEnvironment;
370
- Object.defineProperties(PercyEnvironment.prototype, Object.entries(Object.getOwnPropertyDescriptors(PercyEnvironment.prototype)).reduce((proto, [key, {
350
+ exports.PercyEnv = PercyEnv;
351
+ Object.defineProperties(PercyEnv.prototype, Object.entries(Object.getOwnPropertyDescriptors(PercyEnv.prototype)).reduce((proto, [key, {
371
352
  get,
372
353
  ...descr
373
354
  }]) => !get ? proto : Object.assign(proto, {
@@ -381,4 +362,6 @@ Object.defineProperties(PercyEnvironment.prototype, Object.entries(Object.getOwn
381
362
  }
382
363
 
383
364
  })
384
- }), {}));
365
+ }), {}));
366
+ var _default = PercyEnv;
367
+ exports.default = _default;
package/dist/index.js CHANGED
@@ -3,6 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "PercyEnv", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _environment.PercyEnv;
10
+ }
11
+ });
6
12
  Object.defineProperty(exports, "default", {
7
13
  enumerable: true,
8
14
  get: function () {
@@ -10,8 +16,10 @@ Object.defineProperty(exports, "default", {
10
16
  }
11
17
  });
12
18
 
13
- var _environment = _interopRequireDefault(require("./environment"));
19
+ var _environment = _interopRequireWildcard(require("./environment"));
20
+
21
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
22
 
15
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16
24
 
17
- require('./dotenv').config();
25
+ require('./dotenv').load();
@@ -3,28 +3,27 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.git = git;
7
6
  exports.getCommitData = getCommitData;
8
7
  exports.getJenkinsSha = getJenkinsSha;
8
+ exports.git = git;
9
+ exports.github = github;
9
10
 
10
11
  var _child_process = require("child_process");
11
12
 
13
+ var _fs = require("fs");
14
+
12
15
  const GIT_COMMIT_FORMAT = ['COMMIT_SHA:%H', 'AUTHOR_NAME:%an', 'AUTHOR_EMAIL:%ae', 'COMMITTER_NAME:%cn', 'COMMITTER_EMAIL:%ce', 'COMMITTED_DATE:%ai', // order is important, this must come last because the regex is a multiline match.
13
16
  'COMMIT_MESSAGE:%B'].join('%n'); // git show format uses %n for newlines.
14
17
 
15
18
  function git(args) {
16
19
  try {
17
- let result = (0, _child_process.execSync)(`git ${args}`, {
18
- stdio: 'ignore'
20
+ return (0, _child_process.execSync)(`git ${args}`, {
21
+ stdio: ['ignore', 'pipe', 'ignore'],
22
+ encoding: 'utf-8'
19
23
  });
20
-
21
- if (result && result.status === 0) {
22
- return result.stdout.trim();
23
- }
24
- } catch (e) {// do something?
24
+ } catch (e) {
25
+ return '';
25
26
  }
26
-
27
- return '';
28
27
  } // get raw commit data
29
28
 
30
29
 
@@ -53,4 +52,17 @@ function getCommitData(sha, branch, vars = {}) {
53
52
  function getJenkinsSha() {
54
53
  let data = getCommitData();
55
54
  return data.authorName === 'Jenkins' && data.authorEmail === 'nobody@nowhere' && data.message.match(/^Merge commit [^\s]+ into HEAD$/) && git('rev-parse HEAD^');
55
+ } // github actions are triggered by webhook events which are saved to the filesystem
56
+
57
+
58
+ function github({
59
+ GITHUB_EVENT_PATH
60
+ }) {
61
+ if (!github.payload && GITHUB_EVENT_PATH && (0, _fs.existsSync)(GITHUB_EVENT_PATH)) {
62
+ try {
63
+ github.payload = JSON.parse((0, _fs.readFileSync)(GITHUB_EVENT_PATH, 'utf8'));
64
+ } catch (e) {}
65
+ }
66
+
67
+ return github.payload || (github.payload = {});
56
68
  }
package/package.json CHANGED
@@ -1,28 +1,30 @@
1
1
  {
2
2
  "name": "@percy/env",
3
- "version": "1.0.0-beta.7",
3
+ "version": "1.0.0-beta.73",
4
4
  "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/percy/cli",
8
+ "directory": "packages/env"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
5
13
  "main": "dist/index.js",
6
14
  "files": [
7
15
  "dist"
8
16
  ],
17
+ "engines": {
18
+ "node": ">=12"
19
+ },
9
20
  "scripts": {
10
- "build": "babel --root-mode upward src --out-dir dist",
21
+ "build": "node ../../scripts/build",
11
22
  "lint": "eslint --ignore-path ../../.gitignore .",
12
- "test": "cross-env NODE_ENV=test mocha",
13
- "test:coverage": "nyc yarn test"
14
- },
15
- "publishConfig": {
16
- "access": "public"
17
- },
18
- "mocha": {
19
- "require": "../../scripts/babel-register"
23
+ "test": "node ../../scripts/test",
24
+ "test:coverage": "yarn test --coverage"
20
25
  },
21
26
  "devDependencies": {
22
27
  "mock-require": "^3.0.3"
23
28
  },
24
- "dependencies": {
25
- "dotenv": "^8.2.0"
26
- },
27
- "gitHead": "5be796ec8f17958e93ada0b634899b945c9b0d60"
29
+ "gitHead": "aa8160e02bea3e04ab1d3605762f89fbe79605d4"
28
30
  }