1
+ import * as fs from 'fs' ;
2
+ import * as fsPath from 'path' ;
1
3
import * as upload from '../upload' ;
4
+ import * as yaml from 'js-yaml' ;
2
5
3
6
import { Manifest } from '../manifest' ;
4
7
import { getGitData } from '../gitdata' ;
@@ -12,15 +15,48 @@ interface UploadOptions {
12
15
ttl ?: string ;
13
16
}
14
17
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
+
15
37
export class UploadCommand {
16
38
constructor ( private readonly options : UploadOptions ) {
17
39
this . options = options ;
18
40
}
19
41
20
42
async run ( path = './' ) {
21
43
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 ;
22
58
const manifest = new Manifest (
23
- this . options . site ,
59
+ site ,
24
60
this . options . ref || gitData . ref ,
25
61
this . options . branch || gitData . branch || ''
26
62
) ;
@@ -29,12 +65,6 @@ export class UploadCommand {
29
65
console . log ( `No files found in -> ${ path } ` ) ;
30
66
return ;
31
67
}
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 ) ;
39
69
}
40
70
}
0 commit comments