What is the best way to configure the pkg option scripts in electron-builder? - node.js

I am using the pkg option in my package.json configuration for electron-builder. I have some preinstall scripts that I want to run upon installation. I have followed the electron-builder instructions in order to set where the scripts are, but npm can't seem to find the files correctly.
I have tried changing ownership of files and folders with chmod +x file command, but the error persits.
This is the bit of code about pkg scripts configuration:
"build": {
"mac": {
"target":[
"pkg"
]
},
"pkg":{
"scripts":"build/pkg-scripts"
}
}
The error message I get is:
Error: Exit code: 1. Command failed: pkgbuild --root /Users/user/myApp/myApp-out/electron-builder/mac --component-plist /Users/user/myApp/myApp/electron-builder/com.dessci.myApp.plist --install-location /Applications --scripts /Users/user/myApp/build/build/pkg-scripts /Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg
pkgbuild: error: Cannot write package to "/Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg". (The file “pkg-scripts” couldn’t be opened.)
pkgbuild: Reading components from /Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.plist
pkgbuild: Adding component at myApp.app
pkgbuild: error: Cannot write package to "/Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg". (The file “pkg-scripts” couldn’t be opened.)
at /Users/user/mtdesktop/node_modules/builder-util/src/util.ts:126:16
at ChildProcess.exithandler (child_process.js:306:5)
at ChildProcess.emit (events.js:193:13)
at maybeClose (internal/child_process.js:999:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:266:5)
From previous event:
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:111:16
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:45:10
at FSReqCallback.args [as oncomplete] (fs.js:145:20)
From previous event:
at PkgTarget.buildComponentPackage (/Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:115:113)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:57:12
at Generator.next (<anonymous>)
From previous event:
at PkgTarget.build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:31:42)
at Function.buildAsyncTargets (/Users/user/mtdesktop/node_modules/app-builder-lib/src/platformPackager.ts:140:36)
at MacPackager.packageInDistributableFormat (/Users/user/mtdesktop/node_modules/app-builder-lib/src/platformPackager.ts:119:24)
at nonMasPromise.then.then (/Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:90:26)
at processImmediate (internal/timers.js:443:21)
From previous event:
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:90:10
at Generator.next (<anonymous>)
From previous event:
at MacPackager.pack (/Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:80:95)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:430:24
at Generator.next (<anonymous>)
at xfs.stat (/Users/user/mtdesktop/node_modules/fs-extra-p/node_modules/fs-extra/lib/mkdirs/mkdirs.js:56:16)
at /Users/user/mtdesktop/node_modules/graceful-fs/polyfills.js:285:20
at FSReqCallback.oncomplete (fs.js:159:5)
From previous event:
at Packager.doBuild (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:396:24)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:366:57
at Generator.next (<anonymous>)
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:111:16
at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:45:10
at FSReqCallback.args [as oncomplete] (fs.js:145:20)
From previous event:
at Packager._build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:335:133)
at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:331:23
at Generator.next (<anonymous>)
at processImmediate (internal/timers.js:443:21)
From previous event:
at Packager.build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:288:14)
at build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/index.ts:59:28)
at build (/Users/user/mtdesktop/node_modules/electron-builder/src/builder.ts:227:10)
at then (/Users/user/mtdesktop/node_modules/electron-builder/src/cli/cli.ts:46:19)

You need to provide the relative path of pkg-scripts for scripts value.
package.json
"build": {
"appId": "com.audio.application",
"productName": "Audio-App",
"artifactName": "${productName}-Setup-${version}.${ext}",
"copyright": "Copyright © 2020 Audio Corp",
"mac": {
"category": "com.audio.application",
"target": [
"pkg"
],
"icon": "dist",
"identity": "identity",
"darkModeSupport": true,
"hardenedRuntime": true,
"gatekeeperAssess": false,
"artifactName": "${productName}.${ext}"
},
"pkg": {
"scripts": "../build/pkg-scripts",
"installLocation": "/Applications",
"background": {
"file": "build/icon/sound.png",
"alignment": "bottomleft"
},
"allowAnywhere": true,
"allowCurrentUserHome": true,
"allowRootDirectory": true,
"license": "build/license.html",
"welcome": "build/resources/welcome.txt",
"conclusion": "build/resources/conclusion.txt",
"isVersionChecked": true,
"isRelocatable": false,
"overwriteAction": "upgrade"
},
"directories": {
"buildResources": "release",
"output": "release"
}
},
folder structure of build directory -
(which at root level of project)
preinstall.sh -
(make sure you have run chmod +x on this file to make it executable)
#!/bin/sh
echo "Executing preinstaller script for custom installer"
# Deleting App components from Application folder.
echo "Deleting Audio-Configration Logs"
logsPath=~/Library/Logs/Audio-Configration
if [ -d "$logsPath" ]
then
rm -rf ~/Library/Logs/Audio-Configration
echo "***Deleted Audio-Configration Logs Successfully***"
fi
echo "Deleting Audio-Configration Application Support"
applicationSupportPath=~/Library/Application\ Support/Audio-Configration
if [ -d "$applicationSupportPath" ]
then
rm -rf ~/Library/Application\ Support/Audio-Configration
echo "***Deleted Audio-Configration Application Support Successfully***"
fi
echo "Finished:preflight"
exit 0
Above pre-installation script is to make sure when user re-install the app, need to delete the previous Logs and application related content in Application Support directory

"scripts":"build/pkg-scripts"
You don't need this line as it is a default directory for storing scripts commands, in the case you have define it, you need to create a directory name in ../build/pkg-scripts/ in your app directory and only file name "preinstall" and "postinstall" are allowed in this directory, otherwise electron-builder will get error why running the release function.

Related

I can't run commands from nodejs as child_process, or read the file system in an electron application with snap package configuration

I have working on an electron desktop application, the app is quite simple its about building a file browser for Linux. From Nodejs apis I use child_process, fs, path, os, and so on.
I am using electron-builder to package and build the application.
When I build for linux with target like "zip", "deb", "rpm", the application works as expected.
But when I compile for snap. I can't run commands like:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
await exec('which code');
.....
await openExternalApp('code -n', `"${file.path}"`)
.....
async function openExternalApp(cmd, path) {
const util = require('util');
const exec = util.promisify(require('child_process').exec);
await exec(`${cmd} ${path}`);
}
.....
I got an error:
Error: Command failed: which code
at ChildProcess.exithandler (node:child_process:406:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Socket.<anonymous> (node:internal/child_process:450:11)
at Socket.emit (node:events:390:28)
at Pipe.<anonymous> (node:net:687:12) {
killed: false,
code: 1,
signal: null,
cmd: 'which code',
stdout: '',
stderr: ''
}
Error: Command failed: which code
Also in my program when i have to read directories of the file system related with hard drive or another location simply i can't.
I've started to study a little bit the docs in snap store and I realized that you have to configure the slot, layout and slugs to access the whole file system, and perhaps to run bash or shell command like the one above.
After several configuration i continue with the same issues. Here is my configuration for snap in pakage.json
"snap": {
"plugs": [
"desktop",
"desktop-legacy",
"home",
"x11",
"unity7",
"browser-support",
"network",
"gsettings",
"opengl",
"block-devices",
"classic-support",
"hardware-observe",
"home",
"system-backup",
"system-observe",
"process-control",
"hostname-control",
"removable-media",
{
"system-files": {
"read": [
"/etc",
"/usr",
"/home",
"/media",
"/mnt",
"/var",
"/temp",
"/opt",
"/sys",
"/dev",
"/bin",
"/snap"
],
"write": [
"/home",
"/mnt"
]
}
}
],
"desktop": {
"Encoding": "UTF-8",
"Icon": "${SNAP}/icon.png"
}
}
snap image config in pakage.json

Nuxt installation error : Rule can only have one resource source (provided resource and test + include + exclude)

I successfully installed Vuejs and Nodejs but got a problem when installing Nuxtjs. This is what I get. I already asked some friends but it didn't work. Thanks for your help ! :)
Error : Rule can only have one resource source (provided resource and test + include + exclude)
Rule can only have one resource source (provided resource and test + include + exclude) in {
"use": [
{
"loader": "C:\\Users\\User\\Desktop\\JS\\my-first-project\\node_modules\\babel-loader\\lib\\index.js",
"options": {
"configFile": false,
"babelrc": false,
"cacheDirectory": true,
"envName": "server",
"presets": [
[
"C:\\Users\\User\\Desktop\\JS\\my-first-project\\node_modules\\#nuxt\\babel-preset-app\\src\\index.js",
{
"corejs": {
"version": 3
}
}
]
]
},
"ident": "clonedRuleSet-30[0].rules[0].use[0]"
}
]
}
"use": [
{
"loader": "C:\\Users\\User\\Desktop\\JS\\my-first-project\\node_modules\\babel-loader\\lib\\index.js",
"options": {
"configFile": false,
"babelrc": false,
"cacheDirectory": true,
"envName": "server",
"presets": [
[
"C:\\Users\\User\\Desktop\\JS\\my-first-project\\node_modules\\#nuxt\\babel-preset-app\\src\\index.js",
{
"corejs": {
"version": 3
}
}
]
]
},
"ident": "clonedRuleSet-30[0].rules[0].use[0]"
}
]
}
at checkResourceSource (node_modules\#nuxt\webpack\node_modules\webpack\lib\RuleSet.js:167:11)
at Function.normalizeRule (node_modules\#nuxt\webpack\node_modules\webpack\lib\RuleSet.js:198:4)
at node_modules\#nuxt\webpack\node_modules\webpack\lib\RuleSet.js:110:20
at Array.map (<anonymous>)
at Function.normalizeRules (node_modules\#nuxt\webpack\node_modules\webpack\lib\RuleSet.js:109:17)
at new RuleSet (node_modules\#nuxt\webpack\node_modules\webpack\lib\RuleSet.js:104:24)
at new NormalModuleFactory (node_modules\#nuxt\webpack\node_modules\webpack\lib\NormalModuleFactory.js:115:18)
at Compiler.createNormalModuleFactory (node_modules\#nuxt\webpack\node_modules\webpack\lib\Compiler.js:636:31)
at Compiler.newCompilationParams (node_modules\#nuxt\webpack\node_modules\webpack\lib\Compiler.js:653:30)
at Compiler.compile (node_modules\#nuxt\webpack\node_modules\webpack\lib\Compiler.js:661:23)
at node_modules\#nuxt\webpack\node_modules\webpack\lib\Watching.js:77:18
at AsyncSeriesHook.eval [as callAsync] (eval at create (node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:22:1)
at AsyncSeriesHook.lazyCompileHook (node_modules\tapable\lib\Hook.js:154:20)
at Watching._go (node_modules\#nuxt\webpack\node_modules\webpack\lib\Watching.js:41:32)
at node_modules\#nuxt\webpack\node_modules\webpack\lib\Watching.js:33:9
at Compiler.readRecords (node_modules\#nuxt\webpack\node_modules\webpack\lib\Compiler.js:529:11)
npm i -D webpack#^4.46.0 try this, it worked for me.
I've had the same issue today, it seems to be related to an npm dependencies resolution issue.
I have opened an issue in nuxt.js repository
In my project, the issue was present, cause of #nuxtjs/eslint-module, you can remove it and regen dependencies :
npm uninstall #nuxtjs/eslint-module
rm -rf node_modules package-lock.json
npm install
You will not longer have eslint feedbacks in your build command, but you can still use npm run lint, and you will be able to use nuxt until the issue will be fixed.
I ran into this same error while trying to upgrade one of my old NuxtJS projects (using sass) built on node version 12 to version 16.
To fix this, i also installed #nuxtjs/style-resources that matches my versions of sass-loader and node-sass.
To confirm, uninstall the ones you already have, and run
npm install --save-dev node-sass sass-loader#10 fibers #nuxtjs/style-resources
see this article for more
This happened to me when I installed the most recent version of copy-webpack-plugin in Nuxt v2 project. Apparently it doesn't use webpack5 so I had to downgrade copy-webpack-plugin to last compatible version e.g. copy-webpack-plugin#4.6.0

Starting vue js dev server gives eror: .plugins[0] may only be a two-tuple or three-tuple

I am using Vue CLI version 3 to run a Vue application inside a Docker container. To start the development server, I run:
https://cli.vuejs.org/guide/cli-service.html
This gives the following error:
ERROR Failed to compile with 1 errors 17:05:14
error in ./app/main.js
Module build failed (from ./node_modules/#vue/cli-plugin-babel/node_modules/babel-loader/lib/index.js):
Error: .plugins[0] may only be a two-tuple or three-tuple
at assertPluginItem (/home/src/node_modules/#babel/core/lib/config/validation/option-assertions.js:235:13)
at arr.forEach (/home/src/node_modules/#babel/core/lib/config/validation/option-assertions.js:222:30)
at Array.forEach (<anonymous>)
at assertPluginList (/home/src/node_modules/#babel/core/lib/config/validation/option-assertions.js:222:9)
at Object.keys.forEach.key (/home/src/node_modules/#babel/core/lib/config/validation/options.js:107:5)
at Array.forEach (<anonymous>)
at validateNested (/home/src/node_modules/#babel/core/lib/config/validation/options.js:83:21)
at validate (/home/src/node_modules/#babel/core/lib/config/validation/options.js:74:10)
at file (/home/src/node_modules/#babel/core/lib/config/config-chain.js:174:34)
at cachedFunction (/home/src/node_modules/#babel/core/lib/config/caching.js:33:19)
at buildRootChain (/home/src/node_modules/#babel/core/lib/config/config-chain.js:120:36)
at loadPrivatePartialConfig (/home/src/node_modules/#babel/core/lib/config/partial.js:85:55)
at Object.loadPartialConfig (/home/src/node_modules/#babel/core/lib/config/partial.js:110:18)
at Object.<anonymous> (/home/src/node_modules/#vue/cli-plugin-babel/node_modules/babel-loader/lib/index.js:140:26)
at Generator.next (<anonymous>)
at asyncGeneratorStep (/home/src/node_modules/#vue/cli-plugin-babel/node_modules/babel-loader/lib/index.js:3:103)
# multi (webpack)-dev-server/client/index.js ./node_modules/#vue/cli-service/node_modules/webpack/hot/dev-server.js ./app/main.js
I suspect that the offending code is the following, in my .babelrc file:
"plugins": [
["transform-runtime", "transform-vue-jsx", "transform-regenerator", {
"polyfill": false,
"regenerator": true
}]
Could someone suggest how I could go about resolving this? Thanks!
Your [ is in the wrong place. It should be
"plugins": [
"transform-runtime",
"transform-vue-jsx",
["transform-regenerator", {
"polyfill": false,
"regenerator": true
}]
]

Error keeps popping up: Configuration for rule "react/jsx-uses-react" is invalid

When I save any js file in Sublime text 3 the error pasted below keeps popping up.
I have tried a reinstall of eslint, eslint-plugin-import, eslint-config-airbnb, eslint-plugin-react, but no results.
any ideas?
error: Error: C:\Users\user_name\node_modules\eslint-config-airbnb\rules\react.js:
Configuration for rule "react/jsx-uses-react" is invalid:
Value "[object Object]" should NOT have more than 0 items.
Referenced from: C:\Users\user_name\node_modules\eslint-config-airbnb\index.js
Referenced from: C:\Users\user_name\.eslintrc
Error: C:\Users\user_name\node_modules\eslint-config-airbnb\rules\react.js:
Configuration for rule "react/jsx-uses-react" is invalid:
Value "[object Object]" should NOT have more than 0 items.
Referenced from: C:\Users\user_name\node_modules\eslint-config-airbnb\index.js
Referenced from: C:\Users\user_name\.eslintrc
at validateRuleOptions (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-validator.js:113:15)
at Object.keys.forEach.id (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-validator.js:153:9)
at Array.forEach (native)
at validateRules (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-validator.js:152:30)
at Object.validate (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-validator.js:230:5)
at loadFromDisk (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-file.js:549:19)
at load (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-file.js:592:20)
at configExtends.reduceRight.e (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-file.js:421:36)
at Array.reduceRight (native)
at applyExtends (C:\Users\user_name\Dev\node_modules\eslint\lib\config\config-file.js:403:28)
(node:11548) DeprecationWarning: [eslint] The 'ecmaFeatures' config file property is deprecated, and has no effect. (found in C:\Users\user_name\node_modules\eslint-config-airbnb\rules\react.js)
I had this problem with Atom. You need to configure eslint to support jsx. Add this to your .eslintrc file. Particularly the parserOptions field! Make sure you have eslint-plugin-react installed properly as well.
eslint-plugin-react on github
{
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaVersion": 6,
"ecmaFeatures": {
"jsx": true
}
},
"plugins": [
"react"
],
}

ImageMagick 7.0.4 / NodeJS 6.9.2 / GPLGS 9.15 / Win 7: Failing to convert files

Problem
I am able to do a conversion from PDF to another image format from the command-line using the convert.exe ImageMagick command. However, when running the following JS in Node I get the error that follows the code below.
JavaScript
fs = require('fs');
var PDFImage = require("pdf-image").PDFImage;
console.log("Start");
var pdfImage = new PDFImage('test.pdf');
pdfImage.convertPage(0).then(function (imagePath) {
// 0-th page (first page) of the slide.pdf is available as slide-0.png
console.log('Converted');
fs.existsSync('test-0.png') // => true
}, function (err) {
console.log(err);
});
package.json
{
"name": "pdftester",
"version": "1.0.0",
"description": "PDF Tester",
"main": "PDFTester.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "John Doe",
"license": "ISC",
"dependencies": {
"async": "^2.1.4",
"imagemagick": "^0.1.3",
"pdf-image": "^1.1.0",
"pdftotextjs": "^0.4.0"
}
}
Results
Start
{ message: 'Failed to convert page to image',
error:
{ Error: Command failed: convert 'test.pdf[0]' 'test-0.png'
convert: unable to open image ''test.pdf[0]'': No such file or directory # error/blob.c/OpenBlob/2691.
convert: unable to open module file 'C:\ImageMagick-7.0.4-Q16\modules\coders\IM_MOD_RL_PDF[0]'_.dll': No such file or directory # warning/module.c/GetMagickModulePath/680.
convert: no decode delegate for this image format `PDF[0]'' # error/constitute.c/ReadImage/509.
convert: no images defined `'test-0.png'' # error/convert.c/ConvertImageCommand/3254.
at ChildProcess.exithandler (child_process.js:206:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
killed: false,
code: 1,
signal: null,
cmd: 'convert \'test.pdf[0]\' \'test-0.png\'' },
stdout: '',
stderr: 'convert: unable to open image \'\'test.pdf[0]\'\': No such file or directory # error/blob.c/OpenBlob/2691.\r\nconvert: unable to open module file \'C:\\ImageMagick-7.0.4-Q16\\modules\\coders\\IM_MOD_RL_PDF[0]\'_.dll\': No such file or directory # warning/module.c/GetMagickModulePath/680.\r\nconvert: no decode delegate for this image format `PDF[0]\'\' # error/constitute.c/ReadImage/509.\r\nconvert: no images defined `\'test-0.png\'\' # error/convert.c/ConvertImageCommand/3254.\r\n' }
Analysis
The offending line is the command-line built for convert.exe, that is:
'convert \'test.pdf[0]\' \'test-0.png\''
What is adding the extra slashes, ticks ('), and '[0]'?
Thanks in advance!
according to your question , extra slashes denotes to enter in folder.
you have to put your file with respect to server file from where it starts. or put your pdf file into from where your running your apps.

Resources