Skip to content

Commit

Permalink
feat(build,generator): support electron 5.0.0, fixes #250
Browse files Browse the repository at this point in the history
  • Loading branch information
nklayman committed Apr 25, 2019
1 parent 6a5e2fb commit 34e80e1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 21 deletions.
4 changes: 3 additions & 1 deletion __tests__/createProject.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const createProject = (projectName, useTS, customPlugins = {}) =>
projectName += '-ts'
}
// Install vcp-electron-builder
preset.plugins['vue-cli-plugin-electron-builder'] = {}
preset.plugins['vue-cli-plugin-electron-builder'] = {
electronBuilder: { electronVersion: '^4.0.0' }
}
preset.plugins = { ...preset.plugins, ...customPlugins }
const projectPath = p =>
path.join(process.cwd(), '__tests__/projects/' + projectName, p)
Expand Down
10 changes: 8 additions & 2 deletions __tests__/generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ beforeEach(() => {
// Check that utf8 encoding is set
expect(encoding).toBe('utf8')
if (path === 'apiResolve_./package.json') {
return JSON.stringify({ scripts: {} })
return JSON.stringify({
scripts: {},
devDependencies: { electron: '^5.0.0' }
})
}
// return mock content
return 'existing_content'
Expand Down Expand Up @@ -73,7 +76,10 @@ describe('.gitignore', () => {
// Check that utf8 encoding is set
expect(encoding).toBe('utf8')
if (path === 'apiResolve_./package.json') {
return JSON.stringify({ scripts: {} })
return JSON.stringify({
scripts: {},
devDependencies: { electron: '^5.0.0' }
})
}
// return mock content
return `ignore-this\n${existing}\nignore-that`
Expand Down
24 changes: 18 additions & 6 deletions generator/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const fs = require('fs')
const semver = require('semver')

module.exports = (api, options = {}) => {
if (!options.electronBuilder) options.electronBuilder = {}
const electronVersion = options.electronBuilder.electronVersion
let pkg = fs.readFileSync(api.resolve('./package.json'), 'utf8')
pkg = JSON.parse(pkg)
const usesTS = api.hasPlugin('typescript')
const hasBackground =
fs.existsSync(api.resolve(`./src/background.ts`)) ||
fs.existsSync(api.resolve(`./src/background.js`))

if (!hasBackground) {
// If user does not have a background file it should be created
api.render('./templates/base')
api.render('./templates/base', {
// Scheme registration changed in Electron 5.0.0
newSchemeRegistrationFunction: semver.gte(
(electronVersion || pkg.devDependencies.electron).replace('^', ''),
'5.0.0'
)
})
}
// Add tests
let testFramework
Expand Down Expand Up @@ -44,7 +55,10 @@ module.exports = (api, options = {}) => {
/process\.env\.WEBPACK_DEV_SERVER_URL\s*?\)$/m,
'process.env.WEBPACK_DEV_SERVER_URL as string)'
)
background = background.replace(/let win\s*?$/m, 'let win: BrowserWindow | null')
background = background.replace(
/let win\s*?$/m,
'let win: BrowserWindow | null'
)
fs.writeFileSync(api.resolve('./src/background.ts'), background)
if (api.hasPlugin('router')) {
console.log('\n')
Expand All @@ -60,8 +74,6 @@ module.exports = (api, options = {}) => {
'electron:build': 'vue-cli-service electron:build',
'electron:serve': 'vue-cli-service electron:serve'
}
let pkg = fs.readFileSync(api.resolve('./package.json'), 'utf8')
pkg = JSON.parse(pkg)
const addScript = (name, command) => {
// Add on to existing script if it exists
if (pkg.scripts && pkg.scripts[name]) {
Expand All @@ -81,9 +93,9 @@ module.exports = (api, options = {}) => {
addScript('postinstall', 'electron-builder install-app-deps')
addScript('postuninstall', 'electron-builder install-app-deps')
const devDependencies = {}
if (options.electronBuilder.electronVersion) {
if (electronVersion) {
// Use provided electron version
devDependencies.electron = options.electronBuilder.electronVersion
devDependencies.electron = electronVersion
}
const dependencies = {}
if (testFramework === 'mocha') {
Expand Down
10 changes: 7 additions & 3 deletions generator/templates/base/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ const isDevelopment = process.env.NODE_ENV !== 'production'
// be closed automatically when the JavaScript object is garbage collected.
let win

// Standard scheme must be registered before the app is ready
protocol.registerStandardSchemes(['app'], { secure: true })
// Scheme must be registered before the app is ready
<% if (newSchemeRegistrationFunction) { %>protocol.registerSchemesAsPrivileged([{scheme: 'app', secure: true }])
<% } else { %>protocol.registerStandardSchemes(['app'], { secure: true })
<% } %>
function createWindow () {
// Create the browser window.
win = new BrowserWindow({ width: 800, height: 600 })
win = new BrowserWindow({ width: 800, height: 600, webPreferences: {
nodeIntegration: true
} })

if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"lodash.merge": "^4.6.1",
"portfinder": "^1.0.16",
"pumpify": "^1.5.1",
"semver": "^6.0.0",
"shebang-loader": "^0.0.1",
"spectron": "^5.0.0",
"split2": "^3.0.0",
Expand Down
12 changes: 6 additions & 6 deletions prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ module.exports = [
name: 'electronBuilder.electronVersion',
type: 'list',
message: 'Choose Electron Version',
default: '^4.0.0',
default: '^5.0.0',
choices: [
{
name: '^2.0.0',
value: '^2.0.0',
short: '^2.0.0'
},
{
name: '^3.0.0',
value: '^3.0.0',
Expand All @@ -21,6 +16,11 @@ module.exports = [
name: '^4.0.0',
value: '^4.0.0',
short: '^4.0.0'
},
{
name: '^5.0.0',
value: '^5.0.0',
short: '^5.0.0'
}
],
when: () => {
Expand Down
11 changes: 8 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5715,9 +5715,9 @@ electron-to-chromium@^1.2.7, electron-to-chromium@^1.3.116, electron-to-chromium
integrity sha512-NKwKAXzur5vFCZYBHpdWjTMO8QptNLNP80nItkSIgUOapPAo9Uia+RvkCaZJtO7fhQaVElSvBPWEc2ku6cKsPA==

electron@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/electron/-/electron-4.1.0.tgz#ecba9c83de271e8ba7637332ece9ed023c6ea3f0"
integrity sha512-q/yTi9dT5UEFK/s+vOQaHNkTHWiRK9kEBYVJt34nmWc9piW42hXT+nhKUEHHhccMPr3q18gG0iPZqeR+LG76ow==
version "4.1.5"
resolved "https://registry.yarnpkg.com/electron/-/electron-4.1.5.tgz#d1873147d5be36ab0c8bb9db3a54297a82ee8fef"
integrity sha512-0VZzUd4vZaUeSLdxJI/XMrMnPN7AROjPFZOiNgZZkYRUUEjGHfaSAbCJyxuXtii52KGhzGL0JgW0q5QmQ3ykKQ==
dependencies:
"@types/node" "^10.12.18"
electron-download "^4.1.0"
Expand Down Expand Up @@ -12760,6 +12760,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"
integrity sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==

semver@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.0.0.tgz#05e359ee571e5ad7ed641a6eec1e547ba52dea65"
integrity sha512-0UewU+9rFapKFnlbirLi3byoOuhrSsli/z/ihNnvM24vgF+8sNBiI1LZPBSH9wJKUwaUbw+s3hToDLCXkrghrQ==

[email protected]:
version "0.16.2"
resolved "https://registry.yarnpkg.com/send/-/send-0.16.2.tgz#6ecca1e0f8c156d141597559848df64730a6bbc1"
Expand Down

0 comments on commit 34e80e1

Please sign in to comment.