Skip to content

Commit

Permalink
fix: include individual extra files when not using individual packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Calle Kabo committed Apr 11, 2021
1 parent 9403b93 commit 855db0c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/pack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as path from 'path';
import { intersection, isEmpty, path as get, without } from 'ramda';
import { intersection, isEmpty, lensProp, map, over, path as get, pipe, reject, replace, test, without } from 'ramda';
import * as semver from 'semver';
import { EsbuildPlugin, SERVERLESS_FOLDER } from '.';
import { doSharePath, flatDep, getDepsFromBundle } from './helper';
import * as Packagers from './packagers';
import { IFiles } from './types';
import { humanSize, zip } from './utils';

function setFunctionArtifactPath(this: EsbuildPlugin, func, artifactPath) {
Expand Down Expand Up @@ -38,7 +39,7 @@ export async function pack(this: EsbuildPlugin) {
);

// get a list of all path in build
const files: { localPath: string; rootPath: string }[] = glob
const files: IFiles = glob
.sync('**', {
cwd: this.buildDirPath,
dot: true,
Expand All @@ -57,8 +58,14 @@ export async function pack(this: EsbuildPlugin) {
const zipName = `${this.serverless.service.service}.zip`;
const artifactPath = path.join(this.workDirPath, SERVERLESS_FOLDER, zipName);

// remove prefixes from individual extra files
const filesPathList = pipe<IFiles, IFiles, IFiles>(
reject(test(/^__only_[^/]+$/)) as (x: IFiles) => IFiles,
map(over(lensProp('localPath'), replace(/^__only_[^/]+\//, '')))
)(files);

const startZip = Date.now();
await zip(artifactPath, files);
await zip(artifactPath, filesPathList);
const { size } = fs.statSync(artifactPath);

this.serverless.cli.log(
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type JSONObject = any;

export interface IFile {
readonly localPath: string
readonly rootPath: string
}
export type IFiles = readonly IFile[];
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path';
import { join } from 'ramda';
import { IFiles } from './types';

export class SpawnError extends Error {
constructor(message: string, public stdout: string, public stderr: string) {
Expand Down Expand Up @@ -91,7 +92,7 @@ export const humanSize = (size: number) => {
return `${sanitized} ${['B', 'KB', 'MB', 'GB', 'TB'][i]}`;
};

export const zip = (zipPath: string, filesPathList: { rootPath: string; localPath: string }[]) => {
export const zip = (zipPath: string, filesPathList: IFiles) => {
fs.mkdirpSync(path.dirname(zipPath));

const zip = archiver.create('zip');
Expand Down

0 comments on commit 855db0c

Please sign in to comment.