bajo 1.0.2 → 1.0.3

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.
@@ -1,4 +1,4 @@
1
- import getGlobalModulePath from '../../../lib/get-global-module-path.js'
1
+ import getGlobalPath from 'get-global-path'
2
2
  import resolvePath from './resolve-path.js'
3
3
  import { dropRight } from 'lodash-es'
4
4
  import fs from 'fs-extra'
@@ -6,7 +6,7 @@ import fs from 'fs-extra'
6
6
  function getGlobalModuleDir (pkgName, silent = true) {
7
7
  let nodeModulesDir = process.env.BAJO_GLOBAL_MODULE_DIR
8
8
  if (!nodeModulesDir) {
9
- const npmPath = getGlobalModulePath('npm')
9
+ const npmPath = getGlobalPath('npm')
10
10
  if (!npmPath) {
11
11
  if (silent) return
12
12
  throw this.error('Can\'t locate npm global module directory', { code: 'BAJO_CANT_LOCATE_NPM_GLOBAL_DIR' })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {
@@ -35,6 +35,7 @@
35
35
  "fast-glob": "^3.3.3",
36
36
  "flat": "^6.0.1",
37
37
  "fs-extra": "^11.2.0",
38
+ "get-global-path": "^0.0.1",
38
39
  "lodash-es": "^4.17.21",
39
40
  "ms": "^2.1.3",
40
41
  "nanoid": "^5.0.9",
@@ -1,189 +0,0 @@
1
- // based on global-modules-path, patching it to work on Node LTS
2
- import { spawnSync } from 'child_process'
3
-
4
- import path from 'path'
5
- import fs from 'fs'
6
-
7
- const supportedPlatforms = [
8
- 'win32',
9
- 'linux',
10
- 'darwin'
11
- ]
12
-
13
- const nodeModulesDirName = 'node_modules'
14
-
15
- const getNpmExecutable = (platform) => {
16
- let npmExecutableName = 'npm'
17
- if (platform === 'win32') npmExecutableName += '.cmd'
18
- return npmExecutableName
19
- }
20
-
21
- const spawnSyncWrapper = (command, commandArgs) => {
22
- const result = spawnSync(command, commandArgs, { shell: true })
23
- if (!result) return null
24
- if (result.error) throw result.error
25
- if (result.stdout) return result.stdout.toString().trim()
26
- return null
27
- }
28
-
29
- const getNpmPrefix = (pathToNpm) => {
30
- try {
31
- const npmPrefixStdout = spawnSyncWrapper(pathToNpm, ['config', 'get', 'prefix'])
32
- return npmPrefixStdout && npmPrefixStdout.toString().trim()
33
- } catch (err) {
34
- console.error(err.message)
35
- }
36
-
37
- return null
38
- }
39
-
40
- const getPathFromNpmConfig = (platform, packageName) => {
41
- const pathToNpm = getNpmExecutable(platform)
42
- const npmConfigPrefix = getNpmPrefix(pathToNpm)
43
-
44
- if (npmConfigPrefix) {
45
- let nodeModulesPath = path.join(npmConfigPrefix, nodeModulesDirName)
46
-
47
- if (platform !== 'win32') {
48
- nodeModulesPath = path.join(npmConfigPrefix, 'lib', nodeModulesDirName)
49
- }
50
-
51
- const packagePath = path.join(nodeModulesPath, packageName)
52
- const verifiedPath = getVerifiedPath(packagePath, packageName)
53
-
54
- if (verifiedPath) {
55
- return verifiedPath
56
- }
57
- }
58
-
59
- return null
60
- }
61
-
62
- const getPathFromCmdContent = (packageName, pathToExecutable) => {
63
- if (fs.existsSync(pathToExecutable)) {
64
- const executableContent = fs.readFileSync(pathToExecutable).toString()
65
-
66
- let fullPath
67
-
68
- let windowsPathRegExp = /(%~dp0[\w\\.-]+node_modules).*?'/g
69
- let match = windowsPathRegExp.exec(executableContent)
70
-
71
- if (match && match[1]) {
72
- const realPath = path.normalize(match[1].replace('%~dp0', path.dirname(pathToExecutable)))
73
- fullPath = path.join(realPath, packageName)
74
- }
75
-
76
- if (!fullPath) {
77
- windowsPathRegExp = new RegExp(`(%~dp0[\\w\\\\.-]+?${packageName})(?:\\\\|')`, 'g')
78
- match = windowsPathRegExp.exec(executableContent)
79
- if (match && match[1]) fullPath = path.normalize(match[1].replace('%~dp0', path.dirname(pathToExecutable)))
80
- }
81
-
82
- if (fullPath) {
83
- const pathToPackage = getVerifiedPath(fullPath, packageName)
84
- if (pathToPackage) return pathToPackage
85
- }
86
- }
87
- }
88
-
89
- const getVerifiedPath = (suggestedPath, packageName) => {
90
- const pathToPackageJson = path.join(suggestedPath, 'package.json')
91
- if (fs.existsSync(suggestedPath) && fs.existsSync(pathToPackageJson)) {
92
- try {
93
- const packageJsonContent = JSON.parse(fs.readFileSync(pathToPackageJson))
94
- if (packageJsonContent.name === packageName) return suggestedPath
95
- } catch (err) {
96
- // do nothing
97
- }
98
- }
99
- }
100
-
101
- const getPathFromExecutableNameOnWindows = (packageName, executableName) => {
102
- try {
103
- const whereResult = (spawnSyncWrapper('where', [executableName]) || '').split('\n')
104
- for (const line of whereResult) {
105
- const pathToExecutable = line && line.trim()
106
-
107
- if (pathToExecutable) {
108
- const pathToLib = path.join(path.dirname(pathToExecutable), nodeModulesDirName, packageName)
109
- const verifiedPath = getVerifiedPath(pathToLib, packageName)
110
- if (verifiedPath) return verifiedPath
111
- const pathToExecutableFromContent = getPathFromCmdContent(packageName, pathToExecutable)
112
- if (pathToExecutableFromContent) return pathToExecutableFromContent
113
-
114
- const resolvedPath = getPathWhenExecutableIsAddedDirectlyToPath(packageName, pathToExecutable)
115
- if (resolvedPath) return resolvedPath
116
- }
117
- }
118
- } catch (err) {
119
- console.error(err.message)
120
- }
121
-
122
- return null
123
- }
124
-
125
- const getPathFromExecutableNameOnNonWindows = (packageName, executableName) => {
126
- try {
127
- const whichResult = spawnSyncWrapper('which', [executableName])
128
- const lsLResult = spawnSyncWrapper('ls', ['-l', whichResult])
129
-
130
- if (whichResult && lsLResult) {
131
- const regex = new RegExp(`${whichResult}\\s+->\\s+(.*?)$`)
132
- const match = lsLResult.match(regex)
133
-
134
- if (match && match[1]) {
135
- const pathToRealExecutable = fs.realpathSync(path.join(path.dirname(whichResult), match[1]))
136
- const packagePathMatch = pathToRealExecutable.match(new RegExp(`(.*?${path.join(nodeModulesDirName, packageName)}).*$`))
137
- if (packagePathMatch) {
138
- const verifiedPath = getVerifiedPath(packagePathMatch[1], packageName)
139
- if (verifiedPath) return verifiedPath
140
- }
141
- }
142
-
143
- return getPathWhenExecutableIsAddedDirectlyToPath(packageName, whichResult)
144
- }
145
- } catch (err) {
146
- console.error(err.message)
147
- }
148
-
149
- return null
150
- }
151
-
152
- const getPathWhenExecutableIsAddedDirectlyToPath = (packageName, executablePath) => {
153
- const pathToPackageJson = path.join(path.dirname(executablePath), '..', 'package.json')
154
- if (fs.existsSync(pathToPackageJson)) {
155
- const packageNameFromPackageJson = JSON.parse(fs.readFileSync(pathToPackageJson)).name
156
- if (packageNameFromPackageJson === packageName) return path.dirname(pathToPackageJson)
157
- }
158
-
159
- return null
160
- }
161
-
162
- const getPath = (packageName, executableName) => {
163
- const platform = process.platform
164
-
165
- if (supportedPlatforms.indexOf(platform) === -1) {
166
- throw new Error(`OS '${platform}' is not supported.'`)
167
- }
168
-
169
- let foundPath = null
170
-
171
- if (executableName) {
172
- foundPath = platform === 'win32' ?
173
- getPathFromExecutableNameOnWindows(packageName, executableName) :
174
- getPathFromExecutableNameOnNonWindows(packageName, executableName)
175
- }
176
-
177
- if (!foundPath) foundPath = getPathFromNpmConfig(platform, packageName)
178
- if (foundPath) {
179
- try {
180
- foundPath = fs.realpathSync(foundPath)
181
- } catch (err) {
182
- console.error(err.message)
183
- }
184
- }
185
-
186
- return foundPath
187
- }
188
-
189
- export default getPath