UNPKG

4.78 kBJavaScriptView Raw
1/**
2 * Various utility methods used in AEgir.
3 *
4 * @module aegir/utils
5 */
6'use strict'
7const { constants, createBrotliCompress, createGzip } = require('zlib')
8const os = require('os')
9const ora = require('ora')
10const extract = require('extract-zip')
11const { download } = require('@electron/get')
12const path = require('path')
13const findUp = require('findup-sync')
14const readPkgUp = require('read-pkg-up')
15const fs = require('fs-extra')
16const execa = require('execa')
17const pascalcase = require('pascalcase')
18
19const { packageJson: pkg, path: pkgPath } = readPkgUp.sync({
20 cwd: fs.realpathSync(process.cwd())
21})
22const PKG_FILE = 'package.json'
23const DIST_FOLDER = 'dist'
24const SRC_FOLDER = 'src'
25
26exports.paths = {
27 dist: DIST_FOLDER,
28 src: SRC_FOLDER
29}
30exports.pkg = pkg
31// TODO: get this from aegir package.json
32exports.browserslist = '>1% or node >=10 and not ie 11 and not dead'
33
34exports.repoDirectory = path.dirname(pkgPath)
35exports.fromRoot = (...p) => path.join(exports.repoDirectory, ...p)
36exports.hasFile = (...p) => fs.existsSync(exports.fromRoot(...p))
37exports.fromAegir = (...p) => path.join(__dirname, '..', ...p)
38
39/**
40 * Get package version
41 *
42 * @returns {string} version
43 */
44exports.pkgVersion = async () => {
45 const {
46 version
47 } = await fs.readJson(exports.getPathToPkg())
48 return version
49}
50
51/**
52 * Gets the top level path of the project aegir is executed in.
53 *
54 * @returns {string}
55 */
56exports.getBasePath = () => {
57 return process.cwd()
58}
59
60/**
61 * @returns {string}
62 */
63exports.getPathToPkg = () => {
64 return path.join(exports.getBasePath(), PKG_FILE)
65}
66
67/**
68 * @returns {string}
69 */
70exports.getPathToDist = () => {
71 return path.join(exports.getBasePath(), DIST_FOLDER)
72}
73
74/**
75 * @returns {string}
76 */
77exports.getUserConfigPath = () => {
78 return findUp('.aegir.js')
79}
80
81/**
82 * @returns {Object}
83 */
84exports.getUserConfig = () => {
85 let conf = {}
86 try {
87 const path = exports.getUserConfigPath()
88 if (!path) {
89 return {}
90 }
91 conf = require(path)
92 } catch (err) {
93 console.error(err) // eslint-disable-line no-console
94 }
95 return conf
96}
97
98/**
99 * Converts the given name from something like `peer-id` to `PeerId`.
100 *
101 * @param {string} name
102 *
103 * @returns {string}
104 */
105exports.getLibraryName = (name) => {
106 return pascalcase(name)
107}
108
109/**
110 * Get the absolute path to `node_modules` for aegir itself
111 *
112 * @returns {string}
113 */
114exports.getPathToNodeModules = () => {
115 return path.resolve(__dirname, '../node_modules')
116}
117
118/**
119 * Get the config for Listr.
120 *
121 * @returns {Object}
122 */
123exports.getListrConfig = () => {
124 return {
125 renderer: 'verbose'
126 }
127}
128
129exports.hook = (env, key) => (ctx) => {
130 if (ctx && ctx.hooks) {
131 if (ctx.hooks[env] && ctx.hooks[env][key]) {
132 return ctx.hooks[env][key]()
133 }
134 if (ctx.hooks[key]) {
135 return ctx.hooks[key]()
136 }
137 }
138
139 return Promise.resolve()
140}
141
142exports.exec = (command, args, options = {}) => {
143 const result = execa(command, args, options)
144
145 if (!options.quiet) {
146 result.stdout.pipe(process.stdout)
147 }
148
149 result.stderr.pipe(process.stderr)
150
151 return result
152}
153
154function getPlatformPath () {
155 const platform = process.env.npm_config_platform || os.platform()
156
157 switch (platform) {
158 case 'mas':
159 case 'darwin':
160 return 'Electron.app/Contents/MacOS/Electron'
161 case 'freebsd':
162 case 'openbsd':
163 case 'linux':
164 return 'electron'
165 case 'win32':
166 return 'electron.exe'
167 default:
168 throw new Error('Electron builds are not available on platform: ' + platform)
169 }
170}
171
172exports.getElectron = async () => {
173 const pkg = require('./../package.json')
174 const version = pkg.devDependencies.electron.slice(1)
175 const spinner = ora(`Downloading electron: ${version}`).start()
176 const zipPath = await download(version)
177 const electronPath = path.join(path.dirname(zipPath), getPlatformPath())
178 if (!fs.existsSync(electronPath)) {
179 spinner.text = 'Extracting electron to system cache'
180 await extract(zipPath, { dir: path.dirname(zipPath) })
181 }
182 spinner.succeed('Electron ready to use')
183 return electronPath
184}
185
186exports.brotliSize = (path) => {
187 return new Promise((resolve, reject) => {
188 let size = 0
189 const pipe = fs.createReadStream(path).pipe(createBrotliCompress({
190 params: {
191 [constants.BROTLI_PARAM_QUALITY]: 11
192 }
193 }))
194 pipe.on('error', reject)
195 pipe.on('data', buf => {
196 size += buf.length
197 })
198 pipe.on('end', () => {
199 resolve(size)
200 })
201 })
202}
203
204exports.gzipSize = (path) => {
205 return new Promise((resolve, reject) => {
206 let size = 0
207 const pipe = fs.createReadStream(path).pipe(createGzip({ level: 9 }))
208 pipe.on('error', reject)
209 pipe.on('data', buf => {
210 size += buf.length
211 })
212 pipe.on('end', () => {
213 resolve(size)
214 })
215 })
216}