Skip to content

Commit

Permalink
Fix Windows double-quotes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt authored Jun 18, 2022
1 parent 5375576 commit e4182ca
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 56 deletions.
6 changes: 3 additions & 3 deletions manual-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @author TheJaredWilcurt
*/

const timeLabel = 'Executed in:';
const timeLabel = 'Executed in';
console.time(timeLabel);

let createDesktopShortcuts = require('./index.js');
const createDesktopShortcuts = require('./index.js');

let success = createDesktopShortcuts({
linux: {
Expand All @@ -25,7 +25,7 @@ let success = createDesktopShortcuts({
icon: '..\\..\\..\\PortableApps\\Koa11y_v3.0.0\\package.nw\\_img\\fav.ico',
filePath: 'C:\\PortableApps\\Koa11y_v3.0.0\\Koa11y.exe',
outputPath: '%USERPROFILE%\\Desktop',
arguments: '--my-argument -f \'other stuff\'',
arguments: '--my-argument -f "other stuff"',
windowMode: 'maximized',
hotkey: 'ALT+CTRL+F'
}
Expand Down
93 changes: 53 additions & 40 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "create-desktop-shortcuts",
"main": "index.js",
"version": "1.8.0",
"version": "1.9.0",
"description": "Easy API to create desktop shortcuts with Node",
"author": "The Jared Wilcurt",
"keywords": [
Expand Down Expand Up @@ -38,6 +38,7 @@
"eslint-config-tjw-jsdoc": "^1.0.2",
"eslint-plugin-jsdoc": "^39.3.2",
"fs-extra": "8.1.0",
"get-windows-shortcut-properties": "^1.1.0",
"jest": "24.9.0",
"mock-fs": "^5.1.2"
},
Expand Down
11 changes: 7 additions & 4 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ const library = {
let windowMode = windowModes[options.windows.windowMode] || windowModes.normal;
let hotkey = options.windows.hotkey || '';

// Double quotes must be escaped, VBScript uses double quotes as the escape character
args = args.split('"').join('""');
comment = comment.split('"').join('""');
hotkey = hotkey.split('"').join('""');
// Double quotes (") are used as an escape character in VBScript, and are stripped out of any arguments passed in
function replaceDoubleQuotes (str) {
return str.split('"').join('__DOUBLEQUOTE__');
}
args = replaceDoubleQuotes(args);
comment = replaceDoubleQuotes(comment);
hotkey = replaceDoubleQuotes(hotkey);

if (!icon) {
if (
Expand Down
12 changes: 9 additions & 3 deletions src/windows.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@

option explicit

' Double quotes are stripped out of arguments, so we replace them with this keyword, before passing them in
' Then re-insert them in a way that VBScript permits, via Chr(34)
Function replaceDoubleQuotes(str)
replaceDoubleQuotes = Replace(str, "__DOUBLEQUOTE__", Chr(34))
End Function

dim strOutputPath, strFilePath, strArgs, strComment, strCwd, strIcon, strWindowMode, strHotkey
strOutputPath = Wscript.Arguments(0)
strFilePath = Wscript.Arguments(1)
strArgs = Wscript.Arguments(2)
strComment = Wscript.Arguments(3)
strArgs = replaceDoubleQuotes(Wscript.Arguments(2))
strComment = replaceDoubleQuotes(Wscript.Arguments(3))
strCwd = Wscript.Arguments(4)
strIcon = Wscript.Arguments(5)
strWindowMode = Wscript.Arguments(6)
strHotkey = Wscript.Arguments(7)
strHotkey = replaceDoubleQuotes(Wscript.Arguments(7))

sub createFile()
dim objShell, objLink
Expand Down
43 changes: 41 additions & 2 deletions tests/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ console.time(timeLabel);

const fs = require('fs-extra');
const path = require('path');
const getWindowsShortcutProperties = require('get-windows-shortcut-properties');

const createDesktopShortcuts = require('../index.js');

Expand All @@ -21,6 +22,9 @@ let extension = extensions[process.platform] || '';
const filePath = path.join(__dirname, 'src');
const outputPath = path.join(__dirname, '__mocks__');
const outputFile = path.join(__dirname, '__mocks__', 'src' + extension);
const Arguments = '"test"';
const hotkey = 'Ctrl+Shift+P';
const comment = 'Some "very" good text.';

let success = createDesktopShortcuts({
linux: {
Expand All @@ -34,7 +38,12 @@ let success = createDesktopShortcuts({
},
windows: {
filePath,
outputPath
outputPath,
hotkey,
comment,
arguments: Arguments,
workingDirectory: outputPath,
windowMode: 'maximized'
}
});

Expand Down Expand Up @@ -77,7 +86,9 @@ function alert (pass, message) {
console.log('\n ______________ ' + fill('_'));
console.log('| |' + fill(' ') + '|');
console.log('| E2E ' + state + ' | ' + message + ' |');
console.timeEnd(timeLabel);
if (process.platform !== 'win32') {
console.timeEnd(timeLabel);
}
console.log('| |' + fill(' ') + '|');
console.log(' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ' + fill('¯') + '\n\n');

Expand All @@ -89,6 +100,34 @@ function alert (pass, message) {
if (success) {
if (!fs.existsSync(outputFile)) {
alert(false, 'Could not find desktop shortcut.');
} else if (process.platform === 'win32') {
// We need to log the Windows time now to be accurate, as the
// getWindowsShortcutProperties step adds ~200-400ms that we don't care about
console.log('\n ______________ __________________________');
console.log('| | |');
console.log('| WINDOWS TIME | |');
console.timeEnd(timeLabel);
console.log(' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯' + '\n\n');
// This is here to validate the VBS script outputted a shortcut as expected
const outputProperties = getWindowsShortcutProperties.sync(outputFile)[0];
const expected = {
FullName: outputFile,
Arguments: Arguments,
Description: comment,
Hotkey: hotkey,
IconLocation: filePath + ',0',
RelativePath: '',
TargetPath: filePath,
WindowStyle: '3',
WorkingDirectory: outputPath
};
const windowsShortcutVerified = JSON.stringify(expected) === JSON.stringify(outputProperties);
if (windowsShortcutVerified) {
alert(true, 'Successly created and validated file.');
} else {
alert(false, 'Windows Shortcut properties mismatch');
console.log({ expected, outputProperties });
}
} else {
alert(true, 'Successly created and validated file.');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/src/library.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ describe('library', () => {
library.produceWindowsVBSPath(),
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/file.ext',
'-m ""Some text""',
'-m __DOUBLEQUOTE__Some text__DOUBLEQUOTE__',
'',
'',
'C:/file.ext',
Expand Down Expand Up @@ -505,7 +505,7 @@ describe('library', () => {
'C:/Users/DUMMY/Desktop/file.lnk',
'C:/file.ext',
'',
'Look at what ""I"" made.',
'Look at what __DOUBLEQUOTE__I__DOUBLEQUOTE__ made.',
'',
'C:/file.ext',
1,
Expand Down Expand Up @@ -538,7 +538,7 @@ describe('library', () => {
'',
'C:/file.ext',
1,
'CTRL+SHIFT+""'
'CTRL+SHIFT+__DOUBLEQUOTE__'
]
);
});
Expand Down

0 comments on commit e4182ca

Please sign in to comment.