Skip to content

Commit

Permalink
✨ improve: remove @AWS-SDK check (#2322)
Browse files Browse the repository at this point in the history
- this is vendored and the check is unnecessary

## Type of change

**PATCH: backwards compatible change**
  • Loading branch information
kellymears authored Jun 14, 2023
1 parent d512b81 commit 93aa593
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 74 deletions.
112 changes: 39 additions & 73 deletions sources/@roots/bud-framework/src/services/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Filesystem, json, yml} from '@roots/bud-support/filesystem'
import globby from '@roots/bud-support/globby'
import isUndefined from '@roots/bud-support/lodash/isUndefined'
import logger from '@roots/bud-support/logger'
import {type S3} from '@roots/filesystem/s3'
import {S3} from '@roots/filesystem'
import {join} from 'node:path'

import type {Bud} from '../index.js'
Expand Down Expand Up @@ -33,6 +33,8 @@ export default class FS extends Filesystem implements Contract {
public s3?: S3

/**
* YML
*
* @see {@link https://bud.js.org/docs/bud.fs/yml}
*/
public yml: typeof yml = yml
Expand All @@ -42,6 +44,7 @@ export default class FS extends Filesystem implements Contract {
*/
public constructor(public _app: () => Bud) {
super(_app().context.basedir)
this.s3 = new S3()
}

/**
Expand Down Expand Up @@ -69,16 +72,9 @@ export default class FS extends Filesystem implements Contract {
}

/**
* {@link Contract.bootstrap}
* Fulfills {@link Contract.bootstrap}
*/
public async register(bud: Bud) {
try {
const {S3} = await import(`@roots/filesystem/s3`)
this.s3 = new S3()
} catch (error) {
// fallthrough
}
}
public async register(bud: Bud) {}

/**
* Set bucket
Expand All @@ -87,11 +83,10 @@ export default class FS extends Filesystem implements Contract {
*
* @see {@link https://bud.js.org/docs/bud.fs/s3#setup}
*/
@bind
public setBucket(bucket: string) {
if (!this.s3) this.throwS3Error()

this.app.after(async (bud: Bud) => {
bud.fs.s3.config.set(`bucket`, bucket)
this.app.after(async () => {
this.s3.config.set(`bucket`, bucket)
})

return this
Expand All @@ -104,11 +99,10 @@ export default class FS extends Filesystem implements Contract {
*
* @see {@link https://bud.js.org/docs/bud.fs/s3#setup}
*/
@bind
public setCredentials(credentials: S3[`config`][`credentials`]) {
if (!this.s3) this.throwS3Error()

this.app.after(async (bud: Bud) => {
bud.fs.s3.config.set(`credentials`, credentials)
this.app.after(async () => {
this.s3.config.set(`credentials`, credentials)
})

return this
Expand All @@ -121,11 +115,10 @@ export default class FS extends Filesystem implements Contract {
*
* @see {@link https://bud.js.org/docs/bud.fs/s3#setup}
*/
@bind
public setEndpoint(endpoint: S3[`config`][`endpoint`]) {
if (!this.s3) this.throwS3Error()

this.app.after(async (bud: Bud) => {
bud.fs.s3.config.set(`endpoint`, endpoint)
this.app.after(async () => {
this.s3.config.set(`endpoint`, endpoint)
})

return this
Expand All @@ -138,50 +131,29 @@ export default class FS extends Filesystem implements Contract {
*
* @see {@link https://bud.js.org/docs/bud.fs/s3#setup}
*/
@bind
public setRegion(region: S3[`config`][`region`]) {
if (!this.s3) this.throwS3Error()

this.app.after(async (bud: Bud) => {
bud.fs.s3.config.set(`region`, region)
this.app.after(async () => {
this.s3.config.set(`region`, region)
})

return this
}

/**
* Throw S3 error
*/
@bind
public throwS3Error() {
const dependencies = {
...(this.app.context.files?.[`package.json`]?.module?.dependencies ??
{}),
...(this.app.context.files?.[`package.json`]?.module
?.devDependencies ?? {}),
}
if (!Object.keys(dependencies)?.includes(`@aws-sdk/client-s3`)) {
throw new Error(
`S3 is not available. Please install @aws-sdk/client-s3 to use this feature.`,
)
}
throw new Error(`S3 is not available.`)
}

/**
* Upload files to S3
*
* @param options - upload options
*
* @see {@link https://bud.js.org/docs/bud.fs/s3#uploading-files}
*/
@bind
public upload(options?: {
destination?: string
files?: string
keep?: false | number
source?: string
}): this {
if (!this.s3) this.throwS3Error()

const {destination, files, keep, source} = {
destination: options?.destination,
files: isUndefined(options?.files) ? `**/*` : options.files,
Expand All @@ -194,40 +166,36 @@ export default class FS extends Filesystem implements Contract {
const s3Path = (path: string) =>
destination ? join(destination, path) : path

this.app.after(async (bud: Bud) => {
this.app.after(async () => {
await globby(files, {cwd: source}).then(async files => {
const descriptions = await Promise.all(
files.map(async file => {
const contents = await bud.fs.read(
join(source, file),
`buffer`,
)

const contents = await this.read(join(source, file), `buffer`)
return {contents, file}
}),
)

const manifestExists = await bud.fs.s3.exists(
const manifestExists = await this.s3.exists(
s3Path(`upload-manifest.json`),
)

const entries = Object.entries(
manifestExists
? await bud.fs.s3
? await this.s3
.read(s3Path(`upload-manifest.json`))
.then(bud.fs.json.parse)
.then(this.json.parse)
: {},
)

await Promise.all(
descriptions.map(async ({contents, file}) => {
bud.fs.logger.await(`Upload ${file} to ${bud.fs.s3.ident}`)
this.logger.await(`Upload ${file} to ${this.s3.ident}`)

try {
await bud.fs.s3.write(s3Path(file), contents)
bud.fs.logger.success(`Upload ${file} to ${bud.fs.s3.ident}`)
await this.s3.write(s3Path(file), contents)
this.logger.success(`Upload ${file} to ${this.s3.ident}`)
} catch (error) {
bud.fs.logger.error(`Upload ${file} to ${bud.fs.s3.ident}`)
this.logger.error(`Upload ${file} to ${this.s3.ident}`)
throw error
}
}),
Expand All @@ -248,35 +216,33 @@ export default class FS extends Filesystem implements Contract {
),
)
.map(async key => {
const fileExists = await bud.fs.s3.exists(key)
const fileExists = await this.s3.exists(key)
if (!fileExists) return

bud.fs.logger.await(
`Remove ${key} from ${bud.fs.s3.ident} (stale)`,
this.logger.await(
`Remove ${key} from ${this.s3.ident} (stale)`,
)
await bud.fs.s3.delete(key)
bud.fs.logger.success(
`Remove ${key} from ${bud.fs.s3.ident} (stale)`,
await this.s3.delete(key)
this.logger.success(
`Remove ${key} from ${this.s3.ident} (stale)`,
)
}),
)

bud.fs.logger.await(
`Write upload-manifest.json to ${bud.fs.s3.ident}`,
)
this.logger.await(`Write upload-manifest.json to ${this.s3.ident}`)

await bud.fs.s3.write({
await this.s3.write({
Body: Buffer.from(
bud.fs.json.stringify({
this.json.stringify({
...entries.reduce((acc, [k, v]) => ({...acc, [k]: v}), {}),
[new Date().getTime()]: descriptions.map(({file}) => file),
}),
),
Key: s3Path(`upload-manifest.json`),
})

bud.fs.logger.success(
`Write upload-manifest.json to ${bud.fs.s3.ident}`,
this.logger.success(
`Write upload-manifest.json to ${this.s3.ident}`,
)
})
})
Expand Down
1 change: 0 additions & 1 deletion sources/@roots/filesystem/src/s3/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-console */
import type {
GetObjectOutput,
ListObjectsCommandInput,
Expand Down

0 comments on commit 93aa593

Please sign in to comment.