forked from botpress/botpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
97 lines (81 loc) · 2.28 KB
/
Copy pathutil.js
File metadata and controls
97 lines (81 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import chalk from 'chalk'
import path from 'path'
import Module from 'module'
import fs from 'fs'
const IS_DEV = process.env.NODE_ENV !== 'production'
const NPM_CMD = /^win/.test(process.platform) ? 'npm.cmd' : 'npm'
const print = function(...args) {
const mapping = {
info: chalk.white,
warn: function() { return chalk.yellow('WARN', ...arguments) },
error: function() { return chalk.red('ERR', ...arguments) },
success: function() { return chalk.green('OK', ...arguments) }
}
let level = mapping[args[0]]
const matched = !!level
if (!matched) {
level = mapping.info
} else {
args.splice(0, 1)
}
console.log(chalk.black.bgWhite('[botpress]'), '\t', level(...args))
}
const resolveFromDir = function (fromDir, moduleId) {
fromDir = path.resolve(fromDir)
const fromFile = path.join(fromDir, 'noop.js')
try {
return Module._resolveFilename(moduleId, {
id: fromFile,
filename: fromFile,
paths: Module._nodeModulePaths(fromDir)
})
} catch (err) {
return null
}
}
const resolveModuleRootPath = function(entryPath) {
let current = path.dirname(entryPath)
while (current !== '/') {
const lookup = path.join(current, 'package.json')
if (fs.existsSync(lookup)) {
return current
}
current = path.resolve(path.join(current, '..'))
}
return null
}
const resolveProjectFile = (file, projectLocation, throwIfNotExist) => {
const packagePath = path.resolve(projectLocation || './', file)
if (!fs.existsSync(packagePath)) {
if (throwIfNotExist) {
throw new Error('Could not find bot\'s package.json file')
}
return null
}
return packagePath
}
const getDataLocation = (dataDir, projectLocation) => (
dataDir && path.isAbsolute(dataDir)
? path.resolve(dataDir)
: path.resolve(projectLocation, dataDir || 'data')
)
const getBotpressVersion = () => {
const botpressPackagePath = path.join(__dirname, '../package.json')
const botpressJson = JSON.parse(fs.readFileSync(botpressPackagePath))
return botpressJson.version
}
const collectArgs = (val, memo) => {
memo.push(val)
return memo
}
module.exports = {
print,
resolveFromDir,
isDeveloping: IS_DEV,
resolveModuleRootPath,
resolveProjectFile,
getDataLocation,
npmCmd: NPM_CMD,
getBotpressVersion,
collectArgs
}