@genoacms/adapter-gcp 0.4.2 → 0.5.2-fix.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ export default function (): Promise<void>;
@@ -0,0 +1,67 @@
1
+ import config from '../../config.js';
2
+ import { getBucket } from '../storage/storage.js';
3
+ import { readdir, lstat } from 'node:fs/promises';
4
+ import { createReadStream } from 'node:fs';
5
+ import { join } from 'node:path';
6
+ import { CloudFunctionsServiceClient } from '@google-cloud/functions';
7
+ const functionsClient = new CloudFunctionsServiceClient({
8
+ credentials: config.deployment.credentials
9
+ });
10
+ const projectId = config.deployment.projectId;
11
+ const region = config.deployment.region;
12
+ async function uploadDirectory(bucketName, directoryPath, prefix = '') {
13
+ const bucket = getBucket(bucketName);
14
+ const files = await readdir(directoryPath);
15
+ for (const file of files) {
16
+ const filePath = join(directoryPath, file);
17
+ const destination = join(prefix, file);
18
+ const isFileDirectory = (await lstat(filePath)).isDirectory();
19
+ if (isFileDirectory) {
20
+ await uploadDirectory(bucketName, filePath, destination);
21
+ }
22
+ else {
23
+ const fileStream = createReadStream(filePath);
24
+ const gcsFile = bucket.file(destination);
25
+ await new Promise((resolve, reject) => {
26
+ fileStream
27
+ .pipe(gcsFile.createWriteStream())
28
+ .on('error', reject)
29
+ .on('finish', resolve);
30
+ });
31
+ }
32
+ }
33
+ }
34
+ async function uploadSourceCode(bucketName, source, dest) {
35
+ const bucket = getBucket(bucketName);
36
+ const uploadResponse = await bucket.upload(source, {
37
+ gzip: true,
38
+ destination: dest
39
+ });
40
+ const file = uploadResponse[0];
41
+ return file.cloudStorageURI.toString();
42
+ }
43
+ async function deployFunction(name, source) {
44
+ const location = functionsClient.locationPath(projectId, region);
45
+ const [response] = await functionsClient.createFunction({
46
+ location,
47
+ function: {
48
+ name,
49
+ sourceUploadUrl: source,
50
+ entryPoint: 'handler',
51
+ runtime: 'nodejs20',
52
+ httpsTrigger: {},
53
+ environmentVariables: {
54
+ NODE_ENV: 'production'
55
+ }
56
+ }
57
+ }, {});
58
+ console.log(response);
59
+ }
60
+ export default async function () {
61
+ const bucketName = config.storage.defaultBucket;
62
+ const assetsPath = '.genoacms/deployment/static';
63
+ const buildArchivePath = '.genoacms/deployment/build.zip';
64
+ await uploadDirectory(bucketName, './static', assetsPath);
65
+ const buildArchiveURI = await uploadSourceCode(bucketName, './build', buildArchivePath);
66
+ await deployFunction('genoacms', buildArchiveURI);
67
+ }
@@ -0,0 +1,4 @@
1
+ import type { Adapter } from '@genoacms/cloudabstraction/deployment';
2
+ declare const svelteKitAdapter: Adapter.svelteKitAdapter;
3
+ declare const deployProcedure: Adapter.deployProcedure;
4
+ export { svelteKitAdapter, deployProcedure };
@@ -0,0 +1,6 @@
1
+ const svelteKitAdapter = '@sveltejs/adapter-node';
2
+ const deployProcedure = async () => {
3
+ const deploy = (await import('./deploy.js')).default;
4
+ await deploy();
5
+ };
6
+ export { svelteKitAdapter, deployProcedure };
@@ -1,14 +1,4 @@
1
- import { Storage } from '@google-cloud/storage';
2
- import config from '../../config.js';
3
- const storage = new Storage({
4
- credentials: config.storage.credentials
5
- });
6
- const getBucket = (name) => {
7
- if (!config.storage.buckets.includes(name))
8
- throw new Error('bucket-unregistered');
9
- const bucket = storage.bucket(name);
10
- return bucket;
11
- };
1
+ import { getBucket } from './storage.js';
12
2
  const getObject = async ({ bucket, name }) => {
13
3
  const bucketInstance = getBucket(bucket);
14
4
  const file = bucketInstance.file(name);
@@ -32,10 +22,10 @@ const getSignedURL = async ({ bucket, name }) => {
32
22
  });
33
23
  return url;
34
24
  };
35
- const uploadObject = async ({ bucket, name }, stream) => {
25
+ const uploadObject = async ({ bucket, name }, stream, options) => {
36
26
  const bucketInstance = getBucket(bucket);
37
27
  const file = bucketInstance.file(name);
38
- await file.save(stream);
28
+ await file.save(stream, options);
39
29
  };
40
30
  const deleteObject = async ({ bucket, name }) => {
41
31
  const bucketInstance = getBucket(bucket);
@@ -0,0 +1,3 @@
1
+ import { type Bucket } from '@google-cloud/storage';
2
+ declare const getBucket: (name: string) => Bucket;
3
+ export { getBucket };
@@ -0,0 +1,12 @@
1
+ import config from '../../config.js';
2
+ import { Storage } from '@google-cloud/storage';
3
+ const storage = new Storage({
4
+ credentials: config.storage.credentials
5
+ });
6
+ const getBucket = (name) => {
7
+ if (!config.storage.buckets.includes(name))
8
+ throw new Error('bucket-unregistered');
9
+ const bucket = storage.bucket(name);
10
+ return bucket;
11
+ };
12
+ export { getBucket };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genoacms/adapter-gcp",
3
- "version": "0.4.2",
3
+ "version": "0.5.2-fix.1",
4
4
  "description": "Implementation of abstraction layer of GenoaCMS for GCP",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,8 +17,9 @@
17
17
  "homepage": "https://github.com/GenoaCMS/adapter-gcp#readme",
18
18
  "type": "module",
19
19
  "dependencies": {
20
- "@genoacms/cloudabstraction": "0.4.2-fix.2",
20
+ "@genoacms/cloudabstraction": "^0.5.2",
21
21
  "@google-cloud/firestore": "^7.1.0",
22
+ "@google-cloud/functions": "^3.2.0",
22
23
  "@google-cloud/resource-manager": "^5.1.0",
23
24
  "@google-cloud/storage": "^7.7.0"
24
25
  },
@@ -33,6 +34,10 @@
33
34
  "typescript": "^5.2.2",
34
35
  "vitest": "^1.0.4"
35
36
  },
37
+ "peerDependencies": {
38
+ "@sveltejs/adapter-node": "^4.0.1",
39
+ "@google-cloud/functions-framework": "^3.3.0"
40
+ },
36
41
  "files": [
37
42
  "src",
38
43
  "dist"
@@ -46,6 +51,10 @@
46
51
  "import": "./dist/services/database/index.js",
47
52
  "types": "./dist/services/database/index.d.ts"
48
53
  },
54
+ "./deployment": {
55
+ "import": "./dist/services/deployment/index.js",
56
+ "types": "./dist/services/deployment/index.d.ts"
57
+ },
49
58
  "./storage": {
50
59
  "import": "./dist/services/storage/index.js",
51
60
  "types": "./dist/services/storage/index.d.ts"
@@ -0,0 +1,8 @@
1
+ import { HttpFunction } from '@google-cloud/functions-framework'
2
+ import app from './build'
3
+
4
+ const svelteKitApp = HttpFunction(app.handler)
5
+
6
+ export {
7
+ svelteKitApp
8
+ }
@@ -0,0 +1,74 @@
1
+ import config from '../../config.js'
2
+ import { getBucket } from '../storage/storage.js'
3
+ import { readdir, lstat } from 'node:fs/promises'
4
+ import { createReadStream } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import { CloudFunctionsServiceClient } from '@google-cloud/functions'
7
+
8
+ const functionsClient = new CloudFunctionsServiceClient({
9
+ credentials: config.deployment.credentials
10
+ })
11
+ const projectId = config.deployment.projectId
12
+ const region = config.deployment.region
13
+
14
+ async function uploadDirectory (bucketName: string, directoryPath: string, prefix = ''): Promise<void> {
15
+ const bucket = getBucket(bucketName)
16
+ const files = await readdir(directoryPath)
17
+
18
+ for (const file of files) {
19
+ const filePath = join(directoryPath, file)
20
+ const destination = join(prefix, file)
21
+
22
+ const isFileDirectory = (await lstat(filePath)).isDirectory()
23
+ if (isFileDirectory) {
24
+ await uploadDirectory(bucketName, filePath, destination)
25
+ } else {
26
+ const fileStream = createReadStream(filePath)
27
+ const gcsFile = bucket.file(destination)
28
+
29
+ await new Promise((resolve, reject) => {
30
+ fileStream
31
+ .pipe(gcsFile.createWriteStream())
32
+ .on('error', reject)
33
+ .on('finish', resolve)
34
+ })
35
+ }
36
+ }
37
+ }
38
+
39
+ async function uploadSourceCode (bucketName: string, source: string, dest: string): Promise<string> {
40
+ const bucket = getBucket(bucketName)
41
+ const uploadResponse = await bucket.upload(source, {
42
+ gzip: true,
43
+ destination: dest
44
+ })
45
+ const file = uploadResponse[0]
46
+ return file.cloudStorageURI.toString()
47
+ }
48
+
49
+ async function deployFunction (name: string, source: string): Promise<void> {
50
+ const location = functionsClient.locationPath(projectId, region)
51
+ const [response] = await functionsClient.createFunction({
52
+ location,
53
+ function: {
54
+ name,
55
+ sourceUploadUrl: source,
56
+ entryPoint: 'handler',
57
+ runtime: 'nodejs20',
58
+ httpsTrigger: {},
59
+ environmentVariables: {
60
+ NODE_ENV: 'production'
61
+ }
62
+ }
63
+ }, {})
64
+ console.log(response)
65
+ }
66
+
67
+ export default async function (): Promise<void> {
68
+ const bucketName = config.storage.defaultBucket
69
+ const assetsPath = '.genoacms/deployment/static'
70
+ const buildArchivePath = '.genoacms/deployment/build.zip'
71
+ await uploadDirectory(bucketName, './static', assetsPath)
72
+ const buildArchiveURI = await uploadSourceCode(bucketName, './build', buildArchivePath)
73
+ await deployFunction('genoacms', buildArchiveURI)
74
+ }
@@ -0,0 +1,13 @@
1
+ import type { Adapter } from '@genoacms/cloudabstraction/deployment'
2
+
3
+ const svelteKitAdapter: Adapter.svelteKitAdapter = '@sveltejs/adapter-node'
4
+
5
+ const deployProcedure: Adapter.deployProcedure = async () => {
6
+ const deploy = (await import('./deploy.js')).default
7
+ await deploy()
8
+ }
9
+
10
+ export {
11
+ svelteKitAdapter,
12
+ deployProcedure
13
+ }
@@ -2,18 +2,8 @@ import type {
2
2
  Adapter,
3
3
  StorageObject
4
4
  } from '@genoacms/cloudabstraction/storage'
5
- import { type Bucket, Storage, type File } from '@google-cloud/storage'
6
- import config from '../../config.js'
7
-
8
- const storage = new Storage({
9
- credentials: config.storage.credentials
10
- })
11
-
12
- const getBucket = (name: string): Bucket => {
13
- if (!config.storage.buckets.includes(name)) throw new Error('bucket-unregistered')
14
- const bucket = storage.bucket(name)
15
- return bucket
16
- }
5
+ import { type File } from '@google-cloud/storage'
6
+ import { getBucket } from './storage.js'
17
7
 
18
8
  const getObject: Adapter.getObject = async ({ bucket, name }) => {
19
9
  const bucketInstance = getBucket(bucket)
@@ -42,10 +32,10 @@ const getSignedURL: Adapter.getSignedURL = async ({ bucket, name }) => {
42
32
  return url
43
33
  }
44
34
 
45
- const uploadObject: Adapter.uploadObject = async ({ bucket, name }, stream) => {
35
+ const uploadObject: Adapter.uploadObject = async ({ bucket, name }, stream, options) => {
46
36
  const bucketInstance = getBucket(bucket)
47
37
  const file = bucketInstance.file(name)
48
- await file.save(stream)
38
+ await file.save(stream, options)
49
39
  }
50
40
 
51
41
  const deleteObject: Adapter.deleteObject = async ({ bucket, name }) => {
@@ -0,0 +1,16 @@
1
+ import config from '../../config.js'
2
+ import { type Bucket, Storage } from '@google-cloud/storage'
3
+
4
+ const storage = new Storage({
5
+ credentials: config.storage.credentials
6
+ })
7
+
8
+ const getBucket = (name: string): Bucket => {
9
+ if (!config.storage.buckets.includes(name)) throw new Error('bucket-unregistered')
10
+ const bucket = storage.bucket(name)
11
+ return bucket
12
+ }
13
+
14
+ export {
15
+ getBucket
16
+ }