1- import { Tree } from '@angular-devkit/schematics' ;
1+ import {
2+ Tree ,
3+ FileEntry ,
4+ CircularCollectionException
5+ } from '@angular-devkit/schematics' ;
26import {
37 SchematicTestRunner ,
48 UnitTestTree
59} from '@angular-devkit/schematics/testing' ;
610import * as path from 'path' ;
711import { 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+ } ;
920describe ( '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+ it ( '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
0 commit comments