Skip to content

Instantly share code, notes, and snippets.

@initFabian
Created October 13, 2022 20:40
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 initFabian/2bd8937893922457403ae3cd72c92be4 to your computer and use it in GitHub Desktop.
Save initFabian/2bd8937893922457403ae3cd72c92be4 to your computer and use it in GitHub Desktop.
// 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