Skip to content

Commit 51f47f5

Browse files
committed
test: added a lot tests around the monorepo structure still wip
1 parent 9949d6f commit 51f47f5

File tree

3 files changed

+296
-4
lines changed

3 files changed

+296
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function (options) {
2+
return {
3+
...options,
4+
entry: __dirname + '/main/index.ts',
5+
output: {
6+
libraryTarget: 'commonjs2',
7+
filename: 'apps/<%= project %>/main/index.js'
8+
},
9+
};
10+
};

schematics/install/index.test.ts

Lines changed: 244 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import { Tree } from '@angular-devkit/schematics';
1+
import {
2+
Tree,
3+
FileEntry,
4+
CircularCollectionException
5+
} from '@angular-devkit/schematics';
26
import {
37
SchematicTestRunner,
48
UnitTestTree
59
} from '@angular-devkit/schematics/testing';
610
import * as path from 'path';
711
import { Schema } from './schema';
812

13+
const getFileContent = (tree: Tree, path: string): string => {
14+
const fileEntry: FileEntry = tree.get(path);
15+
if (!fileEntry) {
16+
throw new Error(`The file does not exist.`);
17+
}
18+
return fileEntry.content.toString();
19+
};
920
describe('Schematic Tests Nest Add', () => {
1021
let nestTree: Tree;
1122

@@ -34,9 +45,9 @@ describe('Schematic Tests Nest Add', () => {
3445
expect(files).toEqual([
3546
'/.eslintrc.js',
3647
'/.prettierrc',
37-
'/README.md',
3848
'/nest-cli.json',
3949
'/package.json',
50+
'/README.md',
4051
'/tsconfig.build.json',
4152
'/tsconfig.json',
4253
'/.funcignore',
@@ -83,9 +94,9 @@ describe('Schematic Tests Nest Add', () => {
8394
expect(files).toEqual([
8495
'/.eslintrc.js',
8596
'/.prettierrc',
86-
'/README.md',
8797
'/nest-cli.json',
8898
'/package.json',
99+
'/README.md',
89100
'/tsconfig.build.json',
90101
'/tsconfig.json',
91102
'/src/app.controller.spec.ts',
@@ -105,12 +116,242 @@ describe('Schematic Tests Nest Add', () => {
105116
'/libs/lib1/src/local.settings.json',
106117
'/libs/lib1/src/main.azure.ts',
107118
'/libs/lib1/src/proxies.json',
119+
'/libs/lib1/src/webpack.config.js',
108120
'/libs/lib1/src/main/function.json',
109121
'/libs/lib1/src/main/index.ts',
110122
'/libs/lib1/src/main/sample.dat'
111123
]);
112124
});
113125

126+
it('should have a nest-cli.json for project', async () => {
127+
const options: Schema = {
128+
sourceRoot: '/libs/lib1/src',
129+
skipInstall: true,
130+
rootDir: 'src',
131+
project: 'lib1'
132+
};
133+
await runner
134+
.runExternalSchematicAsync(
135+
'@nestjs/schematics',
136+
'library',
137+
{
138+
name: 'lib1',
139+
prefix: '@app'
140+
},
141+
nestTree
142+
)
143+
.toPromise();
144+
145+
const tree = await runner
146+
.runSchematicAsync('nest-add', options, nestTree)
147+
.toPromise();
148+
const fileContent = getFileContent(tree, '/nest-cli.json');
149+
const parsedFile = JSON.parse(fileContent);
150+
expect(parsedFile.projects.lib1.sourceRoot).toEqual('libs/lib1/src');
151+
});
152+
153+
it('should a nest-cli.json for default app', async () => {
154+
const options: Schema = {
155+
sourceRoot: 'src',
156+
skipInstall: true,
157+
rootDir: 'src',
158+
rootModuleFileName: 'app.module',
159+
rootModuleClassName: 'AppModule'
160+
};
161+
162+
const tree = await runner
163+
.runSchematicAsync('nest-add', options, nestTree)
164+
.toPromise();
165+
166+
const fileContent = getFileContent(tree, '/nest-cli.json');
167+
expect(fileContent).toContain(`"sourceRoot": "src"`);
168+
});
169+
170+
it('should import the app.module int main azure file for project', async () => {
171+
const options: Schema = {
172+
sourceRoot: '/libs/lib1/src',
173+
skipInstall: true,
174+
rootDir: 'src',
175+
project: 'lib1'
176+
};
177+
await runner
178+
.runExternalSchematicAsync(
179+
'@nestjs/schematics',
180+
'library',
181+
{
182+
name: 'lib1',
183+
prefix: '@app'
184+
},
185+
nestTree
186+
)
187+
.toPromise();
188+
189+
const tree = await runner
190+
.runSchematicAsync('nest-add', options, nestTree)
191+
.toPromise();
192+
const fileContent = getFileContent(tree, '/libs/lib1/src/main.azure.ts');
193+
194+
expect(fileContent).toContain(`import { AppModule } from './app.module';`);
195+
});
196+
197+
it('should import the app.module int main azure file for default app', async () => {
198+
const options: Schema = {
199+
sourceRoot: 'src',
200+
skipInstall: true,
201+
rootDir: 'src',
202+
rootModuleFileName: 'app.module',
203+
rootModuleClassName: 'AppModule'
204+
};
205+
206+
const tree = await runner
207+
.runSchematicAsync('nest-add', options, nestTree)
208+
.toPromise();
209+
210+
const fileContent = getFileContent(tree, '/src/main.azure.ts');
211+
212+
expect(fileContent).toContain(`import { AppModule } from './app.module';`);
213+
});
214+
215+
it('should have the root dir for index file in main azure dir for project', async () => {
216+
const options: Schema = {
217+
sourceRoot: '/libs/lib1/src',
218+
skipInstall: true,
219+
rootDir: 'src',
220+
project: 'lib1'
221+
};
222+
await runner
223+
.runExternalSchematicAsync(
224+
'@nestjs/schematics',
225+
'library',
226+
{
227+
name: 'lib1',
228+
prefix: '@app'
229+
},
230+
nestTree
231+
)
232+
.toPromise();
233+
234+
const tree = await runner
235+
.runSchematicAsync('nest-add', options, nestTree)
236+
.toPromise();
237+
const fileContent = getFileContent(tree, '/libs/lib1/src/main/index.ts');
238+
239+
expect(fileContent).toContain(`import { createApp } from '../main.azure';`);
240+
});
241+
242+
it('should have the root dir for index file in main azure dir for default app', async () => {
243+
const options: Schema = {
244+
sourceRoot: 'src',
245+
skipInstall: true,
246+
rootDir: 'src',
247+
rootModuleFileName: 'app.module',
248+
rootModuleClassName: 'AppModule'
249+
};
250+
251+
const tree = await runner
252+
.runSchematicAsync('nest-add', options, nestTree)
253+
.toPromise();
254+
255+
const fileContent = getFileContent(tree, '/main/index.ts');
256+
257+
expect(fileContent).toContain(
258+
`import { createApp } from '../src/main.azure';`
259+
);
260+
});
261+
262+
it('should import the webpack config for a project', async () => {
263+
const options: Schema = {
264+
sourceRoot: '/libs/lib1/src',
265+
skipInstall: true,
266+
rootDir: 'src',
267+
project: 'lib1'
268+
};
269+
await runner
270+
.runExternalSchematicAsync(
271+
'@nestjs/schematics',
272+
'library',
273+
{
274+
name: 'lib1',
275+
prefix: '@app'
276+
},
277+
nestTree
278+
)
279+
.toPromise();
280+
281+
const tree = await runner
282+
.runSchematicAsync('nest-add', options, nestTree)
283+
.toPromise();
284+
285+
const fileContent = getFileContent(
286+
tree,
287+
'/libs/lib1/src/webpack.config.js'
288+
);
289+
expect(fileContent).toContain(`filename: 'apps/lib1/main/index.js'`);
290+
});
291+
292+
it('should not import the webpack config for a default app', async () => {
293+
const options: Schema = {
294+
sourceRoot: 'src',
295+
skipInstall: true,
296+
rootDir: 'src',
297+
rootModuleFileName: 'app.module',
298+
rootModuleClassName: 'AppModule'
299+
};
300+
301+
const tree = await runner
302+
.runSchematicAsync('nest-add', options, nestTree)
303+
.toPromise();
304+
305+
const fileContent = tree.get('webpack.config.js');
306+
307+
expect(fileContent).toBeNull();
308+
});
309+
310+
fit('should add a custom webpack config to the compilerOptions for a project', async () => {
311+
const options: Schema = {
312+
sourceRoot: '/apps/azure-func-http/src',
313+
skipInstall: true,
314+
rootDir: '',
315+
project: 'azure-func-http'
316+
};
317+
// await runner
318+
// .runExternalSchematicAsync(
319+
// '@nestjs/schematics',
320+
// 'application',
321+
// {
322+
// name: 'azure-func-http',
323+
// prefix: '@app'
324+
// },
325+
// nestTree
326+
// )
327+
// .toPromise();
328+
329+
await runner
330+
.runExternalSchematicAsync(
331+
'@nestjs/schematics',
332+
'sub-app',
333+
{
334+
name: 'azure-func-http'
335+
},
336+
nestTree
337+
)
338+
.toPromise();
339+
340+
const tree = await runner
341+
.runSchematicAsync('nest-add', options, nestTree)
342+
.toPromise();
343+
344+
const fileContent = getFileContent(tree, 'nest-cli.json');
345+
const parsedFile = JSON.parse(fileContent);
346+
347+
const compilerOptions = parsedFile.projects.lib1.compilerOptions;
348+
expect(compilerOptions).toContain({
349+
webpack: 'true',
350+
webpackConfigPath: 'apps/lib1/src/webpack.config.js',
351+
tsConfigPath: 'libs/lib1/tsconfig.lib.json'
352+
});
353+
});
354+
114355
async function createTestNest(
115356
runner: SchematicTestRunner,
116357
tree?: Tree

schematics/install/index.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { strings } from '@angular-devkit/core';
1+
import { strings, parseJson } from '@angular-devkit/core';
22
import {
33
apply,
44
chain,
@@ -16,6 +16,7 @@ import {
1616
NodeDependencyType
1717
} from '@schematics/angular/utility/dependencies';
1818
import { Schema as AzureOptions } from './schema';
19+
type UpdateJsonFn<T> = (obj: T) => T | void;
1920

2021
function addDependenciesAndScripts(): Rule {
2122
return (host: Tree) => {
@@ -38,11 +39,51 @@ function addDependenciesAndScripts(): Rule {
3839
};
3940
}
4041

42+
function updateJsonFile<T>(
43+
host: Tree,
44+
path: string,
45+
callback: UpdateJsonFn<T>
46+
): Tree {
47+
const source = host.read(path);
48+
if (source) {
49+
const sourceText = source.toString('utf-8');
50+
const json = parseJson(sourceText);
51+
callback((json as {}) as T);
52+
host.overwrite(path, JSON.stringify(json, null, 2));
53+
}
54+
return host;
55+
}
56+
4157
export default function(options: AzureOptions): Rule {
4258
return (host: Tree, context: SchematicContext) => {
4359
if (!options.skipInstall) {
4460
context.addTask(new NodePackageInstallTask());
4561
}
62+
const projectName = options.project;
63+
console.log(projectName);
64+
if (projectName) {
65+
let nestCliFileExists = host.exists('nest-cli.json');
66+
67+
if (nestCliFileExists) {
68+
updateJsonFile(
69+
host,
70+
'nest-cli.json',
71+
(optionsFile: Record<string, any>) => {
72+
console.log(optionsFile);
73+
if (optionsFile.projects[projectName].compilerOptions) {
74+
optionsFile.projects[projectName].compilerOptions = {
75+
...optionsFile.projects[projectName].compilerOptions,
76+
...{
77+
webpack: true,
78+
webpackConfigPath: `apps/${projectName}/src/webpack.config.js`
79+
}
80+
};
81+
}
82+
}
83+
);
84+
}
85+
}
86+
4687
const rootSource = apply(
4788
options.project ? url('./files/project') : url('./files/root'),
4889
[

0 commit comments

Comments
 (0)