Created
March 21, 2019 19:09
-
-
Save angrykoala/f5456c1f7a00d286b155e67774948855 to your computer and use it in GitHub Desktop.
Preprocess typescript files to make export/import statements work on appscript after compiling with ts2gs
This file contains 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
// Preprocess typescript files to make export/import statements work on appscript after compiling with ts2gs | |
const fs = require('fs'); | |
const path = require('path'); | |
const glob = require("glob") | |
const ncp = require('ncp').ncp; | |
const source = path.join(__dirname, "..", "/lib"); | |
const target = path.join(__dirname, "..", "/bin"); | |
function copyAndGetFiles(){ | |
return new Promise((resolve,reject)=>{ | |
ncp(source, target, (err)=>{ | |
if(err) return reject(err); | |
console.log("Copy lib into bin - OK") | |
glob(target+ "/**/*.ts", {stopOnErr:true}, function (err, files) { | |
if(err) return reject(err); | |
console.log("Files found in lib -", files.length) | |
return resolve(files) | |
}); | |
}); | |
}); | |
} | |
function processFile(file){ | |
return new Promise((resolve,reject)=>{ | |
fs.readFile(file, 'utf8', (err,data)=>{ | |
if(err) return reject(err); | |
data=data.replace(/export(\ default)?\ /g, ""); | |
fs.writeFile(file, data, (err)=>{ | |
if(err) return reject(err); | |
return resolve(); | |
}); | |
}); | |
}) | |
} | |
function processFiles(){ | |
return copyAndGetFiles().then((files)=>{ | |
const promises=files.map((file)=>{ | |
return processFile(file); | |
}); | |
return Promise.all(promises); | |
}); | |
} | |
processFiles().then(()=>{ | |
console.log("Preprocessing Finished"); | |
}).catch((err)=>{ | |
console.error("Preprocessing Errored") | |
throw err; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment