Skip to content

Commit f5f7bb2

Browse files
authored
Merge pull request #57 from LinusU/build-script
🎉 Add support for "build" scripts
2 parents 347f5af + 443ad8f commit f5f7bb2

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

lib/builder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ exports.createZipFile = async function (directory, { customEntrypoint, sshKey })
3737
const cleanPackageInfo = Object.assign({}, packageInfo, { version: '0.0.0', scripts: undefined })
3838
const cleanPackageLock = hasYarnLockfile ? null : Object.assign({}, packageLock, { version: '0.0.0' })
3939

40-
const hasPrepare = Boolean(packageInfo.scripts && packageInfo.scripts.prepare)
40+
const hasBuildScript = Boolean(packageInfo.scripts && packageInfo.scripts.build)
41+
const hasPrepareScript = Boolean(packageInfo.scripts && packageInfo.scripts.prepare)
4142
const hasProductionDependencies = Boolean(packageInfo.dependencies && Object.keys(packageInfo.dependencies).length)
42-
const dockerfileSource = dockerfile.generate({ hasPrepare, hasProductionDependencies, sshKey: Boolean(sshKey), yarn: hasYarnLockfile })
43+
const dockerfileSource = dockerfile.generate({ hasBuildScript, hasPrepareScript, hasProductionDependencies, sshKey: Boolean(sshKey), yarn: hasYarnLockfile })
4344

4445
const hasDockerignore = await pathExists(path.join(directory, '.dockerignore'))
4546
const hasGitignore = await pathExists(path.join(directory, '.gitignore'))

lib/dockerfile.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ const sshPrefix = 'echo $SSH_PRIVATE_KEY | base64 --decode > $HOME/.ssh/id_rsa &
44

55
/**
66
* @param {object} options
7-
* @param {boolean} options.hasPrepare
7+
* @param {boolean} options.hasBuildScript
8+
* @param {boolean} options.hasPrepareScript
89
* @param {boolean} options.hasProductionDependencies
910
* @param {boolean} options.sshKey
1011
* @param {boolean} options.yarn
1112
* @returns {string}
1213
*/
1314
exports.generate = function (options) {
14-
const { hasPrepare, hasProductionDependencies, sshKey, yarn } = options
15+
const { hasBuildScript, hasPrepareScript, hasProductionDependencies, sshKey, yarn } = options
1516
const lines = []
1617

1718
// Base image on Amazon Linux
@@ -43,8 +44,8 @@ exports.generate = function (options) {
4344
if (hasProductionDependencies) lines.push(`RUN ${sshKey ? sshPrefix : ''}${yarn ? 'yarn install --production --frozen-lockfile' : 'npm ci --production'}${sshKey ? sshPostfix : ''}`)
4445
if (hasProductionDependencies) lines.push('RUN zip -9qyr /output.zip node_modules')
4546

46-
// Install dev-dependencies, if there is a `prepare` script present
47-
if (hasPrepare) lines.push(`RUN ${sshKey ? sshPrefix : ''}${yarn ? 'yarn install --frozen-lockfile' : 'npm ci'}${sshKey ? sshPostfix : ''}`)
47+
// Install dev-dependencies, if there is a `prepare` or `build` script present
48+
if (hasPrepareScript || hasBuildScript) lines.push(`RUN ${sshKey ? sshPrefix : ''}${yarn ? 'yarn install --frozen-lockfile' : 'npm ci'}${sshKey ? sshPostfix : ''}`)
4849

4950
// Add the app files, and remove our special files
5051
lines.push('COPY . .')
@@ -54,8 +55,9 @@ exports.generate = function (options) {
5455
lines.push('RUN rm scandium-clean-package.json scandium-clean-package-lock.json scandium-dockerfile')
5556
}
5657

57-
// Run the `prepare` script, if present
58-
if (hasPrepare) lines.push('RUN npm run-script prepare')
58+
// Run the `prepare` or `build` script, if present
59+
if (hasPrepareScript) lines.push('RUN npm run-script prepare')
60+
else if (hasBuildScript) lines.push('RUN npm run-script build')
5961

6062
// Add the local code to the zip
6163
lines.push('RUN rm -r node_modules')

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ scandium update --name=my-awesome-api
3636

3737
By default, Scandium will set up an API Gateway that simply forwards all requests to the Lambda function. If you want to utilise the benefits of API Gateway fully, you can provide a Swagger file describing your API endpoints. Pass the `--swagger=my-api-definition.yml` to either the `create` or `update` command and Scandium will configure the API Gateway for you.
3838

39-
## `prepare`-scripts
39+
## `prepare`/`build`-scripts
4040

41-
Scandium has support for `prepare` scripts, if the script is present in the `package.json` it will make sure that the script is being run with full `devDependencies` installed. The final package being uploaded to Lambda will still only contain the production `dependencies`.
41+
Scandium has support for `prepare`/`build` scripts, if the script is present in the `package.json` it will make sure that the script is being run with full `devDependencies` installed. The final package being uploaded to Lambda will still only contain the production `dependencies`.
42+
43+
If both a `prepare` and a `build` script is present, Scandium will pick the `prepare` script.
4244

4345
## Ignore files
4446

0 commit comments

Comments
 (0)