@toptal/davinci-cli-shared 2.2.2-alpha-chore-build-packages-via-bunchee-1d48b3aa.9 → 2.2.2

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,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 2.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1887](https://github.com/toptal/davinci/pull/1887) [`b3b691f4`](https://github.com/toptal/davinci/commit/b3b691f4bdfcd2c4d05133bb61c69c45bc3c06a6) Thanks [@augustobmoura](https://github.com/augustobmoura)! - ---
8
+
9
+ - guard agains NPE on empty metrics config file
10
+
3
11
  ## 2.2.1
4
12
 
5
13
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toptal/davinci-cli-shared",
3
- "version": "2.2.2-alpha-chore-build-packages-via-bunchee-1d48b3aa.9+1d48b3aa",
3
+ "version": "2.2.2",
4
4
  "description": "Shared CLI code and CLI engine for davinci",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -23,21 +23,20 @@
23
23
  "dependencies": {
24
24
  "analytics-node": "^6.2.0",
25
25
  "chalk": "^4.1.2",
26
- "commander": "^10.0.0",
27
26
  "execa": "^5.1.1",
28
27
  "find-yarn-workspace-root": "^2.0.0",
29
- "gradient-string": "^2.0.0",
30
28
  "inquirer": "^8.2.0",
31
29
  "isomorphic-git": "^1.21.0",
32
30
  "js-yaml": "^4.1.0",
33
31
  "lodash": "^4.17.21",
34
- "ora": "^5.4.1",
32
+ "proper-lockfile": "^4.1.2",
35
33
  "read-pkg-up": "7.0.1",
36
34
  "semver": "^7.3.7",
37
- "uuid": "^9.0.0"
35
+ "commander": "^10.0.0",
36
+ "gradient-string": "^2.0.0",
37
+ "ora": "^5.4.1"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@jest/globals": "^29.4.2"
41
- },
42
- "gitHead": "1d48b3aa9f567d1b4427048d1f0d694f1b9df468"
41
+ }
43
42
  }
@@ -1,63 +1,93 @@
1
- import os from 'os'
2
- import path from 'path'
3
- import fs from 'fs'
1
+ import os from 'node:os'
2
+ import path from 'node:path'
3
+ import fs from 'node:fs/promises'
4
+ import crypto from 'node:crypto'
4
5
  import yaml from 'js-yaml'
5
- import { v1 as uuid } from 'uuid'
6
+ import properLockfile from 'proper-lockfile'
7
+ import noop from 'lodash/noop.js'
6
8
 
7
- const homeDir = os.homedir()
8
- const configDir = path.join(homeDir, '.davinci-metrics')
9
- const configFile = path.join(configDir, 'auth.yml')
10
- const DEFAULT_FILE_CONTENT = 'allow_tracking: "yes"'
11
-
12
- const readConfig = () => {
13
- let configFileContent = DEFAULT_FILE_CONTENT
9
+ const CONFIG_DIR = path.join(os.homedir(), '.davinci-metrics')
10
+ const CONFIG_FILE_PATH = path.join(CONFIG_DIR, 'auth.yml')
14
11
 
12
+ const fileExists = async filePath => {
15
13
  try {
16
- configFileContent = fs.readFileSync(configFile)
17
- } catch (error) {
18
- if (error.code === 'ENOENT') {
19
- // File doesn't exist, lets create a new one automatically
20
- fs.mkdirSync(configDir, { recursive: true })
21
- fs.writeFileSync(configFile, configFileContent)
14
+ await fs.stat(filePath)
15
+
16
+ return true
17
+ } catch (err) {
18
+ if (err.code === 'ENOENT') {
19
+ return false
22
20
  }
21
+
22
+ throw err
23
23
  }
24
+ }
24
25
 
25
- return yaml.load(configFileContent)
26
+ const createFileIfMissing = async filePath => {
27
+ await fs.mkdir(path.dirname(filePath), { recursive: true })
28
+ await fs.open(filePath, 'a').then(fh => fh.close())
26
29
  }
27
30
 
28
- const updateConfig = config => {
29
- const originalConfig = readConfig()
30
- const newConfig = {
31
- ...originalConfig,
32
- ...config,
33
- }
31
+ const resolveConfig = async () => {
32
+ let release = noop
34
33
 
35
- fs.writeFileSync(configFile, yaml.dump(newConfig))
36
- }
34
+ try {
35
+ if (!(await fileExists(CONFIG_FILE_PATH))) {
36
+ await createFileIfMissing(CONFIG_FILE_PATH)
37
+ }
37
38
 
38
- export const isTrackingEnabled = () => {
39
- const config = readConfig()
39
+ release = await properLockfile.lock(CONFIG_FILE_PATH, {
40
+ retries: {
41
+ forever: true,
42
+ maxRetryTime: 10 * 1000, // try locking the file for 10s
43
+ },
44
+ })
40
45
 
41
- return config.allow_tracking !== 'no'
42
- }
46
+ const config = {
47
+ allow_tracking: true,
48
+ }
49
+
50
+ try {
51
+ Object.assign(
52
+ config,
53
+ yaml.load(await fs.readFile(CONFIG_FILE_PATH, 'utf8'))
54
+ )
55
+ } catch (err) {
56
+ if (err.code !== 'ENOENT') {
57
+ throw err
58
+ }
59
+ }
43
60
 
44
- export const getDeviceId = () => {
45
- const config = readConfig()
61
+ if (!config.uuid) {
62
+ config.uuid = crypto.randomUUID()
63
+
64
+ await fs.writeFile(CONFIG_FILE_PATH, yaml.dump(config))
65
+ }
46
66
 
47
- if (config && typeof config.uuid === 'string') {
48
- return config.uuid
67
+ return config
68
+ } finally {
69
+ release()
49
70
  }
71
+ }
50
72
 
51
- const id = uuid()
73
+ let loadedConfig = undefined
52
74
 
53
- updateConfig({
54
- uuid: id,
55
- })
75
+ export const getConfig = async () => {
76
+ if (!loadedConfig) {
77
+ try {
78
+ const yamlConfig = await resolveConfig()
56
79
 
57
- return id
58
- }
80
+ loadedConfig = {
81
+ anonymousId: yamlConfig.uuid,
82
+ allowTracking: yamlConfig.allow_tracking,
83
+ }
84
+ } catch (err) {
85
+ loadedConfig = {
86
+ anonymousId: 'no-uuid',
87
+ allowTracking: false,
88
+ }
89
+ }
90
+ }
59
91
 
60
- export default {
61
- getDeviceId,
62
- isTrackingEnabled,
92
+ return loadedConfig
63
93
  }
@@ -1,13 +1,16 @@
1
1
  import Analytics from 'analytics-node'
2
2
 
3
3
  import { isCi } from '../ci.js'
4
- import { getDeviceId, isTrackingEnabled } from './id-config.js'
4
+ import { getConfig } from './id-config.js'
5
5
 
6
6
  const SEGMENT_WRITE_KEY = 'cwNYguEVVqdxFj5NyTKOhlrvaNQjlsXV'
7
- const anonymousId = getDeviceId()
7
+
8
+ const { anonymousId, allowTracking } = await getConfig()
9
+
10
+ const analyticsEnabled = !process.env.DAVINCI_SKIP_ANALYTICS && allowTracking
8
11
 
9
12
  const analytics = new Analytics(SEGMENT_WRITE_KEY, {
10
- enable: process.env.DAVINCI_SKIP_ANALYTICS !== 'true' && isTrackingEnabled(),
13
+ enable: analyticsEnabled,
11
14
  flushAt: 1,
12
15
  })
13
16