0

I want to use template literals because they are convenient but it looks weird and bad in my code. Is there anyway to write this so it's more readable?

Here's the code:

async function minimize(){

    const xdotool = `#!/bin/sh

pids=$(xdotool search --class "firefox")
for pid in $pids; do
    xdotool windowminimize $pid

done`

    await timeout(15000)

    exec("sh -c " + xdotool)

    minimize()
}
cooldude101
  • 1,215
  • 2
  • 14
  • 29

1 Answers1

0

tl;dr: Transfer your script to its own file if you don't need to pass any variables.

In your case you could just save your script in a separate file and just call it that way:

const path = require( 'path' );
exec( `sh ${ path.join( __dirname, 'xdotool.sh' ) }` );

If you need to load a template-script and swap out variables, you have 2 solutions:

Solution 1:

Use an external template file and replace certain values when loading. You could replace variables with your own regex-replacer or use a template engine like handlebars. You can find this solution here.

Solution 2:

Use proper indentation and correct it afterwards like here.

lumio
  • 7,428
  • 4
  • 40
  • 56
  • interesting, how come dontIndent doesn't need parenthesis for the argument? – cooldude101 Aug 19 '17 at 16:15
  • Because it is a tagged literal. [Here you can read more about it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) :) – lumio Aug 19 '17 at 16:18