|
| 1 | +/** |
| 2 | +* Copyright 2017 Google Inc. |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +* of this software and associated documentation files (the "Software"), to deal |
| 6 | +* in the Software without restriction, including without limitation the rights |
| 7 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +* copies of the Software, and to permit persons to whom the Software is |
| 9 | +* furnished to do so, subject to the following conditions: |
| 10 | +* |
| 11 | +* The above copyright notice and this permission notice shall be included in |
| 12 | +* all copies or substantial portions of the Software. |
| 13 | +* |
| 14 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +* THE SOFTWARE. |
| 21 | +* |
| 22 | +*/ |
| 23 | +const fs = require('fs'); |
| 24 | +const fse = require('fs-extra'); |
| 25 | + |
| 26 | +const {main} = require('./rewrite-declation-statements-for-typescript'); |
| 27 | + |
| 28 | +const TYPESCRIPT_TMP = '.typescript-tmp'; |
| 29 | +const TYPESCRIPT_PKGDIR = `${TYPESCRIPT_TMP}/packages`; |
| 30 | +const SOURCEDIR = './packages'; |
| 31 | + |
| 32 | + |
| 33 | +function logProgress(msg) { |
| 34 | + if (logProgress.__prev_msg_length) { |
| 35 | + const lineClear = ' '.repeat(logProgress.__prev_msg_length + 10); |
| 36 | + process.stdout.write('\r' + lineClear); |
| 37 | + } |
| 38 | + logProgress.__prev_msg_length = msg.length; |
| 39 | + process.stdout.write('\r' + msg); |
| 40 | +} |
| 41 | + |
| 42 | +function getAllPackageFiles() { |
| 43 | + return fs.readdirSync(SOURCEDIR).map((packages) => packages); |
| 44 | +} |
| 45 | + |
| 46 | +function excludeCompiledDirectories(src) { |
| 47 | + if (!src.includes('/node_modules/') && !src.includes('dist')) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +function movePackagesToTypescriptTmp() { |
| 54 | + fs.mkdirSync(TYPESCRIPT_PKGDIR); |
| 55 | + const copyTasks = []; |
| 56 | + const removeTasks = []; |
| 57 | + |
| 58 | + getAllPackageFiles().forEach((packageName) => { |
| 59 | + const packageSourcePath = `${SOURCEDIR}/${packageName}`; |
| 60 | + const packageDestinationPath = `${TYPESCRIPT_PKGDIR}/${packageName}`; |
| 61 | + |
| 62 | + const packageCopyPromise = fse.copy(packageSourcePath, packageDestinationPath, {filter: excludeCompiledDirectories}) |
| 63 | + .then(() => { |
| 64 | + logProgress(`Successfully copied ${packageName}`); |
| 65 | + }).catch((err) => console.log(err)); |
| 66 | + |
| 67 | + copyTasks.push(packageCopyPromise); |
| 68 | + }); |
| 69 | + |
| 70 | + const promises = [].concat(copyTasks, removeTasks); |
| 71 | + |
| 72 | + Promise.all(promises).then(() => { |
| 73 | + console.log('Rewriting all import statements to be internal typescript compatible'); |
| 74 | + main(TYPESCRIPT_PKGDIR); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +console.log('Prepping typescript packages for JS rewrite'); |
| 79 | +fse.emptyDir(TYPESCRIPT_TMP) |
| 80 | + .then(movePackagesToTypescriptTmp) |
| 81 | + .catch((err) => console.log(err)); |
0 commit comments