Skip to content

Commit

Permalink
fix(misc): fix the react-express preset (#4827)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Feb 18, 2021
1 parent 6858873 commit 5f22224
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
51 changes: 41 additions & 10 deletions packages/tao/src/commands/ngcli-adapter.ts
Expand Up @@ -282,7 +282,7 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
.toPromise();
}

private context(
protected context(
path: string
): Observable<{
isWorkspaceConfig: boolean;
Expand Down Expand Up @@ -372,15 +372,45 @@ export class NxScopeHostUsedForWrappedSchematics extends NxScopedHost {
}

read(path: Path): Observable<FileBuffer> {
const targetPath = path.startsWith('/') ? path.substring(1) : path;
const r = this.host
.listChanges()
.find((f) => f.path == targetPath.toString() && f.type !== 'DELETE');
if (r) {
return of(Buffer.from(r.content));
} else {
return super.read(path);
}
return this.context(path).pipe(
switchMap((r) => {
// if it is a workspace config, handle it in a special way
if (r.isWorkspaceConfig) {
const match = this.host
.listChanges()
.find(
(f) => f.path == 'workspace.json' || f.path == 'angular.json'
);

// no match, default to existing behavior
if (!match) {
return super.read(path);
}

// we try to format it, if it changes, return it, otherwise return the original change
try {
const w = JSON.parse(Buffer.from(match.content).toString());
const formatted = toOldFormatOrNull(w);
return of(
formatted
? Buffer.from(JSON.stringify(formatted, null, 2))
: Buffer.from(match.content)
);
} catch (e) {
return super.read(path);
}
} else {
const targetPath = path.startsWith('/') ? path.substring(1) : path;
// found a matching change in the host
const match = this.host
.listChanges()
.find(
(f) => f.path == targetPath.toString() && f.type !== 'DELETE'
);
return match ? of(Buffer.from(match.content)) : super.read(path);
}
})
);
}
}

Expand Down Expand Up @@ -546,6 +576,7 @@ export async function runMigration(
}

const { BaseWorkflow } = require('@angular-devkit/schematics/src/workflow');

class MigrationsWorkflow extends BaseWorkflow {
constructor(host: virtualFs.Host, logger: logging.Logger) {
super({
Expand Down
40 changes: 19 additions & 21 deletions packages/workspace/src/generators/preset/preset.ts
Expand Up @@ -135,7 +135,7 @@ async function createPreset(tree: Tree, options: Schema) {
linter: options.linter,
});
setDefaultCollection(tree, '@nrwl/react');
connectReactAndExpress(options);
connectReactAndExpress(tree, options);
} else if (options.preset === 'nest') {
const { applicationGenerator: nestApplicationGenerator } = require('@nrwl' +
'/nest');
Expand Down Expand Up @@ -256,17 +256,16 @@ export class AppService {
);
}

function connectReactAndExpress(options: Schema) {
return (host: Tree) => {
const scope = options.npmScope;
host.write(
'libs/api-interfaces/src/lib/api-interfaces.ts',
`export interface Message { message: string }`
);
function connectReactAndExpress(host: Tree, options: Schema) {
const scope = options.npmScope;
host.write(
'libs/api-interfaces/src/lib/api-interfaces.ts',
`export interface Message { message: string }`
);

host.write(
`apps/${options.name}/src/app/app.tsx`,
`import React, { useEffect, useState } from 'react';
host.write(
`apps/${options.name}/src/app/app.tsx`,
`import React, { useEffect, useState } from 'react';
import { Message } from '@${scope}/api-interfaces';
export const App = () => {
Expand Down Expand Up @@ -294,11 +293,11 @@ export const App = () => {
export default App;
`
);
);

host.write(
`apps/${options.name}/src/app/app.spec.tsx`,
`import { cleanup, getByText, render, waitFor } from '@testing-library/react';
host.write(
`apps/${options.name}/src/app/app.spec.tsx`,
`import { cleanup, getByText, render, waitFor } from '@testing-library/react';
import React from 'react';
import App from './app';
Expand All @@ -320,11 +319,11 @@ describe('App', () => {
});
});
`
);
);

host.write(
`apps/api/src/main.ts`,
`import * as express from 'express';
host.write(
`apps/api/src/main.ts`,
`import * as express from 'express';
import { Message } from '@${scope}/api-interfaces';
const app = express();
Expand All @@ -341,8 +340,7 @@ const server = app.listen(port, () => {
});
server.on('error', console.error);
`
);
};
);
}

function setDefaultCollection(tree: Tree, defaultCollection: string) {
Expand Down

0 comments on commit 5f22224

Please sign in to comment.