Skip to content

Commit

Permalink
try hooks a different way
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuss committed May 19, 2024
1 parent 424be00 commit c40ef1b
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class ServerlessMake {
this.log = new Log(options);

this.commands = {
[PLUGIN_NAME]: {},
[PLUGIN_NAME]: {
lifecycleEvents: [this.target],
},
};

this.hooks = this.setupHooks();
Expand All @@ -151,9 +153,18 @@ class ServerlessMake {
};
}

get target(): string {
return this.pluginConfig.target || "";
}

get afterTarget(): string {
return `after:make${this.target ? `:${this.target}` : ""}`;
}

setupHooks = () => {
const hooks: Hooks = {
initialize: async () => {},
[this.afterTarget]: async () => {},
"before:offline:start": async () => {
this.log.verbose("before:offline:start");
let errored = false;
Expand Down Expand Up @@ -192,7 +203,7 @@ class ServerlessMake {
const pluginHooks = this.pluginConfig.hooks || {};

Object.entries(pluginHooks).forEach(([hook, target]) => {
if (hooks[hook]) {
if (hooks[hook] && hook !== this.afterTarget) {
this.log.warning(
`Unable to override registered internal hook "${hook}"!`
);
Expand Down Expand Up @@ -226,7 +237,7 @@ class ServerlessMake {
};

build = async (watch?: boolean): Promise<void> => {
const { makefile } = await this.make(this.pluginConfig.target || "");
const { makefile } = await this.make(this.target);

if (watch) {
const paths = [
Expand Down Expand Up @@ -260,7 +271,13 @@ class ServerlessMake {
});
}

await this.serverless.pluginManager.spawn("after:make");
try {
await this.serverless.pluginManager.spawn(this.afterTarget);
} catch (e) {
if (e instanceof Error) {
this.log.warning(`Unable to spawn ${this.afterTarget}`);
}
}
};
}

Expand Down

0 comments on commit c40ef1b

Please sign in to comment.