Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Last active November 15, 2018 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffJacobson/53153e99b3c08ec691b1b2ad8cfa8473 to your computer and use it in GitHub Desktop.
Save JeffJacobson/53153e99b3c08ec691b1b2ad8cfa8473 to your computer and use it in GitHub Desktop.
Generate both an mjs (es6 module) and js file using TypeScript
const fs = require("fs");
const { exec } = require("child_process");
/**
* Response from exec
* @typedef ExecResponse
* @type {object}
* @property {?Error} error - If the operation resulted in an error, this will have a value.
* @property {string} stdout - Text written to stdout
* @property {string} stderr - Text written to stderr
*/
/**
* Wrapper around fs.exec that returns a Promise instead of using callback parameter.
* @param {string} command - Command to execute.
* @returns {Promise.<ExecResponse>}
*/
function execPromise(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
} else {
resolve({ error, stdout, stderr });
}
});
});
}
(async () => {
await execPromise("tsc --target es2015 --module es2015");
await fs.promises.rename("index.js", "index.mjs");
await execPromise("tsc --target es5 --module commonjs");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment