2021-11-04 08:05:24 +01:00
|
|
|
// Yarn is a package manager for Javascript applications
|
|
|
|
package yarn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"dagger.io/dagger"
|
|
|
|
|
2022-01-19 18:50:06 +01:00
|
|
|
"universe.dagger.io/alpine"
|
2021-11-04 08:05:24 +01:00
|
|
|
"universe.dagger.io/bash"
|
2022-02-12 08:28:11 +01:00
|
|
|
"universe.dagger.io/docker"
|
2021-11-04 08:05:24 +01:00
|
|
|
)
|
|
|
|
|
2022-02-17 21:28:41 +01:00
|
|
|
#Build: #Run & {
|
|
|
|
buildDir: *"build" | string
|
|
|
|
script: *"build" | string
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:05:24 +01:00
|
|
|
// Build a Yarn package
|
2022-02-17 21:28:41 +01:00
|
|
|
#Run: {
|
2022-02-07 06:57:06 +01:00
|
|
|
// Custom name for the build.
|
|
|
|
// When building different apps in the same plan, assign
|
|
|
|
// different names for optimal caching.
|
|
|
|
name: string | *""
|
|
|
|
|
2021-11-04 08:05:24 +01:00
|
|
|
// Application source code
|
|
|
|
source: dagger.#FS
|
|
|
|
|
|
|
|
// working directory to use
|
|
|
|
cwd: *"." | string
|
|
|
|
|
|
|
|
// Write the contents of `environment` to this file,
|
|
|
|
// in the "envfile" format
|
|
|
|
writeEnvFile: string | *""
|
|
|
|
|
|
|
|
// Read build output from this directory
|
|
|
|
// (path must be relative to working directory)
|
2022-02-17 21:28:41 +01:00
|
|
|
buildDir?: string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
// Run this yarn script
|
2022-02-17 21:28:41 +01:00
|
|
|
script: string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
2022-01-14 02:43:34 +01:00
|
|
|
// Fix for shadowing issues
|
|
|
|
let yarnScript = script
|
|
|
|
|
2021-11-04 08:05:24 +01:00
|
|
|
// Optional arguments for the script
|
|
|
|
args: [...string] | *[]
|
|
|
|
|
|
|
|
// Secret variables
|
2022-01-14 02:43:34 +01:00
|
|
|
// FIXME: not implemented. Are they needed?
|
2021-11-04 08:05:24 +01:00
|
|
|
secrets: [string]: dagger.#Secret
|
|
|
|
|
2022-01-19 18:50:06 +01:00
|
|
|
// FIXME: Yarn's version depends on Alpine's version
|
2021-11-04 08:05:24 +01:00
|
|
|
// Yarn version
|
2022-01-19 18:50:06 +01:00
|
|
|
// yarnVersion: *"=~1.22" | string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
2022-01-19 18:59:06 +01:00
|
|
|
// FIXME: custom base image not supported
|
2022-02-08 18:54:42 +01:00
|
|
|
_buildImage: alpine.#Build & {
|
2022-01-19 18:58:38 +01:00
|
|
|
packages: {
|
2022-01-19 18:59:06 +01:00
|
|
|
bash: {}
|
|
|
|
yarn: {}
|
2022-01-19 18:58:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-14 02:43:34 +01:00
|
|
|
|
2022-02-08 18:54:42 +01:00
|
|
|
// Run yarn in a docker container
|
|
|
|
container: bash.#Run & {
|
2022-02-12 08:28:11 +01:00
|
|
|
input: *_buildImage.output | docker.#Image
|
2022-02-08 18:54:42 +01:00
|
|
|
|
|
|
|
// FIXME: move shell script to its own file
|
|
|
|
script: contents: #"""
|
2021-11-04 08:05:24 +01:00
|
|
|
# Create $ENVFILE_NAME file if set
|
|
|
|
[ -n "$ENVFILE_NAME" ] && echo "$ENVFILE" > "$ENVFILE_NAME"
|
|
|
|
|
|
|
|
yarn --cwd "$YARN_CWD" install --production false
|
|
|
|
|
|
|
|
opts=( $(echo $YARN_ARGS) )
|
|
|
|
yarn --cwd "$YARN_CWD" run "$YARN_BUILD_SCRIPT" ${opts[@]}
|
2022-02-17 21:28:41 +01:00
|
|
|
if [ ! -z "${YARN_BUILD_DIRECTORY:-}" ]; then
|
|
|
|
mv "$YARN_BUILD_DIRECTORY" /build
|
|
|
|
else
|
|
|
|
mkdir /build
|
|
|
|
fi
|
2022-01-14 02:43:34 +01:00
|
|
|
"""#
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
mounts: {
|
|
|
|
"yarn cache": {
|
|
|
|
dest: "/cache/yarn"
|
2022-02-16 19:37:16 +01:00
|
|
|
contents: dagger.#CacheDir & {
|
2022-02-07 06:57:06 +01:00
|
|
|
// FIXME: are there character limitations in cache ID?
|
|
|
|
id: "universe.dagger.io/yarn.#Build \(name)"
|
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
}
|
|
|
|
"package source": {
|
|
|
|
dest: "/src"
|
|
|
|
contents: source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 23:59:51 +01:00
|
|
|
export: directories: "/build": _
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
env: {
|
2022-02-17 21:28:41 +01:00
|
|
|
YARN_BUILD_SCRIPT: yarnScript
|
|
|
|
YARN_ARGS: strings.Join(args, "\n")
|
|
|
|
YARN_CACHE_FOLDER: "/cache/yarn"
|
|
|
|
YARN_CWD: cwd
|
|
|
|
if buildDir != _|_ {
|
|
|
|
YARN_BUILD_DIRECTORY: buildDir
|
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
if writeEnvFile != "" {
|
|
|
|
ENVFILE_NAME: writeEnvFile
|
|
|
|
ENVFILE: strings.Join([ for k, v in env {"\(k)=\(v)"}], "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
workdir: "/src"
|
|
|
|
}
|
|
|
|
|
|
|
|
// The final contents of the package after build
|
2022-02-08 18:54:42 +01:00
|
|
|
output: container.export.directories."/build".contents
|
2021-11-04 08:05:24 +01:00
|
|
|
}
|