@naturalcycles/nodejs-lib 13.33.0 → 13.34.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.
@@ -7,10 +7,10 @@ const git2_1 = require("./git2");
7
7
  function generateBuildInfo(opt = {}) {
8
8
  const now = js_lib_1.localTime.orNow(opt.overrideTimestamp);
9
9
  const ts = now.unix;
10
- const rev = git2_1.git2.gitCurrentCommitSha();
11
- const branchName = git2_1.git2.gitCurrentBranchName();
12
- const repoName = git2_1.git2.gitCurrentRepoName();
13
- const tsCommit = git2_1.git2.gitCurrentCommitTimestamp();
10
+ const rev = git2_1.git2.getCurrentCommitSha();
11
+ const branchName = git2_1.git2.getCurrentBranchName();
12
+ const repoName = git2_1.git2.getCurrentRepoName();
13
+ const tsCommit = git2_1.git2.getCurrentCommitTimestamp();
14
14
  const ver = [now.toStringCompact(), repoName, branchName, rev].join('_');
15
15
  let { APP_ENV: env } = process.env;
16
16
  if (!env) {
@@ -140,6 +140,11 @@ export interface SpawnOptions {
140
140
  name?: string;
141
141
  cwd?: string;
142
142
  env?: AnyObject;
143
+ /**
144
+ * Defaults to false for security reasons.
145
+ * Set to true to pass `process.env` to the spawned process.
146
+ */
147
+ passProcessEnv?: boolean;
143
148
  }
144
149
  export interface ExecOptions {
145
150
  /**
@@ -163,5 +168,10 @@ export interface ExecOptions {
163
168
  cwd?: string;
164
169
  timeout?: NumberOfMilliseconds;
165
170
  env?: AnyObject;
171
+ /**
172
+ * Defaults to false for security reasons.
173
+ * Set to true to pass `process.env` to the spawned process.
174
+ */
175
+ passProcessEnv?: boolean;
166
176
  }
167
177
  export {};
@@ -57,8 +57,10 @@ class Exec2 {
57
57
  const p = node_child_process_1.default.spawn(cmd, opt.args || [], {
58
58
  shell,
59
59
  cwd,
60
- env,
61
- // ...process.env, // not passing by default for security reasons
60
+ env: {
61
+ ...env,
62
+ ...(opt.passProcessEnv ? process.env : {}),
63
+ },
62
64
  });
63
65
  p.stdout.on('data', data => {
64
66
  if (collectOutputWhileRunning) {
@@ -115,8 +117,10 @@ class Exec2 {
115
117
  stdio: 'inherit',
116
118
  shell,
117
119
  cwd,
118
- env,
119
- // ...process.env, // not passing by default for security reasons
120
+ env: {
121
+ ...env,
122
+ ...(opt.passProcessEnv ? process.env : {}),
123
+ },
120
124
  });
121
125
  this.logFinish(cmd, opt, started);
122
126
  if (r.error) {
@@ -153,8 +157,10 @@ class Exec2 {
153
157
  // shell: undefined,
154
158
  cwd,
155
159
  timeout,
156
- env,
157
- // ...process.env, // not passing by default for security reasons
160
+ env: {
161
+ ...env,
162
+ ...(opt.passProcessEnv ? process.env : {}),
163
+ },
158
164
  })
159
165
  .trim();
160
166
  }
@@ -192,7 +198,13 @@ class Exec2 {
192
198
  logFinish(cmd, opt, started) {
193
199
  if (!opt.logFinish && !opt.log)
194
200
  return;
195
- console.log([(0, colors_1.white)(opt.name || cmd), (0, colors_1.dimGrey)('took ' + (0, js_lib_1._since)(started))].join(' '));
201
+ console.log([
202
+ (0, colors_1.white)(opt.name || cmd),
203
+ ...((!opt.name && opt.args) || []),
204
+ (0, colors_1.dimGrey)('took ' + (0, js_lib_1._since)(started)),
205
+ ]
206
+ .filter(Boolean)
207
+ .join(' '));
196
208
  }
197
209
  }
198
210
  exports.exec2 = new Exec2();
@@ -5,21 +5,21 @@ import type { UnixTimestampNumber } from '@naturalcycles/js-lib';
5
5
  declare class Git2 {
6
6
  getLastGitCommitMsg(): string;
7
7
  commitMessageToTitleMessage(msg: string): string;
8
- gitHasUncommittedChanges(): boolean;
8
+ hasUncommittedChanges(): boolean;
9
9
  /**
10
10
  * Returns true if there were changes
11
11
  */
12
- gitCommitAll(msg: string): boolean;
12
+ commitAll(msg: string): boolean;
13
13
  /**
14
14
  * @returns true if there are not pushed commits.
15
15
  */
16
- gitIsAhead(): boolean;
17
- gitPull(): void;
18
- gitPush(): void;
19
- gitCurrentCommitSha(full?: boolean): string;
20
- gitCurrentCommitTimestamp(): UnixTimestampNumber;
21
- gitCurrentBranchName(): string;
22
- gitCurrentRepoName(): string;
16
+ isAhead(): boolean;
17
+ pull(): void;
18
+ push(): void;
19
+ getCurrentCommitSha(full?: boolean): string;
20
+ getCurrentCommitTimestamp(): UnixTimestampNumber;
21
+ getCurrentBranchName(): string;
22
+ getCurrentRepoName(): string;
23
23
  }
24
24
  export declare const git2: Git2;
25
25
  export {};
package/dist/util/git2.js CHANGED
@@ -18,7 +18,7 @@ class Git2 {
18
18
  const [preTitle, title] = firstLine.split(': ');
19
19
  return title || preTitle;
20
20
  }
21
- gitHasUncommittedChanges() {
21
+ hasUncommittedChanges() {
22
22
  // git diff-index --quiet HEAD -- || echo "untracked"
23
23
  try {
24
24
  node_child_process_1.default.execSync('git diff-index --quiet HEAD --', {
@@ -33,7 +33,7 @@ class Git2 {
33
33
  /**
34
34
  * Returns true if there were changes
35
35
  */
36
- gitCommitAll(msg) {
36
+ commitAll(msg) {
37
37
  // git commit -a -m "style(lint-all): $GIT_MSG" || true
38
38
  const cmd = `git commit -a --no-verify -m "${msg}"`;
39
39
  // const cmd = `git`
@@ -52,14 +52,14 @@ class Git2 {
52
52
  /**
53
53
  * @returns true if there are not pushed commits.
54
54
  */
55
- gitIsAhead() {
55
+ isAhead() {
56
56
  // ahead=`git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
57
57
  const cmd = `git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`;
58
58
  const stdout = exec2_1.exec2.exec(cmd);
59
59
  // console.log(`gitIsAhead: ${stdout}`)
60
60
  return Number(stdout) > 0;
61
61
  }
62
- gitPull() {
62
+ pull() {
63
63
  const cmd = 'git pull';
64
64
  try {
65
65
  node_child_process_1.default.execSync(cmd, {
@@ -68,26 +68,26 @@ class Git2 {
68
68
  }
69
69
  catch { }
70
70
  }
71
- gitPush() {
71
+ push() {
72
72
  // git push --set-upstream origin $CIRCLE_BRANCH && echo "pushed, exiting" && exit 0
73
73
  let cmd = 'git push';
74
- const branchName = this.gitCurrentBranchName();
74
+ const branchName = this.getCurrentBranchName();
75
75
  if (branchName) {
76
76
  cmd += ` --set-upstream origin ${branchName}`;
77
77
  }
78
78
  exec2_1.exec2.spawn(cmd, { logStart: true });
79
79
  }
80
- gitCurrentCommitSha(full = false) {
80
+ getCurrentCommitSha(full = false) {
81
81
  const sha = exec2_1.exec2.exec('git rev-parse HEAD');
82
82
  return full ? sha : sha.slice(0, 7);
83
83
  }
84
- gitCurrentCommitTimestamp() {
84
+ getCurrentCommitTimestamp() {
85
85
  return Number(exec2_1.exec2.exec('git log -1 --format=%ct'));
86
86
  }
87
- gitCurrentBranchName() {
87
+ getCurrentBranchName() {
88
88
  return exec2_1.exec2.exec('git rev-parse --abbrev-ref HEAD');
89
89
  }
90
- gitCurrentRepoName() {
90
+ getCurrentRepoName() {
91
91
  const originUrl = exec2_1.exec2.exec('git config --get remote.origin.url');
92
92
  return node_path_1.default.basename(originUrl, '.git');
93
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.33.0",
3
+ "version": "13.34.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build",
@@ -19,10 +19,10 @@ export function generateBuildInfo(opt: GenerateBuildInfoOptions = {}): BuildInfo
19
19
  const now = localTime.orNow(opt.overrideTimestamp)
20
20
  const ts = now.unix
21
21
 
22
- const rev = git2.gitCurrentCommitSha()
23
- const branchName = git2.gitCurrentBranchName()
24
- const repoName = git2.gitCurrentRepoName()
25
- const tsCommit = git2.gitCurrentCommitTimestamp()
22
+ const rev = git2.getCurrentCommitSha()
23
+ const branchName = git2.getCurrentBranchName()
24
+ const repoName = git2.getCurrentRepoName()
25
+ const tsCommit = git2.getCurrentCommitTimestamp()
26
26
 
27
27
  const ver = [now.toStringCompact(), repoName, branchName, rev].join('_')
28
28
 
package/src/util/exec2.ts CHANGED
@@ -68,8 +68,10 @@ class Exec2 {
68
68
  const p = cp.spawn(cmd, opt.args || [], {
69
69
  shell,
70
70
  cwd,
71
- env,
72
- // ...process.env, // not passing by default for security reasons
71
+ env: {
72
+ ...env,
73
+ ...(opt.passProcessEnv ? process.env : {}),
74
+ },
73
75
  })
74
76
 
75
77
  p.stdout.on('data', data => {
@@ -130,8 +132,10 @@ class Exec2 {
130
132
  stdio: 'inherit',
131
133
  shell,
132
134
  cwd,
133
- env,
134
- // ...process.env, // not passing by default for security reasons
135
+ env: {
136
+ ...env,
137
+ ...(opt.passProcessEnv ? process.env : {}),
138
+ },
135
139
  })
136
140
 
137
141
  this.logFinish(cmd, opt, started)
@@ -172,8 +176,10 @@ class Exec2 {
172
176
  // shell: undefined,
173
177
  cwd,
174
178
  timeout,
175
- env,
176
- // ...process.env, // not passing by default for security reasons
179
+ env: {
180
+ ...env,
181
+ ...(opt.passProcessEnv ? process.env : {}),
182
+ },
177
183
  })
178
184
  .trim()
179
185
  } catch (err) {
@@ -218,7 +224,15 @@ class Exec2 {
218
224
  ): void {
219
225
  if (!opt.logFinish && !opt.log) return
220
226
 
221
- console.log([white(opt.name || cmd), dimGrey('took ' + _since(started))].join(' '))
227
+ console.log(
228
+ [
229
+ white(opt.name || cmd),
230
+ ...((!opt.name && (opt as SpawnOptions).args) || []),
231
+ dimGrey('took ' + _since(started)),
232
+ ]
233
+ .filter(Boolean)
234
+ .join(' '),
235
+ )
222
236
  }
223
237
  }
224
238
 
@@ -297,6 +311,11 @@ export interface SpawnOptions {
297
311
  cwd?: string
298
312
 
299
313
  env?: AnyObject
314
+ /**
315
+ * Defaults to false for security reasons.
316
+ * Set to true to pass `process.env` to the spawned process.
317
+ */
318
+ passProcessEnv?: boolean
300
319
  }
301
320
 
302
321
  export interface ExecOptions {
@@ -323,4 +342,9 @@ export interface ExecOptions {
323
342
  timeout?: NumberOfMilliseconds
324
343
 
325
344
  env?: AnyObject
345
+ /**
346
+ * Defaults to false for security reasons.
347
+ * Set to true to pass `process.env` to the spawned process.
348
+ */
349
+ passProcessEnv?: boolean
326
350
  }
package/src/util/git2.ts CHANGED
@@ -18,7 +18,7 @@ class Git2 {
18
18
  return title || preTitle!
19
19
  }
20
20
 
21
- gitHasUncommittedChanges(): boolean {
21
+ hasUncommittedChanges(): boolean {
22
22
  // git diff-index --quiet HEAD -- || echo "untracked"
23
23
  try {
24
24
  cp.execSync('git diff-index --quiet HEAD --', {
@@ -33,7 +33,7 @@ class Git2 {
33
33
  /**
34
34
  * Returns true if there were changes
35
35
  */
36
- gitCommitAll(msg: string): boolean {
36
+ commitAll(msg: string): boolean {
37
37
  // git commit -a -m "style(lint-all): $GIT_MSG" || true
38
38
  const cmd = `git commit -a --no-verify -m "${msg}"`
39
39
  // const cmd = `git`
@@ -53,7 +53,7 @@ class Git2 {
53
53
  /**
54
54
  * @returns true if there are not pushed commits.
55
55
  */
56
- gitIsAhead(): boolean {
56
+ isAhead(): boolean {
57
57
  // ahead=`git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
58
58
  const cmd = `git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
59
59
  const stdout = exec2.exec(cmd)
@@ -61,7 +61,7 @@ class Git2 {
61
61
  return Number(stdout) > 0
62
62
  }
63
63
 
64
- gitPull(): void {
64
+ pull(): void {
65
65
  const cmd = 'git pull'
66
66
  try {
67
67
  cp.execSync(cmd, {
@@ -70,11 +70,11 @@ class Git2 {
70
70
  } catch {}
71
71
  }
72
72
 
73
- gitPush(): void {
73
+ push(): void {
74
74
  // git push --set-upstream origin $CIRCLE_BRANCH && echo "pushed, exiting" && exit 0
75
75
  let cmd = 'git push'
76
76
 
77
- const branchName = this.gitCurrentBranchName()
77
+ const branchName = this.getCurrentBranchName()
78
78
 
79
79
  if (branchName) {
80
80
  cmd += ` --set-upstream origin ${branchName}`
@@ -83,20 +83,20 @@ class Git2 {
83
83
  exec2.spawn(cmd, { logStart: true })
84
84
  }
85
85
 
86
- gitCurrentCommitSha(full = false): string {
86
+ getCurrentCommitSha(full = false): string {
87
87
  const sha = exec2.exec('git rev-parse HEAD')
88
88
  return full ? sha : sha.slice(0, 7)
89
89
  }
90
90
 
91
- gitCurrentCommitTimestamp(): UnixTimestampNumber {
91
+ getCurrentCommitTimestamp(): UnixTimestampNumber {
92
92
  return Number(exec2.exec('git log -1 --format=%ct'))
93
93
  }
94
94
 
95
- gitCurrentBranchName(): string {
95
+ getCurrentBranchName(): string {
96
96
  return exec2.exec('git rev-parse --abbrev-ref HEAD')
97
97
  }
98
98
 
99
- gitCurrentRepoName(): string {
99
+ getCurrentRepoName(): string {
100
100
  const originUrl = exec2.exec('git config --get remote.origin.url')
101
101
  return path.basename(originUrl, '.git')
102
102
  }