Skip to content

soulehshaikh99/windows-environment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Windows Environment Variable Management Library for JS Lovers

🚀 🔭 A powerful library to manage Environment Variables on Windows using Node.JS for Electron and Node.JS Developers.

Install

$ npm i windows-environment

Environment Variable Management operations offered by this library:

  1. Read all Environment Variables of the specified target (User and Machine).
  2. Read values of a specified Environment Variable.
  3. Create a new or append additional values to an already existing Environment Variable.
  4. Delete an Environment Variable by specifying its name.
  5. Delete a single value from an already existing Environment Variable.
  6. Finds broken paths in an Environment Variable (the path which no longer exits on your File System).
  7. Find duplicate values in an Environment Variable.
  8. Lastly optimize an Environment Variable's value which removes any redundancies (duplicate values) and broken paths.

Pros:

  1. Although the code is not written completely in Node.JS, still it guarantees speed and efficiency.
  2. Library size is only about 200 kb.

Cons:

  1. It requires .NET Framework 3.5 Client Profile as it is build upon C# Code.

Read all Environment Variables of the specified target (User and Machine):

// ES6 Destructuring Assignment
const { Env, Target, ExpandedForm } = require('windows-environment');

// Here the return value is an array of objects with name/value pairs.
let returnValues = Env.get({
    //specifies the target i.e. to search for environment variables inside user target
    target: Target.USER,
    //If this is set to no, all the values will be displayed as is.
    //If set to yes then, the values enclosed inside % symbol will take its long form
    //for e.g. %userprofile% will be C:\Users\UserName
    //for e.g. %systemroot% will be C:\Windows
    expandedForm: ExpandedForm.NO
});

//This looks pretty nice, give it a try.
console.table(returnValues);

// As the return value is an array(iterable object) it can be iterated using a for...of loop
for (let iter of returnValues)   {
    //iter is an object having name and value as keys
    console.log('Name : '+iter.name);
    console.log('Value : '+iter.value);
}

Read values of a specified Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, ExpandedForm } = require('windows-environment');

// Here the return value is a string cause the name field is specified.
let pathString = Env.get({
    //specifies the target i.e. to search for environment variables inside machine target
    target: Target.MACHINE,
    //If this is set to no, all the values will be displayed as is.
    //If set to yes then, the values enclosed inside % symbol will take its long form
    //for e.g. %userprofile% will be C:\Users\UserName
    //for e.g. %systemroot% will be C:\Windows
    expandedForm: ExpandedForm.YES,
    //This can be any name from that target like JDK, PATH, GH_TOKEN, TEMP, TMP, etc.
    name: 'PATH'
});

//Outputs the value of 'PATH' environment variable inside Machine target.
console.log(pathString);

Create a new Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, SetOperationType } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.set({
    //specifies the target, where to create the Environment Variable.
    target: Target.USER,
    //forcefully creates a new environment variable.
    //if an environment variable already exists with the same name
    //it simply overwrites it.
    setOperationType: SetOperationType.NEW,
    //the name of Environment Variable you want to create.
    name: 'PATH',
    //the value you want to give it.
    //Note: semicolons (;) are used for separating two paths if you have only single value then no need to use semicolons.
    value: 'F:\\Git\\PortableGit;C:\\Users\\Souleh\\AppData\\Roaming\\npm;C:\\Program Files (x86)\\Yarn\\bin'
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Append values to an already existing Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, SetOperationType } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.set({
    //specifies the target, where to create the Environment Variable.
    target: Target.MACHINE,
    //forcefully creates a new environment variable.
    //if an environment variable already exists with the same name
    //it simply overwrites it.
    setOperationType: SetOperationType.APPEND,
    //the name of Environment Variable you want to modify.
    name: 'PATH',
    //the value you want to give it.
    //Note: semicolons (;) are used for separating two paths if you have only single value then no need to use semicolons.
    value: 'C:\\Program Files\\Java\\jdk1.8.0_202\\bin'
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Delete an Environment Variable by its name:

// ES6 Destructuring Assignment
const { Env, Target } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.del({
    //specifies the target, form where to delete the Environment Variable.
    target: Target.MACHINE,
    //the name of Environment Variable you want to delete.
    name: 'JAVA_HOME',
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Delete a single value from an already existing Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.del({
    //specifies the target, form where to delete the Environment Variable.
    target: Target.USER,
    //the name of Environment Variable's value you want to delete.
    name: 'PATH',
    //Note the value is case-sensitive,
    //if no matching value is found in the specified Environment Variable's value nothing will be deleted.
    value:'F:\\Git\\PortableGit'
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Finds broken paths in an Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, ExtOperationType } = require('windows-environment');

// Here the return value is an array of strings
let returnValues = Env.ext({
    //specifies the target i.e. to search for environment variables inside user target
    target: Target.USER,
    //finds all broken paths
    extOperationType: ExtOperationType.BROKEN,
    //name of the Environment Variable to find broken paths (the path which no longer exits on your File System).
    name: 'MAVEN_HOME'
});

// As the return value is an array(iterable object) it can be iterated using a for...of loop
for(let iter of returnValues)   {
    console.log(iter);
}

Find duplicate values in an Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, ExtOperationType } = require('windows-environment');

// Here the return value is an array of strings
let returnValues = Env.ext({
    //specifies the target i.e. to search for environment variables inside machine target
    target: Target.MACHINE,
    //finds all duplicates
    extOperationType: ExtOperationType.DUPLICATES,
    //name of the Environment Variable to finds duplicates values in.
    name: 'PATH'
});

// As the return value is an array(iterable object) it can be iterated using a for...of loop
for(let iter of returnValues)   {
    console.log(iter);
}

Optimize an Environment Variable's value:

// ES6 Destructuring Assignment
const { Env, Target, ExtOperationType } = require('windows-environment');

// Here the return value is a string
let optimizedPath = Env.ext({
    //specifies the target i.e. to search for environment variables inside user target
    target: Target.USER,
    //returns an optimized string removing any duplicates and broken paths
    extOperationType: ExtOperationType.OPTIMIZE,
    //name of the Environment Variable to optimize.
    name: 'PATH'
});

console.log(optimizedPath);

Made with ❤️ from Souleh

📋 License:

Licensed under the MIT License.

About

🚀 🔭 A powerful library to manage Environment Variables on Windows using Node.JS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published