Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c7fa2f

Browse files
fix(externals): fix root path, and lock file bug
1 parent 6d7455f commit 3c7fa2f

File tree

4 files changed

+27
-53
lines changed

4 files changed

+27
-53
lines changed

‎src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class NodejsFunction extends lambda.Function {
9292
platform: 'node',
9393
});
9494

95-
packExternalModules(without(exclude, buildOptions.external || []), path.join(projectRoot, BUILD_FOLDER));
95+
packExternalModules(without(exclude, buildOptions.external || []), projectRoot,path.join(projectRoot, BUILD_FOLDER));
9696

9797
super(scope, id, {
9898
...props,

‎src/packExternalModules.ts

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
import * as fs from 'fs-extra';
22
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';
214

225
import * as Packagers from './packagers';
236
import { JSONObject } from './types';
@@ -28,7 +11,7 @@ function rebaseFileReferences(pathToPackageRoot: string, moduleVersion: string)
2811
return replace(
2912
/\\/g,
3013
'/',
31-
`${startsWith('file:',moduleVersion) ? 'file:' : ''}${pathToPackageRoot}/${filePath}`
14+
`${moduleVersion.startsWith('file:') ? 'file:' : ''}${pathToPackageRoot}/${filePath}`
3215
);
3316
}
3417

@@ -40,9 +23,9 @@ function rebaseFileReferences(pathToPackageRoot: string, moduleVersion: string)
4023
*/
4124
function addModulesToPackageJson(externalModules: string[], packageJson: JSONObject, pathToPackageRoot: string) {
4225
forEach(externalModule => {
43-
const splitModule = split('@',externalModule);
26+
const splitModule = externalModule.split('@');
4427
// If we have a scoped module we have to re-add the @
45-
if (startsWith('@',externalModule)) {
28+
if (externalModule.startsWith('@')) {
4629
splitModule.splice(0, 1);
4730
splitModule[0] = '@' + splitModule[0];
4831
}
@@ -57,8 +40,7 @@ function addModulesToPackageJson(externalModules: string[], packageJson: JSONObj
5740
/**
5841
* Resolve the needed versions of production dependencies for external modules.
5942
*/
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) {
6244
// eslint-disable-next-line @typescript-eslint/no-var-requires
6345
const packageJson = require(packageJsonPath);
6446
const prodModules: string[] = [];
@@ -78,7 +60,7 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
7860
// Check if the module has any peer dependencies and include them too
7961
try {
8062
const modulePackagePath = path.join(
81-
path.dirname(path.join(process.cwd(),packagePath)),
63+
path.dirname(packageJsonPath),
8264
'node_modules',
8365
externalModule.external,
8466
'package.json'
@@ -88,7 +70,7 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
8870
console.log(`Adding explicit peers for dependency ${externalModule.external}`);
8971
const peerModules = getProdModules(
9072
compose(map(([external]) => ({ external })), toPairs)(peerDependencies),
91-
packagePath,
73+
packageJsonPath,
9274
dependencyGraph
9375
);
9476
Array.prototype.push.apply(prodModules, peerModules);
@@ -123,8 +105,7 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
123105
}
124106

125107
/**
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.
128109
* (1) We fetch ALL packages needed by ALL functions in a first step
129110
* and use this as a base npm checkout. The checkout will be done to a
130111
* separate temporary directory with a package.json that contains everything.
@@ -135,31 +116,29 @@ function getProdModules(externalModules: { external: string }[], packagePath: st
135116
* This will utilize the npm cache at its best and give us the needed results
136117
* and performance.
137118
*/
138-
export function packExternalModules(externals: string[], compositeModulePath: string) {
119+
export function packExternalModules(externals: string[], cwd: string,compositeModulePath: string) {
139120
if (!externals || !externals.length) {
140121
return;
141122
}
142123

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');
146126

147127
// Determine and create packager
148128
const packager = Packagers.get(Packagers.Installer.NPM);
149129

150130
// Fetch needed original package.json sections
151-
const sectionNames = packager.copyPackageSectionNames;
152131
const packageJson = fs.readJsonSync(packageJsonPath);
153-
const packageSections = pick(sectionNames, packageJson);
132+
const packageSections = pick(packager.copyPackageSectionNames, packageJson);
154133

155134
// Get first level dependency graph
156135
console.log(`Fetch dependency graph from ${packageJsonPath}`);
157136

158137
const dependencyGraph = packager.getProdDependencies(path.dirname(packageJsonPath), 1);
159138

160139
// (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));
163142

164143
if (isEmpty(compositeModules)) {
165144
// The compiled code does not reference any external modules at all
@@ -168,21 +147,20 @@ export function packExternalModules(externals: string[], compositeModulePath: st
168147
}
169148

170149
// (1.a) Install all needed modules
171-
const compositePackageJson = path.join(compositeModulePath, 'package.json');
150+
const compositePackageJsonPath = path.join(compositeModulePath, 'package.json');
172151

173152
// (1.a.1) Create a package.json
174-
const compositePackage = mergeRight(
153+
const compositePackageJson = mergeRight(
175154
{
176155
name: 'externals',
177156
version: '1.0.0',
178-
description: `Packaged externals for ${'externals'}`,
179157
private: true,
180158
},
181159
packageSections
182160
);
183161
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);
186164

187165
// (1.a.2) Copy package-lock.json if it exists, to prevent unwanted upgrades
188166
const packageLockPath = path.join(path.dirname(packageJsonPath), packager.lockfileName);
@@ -192,10 +170,6 @@ export function packExternalModules(externals: string[], compositeModulePath: st
192170
try {
193171
let packageLockFile = fs.readJsonSync(packageLockPath);
194172
packageLockFile = packager.rebaseLockfile(relativePath, packageLockFile);
195-
if (is(Object)(packageLockFile)) {
196-
packageLockFile = JSON.stringify(packageLockFile, null, 2);
197-
}
198-
199173
fs.writeJsonSync(path.join(compositeModulePath, packager.lockfileName), packageLockFile);
200174
} catch (err) {
201175
console.log(`Warning: Could not read lock file: ${err.message}`);

‎src/packagers/npm.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { any, isEmpty, reduce,replace, split,startsWith } from 'ramda';
1+
import { any, isEmpty, replace, split } from 'ramda';
22

33
import { JSONObject } from '../types';
44
import { SpawnError, spawnProcess } from '../utils';
@@ -45,15 +45,15 @@ export class NPM implements Packager {
4545
if (err instanceof SpawnError) {
4646
// Only exit with an error if we have critical npm errors for 2nd level inside
4747
const errors = split('\n', err.stderr);
48-
const failed = reduce((f, error) => {
48+
const failed = errors.reduce((f, error) => {
4949
if (f) {
5050
return true;
5151
}
5252
return (
5353
!isEmpty(error) &&
54-
!any(ignoredError => startsWith(`npm ERR! ${ignoredError.npmError}`,error), ignoredNpmErrors)
54+
!any(ignoredError => error.startsWith(`npm ERR! ${ignoredError.npmError}`), ignoredNpmErrors)
5555
);
56-
}, false,errors);
56+
}, false);
5757

5858
if (!failed && !isEmpty(err.stdout)) {
5959
return { stdout: err.stdout };

‎src/packagers/yarn.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ export class Yarn implements Packager {
3737
} catch (err) {
3838
if (err instanceof SpawnError) {
3939
// Only exit with an error if we have critical npm errors for 2nd level inside
40-
const errors = split('\n',err.stderr);
41-
const failed = reduce((f, error) => {
40+
const errors = err.stderr.split('\n');
41+
const failed = errors.reduce((f, error) => {
4242
if (f) {
4343
return true;
4444
}
4545
return (
4646
!isEmpty(error) &&
47-
!any(ignoredError => startsWith(`npm ERR! ${ignoredError.npmError}`,error), ignoredYarnErrors)
47+
!any(ignoredError => error.startsWith(`npm ERR! ${ignoredError.npmError}`), ignoredYarnErrors)
4848
);
49-
}, false,errors);
49+
}, false);
5050

5151
if (!failed && !isEmpty(err.stdout)) {
5252
return { stdout: err.stdout };

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /