@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 +8 -0
- package/package.json +6 -7
- package/src/utils/metrics/id-config.js +73 -43
- package/src/utils/metrics/metrics.js +6 -3
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
|
|
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
|
-
"
|
|
32
|
+
"proper-lockfile": "^4.1.2",
|
|
35
33
|
"read-pkg-up": "7.0.1",
|
|
36
34
|
"semver": "^7.3.7",
|
|
37
|
-
"
|
|
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
|
|
6
|
+
import properLockfile from 'proper-lockfile'
|
|
7
|
+
import noop from 'lodash/noop.js'
|
|
6
8
|
|
|
7
|
-
const
|
|
8
|
-
const
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
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
|
|
29
|
-
|
|
30
|
-
const newConfig = {
|
|
31
|
-
...originalConfig,
|
|
32
|
-
...config,
|
|
33
|
-
}
|
|
31
|
+
const resolveConfig = async () => {
|
|
32
|
+
let release = noop
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
try {
|
|
35
|
+
if (!(await fileExists(CONFIG_FILE_PATH))) {
|
|
36
|
+
await createFileIfMissing(CONFIG_FILE_PATH)
|
|
37
|
+
}
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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
|
-
|
|
45
|
-
|
|
61
|
+
if (!config.uuid) {
|
|
62
|
+
config.uuid = crypto.randomUUID()
|
|
63
|
+
|
|
64
|
+
await fs.writeFile(CONFIG_FILE_PATH, yaml.dump(config))
|
|
65
|
+
}
|
|
46
66
|
|
|
47
|
-
|
|
48
|
-
|
|
67
|
+
return config
|
|
68
|
+
} finally {
|
|
69
|
+
release()
|
|
49
70
|
}
|
|
71
|
+
}
|
|
50
72
|
|
|
51
|
-
|
|
73
|
+
let loadedConfig = undefined
|
|
52
74
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
75
|
+
export const getConfig = async () => {
|
|
76
|
+
if (!loadedConfig) {
|
|
77
|
+
try {
|
|
78
|
+
const yamlConfig = await resolveConfig()
|
|
56
79
|
|
|
57
|
-
|
|
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
|
-
|
|
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 {
|
|
4
|
+
import { getConfig } from './id-config.js'
|
|
5
5
|
|
|
6
6
|
const SEGMENT_WRITE_KEY = 'cwNYguEVVqdxFj5NyTKOhlrvaNQjlsXV'
|
|
7
|
-
|
|
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:
|
|
13
|
+
enable: analyticsEnabled,
|
|
11
14
|
flushAt: 1,
|
|
12
15
|
})
|
|
13
16
|
|