ihimnm 0.1.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +18 -0
  3. package/index.js +183 -0
  4. package/package.json +17 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Bjorn Lu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # ihimnm
2
+
3
+ You know when you need this.
4
+
5
+ Make sure to install your `node_modules` before running the CLI.
6
+
7
+ ```bash
8
+ # Search for closest package.json and find deps matching the criteria
9
+ npx ihimnm
10
+
11
+ # Search nested package.json files and find deps matching the criteria
12
+ # (useful for monorepos)
13
+ npx ihimnm -r
14
+ ```
15
+
16
+ ## License
17
+
18
+ MIT
package/index.js ADDED
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+
6
+ const isRecursive = process.argv.includes('-r')
7
+ const ignoredFileNameRe = /^(\.|node_modules|dist|build|output|cache)/
8
+ const maxNestedDepth = 10
9
+ /** @type {Map<string, number>} */
10
+ const allFoundDeps = new Map()
11
+
12
+ // If not recursive, use closest package.json
13
+ if (!isRecursive) {
14
+ const packageJsonPath = findClosestPkgJsonPath(process.cwd())
15
+ if (!packageJsonPath) {
16
+ console.error(`No closest package.json found from ${process.cwd()}`)
17
+ process.exit(1)
18
+ }
19
+
20
+ const found = crawlDependencies(packageJsonPath, [], true)
21
+ if (!found) console.log(green('None found!'))
22
+ }
23
+ // If recursive, use nested package.json from cwd
24
+ else {
25
+ const packageJsonPaths = findNestedPkgJsonPathsFromDir(process.cwd())
26
+ if (!packageJsonPaths.length) {
27
+ console.error(`No nested package.json found from ${process.cwd()}`)
28
+ process.exit(1)
29
+ }
30
+
31
+ for (const packageJsonPath of packageJsonPaths) {
32
+ console.log(`${packageJsonPath}:`)
33
+ const found = crawlDependencies(packageJsonPath, [], true)
34
+ if (!found) console.log(green('None found!'))
35
+ }
36
+ }
37
+
38
+ if (allFoundDeps.size) {
39
+ console.log(`Summary of all found dependencies:`)
40
+ const sortedDepNames = Array.from(allFoundDeps.keys()).sort()
41
+ const padNum = sortedDepNames.length.toString().length + 1
42
+ for (let i = 0; i < sortedDepNames.length; i++) {
43
+ const depName = sortedDepNames[i]
44
+ const numStr = dim(`${i + 1}.`.padStart(padNum))
45
+ console.log(
46
+ `${numStr} ${red(depName)} ${dim(`(${allFoundDeps.get(depName)})`)}`
47
+ )
48
+ }
49
+ }
50
+
51
+ /**
52
+ * @param {string} pkgJsonPath
53
+ * @param {string[]} parentDepNames
54
+ * @param {boolean} isRoot
55
+ */
56
+ function crawlDependencies(pkgJsonPath, parentDepNames, isRoot = false) {
57
+ let found = false
58
+ const pkgJsonContent = fs.readFileSync(pkgJsonPath, 'utf8')
59
+ const pkgJson = JSON.parse(pkgJsonContent)
60
+ const pkgDependencies = Object.keys(pkgJson.dependencies || {})
61
+
62
+ if (isRoot) {
63
+ pkgDependencies.push(...Object.keys(pkgJson.devDependencies || {}))
64
+ }
65
+ // use very lax technique to detect:
66
+ // - from github url
67
+ // - from contributors list
68
+ // - from @.../eslint-config dev dep
69
+ else if (pkgJsonContent.includes('ljharb')) {
70
+ logDep(pkgJson.name, parentDepNames)
71
+ found = true
72
+ const foundCount = allFoundDeps.get(pkgJson.name) || 0
73
+ allFoundDeps.set(pkgJson.name, foundCount + 1)
74
+ }
75
+
76
+ for (const depName of pkgDependencies) {
77
+ // Prevent dep loop
78
+ if (parentDepNames.includes(depName)) continue
79
+
80
+ const depPkgJsonPath = findPkgJsonPath(depName, path.dirname(pkgJsonPath))
81
+ if (!depPkgJsonPath) continue
82
+
83
+ const nestedFound = crawlDependencies(
84
+ depPkgJsonPath,
85
+ isRoot ? [] : parentDepNames.concat(pkgJson.name)
86
+ )
87
+
88
+ found = found || nestedFound
89
+ }
90
+
91
+ return found
92
+ }
93
+
94
+ /**
95
+ * @param {string} dir
96
+ */
97
+ function findClosestPkgJsonPath(dir) {
98
+ while (dir) {
99
+ const pkg = path.join(dir, 'package.json')
100
+ try {
101
+ if (fs.existsSync(pkg)) {
102
+ return pkg
103
+ }
104
+ } catch {}
105
+ const nextDir = path.dirname(dir)
106
+ if (nextDir === dir) break
107
+ dir = nextDir
108
+ }
109
+ return undefined
110
+ }
111
+
112
+ /**
113
+ * @param {string} pkgName
114
+ * @param {string} basedir
115
+ */
116
+ function findPkgJsonPath(pkgName, basedir) {
117
+ while (basedir) {
118
+ const pkg = path.join(basedir, 'node_modules', pkgName, 'package.json')
119
+ try {
120
+ if (fs.existsSync(pkg)) {
121
+ return fs.realpathSync(pkg)
122
+ }
123
+ } catch {}
124
+ const nextBasedir = path.dirname(basedir)
125
+ if (nextBasedir === basedir) break
126
+ basedir = nextBasedir
127
+ }
128
+ return undefined
129
+ }
130
+
131
+ /**
132
+ * @param {string} dir
133
+ */
134
+ function findNestedPkgJsonPathsFromDir(dir, currentDepth = 0) {
135
+ /** @type {string[]} */
136
+ const pkgJsonPaths = []
137
+ const files = fs.readdirSync(dir)
138
+ for (const file of files) {
139
+ if (!ignoredFileNameRe.test(file)) {
140
+ const filePath = path.join(dir, file)
141
+ const stat = fs.statSync(filePath)
142
+ if (stat.isFile() && file === 'package.json') {
143
+ pkgJsonPaths.push(filePath)
144
+ } else if (stat.isDirectory() && currentDepth < maxNestedDepth) {
145
+ pkgJsonPaths.push(
146
+ ...findNestedPkgJsonPathsFromDir(filePath, currentDepth + 1)
147
+ )
148
+ }
149
+ }
150
+ }
151
+ return pkgJsonPaths
152
+ }
153
+
154
+ /**
155
+ * @param {string} depName
156
+ * @param {string[]} parentPackageNames
157
+ */
158
+ function logDep(depName, parentPackageNames) {
159
+ console.log(
160
+ dim(parentPackageNames.map((n) => n + ' > ').join('')) + red(depName)
161
+ )
162
+ }
163
+
164
+ /**
165
+ * @param {string} str
166
+ */
167
+ function red(str) {
168
+ return `\x1b[1m\x1b[31m${str}\x1b[0m`
169
+ }
170
+
171
+ /**
172
+ * @param {string} str
173
+ */
174
+ function green(str) {
175
+ return `\x1b[1m\x1b[32m${str}\x1b[0m`
176
+ }
177
+
178
+ /**
179
+ * @param {string} str
180
+ */
181
+ function dim(str) {
182
+ return `\x1b[2m${str}\x1b[0m`
183
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "ihimnm",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "author": "Bjorn Lu",
6
+ "license": "MIT",
7
+ "bin": "./index.js",
8
+ "files": ["index.js"],
9
+ "funding": "https://bjornlu.com/sponsor",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/bluwy/ihimnm.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/bluwy/ihimnm/issues"
16
+ }
17
+ }