Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions lib/stac-browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export class StacBrowser extends Construct {

}

/**
* Extracts the major version number from a stac-browser git tag, e.g.
* "v4.0.1" -> 4, "v5.0.0-rc.2" -> 5, "4.0.1" -> 4.
* Returns null if the tag doesn't match a recognizable semver-ish pattern
* (e.g. a branch name or commit SHA was passed instead of a version tag).
*/
private static parseMajorVersion(tag: string): number | null {
const match = tag.match(/^v?(\d+)\./);
return match ? parseInt(match[1], 10) : null;
}

private buildApp(props: StacBrowserProps, cloneDirectory: string): string {

// Define where to clone and build
Expand Down Expand Up @@ -102,11 +113,32 @@ export class StacBrowser extends Construct {
fs.copyFileSync(props.configFilePath, `${cloneDirectory}/config.js`);
}

// Build the app with catalogUrl
console.log(`Building app with catalogUrl=${props.stacCatalogUrl} into ${cloneDirectory}`)
execSync(`npm run build -- --catalogUrl=${props.stacCatalogUrl}`, { cwd: cloneDirectory });
// Build the app with catalogUrl.
// See: https://github.com/radiantearth/stac-browser/discussions/751
const majorVersion = StacBrowser.parseMajorVersion(props.githubRepoTag);

console.log(`Building app with catalogUrl=${props.stacCatalogUrl} into ${cloneDirectory} (detected major version: ${majorVersion ?? 'unknown'})`)

if (majorVersion !== null && majorVersion <= 3) {
// v3.x: CLI flag passthrough
const args = [`--catalogUrl="${props.stacCatalogUrl}"`];
if (props.pathPrefix) {
args.push(`--pathPrefix="${props.pathPrefix}"`);
}
execSync(`npm run build -- ${args.join(' ')}`, { cwd: cloneDirectory });
} else {
// v4.x and v5.x (and anything newer, by default assumption): SB_* env vars.
execSync('npm run build', {
cwd: cloneDirectory,
env: {
...process.env,
SB_catalogUrl: props.stacCatalogUrl,
...(props.pathPrefix ? { SB_pathPrefix: props.pathPrefix } : {}),
},
});
}

return './stac-browser/dist'
return `${cloneDirectory}/dist`

}

Expand All @@ -132,6 +164,10 @@ export interface StacBrowserProps {
/**
* Path to config file for the STAC browser. If not provided, default configuration in the STAC browser
* repository is used.
*
* Note: config.js option names have changed across major versions (particularly
* into v5, which removed/renamed several options). Make sure the config file you
* provide matches the schema of the `githubRepoTag` version you're building.
*/
readonly configFilePath?: string;

Expand All @@ -140,6 +176,16 @@ export interface StacBrowserProps {
*/
readonly githubRepoTag: string;

/**
* Sub-path the app will be hosted under (e.g. "/stac-browser"), if not deployed at
* the root of the domain.
*
* Passed as a `--pathPrefix` CLI flag for <=v3.x, or as the `SB_pathPrefix`
* environment variable for >=v4.x.
*
* @default - No path prefix. The app is built assuming it is served from the domain root.
*/
readonly pathPrefix?: string;

/**
* The ARN of the cloudfront distribution that will be added to the bucket policy with read access.
Expand Down
Loading