Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
fix: Clean up .serverless folder before packaging new artifact (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarlow12 committed Sep 13, 2019
1 parent 777cc5f commit f5d50dd
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 40 deletions.
66 changes: 45 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"md5": "^2.2.1",
"open": "^6.3.0",
"request": "^2.81.0",
"rimraf": "^2.6.3",
"rimraf": "^2.7.1",
"semver": "^6.3.0",
"xml": "^1.0.1"
},
Expand Down
61 changes: 60 additions & 1 deletion src/models/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,71 @@ export interface ServerlessAzureProvider {
runtime: string;
}

/**
* Expanding type of incomplete `Serverless` type.
*/
export interface ServerlessObject {
/** Input provided by user */
processedInput: {
/** Commands issued after `serverless` */
commands: ServerlessCliCommand[];
/** Flags provided in CLI */
options: any;
};
/** Provider specific configuration */
service: ServerlessAzureConfig;
/** Provider agnostic configuration */
config: ServerlessConfig;
}

/**
* Commands that can be issued in CLI
*/
export enum ServerlessCliCommand {
/** Package service */
PACKAGE = "package",
/** Deploy service */
DEPLOY = "deploy",
/** Invoke HTTP endpoint */
INVOKE = "invoke",
/** Roll back service */
ROLLBACK = "rollback",
/** Run service offline */
OFFLINE = "offline",
/** Start pre-built offline service - subcommand for offline*/
START = "start",
/** Build an offline service - subcommand for offline */
BUILD = "build",
/** Cleanup files from offline build - subcommand for offline */
CLEANUP = "cleanup",
/** List deployments - subcommand for deploy */
LIST = "list",
/** Command to add or remove functions */
FUNC = "func",
/** Add a function - subcommand for func */
ADD = "add",
/** Remove a function - subcommand for func */
REMOVE = "remove"
}

/** Vendor agnostic Serverless configuration */
export interface ServerlessConfig {
/** Path to root Serverless service */
servicePath: string;
/** Path to packaged artifact of service */
packagePath: string;
}

export interface ServerlessAzureConfig {
service: string;
provider: ServerlessAzureProvider;
plugins: string[];
functions: any;
package: any;
package: {
individually: boolean;
artifactDirectoryName: string;
artifact: string;
};
}

export interface ServerlessAzureFunctionConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/apim/azureApimServicePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("APIM Service Plugin", () => {
});

it("can be instantiated", () => {
const serverless = new Serverless();
const serverless = MockFactory.createTestServerless();
const options: Serverless.Options = {
stage: "",
region: "",
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/azureBasePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { Guard } from "../shared/guard";
import Serverless from "serverless";
import { ServerlessAzureConfig, ServerlessCliCommand,
ServerlessCommandMap, ServerlessHookMap, ServerlessObject } from "../models/serverless";
import { Guard } from "../shared/guard";
import { Utils } from "../shared/utils";
import { ServerlessCommandMap, ServerlessHookMap, ServerlessAzureConfig } from "../models/serverless";

export abstract class AzureBasePlugin<TOptions=Serverless.Options> {

public hooks: ServerlessHookMap
protected config: ServerlessAzureConfig;
protected commands: ServerlessCommandMap;
protected processedCommands: ServerlessCliCommand[];

public constructor(
protected serverless: Serverless,
protected options: TOptions,
) {
Guard.null(serverless);
this.config = serverless.service as any;
this.processedCommands = (serverless as any as ServerlessObject).processedInput.commands;
}

protected log(message: string) {
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/package/azurePackagePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ describe("Azure Package Plugin", () => {
it("sets creates function bindings before package:setupProviderConfiguration life cycle event", async () => {
await invokeHook(plugin, "before:package:setupProviderConfiguration");
expect(PackageService.prototype.createBindings).toBeCalled();
expect(PackageService.prototype.cleanUpServerlessDir).toBeCalled();
});

it("prepares the package for webpack before webpack:package:packageModules life cycle event", async () => {
await invokeHook(plugin, "before:webpack:package:packageModules");
expect(PackageService.prototype.createBindings).toBeCalled();
expect(PackageService.prototype.cleanUpServerlessDir).toBeCalled();
expect(PackageService.prototype.prepareWebpack).toBeCalled();
});

Expand All @@ -34,6 +36,7 @@ describe("Azure Package Plugin", () => {

expect(PackageService.prototype.createBindings).toBeCalledTimes(1);
expect(PackageService.prototype.prepareWebpack).toBeCalledTimes(1);
expect(PackageService.prototype.cleanUpServerlessDir).toBeCalledTimes(1);
});

it("cleans up package after package:finalize", async () => {
Expand Down Expand Up @@ -63,19 +66,22 @@ describe("Azure Package Plugin", () => {
it("does not call create bindings if package specified in options", async () => {
await invokeHook(plugin, "before:package:setupProviderConfiguration");
expect(PackageService.prototype.createBindings).not.toBeCalled();
expect(sls.cli.log).lastCalledWith("No need to create bindings. Using pre-existing package");
expect(sls.cli.log).lastCalledWith("Deploying pre-built package. No need to create bindings");
});

it("does not call webpack if package specified in options", async () => {
await invokeHook(plugin, "before:webpack:package:packageModules");
expect(PackageService.prototype.createBindings).not.toBeCalled();
expect(PackageService.prototype.prepareWebpack).not.toBeCalled();
expect(PackageService.prototype.cleanUpServerlessDir).not.toBeCalled();

expect(sls.cli.log).lastCalledWith("No need to perform webpack. Using pre-existing package");
});

it("does not call finalize if package specified in options", async () => {
await invokeHook(plugin, "after:package:finalize");
expect(PackageService.prototype.cleanUp).not.toBeCalled();
expect(PackageService.prototype.cleanUpServerlessDir).not.toBeCalled();
expect(sls.cli.log).lastCalledWith("No need to clean up generated folders & files. Using pre-existing package");
});
})
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/package/azurePackagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Serverless from "serverless";
import AzureProvider from "../../provider/azureProvider";
import { PackageService } from "../../services/packageService";
import { AzureBasePlugin } from "../azureBasePlugin";
import { ServerlessCliCommand } from "../../models/serverless";

export class AzurePackagePlugin extends AzureBasePlugin {
private bindingsCreated: boolean = false;
Expand All @@ -21,14 +22,15 @@ export class AzurePackagePlugin extends AzureBasePlugin {
}

private async setupProviderConfiguration(): Promise<void> {
if (this.getOption("package")) {
this.log("No need to create bindings. Using pre-existing package");
return Promise.resolve();
if (this.processedCommands[0] === ServerlessCliCommand.DEPLOY && this.getOption("package")) {
this.log("Deploying pre-built package. No need to create bindings");
return;
}
if (this.config.package && this.config.package.individually) {
throw new Error("Cannot package Azure Functions individually. " +
"Remove `individually` attribute from the `package` section of the serverless config");
}
this.packageService.cleanUpServerlessDir();
await this.packageService.createBindings();
this.bindingsCreated = true;

Expand Down
Loading

0 comments on commit f5d50dd

Please sign in to comment.