statikly-router
Version:
file system router
68 lines (67 loc) • 2.95 kB
JavaScript
;
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
const helpers_1 = require("./helpers");
class Router {
constructor(options) {
options.glob = options.glob || "**/*(*.html|*.ejs|*.md|*.js|*.hbs)";
options.querySep = options.querySep || ":";
options.path = (0, helpers_1.toAbsolutePath)(options.path);
options.dirNameRoute = options.dirNameRoute || false;
this.options = options;
}
async scan() {
var _a, e_1, _b, _c;
const { glob, path, querySep, dirNameRoute } = this.options;
const routes = {};
const files = await (0, helpers_1.globPromise)(path, glob);
try {
for (var _d = true, files_1 = __asyncValues(files), files_1_1; files_1_1 = await files_1.next(), _a = files_1_1.done, !_a;) {
_c = files_1_1.value;
_d = false;
try {
const file = _c;
const route = (0, helpers_1.pathToRoute)({
path: file,
cwd: path,
querySep,
dirNameRoute,
});
if (routes[route.url] === undefined) {
routes[route.url] = {};
}
routes[route.url][route.ext.replace(".", "")] = route;
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = files_1.return)) await _b.call(files_1);
}
finally { if (e_1) throw e_1.error; }
}
return routes;
}
async glob() {
const { glob, path } = this.options;
const files = await (0, helpers_1.globPromise)(path, glob);
return files;
}
async build(outputPath) {
const routes = await this.scan();
await (0, helpers_1.writeSON)((0, helpers_1.toAbsolutePath)(outputPath), routes);
}
}
exports.Router = Router;
exports.default = Router;