10

It appears as though TypeScript is transpiling target files that are not executable.

I have to run chmod u+x <file> after transpilation to get the files to become executable.

This is the case, even if they have a hashbang:

#!/usr/bin/env node

How can I tell TypeScript / tsc to create files which are executable?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

6

Changing file's permissions isn't typescript responsibility.

Solution 1. Use a separate step in the build process in your package.json. For example:

{
  "name": "temp",
  "version": "1.0.0",
  "scripts": {
    "build": "tsc && chmod +x build/index.js"
  },
  "dependencies": {
    "typescript": "^2.3.4"
  }
}

Solution 2.

Write TypeScript Language Service Plugin. I think, in your case this is overengineering.

galkin
  • 5,264
  • 3
  • 34
  • 51
  • huh? tsc literally writes the files out, .ts -> .js + .d.ts. It can choose what permissions to use to write the files. this build step should be unnecessary. – Alexander Mills Jun 18 '17 at 18:30
  • @AlexanderMills to modify permissions during build would be somewhat irresponsible if they are trying to be secure by default. You could argue that they could do what they do now by default, and offer an option for this, but then they would have to deal with the possibility that there are bugs in that code. The risks outweigh the benefits, especially when typescript is focusing on a very specific problem, and the team liberally ignores other feature requests, which a compiler would normally be responsible for. – Catalyst Jun 18 '17 at 18:36
  • hmm, I really need TS to create .js files which are executable but not writable. I am not sure why TS would not allow us to configure this. – Alexander Mills Jun 18 '17 at 19:29
  • @Alexander Mills, you can use TS plugins for this. – galkin Jun 18 '17 at 19:40
  • @galkin thanks I will check that out, any links would help, but for now I will google – Alexander Mills Jun 18 '17 at 19:41
  • @galkin yeah tbh I am having trouble finding out much through google - I assume we can create a plugin and reference that plugin in a tsconfig.json file and the plugin will be called at some point in the transpilation lifecycle? – Alexander Mills Jun 18 '17 at 19:43
  • 1
    @AlexanderMills, This is what I suggested in the second solution. Did you check the link https://github.com/Microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin – galkin Jun 18 '17 at 21:50
  • I think I need to overengineer this because this is for a library not just an application – Alexander Mills Jun 19 '17 at 00:32
  • I'm with you @AlexanderMills. it should honor the perms set on the source file. that is, if the source file executable, so shall be the target file. Did you end up with just a post-build hack? trouble is, it doesn't help me when I'm using `tsc -w` :/ – cdaringe Oct 09 '18 at 04:26
  • @cdaringe you could wrap `tsc -w` and when it spits out certain stdout, you could then chmod the target files, but that's not pretty lol – Alexander Mills Oct 09 '18 at 21:33