Skip to content

Commit

Permalink
Feature/wrapper script name (#37)
Browse files Browse the repository at this point in the history
* customisable wrapper script name

* security bug fixes

* qa bug fixes

* readme fixes

* version bump
  • Loading branch information
Shubham-Kumar-2000 authored May 1, 2023
1 parent b504a3e commit 4a8d553
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 24 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ProtocolRegistry.register({
override: true, // Use this with caution as it will destroy all previous Registrations on this protocol
terminal: true, // Use this to run your command inside a terminal
script: false,
scriptName: 'my-custom-script-name' // Custom script name.
}).then(async () => {
console.log("Successfully registered");
});
Expand Down Expand Up @@ -94,7 +95,7 @@ console.log(JSON.stringify(data, null, 4));
```
##### Alternative 2

Use can use base64 encrytion to transmit data and decode it there.
Use can use base64 encryption to transmit data and decode it there.

```js
const encode = (str) => Buffer.from(str).toString('base64');
Expand Down Expand Up @@ -177,7 +178,7 @@ ProtocolRegistry.register({
// Above will run tester.js when testproto://** is called as
// node .../tester.js testproto://**
// you can further parse the url to run in different modes
// As overide is not passed true it will throw an error is protocol already exists
// As override is not passed true it will throw an error is protocol already exists

ProtocolRegistry.register(
{
Expand Down Expand Up @@ -220,6 +221,7 @@ ProtocolRegistry.register(
command: commands,
terminal: true, // Terminal is set to false
script: true, // This will save your commands in a script file and execute it when the protocol is hit.
scriptName: 'my-custom-script-name' // This is the name of the script file that will be created if script option is set true.
},
(err) => {
if (err) {
Expand Down Expand Up @@ -262,10 +264,11 @@ Register function accept the below mentioned option
| name | types | default | details |
| ---------------| ------------------ | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| protocol | String (required) | NA | Only alphabets allowed. Your command will be executed when any url starting with this protocol is opened i.e. "myapp://test","testproto://abcd?mode=dev", etc. And please make sure that the protocol is unique to your application. |
| command | String (required) | NA | This command will be executed when the proctocol is called. **$\_URL\_** mentioned anywhere in your command will be replaced by the url by which it is initiated. |
| command | String (required) | NA | This command will be executed when the protocol is called. **$\_URL\_** mentioned anywhere in your command will be replaced by the url by which it is initiated. |
| override | Boolean | false | If this is not true, then you will get an error that protocol is already being used. So, first check if the protocol exist or not then take action accordingly (Refrain from using it). |
| terminal | Boolean | false | If this is set true, then first a terminal is opened and then your command is executed inside it.otherwise your command is executed in background and no logs appear but if your program launches any UI / webpage / file, it will be visible. |
| script | Boolean | false | If this is set true, then your command is saved in a script and that script is executed. This option is recommended if you are using multi-line commands or your command uses any kind of quotes. |
| scriptName | String | `${protocol}` | This is the name of the script file that will be created if script option is set true. |

## Supported platforms

Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protocol-registry",
"version": "1.3.16",
"version": "1.4.0",
"description": "This module allows you to set custom protocol handler for your nodejs app.",
"main": "src/index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ declare namespace ProtocolRegistry {
* This option is recommended if you are using multi-line commands or your command uses any kind of quotes.
*/
script?: boolean;
/**
* **default ${protocol}.**
* This is the name of the script file that will be created if script option is set true.
* It is recommended to use the default value.
*/
scriptName?: string;
};
export function register(params: RegisterOptions): Promise<void>;
export function register(
Expand Down
8 changes: 7 additions & 1 deletion src/linux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const checkifExists = async (protocol) => {
* @param {boolean=} options.override - Command which will be executed when the above protocol is initiated
* @param {boolean=} options.terminal - If set true then your command will open in new terminal
* @param {boolean=} options.script - If set true then your commands will be saved in a script and that script will be executed
* @param {string=} options.scriptName - Name of the script file by default it will be ${protocol}.sh
* @param {function (err)} cb - callback function Optional
*/

Expand Down Expand Up @@ -63,7 +64,12 @@ const register = async (options, cb) => {
const scriptTemplate = join(__dirname, './templates', 'script.ejs');
const scriptFilePath = join(__dirname, '../../temp', 'script.sh');

command = await preProcessCommands(protocol, command, scriptRequired);
command = await preProcessCommands(
protocol,
command,
scriptRequired,
options.scriptName
);

const desktopFileContent = await new Promise((resolve, reject) => {
ejs.renderFile(
Expand Down
8 changes: 7 additions & 1 deletion src/macos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const checkifExists = async (protocol) => {
* @param {boolean=} options.override - Command which will be executed when the above protocol is initiated
* @param {boolean=} options.terminal - If set true then your command will open in new terminal
* @param {boolean=} options.script - If set true then your commands will be saved in a script and that script will be executed
* @param {string=} options.scriptName - Name of the script file by default it will be ${protocol}.sh
* @param {function (err)} cb - callback function Optional
*/

Expand All @@ -66,7 +67,12 @@ const register = async (options, cb) => {
if (!override) throw new Error('Protocol already exists');
}

command = await preProcessCommands(protocol, command, scriptRequired);
command = await preProcessCommands(
protocol,
command,
scriptRequired,
options.scriptName
);

const plistMutator = join(__dirname, 'plistMutator.js');

Expand Down
9 changes: 7 additions & 2 deletions src/utils/processCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ const subtituteCommand = (command, url) => {
const identifier = '$_URL_';
return command.split(identifier).join(url);
};
const preProcessCommands = async (protocol, command, scriptRequired) => {
const preProcessCommands = async (
protocol,
command,
scriptRequired,
scriptName
) => {
if (!scriptRequired)
return subtituteCommand(command, urlArgument[process.platform]);
const newCommand = await handleWrapperScript(protocol, command);
const newCommand = await handleWrapperScript(protocol, command, scriptName);
return newCommand;
};
module.exports = {
Expand Down
17 changes: 12 additions & 5 deletions src/utils/wrapperScript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const constants = require('../config/constants');
const ejs = require('ejs');
const { join } = require('path');
const { writeFileSync } = require('fs');
const { writeFileSync, existsSync, rmSync, mkdirSync } = require('fs');
const shell = require('./shell');

const { homedir } = constants;
Expand Down Expand Up @@ -59,20 +59,27 @@ const getWrapperScriptContent = (command) => {
});
};

const saveWrapperScript = (protocol, content) => {
const saveWrapperScript = (protocol, content, scriptName) => {
if (existsSync(join(homedir, `./${protocol}`))) {
rmSync(join(homedir, `./${protocol}`), {
recursive: true,
force: true
});
}
mkdirSync(join(homedir, `./${protocol}`));
const wrapperScriptPath = join(
homedir,
`./${protocol}Wrapper.${
`./${protocol}/${scriptName || protocol}.${
process.platform === constants.platforms.windows ? 'bat' : 'sh'
}`
);
writeFileSync(wrapperScriptPath, content);
return wrapperScriptPath;
};

exports.handleWrapperScript = async (protocol, command) => {
exports.handleWrapperScript = async (protocol, command, scriptName) => {
const contents = await getWrapperScriptContent(command);
const scriptPath = saveWrapperScript(protocol, contents);
const scriptPath = saveWrapperScript(protocol, contents, scriptName);
if (process.platform !== constants.platforms.windows) {
const chmod = await shell.exec('chmod +x "' + scriptPath + '"');
if (chmod.code != 0 || chmod.stderr) throw new Error(chmod.stderr);
Expand Down
5 changes: 4 additions & 1 deletion src/validation/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ exports.registerSchema = Joi.object({
command: Joi.string().required(),
override: Joi.boolean(),
terminal: Joi.boolean(),
script: Joi.boolean()
script: Joi.boolean(),
scriptName: Joi.string()
.min(3)
.regex(/^[a-zA-Z0-9-]+$/)
});
8 changes: 7 additions & 1 deletion src/windows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const checkifExists = (protocol) => {
* @param {boolean=} options.override - Command which will be executed when the above protocol is initiated
* @param {boolean=} options.terminal - If set true then your command will open in new terminal
* @param {boolean=} options.script - If set true then your commands will be saved in a script and that script will be executed
* @param {string=} options.scriptName - Name of the script file by default it will be ${protocol}.sh
* @param {function (err)} cb - callback function Optional
*/

Expand Down Expand Up @@ -76,7 +77,12 @@ const register = async (options, cb) => {
})
);
}
command = await preProcessCommands(protocol, command, scriptRequired);
command = await preProcessCommands(
protocol,
command,
scriptRequired,
options.scriptName
);

await new Promise((resolve, reject) =>
registry.create((err) => {
Expand Down
3 changes: 2 additions & 1 deletion test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ ProtocolRegistry.register({
command: `node ${path.join(__dirname, './tester.js')} $_URL_`,
override: true,
terminal: true,
script: true
script: true,
scriptName: 'my-custom-script-name'
}).then(async () => {
console.log('Successfully registered');
});
Expand Down

0 comments on commit 4a8d553

Please sign in to comment.