Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,17 @@ google_cloud_project: <AppId>
site: <SiteId>

# Specify a launch schedule. The schedule maps timestamps to branches or commit
# shas. If blank, `master` is used for the default deployment.
# shas. If blank, `main` is used for the default deployment.
schedule:
default: master
default: main

redirects:
- from: /foo
to: /bar
- from: /intl/:locale/
to: /$locale/
- from: /intl/:locale/*wildcard
to: /$locale/$wildcard
```

2. Generate your files.
Expand Down Expand Up @@ -209,12 +217,12 @@ Git branch is determined by inspecting the local Git environment when the
The best way to understand how this works is by following the examples below:

```bash
# master branch
# main branch
# ✓ public
# ✓ production URL
# ✓ also available from staging URL (restricted)

(master) $ fileset upload build
(main) $ fileset upload build
...
Public URL: https://appid.appspot.com
Staging URL: https://default-f3a9abb-dot-fileset-dot-appid.appspot.com
Expand Down
10 changes: 9 additions & 1 deletion example/fileset.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
google_cloud_project: <AppId>
site: <SiteId>
schedule:
default: master
default: main
redirects:
- from: /foo
to: /bar
permanent: True
- from: /intl/:locale/
to: /$locale/
- from: /intl/:locale/*wildcard
to: /$locale/$wildcard
19 changes: 11 additions & 8 deletions src/commands/upload.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as fs from 'fs';
import * as fsPath from 'path';
import * as manifest from '../manifest';
import * as upload from '../upload';
import * as yaml from 'js-yaml';

import {Manifest} from '../manifest';
import {getGitData} from '../gitdata';

interface UploadOptions {
Expand All @@ -29,7 +29,7 @@ function findConfig(path: string) {
// TODO: Validate config schema.
const config = yaml.safeLoad(fs.readFileSync(configPath, 'utf8')) as Record<
string,
string
unknown
>;
return config;
}
Expand Down Expand Up @@ -63,20 +63,23 @@ export class UploadCommand {
throw new Error('Unable to determine the Google Cloud project.');
}

const manifest = new Manifest(
site,
const manifestObj = new manifest.Manifest(
site as string,
this.options.ref || gitData.ref,
this.options.branch || gitData.branch || ''
);
manifest.createFromDirectory(path);
if (!manifest.files.length) {
manifestObj.createFromDirectory(path);
if (config.redirects) {
manifestObj.setRedirects(config.redirects as manifest.Redirect[]);
}
if (!manifestObj.files.length) {
console.log(`No files found in -> ${path}`);
return;
}
upload.uploadManifest(
googleCloudProject,
googleCloudProject as string,
bucket,
manifest,
manifestObj,
this.options.force,
ttl
);
Expand Down
27 changes: 25 additions & 2 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface ManifestFile {
cleanPath: string;
}

export interface Redirect {
from: string;
to: string;
permanent?: boolean;
}

export interface PathToHash {
path?: string;
}
Expand All @@ -32,10 +38,12 @@ export class Manifest {
ref: string;
branch?: string;
files: ManifestFile[];
redirects: Redirect[];
shortSha: string;

constructor(site: string, ref: string, branch?: string) {
this.files = [];
this.redirects = [];
this.site = site;
this.ref = ref;
this.shortSha = ref.slice(0, 7);
Expand All @@ -49,6 +57,10 @@ export class Manifest {
});
}

setRedirects(redirects: Redirect[]) {
this.redirects = redirects;
}

createHash(path: string) {
const contents = fs.readFileSync(path);
const hash = crypto.createHash('sha1');
Expand All @@ -60,7 +72,9 @@ export class Manifest {

async addFile(path: string, dir: string) {
const hash = this.createHash(path);
const cleanPath = path.replace(dir.replace(/^\\+|\\+$/g, ''), '/').replace('//', '/');
const cleanPath = path
.replace(dir.replace(/^\\+|\\+$/g, ''), '/')
.replace('//', '/');
const manifestFile: ManifestFile = {
cleanPath: cleanPath,
hash: hash,
Expand All @@ -70,7 +84,16 @@ export class Manifest {
this.files.push(manifestFile);
}

toJSON() {
async addRedirect(from: string, to: string, permanent: boolean) {
const redirect: Redirect = {
from: from,
to: to,
permanent: permanent,
};
this.redirects.push(redirect);
}

pathsToJSON() {
const pathsToHashes: any = {};
this.files.forEach(file => {
pathsToHashes[file.cleanPath] = file.hash;
Expand Down
57 changes: 57 additions & 0 deletions src/redirects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as manifest from './manifest';
import * as redirects from './redirects';

import {ExecutionContext} from 'ava';
import test from 'ava';

test('Test redirects', (t: ExecutionContext) => {
let numTestsRun = 0;
const config: manifest.Redirect[] = [
{
from: '/foo',
to: '/bar',
permanent: true,
},
{
from: '/intl/:locale/',
to: '/$locale/',
},
{
from: '/intl/:locale/*wildcard',
to: '/$locale/$wildcard',
},
];

const routeTrie = new redirects.RouteTrie();
config.forEach(redirect => {
const code = redirect.permanent ? 301 : 302;
const route = new redirects.RedirectRoute(code, redirect.to);
routeTrie.add(redirect.from, route);
});

let [route, params] = routeTrie.get('/foo');
if (route instanceof redirects.RedirectRoute) {
const [code, destination] = route.getRedirect(params);
t.is(code, 301);
t.is(destination, '/bar');
numTestsRun++;
}

[route, params] = routeTrie.get('/intl/de');
if (route instanceof redirects.RedirectRoute) {
const [code, destination] = route.getRedirect(params);
t.is(code, 302);
t.is(destination, '/de/');
numTestsRun++;
}

[route, params] = routeTrie.get('/intl/ja/foo');
if (route instanceof redirects.RedirectRoute) {
const [code, destination] = route.getRedirect(params);
t.is(code, 302);
t.is(destination, '/ja/foo');
numTestsRun++;
}

t.is(numTestsRun, 3);
});
Loading