-
Notifications
You must be signed in to change notification settings - Fork 58
/
utils.js
195 lines (173 loc) · 3.63 KB
/
utils.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Various utility methods used in AEgir.
*
* @module aegir/utils
*/
'use strict'
const path = require('path')
const findUp = require('findup-sync')
const fs = require('fs-extra')
const _ = require('lodash')
const VerboseRenderer = require('listr-verbose-renderer')
const UpdateRenderer = require('listr-update-renderer')
const PKG_FILE = 'package.json'
const DIST_FOLDER = 'dist'
/**
* Gets the top level path of the project aegir is executed in.
*
* @returns {string}
*/
exports.getBasePath = () => {
return process.cwd()
}
/**
* @returns {string}
*/
exports.getPathToPkg = () => {
return path.join(exports.getBasePath(), PKG_FILE)
}
/**
* @returns {Promise<Object>}
*/
exports.getPkg = () => {
return fs.readJson(exports.getPathToPkg())
}
/**
* @returns {string}
*/
exports.getPathToDist = () => {
return path.join(exports.getBasePath(), DIST_FOLDER)
}
/**
* @returns {string}
*/
exports.getUserConfigPath = () => {
return findUp('.aegir.js')
}
/**
* @returns {Object}
*/
exports.getUserConfig = () => {
let conf = {}
try {
conf = require(exports.getUserConfigPath())
} catch (err) {
}
return conf
}
/**
* Converts the given name from something like `peer-id` to `PeerId`.
*
* @param {string} name
*
* @returns {string}
*/
exports.getLibraryName = (name) => {
return _.upperFirst(_.camelCase(name))
}
/**
* Get the absolute path to `node_modules` for aegir itself
*
* @returns {string}
*/
exports.getPathToNodeModules = () => {
return path.resolve(__dirname, '../node_modules')
}
/**
* Get the config for Listr depending on the current environment.
*
* @returns {Object}
*/
exports.getListrConfig = () => {
const isCI = String(process.env.CI) !== 'undefined'
return {
renderer: isCI ? VerboseRenderer : UpdateRenderer
}
}
/**
* Get current env variables for inclusion.
*
* @param {string} [env='development']
*
* @returns {Object}
*/
exports.getEnv = (env) => {
const PREFIX = /^AEGIR_/i
let NODE_ENV = env || 'development'
if (JSON.stringify(process.env.NODE_ENV) !== JSON.stringify(undefined) && process.env.NODE_ENV) {
NODE_ENV = process.env.NODE_ENV
}
const raw = Object.keys(process.env)
.filter((key) => PREFIX.test(key))
.reduce((env, key) => {
if (key === 'AEGIR_GHTOKEN') {
return env
} else {
env[key] = process.env[key]
return env
}
}, {
NODE_ENV: NODE_ENV
})
const stringifed = {
'process.env': Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key])
return env
}, {})
}
return {
raw: raw,
stringified: stringifed
}
}
/**
* Path to example file.
*
* @returns {string}
*/
exports.getPathToExample = () => {
return path.join(exports.getBasePath(), 'example.js')
}
/**
* Path to documentation config file.
*
* @returns {string}
*/
exports.getPathToDocsConfig = () => {
return path.join(exports.getBasePath(), 'documentation.yml')
}
/**
* Path to documentation folder.
*
* @returns {string}
*/
exports.getPathToDocs = () => {
return path.join(exports.getBasePath(), 'docs')
}
/**
* Path to documentation index.html.
*
* @returns {string}
*/
exports.getPathToDocsFile = () => {
return path.join(exports.getPathToDocs(), 'index.html')
}
/**
* Path to documentation index.md.
*
* @returns {string}
*/
exports.getPathToDocsMdFile = () => {
return path.join(exports.getPathToDocs(), 'index.md')
}
exports.hook = (env, key) => (ctx) => {
if (ctx && ctx.hooks) {
if (ctx.hooks[env] && ctx.hooks[env][key]) {
return ctx.hooks[env][key]()
}
if (ctx.hooks[key]) {
return ctx.hooks[key]()
}
}
return Promise.resolve()
}