Skip to content

Commit

Permalink
feature request: Support build a project as monorepo-like (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
miyukki authored May 6, 2021
1 parent 0b71429 commit 382dabd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ custom:
binDir: .bin # target folder for binary files
cgo: 0 # CGO_ENABLED flag
cmd: GOOS=linux go build -ldflags="-s -w"' # compile command
monorepo: false # if enabled, builds function every directory (useful for monorepo where go.mod is managed by each function
```

## How does it work?
Expand Down
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ConfigDefaults = {
binDir: ".bin",
cgo: 0,
cmd: 'GOOS=linux go build -ldflags="-s -w"',
monorepo: false,
};

const GoRuntime = "go1.x";
Expand Down Expand Up @@ -102,13 +103,25 @@ module.exports = class Plugin {

const absHandler = path.resolve(config.baseDir);
const absBin = path.resolve(config.binDir);
const compileBinPath = path.join(path.relative(absHandler, absBin), name); // binPath is based on cwd no baseDir
let compileBinPath = path.join(path.relative(absHandler, absBin), name); // binPath is based on cwd no baseDir
let cwd = config.baseDir;
let handler = func.handler;
if (config.monorepo) {
if (func.handler.endsWith(".go")) {
cwd = path.relative(absHandler, path.dirname(func.handler));
handler = path.basename(handler);
} else {
cwd = path.relative(absHandler, func.handler);
handler = ".";
}
compileBinPath = path.relative(cwd, compileBinPath);
}
try {
const [env, command] = parseCommand(
`${config.cmd} -o ${compileBinPath} ${func.handler}`
`${config.cmd} -o ${compileBinPath} ${handler}`
);
await exec(command, {
cwd: config.baseDir,
cwd: cwd,
env: Object.assign(
{},
process.env,
Expand All @@ -119,7 +132,7 @@ module.exports = class Plugin {
} catch (e) {
this.serverless.cli.consoleLog(
`Go Plugin: ${chalk.yellow(
`Error compiling "${name}" function (cwd: ${config.baseDir}): ${e.message}`
`Error compiling "${name}" function (cwd: ${cwd}): ${e.message}`
)}`
);
process.exit(1);
Expand Down
48 changes: 48 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,54 @@ describe("Go Plugin", () => {
);
expect(execStub.firstCall.args[1].cwd).to.equal(".");
});

it("compiles Go function w/ monorepo", async () => {
// given
const config = merge(
{
service: {
custom: {
go: {
monorepo: true,
},
},
functions: {
testFunc1: {
name: "testFunc1",
runtime: "go1.x",
handler: "functions/func1",
},
testFunc2: {
name: "testFunc2",
runtime: "go1.x",
handler: "functions/func2/main.go",
},
},
},
},
serverlessStub
);
const plugin = new Plugin(config);

// when
await plugin.hooks["before:package:createDeploymentArtifacts"]();

// then
expect(config.service.functions.testFunc1.handler).to.equal(
`.bin/testFunc1`
);
expect(execStub).to.have.been.calledWith(
`go build -ldflags="-s -w" -o ../../.bin/testFunc1 .`
);
expect(execStub.firstCall.args[1].cwd).to.equal("functions/func1");
expect(config.service.functions.testFunc2.handler).to.equal(
`.bin/testFunc2`
);
expect(execStub).to.have.been.calledWith(
`go build -ldflags="-s -w" -o ../../.bin/testFunc2 main.go`
);
expect(execStub.secondCall.args[1].cwd).to.equal("functions/func2");
});
});

describe(`serverless go build`, () => {
Expand Down

0 comments on commit 382dabd

Please sign in to comment.