Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the need for an exact path to be used #48

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 1 addition & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,48 +274,7 @@ Register function accept the below mentioned option

- [`Windows`](https://g.co/kgs/bm4Z4b) - OS - Supported
- [`linux`](https://g.co/kgs/xXAi4C) - OS - Supported
- [`MacOS`](https://g.co/kgs/k8yG4U) - OS - Supported with some anomalies mentioned below.

## MacOS Anomalies

### terminal: false

In MacOS if you don't launch the terminal it will run your command without logging in.

Thus you need to use absolute address of each command in your command string.

#### Example

Suppose you want to run :

```
$ node /path/to/index.js
```

Then first you need to find the path of node using the command below in terminal :

```
$ type node
> node is /usr/local/bin/node
```

Then replace the address of node in original command.
So your final command will be :

```
$ /usr/local/bin/node /path/to/index.js
```

To check if your program is running in MacOS you can use the code below:

```js
if (process.platform === "darwin") {
// running in MacOS do some thing
}
```

To run shell commands such as "type node" using nodeJS
please check the [`ShellJS documentation`](https://www.npmjs.com/package/shelljs#execcommand--options--callback)
- [`MacOS`](https://g.co/kgs/k8yG4U) - OS - Supported

## Contributors:

Expand Down
78 changes: 58 additions & 20 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@
"is-wsl": "^2.2.0",
"joi": "^17.9.1",
"plist": "^3.1.0",
"which": "^4.0.0",
"winreg": "^1.2.4"
},
"devDependencies": {
"eslint": "^7.22.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.1",
"husky": "^6.0.0",
"nodemon": "^2.0.13",
"prettier": "2.2.1",
"husky": "^6.0.0"
"prettier": "2.2.1"
}
}
5 changes: 5 additions & 0 deletions src/macos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const validator = require('../utils/validator');
const { registerSchema } = require('../validation/common');
const { homedir } = constants;

const replacePath = require('../utils/replaceWithExactPath');

if (process.platform === constants.platforms.macos) {
shell
.exec(`chmod +x '${join(__dirname, './defaultAppExist.sh')}'`)
Expand Down Expand Up @@ -58,6 +60,9 @@ const register = async (options, cb) => {
script: scriptRequired
} = validOptions;
let { command } = validOptions;

if (!terminal) command = replacePath(command); // Only run if it is not in terminal mode.

if (cb && typeof cb !== 'function')
throw new Error('Callback is not function');
try {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/replaceWithExactPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const which = require('which');

function replace(command) {
return command
.split(/&&|;|\n/)
Grayseon marked this conversation as resolved.
Show resolved Hide resolved
.map((cmd) => {
cmd = cmd.trim().split(' ');
try {
cmd[0] = which.sync(cmd[0]); // Replace the command to it's absolute path using which.
} catch {
throw new Error(`Command doesn't exist: ${cmd[0]}`);
}
return cmd.join(' ');
})
.join(' && ');
}

module.exports = replace;
20 changes: 20 additions & 0 deletions test/exactPathTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');

const ProtocolRegistry = require('../src');

console.log('Registering...');
ProtocolRegistry.register({
protocol: 'testproto',
command: `node ${path.join(__dirname, './speak.js')} $_URL_
say running; say running2`,
override: true,
terminal: false,
script: true,
scriptName: 'my-custom-script-name'
}).then(async () => {
console.log('Successfully registered');
});

ProtocolRegistry.checkifExists('testproto').then(console.log);

// In this test, when you go to testproto:// it should say "hello world" "running" "running2"
1 change: 1 addition & 0 deletions test/speak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('child_process').execSync('say "Hello World"'); // Running say because terminal mode is disabled and this is an easy way to see if there is an output.