Jason Jean 3d4f590c07
fix(core): do not add typescript in new empty repos (#15163)
Co-authored-by: Emily Xiong <xiongemi@gmail.com>
2023-02-27 12:08:36 -05:00

32 lines
886 B
TypeScript

import type { Tree } from 'nx/src/generators/tree';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { typescriptVersion } from 'nx/src/utils/versions';
import { ensurePackage } from '../utils/package-json';
/**
* Rename and transpile any new typescript files created to javascript files
*/
export function toJS(tree: Tree): void {
const { JsxEmit, ScriptTarget, transpile } = ensurePackage(
'typescript',
typescriptVersion
);
for (const c of tree.listChanges()) {
if (
(c.path.endsWith('.ts') || c.path.endsWith('tsx')) &&
c.type === 'CREATE'
) {
tree.write(
c.path,
transpile(c.content.toString('utf-8'), {
allowJs: true,
jsx: JsxEmit.Preserve,
target: ScriptTarget.ESNext,
})
);
tree.rename(c.path, c.path.replace(/\.tsx?$/, '.js'));
}
}
}