1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict'
|
7 | const { constants, createBrotliCompress, createGzip } = require('zlib')
|
8 | const os = require('os')
|
9 | const ora = require('ora')
|
10 | const extract = require('extract-zip')
|
11 | const { download } = require('@electron/get')
|
12 | const path = require('path')
|
13 | const findUp = require('findup-sync')
|
14 | const readPkgUp = require('read-pkg-up')
|
15 | const fs = require('fs-extra')
|
16 | const execa = require('execa')
|
17 | const pascalcase = require('pascalcase')
|
18 |
|
19 | const { packageJson: pkg, path: pkgPath } = readPkgUp.sync({
|
20 | cwd: fs.realpathSync(process.cwd())
|
21 | })
|
22 | const PKG_FILE = 'package.json'
|
23 | const DIST_FOLDER = 'dist'
|
24 | const SRC_FOLDER = 'src'
|
25 |
|
26 | exports.paths = {
|
27 | dist: DIST_FOLDER,
|
28 | src: SRC_FOLDER
|
29 | }
|
30 | exports.pkg = pkg
|
31 |
|
32 | exports.browserslist = '>1% or node >=10 and not ie 11 and not dead'
|
33 |
|
34 | exports.repoDirectory = path.dirname(pkgPath)
|
35 | exports.fromRoot = (...p) => path.join(exports.repoDirectory, ...p)
|
36 | exports.hasFile = (...p) => fs.existsSync(exports.fromRoot(...p))
|
37 | exports.fromAegir = (...p) => path.join(__dirname, '..', ...p)
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | exports.pkgVersion = async () => {
|
45 | const {
|
46 | version
|
47 | } = await fs.readJson(exports.getPathToPkg())
|
48 | return version
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | exports.getBasePath = () => {
|
57 | return process.cwd()
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | exports.getPathToPkg = () => {
|
64 | return path.join(exports.getBasePath(), PKG_FILE)
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | exports.getPathToDist = () => {
|
71 | return path.join(exports.getBasePath(), DIST_FOLDER)
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | exports.getUserConfigPath = () => {
|
78 | return findUp('.aegir.js')
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | exports.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)
|
94 | }
|
95 | return conf
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | exports.getLibraryName = (name) => {
|
106 | return pascalcase(name)
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | exports.getPathToNodeModules = () => {
|
115 | return path.resolve(__dirname, '../node_modules')
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | exports.getListrConfig = () => {
|
124 | return {
|
125 | renderer: 'verbose'
|
126 | }
|
127 | }
|
128 |
|
129 | exports.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 |
|
142 | exports.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 |
|
154 | function 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 |
|
172 | exports.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 |
|
186 | exports.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 |
|
204 | exports.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 | }
|