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

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,10 @@
1
1
  import config from '../../config.js';
2
2
  import { getBucket } from '../storage/storage.js';
3
3
  import { readdir, lstat } from 'node:fs/promises';
4
- import { createReadStream } from 'node:fs';
4
+ import { createReadStream, createWriteStream } from 'node:fs';
5
5
  import { join } from 'node:path';
6
6
  import { CloudFunctionsServiceClient } from '@google-cloud/functions';
7
+ import archiver from 'archiver';
7
8
  const functionsClient = new CloudFunctionsServiceClient({
8
9
  credentials: config.deployment.credentials
9
10
  });
@@ -31,17 +32,24 @@ async function uploadDirectory(bucketName, directoryPath, prefix = '') {
31
32
  }
32
33
  }
33
34
  }
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
35
+ async function zipDirectory(source, out) {
36
+ await new Promise((resolve, reject) => {
37
+ const output = createWriteStream(out);
38
+ const archive = archiver('zip', { zlib: { level: 9 } });
39
+ output.on('close', () => {
40
+ resolve();
41
+ });
42
+ archive.on('error', (err) => {
43
+ reject(err);
44
+ });
45
+ archive.pipe(output);
46
+ archive.directory(source, false);
47
+ archive.finalize();
39
48
  });
40
- const file = uploadResponse[0];
41
- return file.cloudStorageURI.toString();
42
49
  }
43
- async function deployFunction(name, source) {
50
+ async function deployFunction(functionName, source) {
44
51
  const location = functionsClient.locationPath(projectId, region);
52
+ const name = functionsClient.cloudFunctionPath(projectId, region, functionName);
45
53
  const [response] = await functionsClient.createFunction({
46
54
  location,
47
55
  function: {
@@ -60,8 +68,11 @@ async function deployFunction(name, source) {
60
68
  export default async function () {
61
69
  const bucketName = config.storage.defaultBucket;
62
70
  const assetsPath = '.genoacms/deployment/static';
63
- const buildArchivePath = '.genoacms/deployment/build.zip';
71
+ const buildArchiveSrc = '.build.zip';
72
+ const buildArchiveDest = '.genoacms/deployment/build.zip';
73
+ const buildArchiveRef = `gs://${bucketName}/${buildArchiveDest}`;
74
+ await zipDirectory('./build', buildArchiveSrc);
64
75
  await uploadDirectory(bucketName, './static', assetsPath);
65
- const buildArchiveURI = await uploadSourceCode(bucketName, './build', buildArchivePath);
66
- await deployFunction('genoacms', buildArchiveURI);
76
+ await uploadDirectory(bucketName, buildArchiveSrc, buildArchiveDest);
77
+ await deployFunction('genoacms', buildArchiveRef);
67
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genoacms/adapter-gcp",
3
- "version": "0.5.2-fix.1",
3
+ "version": "0.5.2-fix.2",
4
4
  "description": "Implementation of abstraction layer of GenoaCMS for GCP",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,9 +21,11 @@
21
21
  "@google-cloud/firestore": "^7.1.0",
22
22
  "@google-cloud/functions": "^3.2.0",
23
23
  "@google-cloud/resource-manager": "^5.1.0",
24
- "@google-cloud/storage": "^7.7.0"
24
+ "@google-cloud/storage": "^7.7.0",
25
+ "archiver": "^7.0.0"
25
26
  },
26
27
  "devDependencies": {
28
+ "@types/archiver": "^6.0.2",
27
29
  "@typescript-eslint/eslint-plugin": "^6.9.0",
28
30
  "eslint": "^8.52.0",
29
31
  "eslint-config-standard-with-typescript": "^39.1.1",
@@ -35,8 +37,8 @@
35
37
  "vitest": "^1.0.4"
36
38
  },
37
39
  "peerDependencies": {
38
- "@sveltejs/adapter-node": "^4.0.1",
39
- "@google-cloud/functions-framework": "^3.3.0"
40
+ "@google-cloud/functions-framework": "^3.3.0",
41
+ "@sveltejs/adapter-node": "^4.0.1"
40
42
  },
41
43
  "files": [
42
44
  "src",
@@ -1,9 +1,10 @@
1
1
  import config from '../../config.js'
2
2
  import { getBucket } from '../storage/storage.js'
3
3
  import { readdir, lstat } from 'node:fs/promises'
4
- import { createReadStream } from 'node:fs'
4
+ import { createReadStream, createWriteStream } from 'node:fs'
5
5
  import { join } from 'node:path'
6
6
  import { CloudFunctionsServiceClient } from '@google-cloud/functions'
7
+ import archiver from 'archiver'
7
8
 
8
9
  const functionsClient = new CloudFunctionsServiceClient({
9
10
  credentials: config.deployment.credentials
@@ -36,18 +37,28 @@ async function uploadDirectory (bucketName: string, directoryPath: string, prefi
36
37
  }
37
38
  }
38
39
 
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
40
+ async function zipDirectory (source: string, out: string): Promise<void> {
41
+ await new Promise<void>((resolve, reject) => {
42
+ const output = createWriteStream(out)
43
+ const archive = archiver('zip', { zlib: { level: 9 } })
44
+
45
+ output.on('close', () => {
46
+ resolve()
47
+ })
48
+
49
+ archive.on('error', (err) => {
50
+ reject(err)
51
+ })
52
+
53
+ archive.pipe(output)
54
+ archive.directory(source, false)
55
+ archive.finalize()
44
56
  })
45
- const file = uploadResponse[0]
46
- return file.cloudStorageURI.toString()
47
57
  }
48
58
 
49
- async function deployFunction (name: string, source: string): Promise<void> {
59
+ async function deployFunction (functionName: string, source: string): Promise<void> {
50
60
  const location = functionsClient.locationPath(projectId, region)
61
+ const name = functionsClient.cloudFunctionPath(projectId, region, functionName)
51
62
  const [response] = await functionsClient.createFunction({
52
63
  location,
53
64
  function: {
@@ -67,8 +78,11 @@ async function deployFunction (name: string, source: string): Promise<void> {
67
78
  export default async function (): Promise<void> {
68
79
  const bucketName = config.storage.defaultBucket
69
80
  const assetsPath = '.genoacms/deployment/static'
70
- const buildArchivePath = '.genoacms/deployment/build.zip'
81
+ const buildArchiveSrc = '.build.zip'
82
+ const buildArchiveDest = '.genoacms/deployment/build.zip'
83
+ const buildArchiveRef = `gs://${bucketName}/${buildArchiveDest}`
84
+ await zipDirectory('./build', buildArchiveSrc)
71
85
  await uploadDirectory(bucketName, './static', assetsPath)
72
- const buildArchiveURI = await uploadSourceCode(bucketName, './build', buildArchivePath)
73
- await deployFunction('genoacms', buildArchiveURI)
86
+ await uploadDirectory(bucketName, buildArchiveSrc, buildArchiveDest)
87
+ await deployFunction('genoacms', buildArchiveRef)
74
88
  }