Skip to content

Commit

Permalink
components: $asset
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Sep 17, 2024
1 parent bfc338a commit 7e43a34
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 80 deletions.
4 changes: 1 addition & 3 deletions examples/aws-router-bucket/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export default $config({
bucket: bucket.name,
key: "public/spongebob.svg",
contentType: "image/svg+xml",
source: new $util.asset.FileAsset(
path.join($cli.paths.root, "spongebob.svg")
),
source: $asset("spongebob.svg"),
});

const router = new sst.aws.Router("MyRouter", {
Expand Down
17 changes: 17 additions & 0 deletions platform/src/components/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import {
Inputs,
runtime,
output,
asset as pulumiAsset,
} from "@pulumi/pulumi";
import { physicalName } from "./naming.js";
import { VisibleError } from "./error.js";
import { getRegionOutput } from "@pulumi/aws";
import path from "path";
import { statSync } from "fs";

/**
* Helper type to inline nested types
Expand Down Expand Up @@ -374,6 +377,20 @@ export function $transform<T, Args, Options>(
});
}

export function $asset(assetPath: string) {
const fullPath = path.isAbsolute(assetPath)
? assetPath
: path.join($cli.paths.root, assetPath);

try {
return statSync(fullPath).isDirectory()
? new pulumiAsset.FileArchive(fullPath)
: new pulumiAsset.FileAsset(fullPath);
} catch (e) {
throw new VisibleError(`Asset not found: ${fullPath}`);
}
}

export function $lazy<T>(fn: () => T) {
return output(undefined)
.apply(async () => output(fn()))
Expand Down
43 changes: 42 additions & 1 deletion platform/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface $APP
* The providers currently being used in the app.
*/
providers: App["providers"];
}> { }
}> {}

declare global {
// @ts-expect-error
Expand Down Expand Up @@ -239,6 +239,47 @@ declare global {
*/
export const $transform: typeof import("./components/component").$transform;

/**
* Packages a file or directory into a Pulumi asset.
*
* When the asset path is a file, this returns a `FileAsset`.
* When the asset path is a directory, this returns a `FileArchive` with the contents of the directory.
*
* If the asset path is absolute, it'll be used as is. Otherwise, it'll be relative to the root of the app.
*
* @example
*
* #### Uploading a file to S3
*
* If you have a file inside the `images` directory at the root of your app, you can upload it
* to S3 like this.
*
* ```ts title="sst.config.ts" {7}
* const bucket = new aws.s3.Bucket("MyBucket");
*
* new aws.s3.BucketObjectv2("MyImage", {
* bucket: bucket.name,
* key: "public/spongebob.svg",
* contentType: "image/svg+xml",
* source: $asset("images/spongebob.svg"),
* });
* ```
*
* #### Uploading a directory as a zip
*
* You can also use this to zip up the files in the "files" directory and upload it to S3.
*
* ```ts title="sst.config.ts" {5}
* new aws.s3.BucketObjectv2("MyZip", {
* bucket: bucket.name,
* key: "public/spongebob.zip",
* contentType: "application/zip",
* source: $asset("files"),
* });
* ```
*/
export const $asset: typeof import("./components/component").$asset;

/**
* Returns `true` if the app is running in `sst dev`.
*/
Expand Down
3 changes: 2 additions & 1 deletion platform/src/shim/run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as util from "@pulumi/pulumi";
import { Link } from "../components/link";
import { $config } from "../config";
import { $transform } from "../components/component";
import { $transform, $asset } from "../components/component";

const $secrets = JSON.parse(process.env.SST_SECRETS || "{}");
const { output, apply, all, interpolate, concat, jsonParse, jsonStringify } =
Expand All @@ -18,6 +18,7 @@ export {
jsonParse as "$jsonParse",
jsonStringify as "$jsonStringify",
util as "$util",
$asset as "$asset",
$config as "$config",
$transform as "$transform",
$secrets as "$secrets",
Expand Down
Loading

0 comments on commit 7e43a34

Please sign in to comment.