fix some typescript stuff

This commit is contained in:
Rich Harris
2018-02-14 12:25:24 -05:00
parent 9adb6ca7e6
commit 0f390920a8
4 changed files with 57 additions and 23 deletions

View File

@@ -1,9 +1,23 @@
import * as path from 'path';
import glob from 'glob';
export default function create_routes({ src, files = glob.sync('**/*.+(html|js|mjs)', { cwd: src }) }) {
const routes = files
.map(file => {
type Route = {
id: string;
type: 'page' | 'route';
file: string;
pattern: RegExp;
test: (url: string) => boolean;
exec: (url: string) => Record<string, string>;
parts: string[];
dynamic: string[];
}
export default function create_routes({ src, files = glob.sync('**/*.+(html|js|mjs)', { cwd: src }) }: {
src: string;
files?: string[];
}) {
const routes: Route[] = files
.map((file: string) => {
if (/(^|\/|\\)_/.test(file)) return;
const parts = file.replace(/\.(html|js|mjs)$/, '').split('/'); // glob output is always posix-style
@@ -34,13 +48,13 @@ export default function create_routes({ src, files = glob.sync('**/*.+(html|js|m
const pattern = new RegExp(`^${pattern_string}\\/?$`);
const test = url => pattern.test(url);
const test = (url: string) => pattern.test(url);
const exec = url => {
const exec = (url: string) => {
const match = pattern.exec(url);
if (!match) return;
const params = {};
const params: Record<string, string> = {};
dynamic.forEach((param, i) => {
params[param] = match[i + 1];
});
@@ -60,7 +74,7 @@ export default function create_routes({ src, files = glob.sync('**/*.+(html|js|m
};
})
.filter(Boolean)
.sort((a, b) => {
.sort((a: Route, b: Route) => {
let same = true;
for (let i = 0; true; i += 1) {