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 stripComments = require('strip-json-comments')
|
12 | const stripBom = require('strip-bom')
|
13 | const { download } = require('@electron/get')
|
14 | const path = require('path')
|
15 | const findUp = require('findup-sync')
|
16 | const readPkgUp = require('read-pkg-up')
|
17 | const fs = require('fs-extra')
|
18 | const execa = require('execa')
|
19 | const pascalcase = require('pascalcase')
|
20 | const ghPages = require('gh-pages')
|
21 | const { promisify } = require('util')
|
22 |
|
23 | const publishPages = promisify(ghPages.publish)
|
24 |
|
25 | const { packageJson: pkg, path: pkgPath } = readPkgUp.sync({
|
26 | cwd: fs.realpathSync(process.cwd())
|
27 | })
|
28 | const PKG_FILE = 'package.json'
|
29 | const DIST_FOLDER = 'dist'
|
30 | const SRC_FOLDER = 'src'
|
31 |
|
32 | exports.paths = {
|
33 | dist: DIST_FOLDER,
|
34 | src: SRC_FOLDER
|
35 | }
|
36 | exports.pkg = pkg
|
37 |
|
38 | exports.browserslist = '>1% or node >=10 and not ie 11 and not dead'
|
39 | exports.repoDirectory = path.dirname(pkgPath)
|
40 | exports.fromRoot = (...p) => path.join(exports.repoDirectory, ...p)
|
41 | exports.hasFile = (...p) => fs.existsSync(exports.fromRoot(...p))
|
42 | exports.fromAegir = (...p) => path.join(__dirname, '..', ...p)
|
43 | exports.hasTsconfig = exports.hasFile('tsconfig.json')
|
44 |
|
45 | exports.parseJson = (contents) => {
|
46 | const data = stripComments(stripBom(contents))
|
47 |
|
48 |
|
49 | if (/^\s*$/.test(data)) {
|
50 | return {}
|
51 | }
|
52 |
|
53 | return JSON.parse(data)
|
54 | }
|
55 |
|
56 | exports.readJson = (filePath) => {
|
57 | return exports.parseJson(fs.readFileSync(filePath, { encoding: 'utf-8' }))
|
58 | }
|
59 |
|
60 | exports.publishDocs = () => {
|
61 | return publishPages(
|
62 | 'docs',
|
63 | {
|
64 | dotfiles: true,
|
65 | message: 'chore: update documentation'
|
66 | }
|
67 | )
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | exports.pkgVersion = async () => {
|
76 | const {
|
77 | version
|
78 | } = await fs.readJson(exports.getPathToPkg())
|
79 | return version
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | exports.getBasePath = () => {
|
88 | return process.cwd()
|
89 | }
|
90 |
|
91 | exports.getPathToPkg = () => {
|
92 | return path.join(exports.getBasePath(), PKG_FILE)
|
93 | }
|
94 |
|
95 | exports.getPathToDist = () => {
|
96 | return path.join(exports.getBasePath(), DIST_FOLDER)
|
97 | }
|
98 |
|
99 | exports.getUserConfigPath = () => {
|
100 | return findUp('.aegir.js')
|
101 | }
|
102 |
|
103 | exports.getUserConfig = () => {
|
104 | let conf = {}
|
105 | try {
|
106 | const path = exports.getUserConfigPath()
|
107 | if (!path) {
|
108 | return {}
|
109 | }
|
110 | conf = require(path)
|
111 | } catch (err) {
|
112 | console.error(err)
|
113 | }
|
114 | return conf
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | exports.getLibraryName = (name) => {
|
124 | return pascalcase(name)
|
125 | }
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | exports.getPathToNodeModules = () => {
|
131 | return path.resolve(__dirname, '../node_modules')
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | exports.getListrConfig = () => {
|
140 | return {
|
141 | renderer: 'verbose'
|
142 | }
|
143 | }
|
144 |
|
145 | exports.hook = (env, key) => (ctx) => {
|
146 | if (ctx && ctx.hooks) {
|
147 | if (ctx.hooks[env] && ctx.hooks[env][key]) {
|
148 | return ctx.hooks[env][key]()
|
149 | }
|
150 | if (ctx.hooks[key]) {
|
151 | return ctx.hooks[key]()
|
152 | }
|
153 | }
|
154 |
|
155 | return Promise.resolve()
|
156 | }
|
157 |
|
158 | exports.exec = (command, args, options = {}) => {
|
159 | const result = execa(command, args, options)
|
160 |
|
161 | if (!options.quiet) {
|
162 | result.stdout.pipe(process.stdout)
|
163 | }
|
164 |
|
165 | result.stderr.pipe(process.stderr)
|
166 |
|
167 | return result
|
168 | }
|
169 |
|
170 | function getPlatformPath () {
|
171 | const platform = process.env.npm_config_platform || os.platform()
|
172 |
|
173 | switch (platform) {
|
174 | case 'mas':
|
175 | case 'darwin':
|
176 | return 'Electron.app/Contents/MacOS/Electron'
|
177 | case 'freebsd':
|
178 | case 'openbsd':
|
179 | case 'linux':
|
180 | return 'electron'
|
181 | case 'win32':
|
182 | return 'electron.exe'
|
183 | default:
|
184 | throw new Error('Electron builds are not available on platform: ' + platform)
|
185 | }
|
186 | }
|
187 |
|
188 | exports.getElectron = async () => {
|
189 | const pkg = require('./../package.json')
|
190 | const version = pkg.devDependencies.electron.slice(1)
|
191 | const spinner = ora(`Downloading electron: ${version}`).start()
|
192 | const zipPath = await download(version)
|
193 | const electronPath = path.join(path.dirname(zipPath), getPlatformPath())
|
194 | if (!fs.existsSync(electronPath)) {
|
195 | spinner.text = 'Extracting electron to system cache'
|
196 | await extract(zipPath, { dir: path.dirname(zipPath) })
|
197 | }
|
198 | spinner.succeed('Electron ready to use')
|
199 | return electronPath
|
200 | }
|
201 |
|
202 | exports.brotliSize = (path) => {
|
203 | return new Promise((resolve, reject) => {
|
204 | let size = 0
|
205 | const pipe = fs.createReadStream(path).pipe(createBrotliCompress({
|
206 | params: {
|
207 | [constants.BROTLI_PARAM_QUALITY]: 11
|
208 | }
|
209 | }))
|
210 | pipe.on('error', reject)
|
211 | pipe.on('data', buf => {
|
212 | size += buf.length
|
213 | })
|
214 | pipe.on('end', () => {
|
215 | resolve(size)
|
216 | })
|
217 | })
|
218 | }
|
219 |
|
220 | exports.gzipSize = (path) => {
|
221 | return new Promise((resolve, reject) => {
|
222 | let size = 0
|
223 | const pipe = fs.createReadStream(path).pipe(createGzip({ level: 9 }))
|
224 | pipe.on('error', reject)
|
225 | pipe.on('data', buf => {
|
226 | size += buf.length
|
227 | })
|
228 | pipe.on('end', () => {
|
229 | resolve(size)
|
230 | })
|
231 | })
|
232 | }
|