0

I have a REST API backend project written in TypeScript where I want to be able to see when the Javascripts were generated.

In my Express routes I want to have a route called '/version' which only returns a string, '2018-12-19 08:39:00.000'.

Is there a simple way, in my .ts file, to make sure that the .js file generated gets the actual timestamp from when the transpilation from .ts to .js was done?

My project uses tsc to compile. It doesn't use webpack.

Frode Lillerud
  • 7,324
  • 17
  • 58
  • 69

2 Answers2

1

You could paste directory name as compiler option (--outDir). It will generate directory with date, when compilation starts If you use linux machine use date command to generate date:

tsc --outDir "path/versions/$(date '+%Y-%m-%d %H:%M:%S')"

You can use utc timestamp with date -u ...

More information about date here: https://www.computerhope.com/unix/udate.htm

1

There is no clean/easy way to inject code into the .js file during the tsc execution but you could have the file check its own last modified time during execution.

let fs = require('fs');

fs.stat("my-router-file.js", function(err, stats){
    var mtime = new Date(stats.mtime);
    console.log(mtime);
});

fs docs: https://nodejs.org/api/fs.html#fs_stats_mtime

Rachel Church
  • 228
  • 2
  • 9