Skip to content
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
6 changes: 3 additions & 3 deletions packages/schematics/angular/web-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ function addSnippet(options: WebWorkerOptions): Rule {
return;
}

const fileRegExp = new RegExp(`^${options.name}.*\\.ts`);
const fileRegExp = new RegExp(`^${options.name}(?:\\..+)?\\.ts$`);
const siblingModules = host
.getDir(options.path)
.subfiles // Find all files that start with the same name, are ts files,
// and aren't spec or module files.
.filter((f) => fileRegExp.test(f) && !/(module|spec|config|routes)\.ts$/.test(f))
// and aren't spec, module, declaration, or configuration files (including their server variants).
.filter((f) => fileRegExp.test(f) && !/\.(module|spec|config|routes|server|d)\.ts$/.test(f))
Comment thread
alan-agius4 marked this conversation as resolved.
// Sort alphabetically for consistency.
.sort();

Expand Down
40 changes: 40 additions & 0 deletions packages/schematics/angular/web-worker/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,46 @@ describe('Web Worker Schematic', () => {
expect(appComponent).toContain('console.log(`page got message: ${data}`)');
});

it('should add snippet to the component file and not a server configuration file', async () => {
appTree.create('/projects/bar/src/app/app.config.server.ts', 'export const config = {};\n');
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);

const appComponent = tree.readContent('/projects/bar/src/app/app.ts');
expect(appComponent).toContain(`new Worker(new URL('./${defaultOptions.name}.worker`);

const serverConfig = tree.readContent('/projects/bar/src/app/app.config.server.ts');
expect(serverConfig).not.toContain('new Worker');
});
Comment thread
MeAkib marked this conversation as resolved.

it(`should add snippet to a sibling file whose name ends with 'server'`, async () => {
appTree.create('/projects/bar/src/app/observer.ts', 'export class Observer {}\n');
const options = { ...defaultOptions, name: 'observer' };
const tree = await schematicRunner.runSchematic('web-worker', options, appTree);

const content = tree.readContent('/projects/bar/src/app/observer.ts');
expect(content).toContain(`new Worker(new URL('./observer.worker`);
});

it('should not add snippet to a file whose name only starts with the worker name', async () => {
appTree.create('/projects/bar/src/app/app-header.ts', 'export class AppHeader {}\n');
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);

expect(tree.readContent('/projects/bar/src/app/app-header.ts')).not.toContain('new Worker');
expect(tree.readContent('/projects/bar/src/app/app.ts')).toContain(
`new Worker(new URL('./${defaultOptions.name}.worker`,
);
});

it('should not add snippet to a declaration file', async () => {
appTree.create('/projects/bar/src/app/app.d.ts', 'declare const foo: string;\n');
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);

expect(tree.readContent('/projects/bar/src/app/app.d.ts')).not.toContain('new Worker');
expect(tree.readContent('/projects/bar/src/app/app.ts')).toContain(
`new Worker(new URL('./${defaultOptions.name}.worker`,
);
});

it(`should add 'tsconfig.worker.json' outside of 'src' directory in root app`, async () => {
const rootAppOptions = { ...appOptions, projectRoot: '', name: 'foo' };
const workerOptions = { ...defaultOptions, project: 'foo' };
Expand Down
Loading