Skip to content

Commit

Permalink
fix(js): ensure typescript before using tsquery
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Feb 18, 2023
1 parent 9896e48 commit c86ebea
Show file tree
Hide file tree
Showing 47 changed files with 310 additions and 177 deletions.
105 changes: 69 additions & 36 deletions docs/generated/devkit/nrwl_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ It only uses language primitives and immutable objects
- [output](../../devkit/documents/nrwl_devkit#output)
- [workspaceRoot](../../devkit/documents/nrwl_devkit#workspaceroot)

### Functions
### Other Functions

- [addDependenciesToPackageJson](../../devkit/documents/nrwl_devkit#adddependenciestopackagejson)
- [addProjectConfiguration](../../devkit/documents/nrwl_devkit#addprojectconfiguration)
Expand All @@ -149,7 +149,6 @@ It only uses language primitives and immutable objects
- [defaultTasksRunner](../../devkit/documents/nrwl_devkit#defaulttasksrunner)
- [detectPackageManager](../../devkit/documents/nrwl_devkit#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/documents/nrwl_devkit#detectworkspacescope)
- [ensurePackage](../../devkit/documents/nrwl_devkit#ensurepackage)
- [extractLayoutDirectory](../../devkit/documents/nrwl_devkit#extractlayoutdirectory)
- [formatFiles](../../devkit/documents/nrwl_devkit#formatfiles)
- [generateFiles](../../devkit/documents/nrwl_devkit#generatefiles)
Expand Down Expand Up @@ -203,6 +202,10 @@ It only uses language primitives and immutable objects
- [writeJson](../../devkit/documents/nrwl_devkit#writejson)
- [writeJsonFile](../../devkit/documents/nrwl_devkit#writejsonfile)

### Utils Functions

- [ensurePackage](../../devkit/documents/nrwl_devkit#ensurepackage)

## Project Graph Enumerations

### DependencyType
Expand Down Expand Up @@ -829,7 +832,7 @@ Implementation of a target of a project that handles multiple projects to be bat

**workspaceRoot**: `string`

## Functions
## Other Functions

### addDependenciesToPackageJson

Expand Down Expand Up @@ -1188,39 +1191,6 @@ Detect workspace scope from the package.json name

---

### ensurePackage

**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`

Ensure that dependencies and devDependencies from package.json are installed at the required versions.

For example:

```typescript
ensurePackage(tree, '@nrwl/jest', nxVersion);
```

This will check that @nrwl/jest@<nxVersion> exists in devDependencies.
If it exists then function returns, otherwise it will install the package before continuing.
When running with --dryRun, the function will throw when dependencies are missing.

#### Parameters

| Name | Type | Description |
| :------------------------ | :------------------------------------------------ | :----------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/documents/nrwl_devkit#tree) | the file system tree |
| `pkg` | `string` | the package to check (e.g. @nrwl/jest) |
| `requiredVersion` | `string` | the version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0) |
| `options` | `Object` | |
| `options.dev?` | `boolean` | - |
| `options.throwOnMissing?` | `boolean` | - |

#### Returns

`void`

---

### extractLayoutDirectory

**extractLayoutDirectory**(`directory`): `Object`
Expand Down Expand Up @@ -2437,3 +2407,66 @@ Serializes the given data to JSON and writes it to a file.
#### Returns

`void`

---

## Utils Functions

### ensurePackage

**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`

**`deprecated`** Use the other function signature without a Tree

Use a package that has not been installed as a dependency.

For example:

```typescript
ensurePackage(tree, '@nrwl/jest', nxVersion);
```

This install the @nrwl/jest@<nxVersion> and return the module
When running with --dryRun, the function will throw when dependencies are missing.

#### Parameters

| Name | Type | Description |
| :------------------------ | :------------------------------------------------ | :----------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/documents/nrwl_devkit#tree) | the file system tree |
| `pkg` | `string` | the package to check (e.g. @nrwl/jest) |
| `requiredVersion` | `string` | the version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0) |
| `options?` | `Object` | - |
| `options.dev?` | `boolean` | - |
| `options.throwOnMissing?` | `boolean` | - |

#### Returns

`void`

**ensurePackage**<`T`\>(`pkg`, `version`): `T`

Ensure that dependencies and devDependencies from package.json are installed at the required versions.

For example:

```typescript
ensurePackage(tree, '@nrwl/jest', nxVersion);
```

#### Type parameters

| Name | Type |
| :--- | :------------------------ |
| `T` | extends `unknown` = `any` |

#### Parameters

| Name | Type | Description |
| :-------- | :------- | :---------------------------------------------------------- |
| `pkg` | `string` | the package to install and require |
| `version` | `string` | the version to install if the package doesn't exist already |

#### Returns

`T`
105 changes: 69 additions & 36 deletions docs/generated/packages/devkit/documents/nrwl_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ It only uses language primitives and immutable objects
- [output](../../devkit/documents/nrwl_devkit#output)
- [workspaceRoot](../../devkit/documents/nrwl_devkit#workspaceroot)

### Functions
### Other Functions

- [addDependenciesToPackageJson](../../devkit/documents/nrwl_devkit#adddependenciestopackagejson)
- [addProjectConfiguration](../../devkit/documents/nrwl_devkit#addprojectconfiguration)
Expand All @@ -149,7 +149,6 @@ It only uses language primitives and immutable objects
- [defaultTasksRunner](../../devkit/documents/nrwl_devkit#defaulttasksrunner)
- [detectPackageManager](../../devkit/documents/nrwl_devkit#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/documents/nrwl_devkit#detectworkspacescope)
- [ensurePackage](../../devkit/documents/nrwl_devkit#ensurepackage)
- [extractLayoutDirectory](../../devkit/documents/nrwl_devkit#extractlayoutdirectory)
- [formatFiles](../../devkit/documents/nrwl_devkit#formatfiles)
- [generateFiles](../../devkit/documents/nrwl_devkit#generatefiles)
Expand Down Expand Up @@ -203,6 +202,10 @@ It only uses language primitives and immutable objects
- [writeJson](../../devkit/documents/nrwl_devkit#writejson)
- [writeJsonFile](../../devkit/documents/nrwl_devkit#writejsonfile)

### Utils Functions

- [ensurePackage](../../devkit/documents/nrwl_devkit#ensurepackage)

## Project Graph Enumerations

### DependencyType
Expand Down Expand Up @@ -829,7 +832,7 @@ Implementation of a target of a project that handles multiple projects to be bat

**workspaceRoot**: `string`

## Functions
## Other Functions

### addDependenciesToPackageJson

Expand Down Expand Up @@ -1188,39 +1191,6 @@ Detect workspace scope from the package.json name

---

### ensurePackage

**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`

Ensure that dependencies and devDependencies from package.json are installed at the required versions.

For example:

```typescript
ensurePackage(tree, '@nrwl/jest', nxVersion);
```

This will check that @nrwl/jest@<nxVersion> exists in devDependencies.
If it exists then function returns, otherwise it will install the package before continuing.
When running with --dryRun, the function will throw when dependencies are missing.

#### Parameters

| Name | Type | Description |
| :------------------------ | :------------------------------------------------ | :----------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/documents/nrwl_devkit#tree) | the file system tree |
| `pkg` | `string` | the package to check (e.g. @nrwl/jest) |
| `requiredVersion` | `string` | the version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0) |
| `options` | `Object` | |
| `options.dev?` | `boolean` | - |
| `options.throwOnMissing?` | `boolean` | - |

#### Returns

`void`

---

### extractLayoutDirectory

**extractLayoutDirectory**(`directory`): `Object`
Expand Down Expand Up @@ -2437,3 +2407,66 @@ Serializes the given data to JSON and writes it to a file.
#### Returns

`void`

---

## Utils Functions

### ensurePackage

**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`

**`deprecated`** Use the other function signature without a Tree

Use a package that has not been installed as a dependency.

For example:

```typescript
ensurePackage(tree, '@nrwl/jest', nxVersion);
```

This install the @nrwl/jest@<nxVersion> and return the module
When running with --dryRun, the function will throw when dependencies are missing.

#### Parameters

| Name | Type | Description |
| :------------------------ | :------------------------------------------------ | :----------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/documents/nrwl_devkit#tree) | the file system tree |
| `pkg` | `string` | the package to check (e.g. @nrwl/jest) |
| `requiredVersion` | `string` | the version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0) |
| `options?` | `Object` | - |
| `options.dev?` | `boolean` | - |
| `options.throwOnMissing?` | `boolean` | - |

#### Returns

`void`

**ensurePackage**<`T`\>(`pkg`, `version`): `T`

Ensure that dependencies and devDependencies from package.json are installed at the required versions.

For example:

```typescript
ensurePackage(tree, '@nrwl/jest', nxVersion);
```

#### Type parameters

| Name | Type |
| :--- | :------------------------ |
| `T` | extends `unknown` = `any` |

#### Parameters

| Name | Type | Description |
| :-------- | :------- | :---------------------------------------------------------- |
| `pkg` | `string` | the package to install and require |
| `version` | `string` | the version to install if the package doesn't exist already |

#### Returns

`T`
6 changes: 5 additions & 1 deletion docs/generated/packages/js/generators/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
"type": "boolean",
"aliases": ["skip-format"],
"description": "Skip formatting files.",
"default": true,
"x-priority": "internal"
},
"skipPackageJson": {
"type": "boolean",
"description": "Skip adding package.json dependencies",
"x-priority": "internal"
},
"tsConfigName": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

export function convertToStandaloneApp(tree: Tree, options: NormalizedSchema) {
const pathToAppModule = joinPathFragments(
Expand All @@ -23,6 +24,7 @@ function updateMainEntrypoint(
) {
let routerModuleSetup: string;
if (options.routing) {
ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');
const appModuleContents = tree.read(pathToAppModule, 'utf-8');
const ast = tsquery.ast(appModuleContents);
Expand Down Expand Up @@ -73,6 +75,7 @@ function updateAppComponent(tree: Tree, options: NormalizedSchema) {
);
const appComponentContents = tree.read(pathToAppComponent, 'utf-8');

ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');
const ast = tsquery.ast(appComponentContents);
const COMPONENT_DECORATOR_SELECTOR =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

export function convertToStandaloneApp(tree: Tree, options: NormalizedSchema) {
const pathToAppModule = joinPathFragments(
Expand All @@ -21,6 +22,7 @@ function updateMainEntrypoint(
tree: Tree,
pathToAppModule: string
) {
ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');
let routerModuleSetup: string;
if (options.routing) {
Expand Down Expand Up @@ -63,6 +65,7 @@ bootstrapApplication(AppComponent${
}).catch((err) => console.error(err));`;

function updateAppComponent(tree: Tree, options: NormalizedSchema) {
ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');
const pathToAppComponent = joinPathFragments(
options.appProjectRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ export async function angularChangeStorybookTargestGenerator(
);
await changeStorybookTargetsGenerator(tree);

if (!schema.skipFormat) {
await formatFiles(tree);
}
return addDependenciesToPackageJson(
const installTask = addDependenciesToPackageJson(
tree,
{},
{ '@nrwl/storybook': nxVersion }
);

if (!schema.skipFormat) {
await formatFiles(tree);
}

return installTask;
}

export default angularChangeStorybookTargestGenerator;
2 changes: 2 additions & 0 deletions packages/angular/src/generators/component/lib/entry-point.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Tree } from '@nrwl/devkit';
import type { StringLiteral } from 'typescript';
import { getRelativeImportToFile } from '../../utils/path';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

export function shouldExportInEntryPoint(
tree: Tree,
Expand All @@ -11,6 +12,7 @@ export function shouldExportInEntryPoint(
return false;
}

ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');
const moduleImportPath = getRelativeImportToFile(entryPoint, modulePath);
const entryPointContent = tree.read(entryPoint, 'utf-8');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { SourceFile } from 'typescript';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

export function checkOutputNameMatchesProjectName(
ast: SourceFile,
projectName: string
) {
ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');
const OUTPUT_SELECTOR =
'PropertyAssignment:has(Identifier[name=output]) > ObjectLiteralExpression:has(PropertyAssignment:has(Identifier[name=uniqueName]))';
Expand Down
Loading

0 comments on commit c86ebea

Please sign in to comment.