Skip to content

Commit 2c026c8

Browse files
committed
Load GCP project ID from config file if it exists
1 parent 1e290f5 commit 2c026c8

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

package-lock.json

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/cli-progress": "^3.8.0",
3838
"@types/express": "^4.17.9",
3939
"@types/http-proxy": "^1.17.4",
40+
"@types/js-yaml": "^3.12.5",
4041
"@types/mime-types": "^2.1.0",
4142
"@types/node": "^13.11.1",
4243
"ava": "^3.14.0",
@@ -59,6 +60,7 @@
5960
"google-auth-library": "^6.1.3",
6061
"http-proxy": "^1.18.1",
6162
"isomorphic-git": "^1.8.0",
63+
"js-yaml": "^3.14.1",
6264
"mime-types": "^2.1.27"
6365
},
6466
"files": [

src/commands/upload.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import * as fs from 'fs';
2+
import * as fsPath from 'path';
13
import * as upload from '../upload';
4+
import * as yaml from 'js-yaml';
25

36
import {Manifest} from '../manifest';
47
import {getGitData} from '../gitdata';
@@ -12,15 +15,48 @@ interface UploadOptions {
1215
ttl?: string;
1316
}
1417

18+
function findConfig(path: string) {
19+
let configPath = null;
20+
const immediatePath = fsPath.join(path, 'fileset.yaml');
21+
const ancestorPath = fsPath.join(fsPath.dirname(path), 'fileset.yaml');
22+
if (fs.existsSync(immediatePath)) {
23+
configPath = immediatePath;
24+
} else if (fs.existsSync(ancestorPath)) {
25+
configPath = ancestorPath;
26+
} else {
27+
return {};
28+
}
29+
// TODO: Validate config schema.
30+
const config = yaml.safeLoad(fs.readFileSync(configPath, 'utf8')) as Record<
31+
string,
32+
string
33+
>;
34+
return config;
35+
}
36+
1537
export class UploadCommand {
1638
constructor(private readonly options: UploadOptions) {
1739
this.options = options;
1840
}
1941

2042
async run(path = './') {
2143
const gitData = await getGitData(path);
44+
const config = findConfig(path);
45+
const ttl = this.options.ttl ? new Date(this.options.ttl) : undefined;
46+
let bucket = this.options.bucket;
47+
if (!bucket && config.google_cloud_project) {
48+
bucket = `${config.google_cloud_project}.appspot.com`;
49+
} else if (!bucket && process.env.GOOGLE_CLOUD_PROJECT) {
50+
bucket = `${process.env.GOOGLE_CLOUD_PROJECT}.appspot.com`;
51+
}
52+
if (!bucket) {
53+
throw new Error(
54+
'Unable to determine which Google Cloud Storage bucket to use. You must specify a `google_cloud_project` in `fileset.yaml` or specify a `GOOGLE_CLOUD_PROJECT` environment variable.'
55+
);
56+
}
57+
const site = this.options.site || config.site;
2258
const manifest = new Manifest(
23-
this.options.site,
59+
site,
2460
this.options.ref || gitData.ref,
2561
this.options.branch || gitData.branch || ''
2662
);
@@ -29,12 +65,6 @@ export class UploadCommand {
2965
console.log(`No files found in -> ${path}`);
3066
return;
3167
}
32-
const ttl = this.options.ttl ? new Date(this.options.ttl) : undefined;
33-
upload.uploadManifest(
34-
this.options.bucket,
35-
manifest,
36-
this.options.force,
37-
ttl
38-
);
68+
upload.uploadManifest(bucket, manifest, this.options.force, ttl);
3969
}
4070
}

src/upload.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import _colors = require('colors');
2+
3+
import * as cliProgress from 'cli-progress';
4+
5+
import {Manifest, ManifestFile} from './manifest';
26
import {asyncify, mapLimit} from 'async';
7+
38
import {Datastore} from '@google-cloud/datastore';
4-
import {entity} from '@google-cloud/datastore/build/src/entity';
5-
import {Manifest, ManifestFile} from './manifest';
69
import {Storage} from '@google-cloud/storage';
7-
import * as cliProgress from 'cli-progress';
10+
import {entity} from '@google-cloud/datastore/build/src/entity';
811

912
const datastore = new Datastore();
10-
const DEFAULT_BUCKET = `${process.env.GCLOUD_PROJECT}.appspot.com`;
1113
const NUM_CONCURRENT_UPLOADS = 64;
1214

1315
function getBlobPath(siteId: string, hash: string) {
@@ -54,7 +56,6 @@ export async function uploadManifest(
5456
force?: boolean,
5557
ttl?: Date
5658
) {
57-
bucket = bucket || DEFAULT_BUCKET;
5859
console.log(`Using storage: ${bucket}/${getBlobPath(manifest.site, '')}`);
5960
const storageBucket = new Storage().bucket(bucket);
6061
const bar = createProgressBar();
@@ -180,6 +181,6 @@ async function finalize(manifest: Manifest, ttl?: Date) {
180181
);
181182
// TODO: Allow customizing the staging URL using `fileset.yaml` configuration.
182183
console.log(
183-
`Staged: https://${manifest.site}-${manifest.shortSha}-dot-fileset2-dot-${process.env.GCLOUD_PROJECT}.appspot.com`
184+
`Staged: https://${manifest.site}-${manifest.shortSha}-dot-fileset2-dot-${process.env.GOOGLE_CLOUD_PROJECT}.appspot.com`
184185
);
185186
}

0 commit comments

Comments
 (0)