1
1
import * as fs from 'fs-extra' ;
2
2
import * as path from 'path' ;
3
- import {
4
- compose ,
5
- forEach ,
6
- head ,
7
- includes ,
8
- is ,
9
- isEmpty ,
10
- join ,
11
- map ,
12
- mergeRight ,
13
- pick ,
14
- replace ,
15
- split ,
16
- startsWith ,
17
- tail ,
18
- toPairs ,
19
- uniq ,
20
- } from 'ramda' ;
3
+ import { compose , forEach , head , includes , isEmpty , join , map , mergeRight , pick , replace , tail , toPairs , uniq } from 'ramda' ;
21
4
22
5
import * as Packagers from './packagers' ;
23
6
import { JSONObject } from './types' ;
@@ -28,7 +11,7 @@ function rebaseFileReferences(pathToPackageRoot: string, moduleVersion: string)
28
11
return replace (
29
12
/ \\ / g,
30
13
'/' ,
31
- `${ startsWith ( 'file:' , moduleVersion ) ? 'file:' : '' } ${ pathToPackageRoot } /${ filePath } `
14
+ `${ moduleVersion . startsWith ( 'file:' ) ? 'file:' : '' } ${ pathToPackageRoot } /${ filePath } `
32
15
) ;
33
16
}
34
17
@@ -40,9 +23,9 @@ function rebaseFileReferences(pathToPackageRoot: string, moduleVersion: string)
40
23
*/
41
24
function addModulesToPackageJson ( externalModules : string [ ] , packageJson : JSONObject , pathToPackageRoot : string ) {
42
25
forEach ( externalModule => {
43
- const splitModule = split ( '@' , externalModule ) ;
26
+ const splitModule = externalModule . split ( '@' ) ;
44
27
// If we have a scoped module we have to re-add the @
45
- if ( startsWith ( '@' , externalModule ) ) {
28
+ if ( externalModule . startsWith ( '@' ) ) {
46
29
splitModule . splice ( 0 , 1 ) ;
47
30
splitModule [ 0 ] = '@' + splitModule [ 0 ] ;
48
31
}
@@ -57,8 +40,7 @@ function addModulesToPackageJson(externalModules: string[], packageJson: JSONObj
57
40
/**
58
41
* Resolve the needed versions of production dependencies for external modules.
59
42
*/
60
- function getProdModules ( externalModules : { external : string } [ ] , packagePath : string , dependencyGraph : JSONObject ) {
61
- const packageJsonPath = path . join ( process . cwd ( ) , packagePath ) ;
43
+ function getProdModules ( externalModules : { external : string } [ ] , packageJsonPath : string , dependencyGraph : JSONObject ) {
62
44
// eslint-disable-next-line @typescript-eslint/no-var-requires
63
45
const packageJson = require ( packageJsonPath ) ;
64
46
const prodModules : string [ ] = [ ] ;
@@ -78,7 +60,7 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
78
60
// Check if the module has any peer dependencies and include them too
79
61
try {
80
62
const modulePackagePath = path . join (
81
- path . dirname ( path . join ( process . cwd ( ) , packagePath ) ) ,
63
+ path . dirname ( packageJsonPath ) ,
82
64
'node_modules' ,
83
65
externalModule . external ,
84
66
'package.json'
@@ -88,7 +70,7 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
88
70
console . log ( `Adding explicit peers for dependency ${ externalModule . external } ` ) ;
89
71
const peerModules = getProdModules (
90
72
compose ( map ( ( [ external ] ) => ( { external } ) ) , toPairs ) ( peerDependencies ) ,
91
- packagePath ,
73
+ packageJsonPath ,
92
74
dependencyGraph
93
75
) ;
94
76
Array . prototype . push . apply ( prodModules , peerModules ) ;
@@ -123,8 +105,7 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
123
105
}
124
106
125
107
/**
126
- * We need a performant algorithm to install the packages for each single
127
- * function (in case we package individually).
108
+ * We need a performant algorithm to install the packages for each single function.
128
109
* (1) We fetch ALL packages needed by ALL functions in a first step
129
110
* and use this as a base npm checkout. The checkout will be done to a
130
111
* separate temporary directory with a package.json that contains everything.
@@ -135,31 +116,29 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
135
116
* This will utilize the npm cache at its best and give us the needed results
136
117
* and performance.
137
118
*/
138
- export function packExternalModules ( externals : string [ ] , compositeModulePath : string ) {
119
+ export function packExternalModules ( externals : string [ ] , cwd : string , compositeModulePath : string ) {
139
120
if ( ! externals || ! externals . length ) {
140
121
return ;
141
122
}
142
123
143
- // Read plugin configuration
144
- const packagePath = './package.json' ;
145
- const packageJsonPath = path . join ( process . cwd ( ) , packagePath ) ;
124
+ // Read function package.json
125
+ const packageJsonPath = path . join ( cwd , 'package.json' ) ;
146
126
147
127
// Determine and create packager
148
128
const packager = Packagers . get ( Packagers . Installer . NPM ) ;
149
129
150
130
// Fetch needed original package.json sections
151
- const sectionNames = packager . copyPackageSectionNames ;
152
131
const packageJson = fs . readJsonSync ( packageJsonPath ) ;
153
- const packageSections = pick ( sectionNames , packageJson ) ;
132
+ const packageSections = pick ( packager . copyPackageSectionNames , packageJson ) ;
154
133
155
134
// Get first level dependency graph
156
135
console . log ( `Fetch dependency graph from ${ packageJsonPath } ` ) ;
157
136
158
137
const dependencyGraph = packager . getProdDependencies ( path . dirname ( packageJsonPath ) , 1 ) ;
159
138
160
139
// (1) Generate dependency composition
161
- const externalModules = map ( external => ( { external } ) , externals ) ;
162
- const compositeModules : JSONObject = uniq ( getProdModules ( externalModules , packagePath , dependencyGraph ) ) ;
140
+ const externalModules = externals . map ( external => ( { external } ) ) ;
141
+ const compositeModules : JSONObject = uniq ( getProdModules ( externalModules , packageJsonPath , dependencyGraph ) ) ;
163
142
164
143
if ( isEmpty ( compositeModules ) ) {
165
144
// The compiled code does not reference any external modules at all
@@ -168,21 +147,20 @@ export function packExternalModules(externals: string[], compositeModulePath: st
168
147
}
169
148
170
149
// (1.a) Install all needed modules
171
- const compositePackageJson = path . join ( compositeModulePath , 'package.json' ) ;
150
+ const compositePackageJsonPath = path . join ( compositeModulePath , 'package.json' ) ;
172
151
173
152
// (1.a.1) Create a package.json
174
- const compositePackage = mergeRight (
153
+ const compositePackageJson = mergeRight (
175
154
{
176
155
name : 'externals' ,
177
156
version : '1.0.0' ,
178
- description : `Packaged externals for ${ 'externals' } ` ,
179
157
private : true ,
180
158
} ,
181
159
packageSections
182
160
) ;
183
161
const relativePath = path . relative ( compositeModulePath , path . dirname ( packageJsonPath ) ) ;
184
- addModulesToPackageJson ( compositeModules , compositePackage , relativePath ) ;
185
- fs . writeJsonSync ( compositePackageJson , compositePackage ) ;
162
+ addModulesToPackageJson ( compositeModules , compositePackageJson , relativePath ) ;
163
+ fs . writeJsonSync ( compositePackageJsonPath , compositePackageJson ) ;
186
164
187
165
// (1.a.2) Copy package-lock.json if it exists, to prevent unwanted upgrades
188
166
const packageLockPath = path . join ( path . dirname ( packageJsonPath ) , packager . lockfileName ) ;
@@ -192,10 +170,6 @@ export function packExternalModules(externals: string[], compositeModulePath: st
192
170
try {
193
171
let packageLockFile = fs . readJsonSync ( packageLockPath ) ;
194
172
packageLockFile = packager . rebaseLockfile ( relativePath , packageLockFile ) ;
195
- if ( is ( Object ) ( packageLockFile ) ) {
196
- packageLockFile = JSON . stringify ( packageLockFile , null , 2 ) ;
197
- }
198
-
199
173
fs . writeJsonSync ( path . join ( compositeModulePath , packager . lockfileName ) , packageLockFile ) ;
200
174
} catch ( err ) {
201
175
console . log ( `Warning: Could not read lock file: ${ err . message } ` ) ;
0 commit comments