diff --git a/src/pack.ts b/src/pack.ts index 4dca3ddf..8f6648ac 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -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) { @@ -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, @@ -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( + 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( diff --git a/src/types.ts b/src/types.ts index f550abad..042159c5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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[]; diff --git a/src/utils.ts b/src/utils.ts index 4f7de55b..96f8c8fb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) { @@ -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');