How to use sed as an external tool? Wildcard issue

So I'm using TypeScript, transpiling to ES6. Known bug: Typescript outputs imports without a file extension.
So I have a sed command to search for "import { something } "./something"; and add .js to the end of the filename. More-or-less.
It works great in the terminal, but when I run it from WebStorm I get an error:

/bin/sed -i "/^import { \w* } from \"\.\/\w*(?!\.js)\";/s/\";/.js\";/" *.js
/bin/sed: can't read *.js: No such file or directory

I've tried adding a macro so that it uses the project path followed /*.js but that doesn't seem to make any difference. I've tried setting the working directory too. Right now I have no idea if it's looking in the wrong place for all the js files, or whether it's wrapping the parameter in quotes and actually looking for a file called '*.js'...

0
3 comments

It's a known issue, see https://youtrack.jetbrains.com/issue/IDEA-162745 and linked tickets. As a workaround, I'd suggest creating a shell script (.sh) that runs your sed command and configure this script as external tool

0

Thanks Lena

I actually did the script with a bit of simple typescript with a smattering of node, and run the produced js with node. Now I just have to find a way to deal with external libraries, especially import * as X from "module"... :)

let fs = require('fs');
fs.readdir(".", (err: any, items: string[]) => {
for(let i:number = 0; i < items.length; i++){
if (!items[i].endsWith(".js")) continue;
let data: string = fs.readFileSync(items[i]).toString();
let outData: string = "";
let lines: string[] = data.split(/\r?\n/);
for (let j: number = 0; j < lines.length; j++) {
if (lines[j].startsWith("import ") && lines[j].includes("./") && !lines[j].includes(".js")) {
lines[j] = lines[j].replace("\";", ".js\";");
}
outData = `${outData + lines[j]}\n`;
}
fs.writeFileSync(items[i], outData);
}
});

 

1

For anyone who manages to get here because they're using sed, I also learnt that sed doesn't support regex lookahead of any kind.

0

Please sign in to leave a comment.