Last active
November 15, 2018 22:58
-
-
Save JeffJacobson/53153e99b3c08ec691b1b2ad8cfa8473 to your computer and use it in GitHub Desktop.
Generate both an mjs (es6 module) and js file using TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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