Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
feat: Async spawn (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian authored Apr 8, 2019
1 parent 0f551f4 commit cd42a38
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# windows-shortcut-maker
Native and lightweight module to make file shortcuts on Windows using Node.js
Native and lightweight module to make file shortcuts on Windows using Node.js.

Based on https://github.com/phtdacosta/windows-shortcut-maker.

[![npm](https://img.shields.io/npm/dy/windows-shortcut-maker.svg)](https://www.npmjs.com/package/windows-shortcut-maker)
[![npm version](https://badge.fury.io/js/windows-shortcut-maker.svg)](https://badge.fury.io/js/windows-shortcut-maker)
Expand Down Expand Up @@ -35,6 +37,13 @@ try {
} catch (error) {
console.log(error)
}

// It asynchronously creates a "GIMP" shortcut file in the desktop
sm
.make(options)
.catch((error) => {
console.log(error)
}
```
# Documentation
Expand All @@ -48,6 +57,8 @@ try {
**Optional:** **`options.lnkName`** is the name given for the new shortcut file which obeys the same name rules as a regular file does.
**Optional:** **`options.force`** create the shortcut even if the original file cannot be found.
**Optional:** **`options.lnkArgs`** are the arguments passed to the original file when the new shortcut is executed.
**Optional:** **`options.lnkDes`** is the description message shown when the cursor stands over the new shortcut without clicking it.
Expand All @@ -64,6 +75,5 @@ try {
This project exists under the [GPL-3.0 license](https://github.com/phtdacosta/windows-shortcut-maker/blob/master/LICENSE).
# Development
1. Add an **asynchronous** function version of `makeSync`.
2. Let developers to make shortcuts anywhere.
3. Add support for making internet shortcuts.
1. Let developers to make shortcuts anywhere.
2. Add support for making internet shortcuts.
44 changes: 40 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ const getName = (path) => {
}

const makeSync = (options) => {
if (fs.existsSync(options.filepath) === false) return new Error('File "'+ options.filepath +'" does not exist')
if (options.force || fs.existsSync(options.filepath) === false) return new Error('File "'+ options.filepath +'" does not exist')
const rawName = getName(options.filepath).split('.')[0]
if (isString(options.lnkName) === false) options.lnkName = rawName
if (isString(options.lnkArgs) === false) options.lnkArgs = ''
if (isString(options.lnkDes) === false) options.lnkDes = rawName
if (isString(options.lnkCwd) === false) options.lnkCwd = ''
if (isString(options.lnkIco) === false) options.lnkIco = filepath
if (isString(options.lnkIco) === false) options.lnkIco = options.filepath
if (isString(options.lnkWin) === false) options.lnkWin = 4
if (isString(options.lnkHtk) === false) options.lnkHtk = ''
child_process.spawnSync(
'wscript',
'wscript',
[__dirname + '\\scripts\\lnk.vbs',
options.filepath,
options.lnkName,
Expand All @@ -35,6 +35,42 @@ const makeSync = (options) => {
)
}

const make = (options) => {
return new Promise((resolve, reject) => {
return fs.exists(options.filepath, exists => {
return exists || options.force ? resolve() : reject(new Error('File "'+ options.filepath +'" does not exist'));
});
}).then(() => {
const rawName = getName(options.filepath).split('.')[0]
if (isString(options.lnkName) === false) options.lnkName = rawName
if (isString(options.lnkArgs) === false) options.lnkArgs = ''
if (isString(options.lnkDes) === false) options.lnkDes = rawName
if (isString(options.lnkCwd) === false) options.lnkCwd = ''
if (isString(options.lnkIco) === false) options.lnkIco = options.filepath
if (isString(options.lnkWin) === false) options.lnkWin = 4
if (isString(options.lnkHtk) === false) options.lnkHtk = ''

return new Promise((resolve, reject) => {
child_process.spawn(
'wscript',
[__dirname + '\\scripts\\lnk.vbs',
options.filepath,
options.lnkName,
options.lnkArgs,
options.lnkDes,
options.lnkCwd,
options.lnkIco,
options.lnkWin,
options.lnkHtk
],
)
.on('error', error => reject(error))
.on('exit', () => resolve())
});
});
}

module.exports = {
make: make,
makeSync: makeSync
}
}

0 comments on commit cd42a38

Please sign in to comment.