Skip to content

Commit fc4fb12

Browse files
Handle PATH variables
* Remove default Windows Comment value * update deps
1 parent 4513620 commit fc4fb12

File tree

8 files changed

+149
-40
lines changed

8 files changed

+149
-40
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
## Small, lightweight, cross-platform, built in validation!
88

99

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

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

1414
This library is completely **synchronous**.
1515

16+
It uses 1 dependency, which has it's own single dependency
17+
18+
* **create-desktop-shortcuts** - small - 100% test coverage
19+
* **[which](https://github.com/npm/node-which)** - small - 100% test coverage - used by 9 million projects
20+
* **[isexe](https://github.com/isaacs/isexe)** - very small - used by 9 million projects
21+
1622

1723
## Installation
1824

package-lock.json

Lines changed: 43 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
"e2e": "node ./tests/e2e.js",
2525
"validate": "npm run lint && npm test && npm run e2e && git status"
2626
},
27+
"dependencies": {
28+
"which": "^2.0.2"
29+
},
2730
"devDependencies": {
2831
"@babel/core": "^7.14.3",
2932
"@babel/eslint-parser": "^7.14.3",
30-
"eslint": "^7.26.0",
33+
"eslint": "^7.27.0",
3134
"eslint-config-tjw-base": "^1.0.0",
3235
"eslint-config-tjw-jest": "^1.0.0",
33-
"eslint-plugin-jsdoc": "^34.8.1",
36+
"eslint-plugin-jsdoc": "^34.8.2",
3437
"fs-extra": "^10.0.0",
3538
"jest": "24.9.0",
3639
"mock-fs": "^4.14.0"

src/library.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ const library = {
140140
let success = true;
141141

142142
const vbsScript = this.produceWindowsVBSPath();
143-
const filePathName = path.parse(options.windows.filePath).name;
144143
if (!fs.existsSync(vbsScript)) {
145144
helpers.throwError(options, 'Could not locate required "windows.vbs" file.');
146145
success = false;
@@ -156,7 +155,7 @@ const library = {
156155
let outputPath = options.windows.outputPath;
157156
let filePath = options.windows.filePath;
158157
let args = options.windows.arguments || '';
159-
let comment = options.windows.comment || filePathName;
158+
let comment = options.windows.comment || '';
160159
let cwd = options.windows.workingDirectory || '';
161160
let icon = options.windows.icon;
162161
let windowMode = windowModes[options.windows.windowMode] || windowModes.normal;

src/validation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const fs = require('fs');
99
const path = require('path');
1010
const os = require('os');
11+
const which = require('which');
1112

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

@@ -137,6 +138,23 @@ const validation = {
137138
}
138139
return options;
139140
},
141+
/**
142+
* Finds executables in the user's PATH and returns the full filepath to them.
143+
* 'node' becomes 'C:\\Program Files\\nodejs\\node.exe'
144+
* If file does not exist or isn't an executable, returns the original string.
145+
*
146+
* @example
147+
* resolvePATH('node');
148+
*
149+
* @param {string} filePath The executable the shortcut will link to
150+
* @return {string} A resolved path, or the original string
151+
*/
152+
resolvePATH: function (filePath) {
153+
if (filePath) {
154+
return which.sync(filePath, { nothrow: true }) || filePath;
155+
}
156+
return filePath;
157+
},
140158
/**
141159
* Generic validation method to ensure a specific key on a specific OS
142160
* object is a boolean, and if not, give it the correct default value.
@@ -189,6 +207,7 @@ const validation = {
189207

190208
if (options.linux.filePath) {
191209
options.linux.filePath = helpers.resolveTilde(options.linux.filePath);
210+
options.linux.filePath = this.resolvePATH(options.linux.filePath);
192211
}
193212

194213
options = this.validateLinuxType(options);
@@ -363,6 +382,7 @@ const validation = {
363382

364383
if (options.windows.filePath) {
365384
options.windows.filePath = helpers.resolveWindowsEnvironmentVariables(options.windows.filePath);
385+
options.windows.filePath = this.resolvePATH(options.windows.filePath);
366386
}
367387

368388
if (
@@ -573,6 +593,7 @@ const validation = {
573593

574594
if (options.osx.filePath) {
575595
options.osx.filePath = helpers.resolveTilde(options.osx.filePath);
596+
options.osx.filePath = this.resolvePATH(options.osx.filePath);
576597
}
577598

578599
if (

tests/src/library.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ describe('library', () => {
314314
'C:/Users/DUMMY/Desktop/file.lnk',
315315
'C:/file.ext',
316316
'',
317-
'file',
317+
'',
318318
'',
319319
'C:/file.ext',
320320
1,
@@ -343,7 +343,7 @@ describe('library', () => {
343343
'C:/Users/DUMMY/Desktop/file.lnk',
344344
'C:/file.ext',
345345
'',
346-
'file',
346+
'',
347347
'',
348348
'C:/file.ext',
349349
1,
@@ -372,7 +372,7 @@ describe('library', () => {
372372
'C:/Users/DUMMY/Desktop/file.lnk',
373373
'C:/file.ext',
374374
'',
375-
'file',
375+
'',
376376
'',
377377
'C:/Users/DUMMY/icon.ico',
378378
1,
@@ -402,7 +402,7 @@ describe('library', () => {
402402
'C:/Users/DUMMY/Desktop/file.lnk',
403403
'C:/Users/DUMMY/icon.dll',
404404
'',
405-
'icon',
405+
'',
406406
'',
407407
'C:/Users/DUMMY/icon.dll,0',
408408
1,
@@ -457,7 +457,7 @@ describe('library', () => {
457457
'C:/Users/DUMMY/Desktop/file.lnk',
458458
'Throw Error',
459459
'',
460-
'Throw Error',
460+
'',
461461
'',
462462
'Throw Error',
463463
1,
@@ -625,7 +625,7 @@ describe('library', () => {
625625
'C:/Users/DUMMY/Desktop/file.lnk',
626626
'C:/file.ext',
627627
'',
628-
'file',
628+
'',
629629
'',
630630
'C:/file.ext',
631631
1,
@@ -747,7 +747,7 @@ describe('library', () => {
747747
'/home/DUMMY/Desktop/file.desktop',
748748
'C:/file.ext',
749749
'',
750-
'file',
750+
'',
751751
'',
752752
'C:/file.ext',
753753
1,
@@ -797,7 +797,7 @@ describe('library', () => {
797797
'/home/DUMMY/Desktop/file.desktop',
798798
'C:/file.ext',
799799
'',
800-
'file',
800+
'',
801801
'',
802802
'C:/file.ext',
803803
1,

tests/src/validation.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Validation', () => {
2222

2323
afterEach(() => {
2424
testHelpers.restoreMockFs();
25+
testHelpers.restoreEnvPATH();
2526
});
2627

2728
describe('validateOptions', () => {
@@ -308,6 +309,26 @@ describe('Validation', () => {
308309
});
309310
});
310311

312+
describe('resolvePATH', () => {
313+
beforeEach(() => {
314+
if (process.platform !== 'win32') {
315+
testHelpers.mockPlatform('linux');
316+
}
317+
testHelpers.mockEnvPATH();
318+
mockfs();
319+
});
320+
321+
test('Undefined', () => {
322+
expect(validation.resolvePATH(undefined))
323+
.toEqual(undefined);
324+
});
325+
326+
test('Resolves PATH', async () => {
327+
expect(['/home/DUMMY/app.exe', 'C:\\Program Files\\DUMMY\\app.exe'].includes(validation.resolvePATH('app.exe')))
328+
.toEqual(true);
329+
});
330+
});
331+
311332
describe('defaultBoolean', () => {
312333
beforeEach(() => {
313334
options = {

0 commit comments

Comments
 (0)