ganeshrvel / tutorial-electron-nodejs-import-packageinfo

Import values from package.json into electron/nodejs application.

Home Page:https://github.com/ganeshrvel/tutorial-electron-nodejs-import-packageinfo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Import values from package.json into electron/nodejs application.

Introduction

I have seen many users importing 'package.json' directly into the node.js project. This is a programming disaster as it could leak your sensitive information such as "scripts" or private TOKENS into the bundled js files.
There is no single clearcut solution to get this done inside an electron app. It has to be handled intelligently using algorithms and fallbacks.
I have spent a significant amount of time researching how to get this done. This was originally implemented inside OpenMTP - Advanced Android File Transfer Application for macOS.

Implementation

  • Install npm packages
$ npm install electron-root-path

or 

$ yarn add electron-root-path
  • Add the below code inside your webpack.config.js file (for both production and development)
import { rootPath } from 'electron-root-path';

const pkg = require(join(rootPath, 'package.json'));

plugins: [
    new webpack.DefinePlugin({
      PKG_INFO: {
        productName: JSON.stringify(pkg.productName),
        description: JSON.stringify(pkg.description),
        name: JSON.stringify(pkg.name),
        author: JSON.stringify(pkg.author),
        version: JSON.stringify(pkg.version),
        repository: JSON.stringify(pkg.repository),
        homepage: JSON.stringify(pkg.homepage)
      }
    }),
  ]
  • Create a file ./app/pkginfo.js and add the below code
'use strict';

import { join } from 'path';
import { readFileSync } from 'fs';
import { rootPath } from 'electron-root-path';

let _pkginfo = {};

// eslint-disable-next-line no-undef
if (typeof PKG_INFO !== 'undefined' && PKG_INFO !== null) {
  // eslint-disable-next-line no-undef
  _pkginfo = PKG_INFO;
} else {
  /* This is a fallback incase the webpack DefinePlugin modules hasn't been initialized yet. */
  /* Developement mode only */
  _pkginfo = JSON.parse(
    readFileSync(join(rootPath, 'package.json'), { encoding: 'utf8' })
  );
}

export const pkginfo = _pkginfo;

Clone

$ git clone --depth 1 --single-branch --branch master https://github.com/ganeshrvel/tutorial-electron-nodejs-import-packageinfo.git

$ cd tutorial-electron-nodejs-import-packageinfo

Contribute

  • Fork the repo and create your branch from master.
  • Ensure that the changes pass linting.
  • Update the documentation if needed.
  • Make sure your code lints.
  • Issue a pull request!

When you submit code changes, your submissions are understood to be under the same MIT License that covers the project. Feel free to contact the maintainers if that's a concern.

Buy me a coffee

Help me keep the app FREE and open for all. Paypal me: paypal.me/ganeshrvel

Contacts

Please feel free to contact me at ganeshrvel@outlook.com

More repos

License

tutorial-electron-nodejs-import-packageinfo is released under MIT License.

Copyright © 2018-Present Ganesh Rathinavel

About

Import values from package.json into electron/nodejs application.

https://github.com/ganeshrvel/tutorial-electron-nodejs-import-packageinfo

License:MIT License