Skip to content

Commit

Permalink
fix(toolchain): keep the profile and do argparse right
Browse files Browse the repository at this point in the history
did someone forget to run prettier?
  • Loading branch information
Artoria2e5 authored and Misaka-0x447f committed Sep 10, 2019
1 parent 1e6613f commit 68f30a6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
60 changes: 41 additions & 19 deletions config-overrides/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const fs = require('fs')
const { spawn } = require('../scripts/spawn')

const src = file => path.join(__dirname, '../', file)
const argv = require('yargs').argv

const publicDir = src('./public')
const dist = src('./dist')
Expand All @@ -13,11 +15,12 @@ process.env.BROWSER = 'none'

const SSRPlugin = require('./SSRPlugin')
const WebExtPlugin = require('webpack-web-ext-plugin')
const firefoxProfile = path.join(process.cwd(), `.firefox`)

/**
* @type {import("webpack").Configuration}
*/
function override(config, env) {
async function override(config, env) {
// CSP bans eval
// And non-inline source-map not working
if (env === 'development') config.devtool = 'inline-source-map'
Expand Down Expand Up @@ -59,11 +62,30 @@ function override(config, env) {
})
}

if (process.argv.indexOf('--firefox') !== -1) {
config.plugins.push(new WebExtPlugin({ sourceDir: dist }))
if (argv.firefox) {
// HACK: WE SHOULD NOT DO THIS IN OVERRIDE() BUT THERE IS NOWHERE ELSE FOR IT.
if (!fs.existsSync(profile) || argv.fresh) {
try {
const timestamp = Date.now().toString()
await spawn('firefox', ['-CreateProfile', `"${timestamp} ${path.join(profile, timestamp)}"`])
} catch {
throw new Error('Cannot locate or create a profile for firefox. Add firefox to your PATH.')
}
if (argv.fresh) {
console.warn('new profile generated. old firefox profile can be cleaned with "firefox -P".')
}
}
config.plugins.push(
new WebExtPlugin({
sourceDir: dist,
target: 'firefox',
firefoxProfile: path.join(profile, last(await fs.readdir(profile))),
keepProfileChanges: true,
}),
)
}

if (process.argv.indexOf('--firefox-android') !== -1) {
if (argv['firefox-android']) {
config.plugins.push(new WebExtPlugin({ sourceDir: dist, target: 'firefox-android' }))
}

Expand All @@ -77,19 +99,16 @@ function override(config, env) {
config.plugins.push(
new webpack.BannerPlugin(
'Maskbook is a open source project under GNU AGPL 3.0 licence.\n\n\n' +
'More info about our project at https://github.com/DimensionDev/Maskbook\n\n' +
'Maskbook is built on CircleCI, in which all the building process is available to the public.\n\n' +
'We directly take the output to submit to the Web Store. We will integrate the automatic submission\n' +
'into the CircleCI in the near future.',
'More info about our project at https://github.com/DimensionDev/Maskbook\n\n' +
'Maskbook is built on CircleCI, in which all the building process is available to the public.\n\n' +
'We directly take the output to submit to the Web Store. We will integrate the automatic submission\n' +
'into the CircleCI in the near future.',
),
)

// Write files to /public
config.plugins.push(
new (require('copy-webpack-plugin'))(
[{ from: publicDir, to: dist }],
{ ignore: ['index.html'] },
),
new (require('copy-webpack-plugin'))([{ from: publicDir, to: dist }], { ignore: ['index.html'] }),
)
if (env !== 'development') {
config.plugins.push(new SSRPlugin('popup.html', src('./src/extension/popup-page/index.tsx')))
Expand Down Expand Up @@ -144,15 +163,18 @@ function override(config, env) {
return config
}

const last = array => {
const length = array == null ? 0 : array.length
return length ? array[length - 1] : undefined
}

module.exports = {
webpack: override,
devServer: function(configFunction) {
return function(proxy, allowedHost) {
const config = configFunction(proxy, allowedHost)

config.writeToDisk = true

return config;
};
return function(proxy, allowedHost) {
const config = configFunction(proxy, allowedHost)
config.writeToDisk = true
return config
}
},
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"react-devtools": "^3.6.1",
"storybook-addon-material-ui": "^0.9.0-alpha.19",
"ts-node": "^8.3.0",
"web-ext": "^3.1.1"
"web-ext": "^3.1.1",
"yargs": "^14.0.0"
},
"browserslist": {
"production": [
Expand Down
3 changes: 2 additions & 1 deletion scripts/lint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { spawn } = require('./spawn')
const path = require('path')
const argv = require('yargs').argv

const base = path.join(__dirname, '../')
process.chdir(base)

const reportOnly = process.argv.indexOf('--reportOnly') !== -1
const reportOnly = argv.reportOnly

;(async () => {
await spawn('eslint', ['--ignore-path', '.prettierignore', '--ext', 'tsx,ts,jsx,js', './src/', ...reportOnly ? [] : ['--cache', '--fix'] ])
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17458,6 +17458,23 @@ yargs@^13.2.4, yargs@^13.3.0:
y18n "^4.0.0"
yargs-parser "^13.1.1"

yargs@^14.0.0:
version "14.0.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-14.0.0.tgz#ba4cacc802b3c0b3e36a9e791723763d57a85066"
integrity sha512-ssa5JuRjMeZEUjg7bEL99AwpitxU/zWGAGpdj0di41pOEmJti8NR6kyUIJBkR78DTYNPZOU08luUo0GTHuB+ow==
dependencies:
cliui "^5.0.0"
decamelize "^1.2.0"
find-up "^3.0.0"
get-caller-file "^2.0.1"
require-directory "^2.1.1"
require-main-filename "^2.0.0"
set-blocking "^2.0.0"
string-width "^3.0.0"
which-module "^2.0.0"
y18n "^4.0.0"
yargs-parser "^13.1.1"

yargs@^3.19.0:
version "3.32.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.32.0.tgz#03088e9ebf9e756b69751611d2a5ef591482c995"
Expand Down

0 comments on commit 68f30a6

Please sign in to comment.