Skip to content

Commit

Permalink
fix: install plugins with deps and rebuild native if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
IT-MikeS committed May 17, 2021
1 parent 84ebd89 commit 27a20ed
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"strict": false,
"target": "ES2017"
},
"include": ["src/index.ts"]
"include": ["src/**/*"]
}
2 changes: 1 addition & 1 deletion plugin-example/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"strict": false,
"target": "ES2017"
},
"include": ["src/index.ts"]
"include": ["src/**/*"]
}
```
8. Modify the main `package.json` in the root directory, add a property to the `capacitor` object so it reflects the following (android and ios shown for example only):
Expand Down
40 changes: 24 additions & 16 deletions src/cli-scripts/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
resolveElectronPlugin,
hashJsFileName,
getCwd,
runExec
} from "./common";


Expand All @@ -32,23 +33,36 @@ export async function doUpdate() {
// Filter out null returns
plugins = plugins.filter((p) => !!p);
// Get only the ones with electron "native" plugins
let pluginMap: {name: string; path: string | null}[] = plugins.map((plugin) => {
let pluginMap: {name: string; path: string | null; installStr: string; id: string}[] = plugins.map((plugin) => {
const installStr = `${plugin!.id}@${plugin!.version}`
const path = resolveElectronPlugin(plugin!)
const name = plugin!.name
return {name, path}
const id = plugin!.id
return {name, path, installStr, id}
}).filter(plugin => plugin.path !== null);

let npmIStr = ''

let outStr = `/* eslint-disable @typescript-eslint/no-var-requires */\n`;
for (const electronPlugin of pluginMap) {
outStr += `const ${electronPlugin.name} = require('${electronPlugin.path!.replace(/\\/g, '\\\\')}')\n`
npmIStr += ` ${electronPlugin.installStr}`
const tmpPath = join(
usersProjectDir,
"electron",
"node_modules",
electronPlugin.id,
"electron",
"dist/plugin.js"
)
outStr += `const ${electronPlugin.name} = require('${tmpPath.replace(/\\/g, '\\\\')}')\n`
}
outStr += '\nmodule.exports = {\n'
for (const electronPlugin of pluginMap) {
outStr += ` ${electronPlugin.name},\n`
}
outStr += '}'

console.log('\n' + outStr + '\n\n')
// console.log('\n' + outStr + '\n\n')

const capacitorElectronRuntimeFilePath = join(
usersProjectDir,
Expand All @@ -59,21 +73,15 @@ export async function doUpdate() {
"dist",
"runtime"
);

/*
let rtFileContents = readFileSync(capacitorElectronRuntimeFilePath, {encoding: 'utf-8'}).toString()
console.log(rtFileContents + '\n\n')
rtFileContents = rtFileContents.replace(/(\/\/--S--\/\/\n\/\/--E--\/\/)|(\/\/--S--\/\/\n(.|\n)*\n\/\/--E--\/\/)/g, outStr)
console.log(rtFileContents + '\n\n')
*/

console.log(join(capacitorElectronRuntimeFilePath, 'electron-plugins.js'))
// console.log(join(capacitorElectronRuntimeFilePath, 'electron-plugins.js'))

writeFileSync(join(capacitorElectronRuntimeFilePath, 'electron-plugins.js'), outStr, {encoding: 'utf-8'})


if (npmIStr.length > 0) {
console.log(`\n\nWill install:${npmIStr}\n\n`)
await runExec(`cd ${join(usersProjectDir, "electron")} && npm i${npmIStr} && npm run rebuild-deps`);
}
} catch (e) {
throw e;
}
Expand Down
10 changes: 6 additions & 4 deletions template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
"electron:start": "npm run build && electron ./",
"electron:pack": "npm run build && electron-builder build --dir",
"electron:build-windows": "npm run build && electron-builder build --windows",
"electron:build-mac": "npm run build && electron-builder build --mac"
"electron:build-mac": "npm run build && electron-builder build --mac",
"rebuild-deps": "electron-rebuild"
},
"dependencies": {
"@capacitor-community/electron": "^3.0.0-beta.3"
"@capacitor-community/electron": "^3.0.0-beta.5"
},
"devDependencies": {
"electron": "~12.0.5",
"electron": "~12.0.6",
"electron-builder": "~22.9.1",
"typescript": "~4.0.5"
"typescript": "~4.0.5",
"electron-rebuild": "~2.3.5"
},
"keywords": [
"capacitor",
Expand Down

0 comments on commit 27a20ed

Please sign in to comment.