@toptal/davinci-workspace-root 1.0.1-alpha-ff-65-0dda0627.2600

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/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@toptal/davinci-workspace-root",
3
+ "version": "1.0.1-alpha-ff-65-0dda0627.2600+0dda0627",
4
+ "description": "Package-manager-agnostic workspace root finder for Yarn and pnpm",
5
+ "repository": "toptal/davinci.git",
6
+ "author": "Toptal",
7
+ "license": "SEE LICENSE IN LICENSE.MD",
8
+ "bugs": "https://github.com/toptal/davinci/issues",
9
+ "type": "module",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./src/index.js",
13
+ "require": "./src/index.cjs"
14
+ }
15
+ },
16
+ "main": "./src/index.cjs",
17
+ "types": "./src/index.d.ts",
18
+ "files": [
19
+ "src"
20
+ ],
21
+ "dependencies": {
22
+ "micromatch": "^4.0.8"
23
+ },
24
+ "module": "./src/index.js",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "gitHead": "0dda06271bbece67941c41de8cef2bc09de516c2"
29
+ }
package/src/index.cjs ADDED
@@ -0,0 +1,83 @@
1
+ const path = require('path')
2
+ const fs = require('fs')
3
+ const micromatch = require('micromatch')
4
+
5
+ const readJSON = (filePath) => {
6
+ try {
7
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'))
8
+ } catch {
9
+ return null
10
+ }
11
+ }
12
+
13
+ const extractYarnWorkspaces = (dir) => {
14
+ const pkg = readJSON(path.join(dir, 'package.json'))
15
+ const workspaces = pkg?.workspaces
16
+
17
+ if (Array.isArray(workspaces)) {
18
+ return workspaces
19
+ }
20
+
21
+ if (workspaces?.packages) {
22
+ return workspaces.packages
23
+ }
24
+
25
+ return null
26
+ }
27
+
28
+ const extractPnpmWorkspaces = (dir) => {
29
+ const yamlPath = path.join(dir, 'pnpm-workspace.yaml')
30
+
31
+ if (!fs.existsSync(yamlPath)) {
32
+ return null
33
+ }
34
+
35
+ const content = fs.readFileSync(yamlPath, 'utf8')
36
+ const match = content.match(/packages:\s*\n((?:\s+-\s+.+\n?)+)/)
37
+
38
+ if (!match) {
39
+ return null
40
+ }
41
+
42
+ return match[1]
43
+ .split('\n')
44
+ .map((line) => line.replace(/^\s+-\s+['"]?/, '').replace(/['"]?\s*$/, ''))
45
+ .filter(Boolean)
46
+ }
47
+
48
+ const isInWorkspace = (rootDir, initial, globs) => {
49
+ const relativePath = path.relative(rootDir, initial)
50
+
51
+ if (relativePath === '') {
52
+ return true
53
+ }
54
+
55
+ return micromatch([relativePath], globs).length > 0
56
+ }
57
+
58
+ const findWorkspaceRoot = (initial) => {
59
+ const startDir = initial ? path.resolve(initial) : process.cwd()
60
+ let dir = startDir
61
+ let parent = path.dirname(dir)
62
+
63
+ while (dir !== parent) {
64
+ const pnpmGlobs = extractPnpmWorkspaces(dir)
65
+
66
+ if (pnpmGlobs) {
67
+ return isInWorkspace(dir, startDir, pnpmGlobs) ? dir : null
68
+ }
69
+
70
+ const yarnGlobs = extractYarnWorkspaces(dir)
71
+
72
+ if (yarnGlobs) {
73
+ return isInWorkspace(dir, startDir, yarnGlobs) ? dir : null
74
+ }
75
+
76
+ dir = parent
77
+ parent = path.dirname(dir)
78
+ }
79
+
80
+ return null
81
+ }
82
+
83
+ module.exports = findWorkspaceRoot
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare function findWorkspaceRoot(initial?: string): string | null
2
+ export default findWorkspaceRoot
package/src/index.js ADDED
@@ -0,0 +1,83 @@
1
+ import path from 'path'
2
+ import fs from 'fs'
3
+ import micromatch from 'micromatch'
4
+
5
+ const readJSON = filePath => {
6
+ try {
7
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'))
8
+ } catch {
9
+ return null
10
+ }
11
+ }
12
+
13
+ const extractYarnWorkspaces = dir => {
14
+ const pkg = readJSON(path.join(dir, 'package.json'))
15
+ const workspaces = pkg?.workspaces
16
+
17
+ if (Array.isArray(workspaces)) {
18
+ return workspaces
19
+ }
20
+
21
+ if (workspaces?.packages) {
22
+ return workspaces.packages
23
+ }
24
+
25
+ return null
26
+ }
27
+
28
+ const extractPnpmWorkspaces = dir => {
29
+ const yamlPath = path.join(dir, 'pnpm-workspace.yaml')
30
+
31
+ if (!fs.existsSync(yamlPath)) {
32
+ return null
33
+ }
34
+
35
+ const content = fs.readFileSync(yamlPath, 'utf8')
36
+ const match = content.match(/packages:\s*\n((?:\s+-\s+.+\n?)+)/)
37
+
38
+ if (!match) {
39
+ return null
40
+ }
41
+
42
+ return match[1]
43
+ .split('\n')
44
+ .map(line => line.replace(/^\s+-\s+['"]?/, '').replace(/['"]?\s*$/, ''))
45
+ .filter(Boolean)
46
+ }
47
+
48
+ const isInWorkspace = (rootDir, initial, globs) => {
49
+ const relativePath = path.relative(rootDir, initial)
50
+
51
+ if (relativePath === '') {
52
+ return true
53
+ }
54
+
55
+ return micromatch([relativePath], globs).length > 0
56
+ }
57
+
58
+ const findWorkspaceRoot = initial => {
59
+ const startDir = initial ? path.resolve(initial) : process.cwd()
60
+ let dir = startDir
61
+ let parent = path.dirname(dir)
62
+
63
+ while (dir !== parent) {
64
+ const pnpmGlobs = extractPnpmWorkspaces(dir)
65
+
66
+ if (pnpmGlobs) {
67
+ return isInWorkspace(dir, startDir, pnpmGlobs) ? dir : null
68
+ }
69
+
70
+ const yarnGlobs = extractYarnWorkspaces(dir)
71
+
72
+ if (yarnGlobs) {
73
+ return isInWorkspace(dir, startDir, yarnGlobs) ? dir : null
74
+ }
75
+
76
+ dir = parent
77
+ parent = path.dirname(dir)
78
+ }
79
+
80
+ return null
81
+ }
82
+
83
+ export default findWorkspaceRoot