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

Commit cd42a38

Browse files
authored
feat: Async spawn (#1)
1 parent 0f551f4 commit cd42a38

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# windows-shortcut-maker
2-
Native and lightweight module to make file shortcuts on Windows using Node.js
2+
Native and lightweight module to make file shortcuts on Windows using Node.js.
3+
4+
Based on https://github.com/phtdacosta/windows-shortcut-maker.
35

46
[![npm](https://img.shields.io/npm/dy/windows-shortcut-maker.svg)](https://www.npmjs.com/package/windows-shortcut-maker)
57
[![npm version](https://badge.fury.io/js/windows-shortcut-maker.svg)](https://badge.fury.io/js/windows-shortcut-maker)
@@ -35,6 +37,13 @@ try {
3537
} catch (error) {
3638
console.log(error)
3739
}
40+
41+
// It asynchronously creates a "GIMP" shortcut file in the desktop
42+
sm
43+
.make(options)
44+
.catch((error) => {
45+
console.log(error)
46+
}
3847
```
3948
4049
# Documentation
@@ -48,6 +57,8 @@ try {
4857
4958
**Optional:** **`options.lnkName`** is the name given for the new shortcut file which obeys the same name rules as a regular file does.
5059
60+
**Optional:** **`options.force`** create the shortcut even if the original file cannot be found.
61+
5162
**Optional:** **`options.lnkArgs`** are the arguments passed to the original file when the new shortcut is executed.
5263
5364
**Optional:** **`options.lnkDes`** is the description message shown when the cursor stands over the new shortcut without clicking it.
@@ -64,6 +75,5 @@ try {
6475
This project exists under the [GPL-3.0 license](https://github.com/phtdacosta/windows-shortcut-maker/blob/master/LICENSE).
6576
6677
# Development
67-
1. Add an **asynchronous** function version of `makeSync`.
68-
2. Let developers to make shortcuts anywhere.
69-
3. Add support for making internet shortcuts.
78+
1. Let developers to make shortcuts anywhere.
79+
2. Add support for making internet shortcuts.

index.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ const getName = (path) => {
1111
}
1212

1313
const makeSync = (options) => {
14-
if (fs.existsSync(options.filepath) === false) return new Error('File "'+ options.filepath +'" does not exist')
14+
if (options.force || fs.existsSync(options.filepath) === false) return new Error('File "'+ options.filepath +'" does not exist')
1515
const rawName = getName(options.filepath).split('.')[0]
1616
if (isString(options.lnkName) === false) options.lnkName = rawName
1717
if (isString(options.lnkArgs) === false) options.lnkArgs = ''
1818
if (isString(options.lnkDes) === false) options.lnkDes = rawName
1919
if (isString(options.lnkCwd) === false) options.lnkCwd = ''
20-
if (isString(options.lnkIco) === false) options.lnkIco = filepath
20+
if (isString(options.lnkIco) === false) options.lnkIco = options.filepath
2121
if (isString(options.lnkWin) === false) options.lnkWin = 4
2222
if (isString(options.lnkHtk) === false) options.lnkHtk = ''
2323
child_process.spawnSync(
24-
'wscript',
24+
'wscript',
2525
[__dirname + '\\scripts\\lnk.vbs',
2626
options.filepath,
2727
options.lnkName,
@@ -35,6 +35,42 @@ const makeSync = (options) => {
3535
)
3636
}
3737

38+
const make = (options) => {
39+
return new Promise((resolve, reject) => {
40+
return fs.exists(options.filepath, exists => {
41+
return exists || options.force ? resolve() : reject(new Error('File "'+ options.filepath +'" does not exist'));
42+
});
43+
}).then(() => {
44+
const rawName = getName(options.filepath).split('.')[0]
45+
if (isString(options.lnkName) === false) options.lnkName = rawName
46+
if (isString(options.lnkArgs) === false) options.lnkArgs = ''
47+
if (isString(options.lnkDes) === false) options.lnkDes = rawName
48+
if (isString(options.lnkCwd) === false) options.lnkCwd = ''
49+
if (isString(options.lnkIco) === false) options.lnkIco = options.filepath
50+
if (isString(options.lnkWin) === false) options.lnkWin = 4
51+
if (isString(options.lnkHtk) === false) options.lnkHtk = ''
52+
53+
return new Promise((resolve, reject) => {
54+
child_process.spawn(
55+
'wscript',
56+
[__dirname + '\\scripts\\lnk.vbs',
57+
options.filepath,
58+
options.lnkName,
59+
options.lnkArgs,
60+
options.lnkDes,
61+
options.lnkCwd,
62+
options.lnkIco,
63+
options.lnkWin,
64+
options.lnkHtk
65+
],
66+
)
67+
.on('error', error => reject(error))
68+
.on('exit', () => resolve())
69+
});
70+
});
71+
}
72+
3873
module.exports = {
74+
make: make,
3975
makeSync: makeSync
40-
}
76+
}

0 commit comments

Comments
 (0)