Skip to content

Commit

Permalink
Handle PATH variables
Browse files Browse the repository at this point in the history
* Remove default Windows Comment value
* update deps
  • Loading branch information
TheJaredWilcurt authored May 23, 2021
1 parent 4513620 commit fc4fb12
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 40 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
## Small, lightweight, cross-platform, built in validation!


### Zero Dependencies, 100% Test Coverage, Automated Cross-Platform End-to-End tested
### 100% Test Coverage, Automated Cross-Platform End-to-End tested

An easy, cross-platform, API to create desktop shortcuts with Node. (*Works in [NW.js](https://nwjs.io) too!*)

This library is completely **synchronous**.

It uses 1 dependency, which has it's own single dependency

* **create-desktop-shortcuts** - small - 100% test coverage
* **[which](https://github.com/npm/node-which)** - small - 100% test coverage - used by 9 million projects
* **[isexe](https://github.com/isaacs/isexe)** - very small - used by 9 million projects


## Installation

Expand Down
70 changes: 43 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
"e2e": "node ./tests/e2e.js",
"validate": "npm run lint && npm test && npm run e2e && git status"
},
"dependencies": {
"which": "^2.0.2"
},
"devDependencies": {
"@babel/core": "^7.14.3",
"@babel/eslint-parser": "^7.14.3",
"eslint": "^7.26.0",
"eslint": "^7.27.0",
"eslint-config-tjw-base": "^1.0.0",
"eslint-config-tjw-jest": "^1.0.0",
"eslint-plugin-jsdoc": "^34.8.1",
"eslint-plugin-jsdoc": "^34.8.2",
"fs-extra": "^10.0.0",
"jest": "24.9.0",
"mock-fs": "^4.14.0"
Expand Down
3 changes: 1 addition & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ const library = {
let success = true;

const vbsScript = this.produceWindowsVBSPath();
const filePathName = path.parse(options.windows.filePath).name;
if (!fs.existsSync(vbsScript)) {
helpers.throwError(options, 'Could not locate required "windows.vbs" file.');
success = false;
Expand All @@ -156,7 +155,7 @@ const library = {
let outputPath = options.windows.outputPath;
let filePath = options.windows.filePath;
let args = options.windows.arguments || '';
let comment = options.windows.comment || filePathName;
let comment = options.windows.comment || '';
let cwd = options.windows.workingDirectory || '';
let icon = options.windows.icon;
let windowMode = windowModes[options.windows.windowMode] || windowModes.normal;
Expand Down
21 changes: 21 additions & 0 deletions src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const fs = require('fs');
const path = require('path');
const os = require('os');
const which = require('which');

const helpers = require('./helpers.js');

Expand Down Expand Up @@ -137,6 +138,23 @@ const validation = {
}
return options;
},
/**
* Finds executables in the user's PATH and returns the full filepath to them.
* 'node' becomes 'C:\\Program Files\\nodejs\\node.exe'
* If file does not exist or isn't an executable, returns the original string.
*
* @example
* resolvePATH('node');
*
* @param {string} filePath The executable the shortcut will link to
* @return {string} A resolved path, or the original string
*/
resolvePATH: function (filePath) {
if (filePath) {
return which.sync(filePath, { nothrow: true }) || filePath;
}
return filePath;
},
/**
* Generic validation method to ensure a specific key on a specific OS
* object is a boolean, and if not, give it the correct default value.
Expand Down Expand Up @@ -189,6 +207,7 @@ const validation = {

if (options.linux.filePath) {
options.linux.filePath = helpers.resolveTilde(options.linux.filePath);
options.linux.filePath = this.resolvePATH(options.linux.filePath);
}

options = this.validateLinuxType(options);
Expand Down Expand Up @@ -363,6 +382,7 @@ const validation = {

if (options.windows.filePath) {
options.windows.filePath = helpers.resolveWindowsEnvironmentVariables(options.windows.filePath);
options.windows.filePath = this.resolvePATH(options.windows.filePath);
}

if (
Expand Down Expand Up @@ -573,6 +593,7 @@ const validation = {

if (options.osx.filePath) {
options.osx.filePath = helpers.resolveTilde(options.osx.filePath);
options.osx.filePath = this.resolvePATH(options.osx.filePath);
}

if (
Expand Down
16 changes: 8 additions & 8 deletions tests/src/library.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/file.ext',
'',
'file',
'',
'',
'C:/file.ext',
1,
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/file.ext',
'',
'file',
'',
'',
'C:/file.ext',
1,
Expand Down Expand Up @@ -372,7 +372,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/file.ext',
'',
'file',
'',
'',
'C:/Users/DUMMY/icon.ico',
1,
Expand Down Expand Up @@ -402,7 +402,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/Users/DUMMY/icon.dll',
'',
'icon',
'',
'',
'C:/Users/DUMMY/icon.dll,0',
1,
Expand Down Expand Up @@ -457,7 +457,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'Throw Error',
'',
'Throw Error',
'',
'',
'Throw Error',
1,
Expand Down Expand Up @@ -625,7 +625,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/file.ext',
'',
'file',
'',
'',
'C:/file.ext',
1,
Expand Down Expand Up @@ -747,7 +747,7 @@ describe('library', () => {
'/home/DUMMY/Desktop/file.desktop',
'C:/file.ext',
'',
'file',
'',
'',
'C:/file.ext',
1,
Expand Down Expand Up @@ -797,7 +797,7 @@ describe('library', () => {
'/home/DUMMY/Desktop/file.desktop',
'C:/file.ext',
'',
'file',
'',
'',
'C:/file.ext',
1,
Expand Down
21 changes: 21 additions & 0 deletions tests/src/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('Validation', () => {

afterEach(() => {
testHelpers.restoreMockFs();
testHelpers.restoreEnvPATH();
});

describe('validateOptions', () => {
Expand Down Expand Up @@ -308,6 +309,26 @@ describe('Validation', () => {
});
});

describe('resolvePATH', () => {
beforeEach(() => {
if (process.platform !== 'win32') {
testHelpers.mockPlatform('linux');
}
testHelpers.mockEnvPATH();
mockfs();
});

test('Undefined', () => {
expect(validation.resolvePATH(undefined))
.toEqual(undefined);
});

test('Resolves PATH', async () => {
expect(['/home/DUMMY/app.exe', 'C:\\Program Files\\DUMMY\\app.exe'].includes(validation.resolvePATH('app.exe')))
.toEqual(true);
});
});

describe('defaultBoolean', () => {
beforeEach(() => {
options = {
Expand Down
Loading

0 comments on commit fc4fb12

Please sign in to comment.