Created
October 13, 2022 20:40
-
-
Save initFabian/2bd8937893922457403ae3cd72c92be4 to your computer and use it in GitHub Desktop.
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
// npm i ts-morph | |
const SOURCE_PATH = '/src/'; | |
const TS_CONFIG_PATH = './tsconfig.json'; | |
const { Project, Node } = require('ts-morph'); | |
async function main() { | |
const project = new Project({ tsConfigFilePath: TS_CONFIG_PATH }); | |
project.getSourceFiles().forEach(file => { | |
if (!file.getFilePath().includes(SOURCE_PATH)) { | |
return | |
} | |
console.log(file.getFilePath()); | |
file.getStatements().forEach((statement) => { | |
if (!Node.isExportable(statement) || !Node.isReferenceFindable(statement)) { | |
return; | |
} | |
const anyExternalReference = statement.findReferencesAsNodes().find((ref) => ref.getSourceFile() !== file); | |
if (anyExternalReference) return; | |
statement.setIsExported(false); | |
}); | |
file.fixUnusedIdentifiers(); | |
}); | |
console.info('saving project...'); | |
await project.save() | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment