Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): do not add typescript in new empty repos #15163

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import type { Tree } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';

import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import * as ts from 'typescript';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { addImportToModule } from '../../../../utils/nx-devkit/ast-utils';
import type { NormalizedSchema } from './normalized-schema';

let tsModule: typeof import('typescript');

export function addRouterRootConfiguration(
host: Tree,
options: NormalizedSchema
) {
if (!tsModule) {
tsModule = ensureTypescript();
}
const modulePath = `${options.appProjectRoot}/src/app/app.module.ts`;
const moduleSource = host.read(modulePath, 'utf-8');

let sourceFile = ts.createSourceFile(
let sourceFile = tsModule.createSourceFile(
modulePath,
moduleSource,
ts.ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import type { Tree } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';

import * as ts from 'typescript';
import type * as ts from 'typescript';
import { replaceNodeValue } from '@nrwl/workspace/src/utilities/ast-utils';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { getDecoratorPropertyValueNode } from '../../../../utils/nx-devkit/ast-utils';

import { nrwlHomeTemplate } from './nrwl-home-tpl';
import type { NormalizedSchema } from './normalized-schema';

let tsModule: typeof import('typescript');

export function updateAppComponentTemplate(
host: Tree,
options: NormalizedSchema
) {
if (!tsModule) {
tsModule = ensureTypescript();
}
const content = options.routing
? `${nrwlHomeTemplate.getSelector(
options.prefix
Expand All @@ -35,10 +39,10 @@ export function updateAppComponentTemplate(

replaceNodeValue(
host,
ts.createSourceFile(
tsModule.createSourceFile(
componentPath,
host.read(componentPath, 'utf-8'),
ts.ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
),
componentPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Tree } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';

import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import {
addImportToTestBed,
replaceIntoToTestBed,
} from '../../../../utils/nx-devkit/ast-utils';
import type { NormalizedSchema } from './normalized-schema';

let tsModule: typeof import('typescript');

export function updateComponentSpec(host: Tree, options: NormalizedSchema) {
if (!tsModule) {
tsModule = require('typescript');
tsModule = ensureTypescript();
}
if (options.skipTests !== true) {
const componentSpecPath = `${options.appProjectRoot}/src/app/app.component.spec.ts`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import type { PropertyDeclaration } from 'typescript';
import { SyntaxKind } from 'typescript';

let tsModule: typeof import('typescript');

export function getArgsDefaultValue(
property: PropertyDeclaration
): string | undefined {
if (!property.initializer) {
return undefined;
}
if (!tsModule) {
tsModule = ensureTypescript();
}
switch (property.initializer.kind) {
case SyntaxKind.StringLiteral:
case tsModule.SyntaxKind.StringLiteral:
const returnString = property.initializer.getText().slice(1, -1);
return returnString.replace(/\s/g, '+');
case SyntaxKind.NumericLiteral:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
case tsModule.SyntaxKind.NumericLiteral:
case tsModule.SyntaxKind.TrueKeyword:
case tsModule.SyntaxKind.FalseKeyword:
return property.initializer.getText();
default:
return undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { Tree } from '@nrwl/devkit';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { findNodes } from 'nx/src/utils/typescript';
import type { PropertyAssignment } from 'typescript';
import { SyntaxKind } from 'typescript';

import {
getDecoratorMetadata,
getTsSourceFile,
} from '../../../utils/nx-devkit/ast-utils';

let tsModule: typeof import('typescript');

export function getComponentSelector(tree: Tree, path: string): string {
if (!tsModule) {
tsModule = ensureTypescript();
}
const file = getTsSourceFile(tree, path);

const componentDecorators = getDecoratorMetadata(
Expand All @@ -20,7 +26,7 @@ export function getComponentSelector(tree: Tree, path: string): string {
}
const componentDecorator = componentDecorators[0];
const selectorNode = <PropertyAssignment>(
findNodes(componentDecorator, SyntaxKind.PropertyAssignment).find(
findNodes(componentDecorator, tsModule.SyntaxKind.PropertyAssignment).find(
(node: PropertyAssignment) => node.name.getText() === 'selector'
)
);
Expand Down
11 changes: 8 additions & 3 deletions packages/angular/src/generators/ngrx/lib/add-exports-barrel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments, names } from '@nrwl/devkit';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { addGlobal } from '@nrwl/workspace/src/utilities/ast-utils';
import { createSourceFile, ScriptTarget } from 'typescript';
import type { NormalizedNgRxGeneratorOptions } from './normalize-options';

let tsModule: typeof import('typescript');

/**
* Add ngrx feature exports to the public barrel in the feature library
*/
Expand All @@ -25,11 +27,14 @@ export function addExportsToBarrel(
return;
}

if (!tsModule) {
tsModule = ensureTypescript();
}
const indexSourceText = tree.read(indexFilePath, 'utf-8');
let sourceFile = createSourceFile(
let sourceFile = tsModule.createSourceFile(
indexFilePath,
indexSourceText,
ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Tree } from '@nrwl/devkit';
import { names } from '@nrwl/devkit';
import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import type { SourceFile } from 'typescript';
import { createSourceFile, ScriptTarget } from 'typescript';
import {
addImportToModule,
addProviderToBootstrapApplication,
Expand All @@ -11,6 +11,8 @@ import {
import type { NormalizedNgRxGeneratorOptions } from './normalize-options';
import { addProviderToRoute } from '../../../utils/nx-devkit/route-utils';

let tsModule: typeof import('typescript');

function addRootStoreImport(
tree: Tree,
isParentStandalone: boolean,
Expand Down Expand Up @@ -143,12 +145,15 @@ export function addImportsToModule(
tree: Tree,
options: NormalizedNgRxGeneratorOptions
): void {
if (!tsModule) {
tsModule = ensureTypescript();
}
const parentPath = options.module ?? options.parent;
const sourceText = tree.read(parentPath, 'utf-8');
let sourceFile = createSourceFile(
let sourceFile = tsModule.createSourceFile(
parentPath,
sourceText,
ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Tree } from '@nrwl/devkit';
import { tsquery } from '@phenomnomnominal/tsquery';
import { coerce, lt, major } from 'semver';
import {
getInstalledAngularVersionInfo,
Expand Down Expand Up @@ -36,6 +35,7 @@ export function validateOptions(
if (lt(angularVersionInfo.version, '14.1.0') || ngrxMajorVersion < 15) {
const parentPath = options.parent ?? options.module;
const parentContent = tree.read(parentPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const ast = tsquery.ast(parentContent);

const NG_MODULE_DECORATOR_SELECTOR =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments, names } from '@nrwl/devkit';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import { createSourceFile, ScriptTarget } from 'typescript';
import type { FileInfo } from '../../utils/file-info';
import type { NormalizedSchema } from '../schema';

let tsModule: typeof import('typescript');

export function convertDirectiveToScam(
tree: Tree,
directiveFileInfo: FileInfo,
Expand All @@ -15,6 +17,9 @@ export function convertDirectiveToScam(
`Couldn't find directive at path ${directiveFileInfo.filePath} to add SCAM setup.`
);
}
if (!tsModule) {
tsModule = ensureTypescript();
}

const directiveNames = names(options.name);
const typeNames = names('directive');
Expand All @@ -25,10 +30,10 @@ export function convertDirectiveToScam(
directiveFileInfo.filePath,
'utf-8'
);
let source = createSourceFile(
let source = tsModule.createSourceFile(
directiveFileInfo.filePath,
currentDirectiveContents,
ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments, names } from '@nrwl/devkit';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import { createSourceFile, ScriptTarget } from 'typescript';
import type { FileInfo } from '../../utils/file-info';
import type { NormalizedSchema } from '../schema';

let tsModule: typeof import('typescript');

export function convertPipeToScam(
tree: Tree,
pipeFileInfo: FileInfo,
Expand All @@ -15,17 +17,20 @@ export function convertPipeToScam(
`Couldn't find pipe at path ${pipeFileInfo.filePath} to add SCAM setup.`
);
}
if (!tsModule) {
tsModule = ensureTypescript();
}

const pipeNames = names(options.name);
const typeNames = names('pipe');
const pipeClassName = `${pipeNames.className}${typeNames.className}`;

if (options.inlineScam) {
const currentPipeContents = tree.read(pipeFileInfo.filePath, 'utf-8');
let source = createSourceFile(
let source = tsModule.createSourceFile(
pipeFileInfo.filePath,
currentPipeContents,
ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Node, SourceFile } from 'typescript';
import type { Node, SourceFile } from 'typescript';
import { Tree } from 'nx/src/generators/tree';
import { tsquery } from '@phenomnomnominal/tsquery';
import { parse } from 'path';
import { joinPathFragments } from 'nx/src/utils/path';

Expand All @@ -17,6 +16,7 @@ export function convertScamToStandalone(
let newComponentContents = '';
const COMPONENT_PROPERTY_SELECTOR =
'ClassDeclaration > Decorator > CallExpression:has(Identifier[name=Component]) ObjectLiteralExpression';
const { tsquery } = require('@phenomnomnominal/tsquery');
const componentDecoratorMetadataNode = tsquery(
componentAST,
COMPONENT_PROPERTY_SELECTOR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Tree } from 'nx/src/generators/tree';
import { tsquery } from '@phenomnomnominal/tsquery';
import type { Tree } from 'nx/src/generators/tree';

export function getComponentDataFromAST(
tree: Tree,
Expand All @@ -11,6 +10,7 @@ export function getComponentDataFromAST(
'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=Component])) > Identifier';

const componentFileContents = tree.read(normalizedComponentPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const componentAST = tsquery.ast(componentFileContents);

const componentNode = tsquery(componentAST, COMPONENT_CONTENT_SELECTOR, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SourceFile } from 'typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import type { SourceFile } from 'typescript';

export function getModuleMetadataFromAST(
componentAST: SourceFile,
componentFileContents: string
) {
const NGMODULE_CONTENT_SELECTOR =
'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=NgModule]))';
const { tsquery } = require('@phenomnomnominal/tsquery');
const moduleNodes = tsquery(componentAST, NGMODULE_CONTENT_SELECTOR, {
visitAllChildren: true,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SourceFile } from 'typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import type { SourceFile } from 'typescript';

export function selectorExistsInAST(selector: string, ast: SourceFile) {
const { tsquery } = require('@phenomnomnominal/tsquery');
return tsquery(ast, selector, { visitAllChildren: true }).length > 0;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { selectorExistsInAST } from './selector-exists-in-ast';
import { SourceFile } from 'typescript';
import type { SourceFile } from 'typescript';

export function verifyIsInlineScam(componentAST: SourceFile) {
const NGMODULE_DECORATOR_SELECTOR =
Expand Down
Loading