@@ -2,13 +2,14 @@ import * as path from 'path';
2
2
import { NgPackageConfig } from '../../ng-package.schema' ;
3
3
import { NgPackageData , DEFAULT_BUILD_FOLDER } from '../model/ng-package-data' ;
4
4
import { NgArtifacts } from '../model/ng-artifacts' ;
5
- import { copyFiles } from '../util/copy' ;
6
5
import { tryReadJson } from '../util/json' ;
7
6
import { readJson , writeJson , readdir , lstat , Stats } from 'fs-extra' ;
8
7
import { merge , isArray } from 'lodash' ;
9
8
import * as log from '../util/log' ;
10
9
import { PackageSearchResult } from '../model/package-search-result' ;
11
10
11
+ const PACKAGE_JSON_FILE_NAME = 'package.json' ;
12
+
12
13
// this prevents array objects from getting merged to each other one by one
13
14
function arrayMergeLogic ( objValue , srcValue ) {
14
15
if ( isArray ( objValue ) ) {
@@ -29,7 +30,7 @@ function resolvePaths(workingDirectory: string, packageConfig: NgPackageConfig):
29
30
}
30
31
} ;
31
32
32
- async function readNgPackageFile ( filePath : string ) : Promise < NgPackageConfig > {
33
+ async function readNgPackageFile ( filePath : string ) : Promise < NgPackageConfig | null > {
33
34
34
35
log . debug ( 'Searching for ng-package config at ' + filePath ) ;
35
36
try {
@@ -42,7 +43,7 @@ async function readNgPackageFile(filePath: string): Promise<NgPackageConfig> {
42
43
if ( error . code === 'ENOENT' ) {
43
44
log . debug ( 'ng-package config file not found' ) ;
44
45
// if the file does not exist, that's ok
45
- return { } ;
46
+ return null ;
46
47
}
47
48
throw error ;
48
49
}
@@ -102,7 +103,7 @@ async function findSecondaryPackagePaths(rootPackage: NgPackageData): Promise<st
102
103
if ( pathStats . isDirectory ( ) && ! shouldExcludeFromDirectorySearch ( fileSystemPath , foldersToExclude ) ) {
103
104
directoriesToSearch . push ( fullPath ) ;
104
105
} else if ( ! packageFileFound && pathStats . isFile ( ) ) {
105
- if ( fileSystemPath . endsWith ( 'package.json' ) ) {
106
+ if ( fileSystemPath . endsWith ( PACKAGE_JSON_FILE_NAME ) ) {
106
107
packagePaths . push ( fullPath ) ;
107
108
packageFileFound = true ;
108
109
// we can't `break` here because doing so might cause us to miss some directories
@@ -133,14 +134,16 @@ async function readRootPackage(filePath: string): Promise<NgPackageData> {
133
134
const baseDirectory = path . dirname ( filePath ) ;
134
135
135
136
// read custom ng-package config file
136
- let promiseChain : Promise < NgPackageConfig > = readNgPackageFile ( filePath ) ;
137
+ let promiseChain : Promise < NgPackageConfig > = readNgPackageFile ( filePath )
138
+ . then ( ( ngPkg : NgPackageConfig | null ) => ngPkg || { } ) ;
137
139
138
140
const defaultPath = path . join ( baseDirectory , 'ng-package.json' ) ;
139
141
if ( defaultPath !== filePath ) {
140
142
// read default ng-package config file
141
143
promiseChain = promiseChain . then ( async ( ngPkg : NgPackageConfig ) => {
142
- const otherNgPkg : NgPackageConfig = await readNgPackageFile ( defaultPath ) ;
144
+ const otherNgPkg : NgPackageConfig | null = await readNgPackageFile ( defaultPath ) ;
143
145
// merge both ng-package config objects
146
+ // merge will never return null
144
147
return merge ( ngPkg , otherNgPkg , arrayMergeLogic ) ;
145
148
} ) ;
146
149
}
@@ -152,7 +155,7 @@ async function readRootPackage(filePath: string): Promise<NgPackageData> {
152
155
// read 'package.json'
153
156
log . debug ( 'loading package.json' ) ;
154
157
155
- const pkg : any = await readJson ( path . resolve ( packageConfigurationDirectory , 'package.json' ) ) ;
158
+ const pkg : any = await readJson ( path . resolve ( packageConfigurationDirectory , PACKAGE_JSON_FILE_NAME ) ) ;
156
159
// merge package.json ng-package config
157
160
const finalPackageConfig = merge ( ngPkg , pkg . ngPackage , arrayMergeLogic ) ;
158
161
// make sure we provide default values for src and dest
@@ -168,14 +171,22 @@ async function readRootPackage(filePath: string): Promise<NgPackageData> {
168
171
) ;
169
172
}
170
173
171
- async function readSecondaryPackage ( rootPackage : NgPackageData , filePath : string ) : Promise < NgPackageData > {
174
+ async function readSecondaryPackage ( rootPackage : NgPackageData , filePath : string ) : Promise < NgPackageData | null > {
172
175
const baseDirectory = path . dirname ( filePath ) ;
173
176
const ngPackageFile = path . resolve ( baseDirectory , 'ng-package.json' ) ;
174
- const packageJsonFile = path . resolve ( baseDirectory , 'package.json' ) ;
177
+ const packageJsonFile = path . resolve ( baseDirectory , PACKAGE_JSON_FILE_NAME ) ;
175
178
176
- let ngPackage : NgPackageConfig = await readNgPackageFile ( ngPackageFile ) ;
179
+ let ngPackage : NgPackageConfig | null = await readNgPackageFile ( ngPackageFile ) ;
177
180
const packageJson : any = await tryReadJson ( packageJsonFile ) ;
178
181
182
+ // if we don't detect any explicit package configurations, then ignore
183
+ if ( ! ngPackage ) {
184
+ if ( ! packageJson || ! packageJson . ngPackage ) {
185
+ log . debug ( `No secondary package found in ${ baseDirectory } ` ) ;
186
+ return null ;
187
+ }
188
+ }
189
+
179
190
ngPackage = merge ( ngPackage , packageJson . ngPackage , arrayMergeLogic ) ;
180
191
if ( ! ngPackage . lib ) {
181
192
ngPackage . lib = { } ;
@@ -212,7 +223,10 @@ export async function discoverPackages(rootPath: string): Promise<PackageSearchR
212
223
const secondaryPackagePromises : Promise < NgPackageData > [ ] = secondaryPackagePaths
213
224
. map ( secondaryPackagePath => readSecondaryPackage ( rootPackage , secondaryPackagePath ) ) ;
214
225
215
- const secondaryPackages : NgPackageData [ ] = await Promise . all ( secondaryPackagePromises ) ;
226
+ const secondaryPackagesFromPaths : ( NgPackageData | null ) [ ] = await Promise . all ( secondaryPackagePromises ) ;
227
+
228
+ // The packages that are null should be excluded because they were not explicitly meant to be secondary entries
229
+ const secondaryPackages : NgPackageData [ ] = secondaryPackagesFromPaths . filter ( x => ! ! x ) ;
216
230
217
231
return {
218
232
rootPackage,
@@ -231,13 +245,17 @@ export async function discoverPackages(rootPath: string): Promise<PackageSearchR
231
245
export async function writePackage ( ngPkg : NgPackageData , packageArtifacts : NgArtifacts ) : Promise < void > {
232
246
233
247
log . debug ( 'writePackage' ) ;
234
- const packageJson : any = await readJson ( path . resolve ( ngPkg . sourcePath , 'package.json' ) ) ;
248
+ const packageJson : any = await readJson ( path . resolve ( ngPkg . sourcePath , PACKAGE_JSON_FILE_NAME ) ) ;
235
249
// set additional properties
236
250
for ( const fieldName in packageArtifacts ) {
237
251
packageJson [ fieldName ] = packageArtifacts [ fieldName ] ;
238
252
}
239
253
240
254
packageJson . name = ngPkg . fullPackageName ;
241
255
242
- await writeJson ( `${ ngPkg . destinationPath } /package.json` , packageJson ) ;
256
+ // keep the dist package.json clean
257
+ // this will not throw if ngPackage field does not exist
258
+ delete packageJson . ngPackage ;
259
+
260
+ await writeJson ( path . resolve ( ngPkg . destinationPath , PACKAGE_JSON_FILE_NAME ) , packageJson ) ;
243
261
}
0 commit comments