Jonathan Cammisuli d9aef75bd5
chore(react): move react schematics to generators (#4745)
* chore(react): move react schematics to generators

* chore(react): update lib generators

* chore(react): update redux generators

* chore(react): move react story book generators

* chore(react): add old implementation for update babel in next

* chore(react): rename tsconfig json template files to include __tmpl__

* chore(react): update deps

* chore(react): fix component template file

* chore(react): remove angular-devkit deps

* chore(react): remove angular-devkit deps
2021-02-10 21:30:55 -05:00

51 lines
1.3 KiB
TypeScript

import * as ts from 'typescript';
import { addInitialRoutes } from '../../../utils/ast-utils';
import { NormalizedSchema } from '../schema';
import {
reactRouterDomVersion,
typesReactRouterDomVersion,
} from '../../../utils/versions';
import {
joinPathFragments,
Tree,
StringInsertion,
applyChangesToString,
addDependenciesToPackageJson,
} from '@nrwl/devkit';
export function addRouting(host: Tree, options: NormalizedSchema) {
if (!options.routing) {
return;
}
const appPath = joinPathFragments(
options.appProjectRoot,
maybeJs(options, `src/app/${options.fileName}.tsx`)
);
const appFileContent = host.read(appPath).toString('utf-8');
const appSource = ts.createSourceFile(
appPath,
appFileContent,
ts.ScriptTarget.Latest,
true
);
const changes = applyChangesToString(
appFileContent,
addInitialRoutes(appPath, appSource)
);
host.write(appPath, changes);
addDependenciesToPackageJson(
host,
{ 'react-router-dom': reactRouterDomVersion },
{ '@types/react-router-dom': typesReactRouterDomVersion }
);
}
function maybeJs(options: NormalizedSchema, path: string): string {
return options.js && (path.endsWith('.ts') || path.endsWith('.tsx'))
? path.replace(/\.tsx?$/, '.js')
: path;
}