2020-02-25 19:37:10 -08:00
|
|
|
|
|
|
|
const path = require('path');
|
2020-02-28 10:33:39 -08:00
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
2020-02-25 19:37:10 -08:00
|
|
|
const fs = require('fs-extra');
|
2020-02-28 10:31:13 -08:00
|
|
|
const { is: _is } = require('./lib/util');
|
2020-02-27 18:57:39 -08:00
|
|
|
|
|
|
|
function is (...args) {
|
|
|
|
const fn = _is(...args);
|
|
|
|
const ret = (ext) => fn(normalizedExt(ext));
|
|
|
|
ret.matching = args;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dictMatch (dict, def) {
|
|
|
|
const arr = Object.entries(dict);
|
|
|
|
|
|
|
|
return (tok) => {
|
|
|
|
for (const [ key, fn ] of arr) {
|
|
|
|
// console.log({ key, tok, r: fn(tok), matching: fn.matching })
|
|
|
|
if (fn(tok)) return key;
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const EXT = exports.EXT = {
|
|
|
|
JPG: '.jpg',
|
|
|
|
JPEG: '.jpeg',
|
|
|
|
PNG: '.png',
|
|
|
|
GIF: '.gif',
|
|
|
|
MP4: '.mp4',
|
|
|
|
M4V: '.m4v',
|
|
|
|
MD: '.md',
|
|
|
|
HBS: '.hbs',
|
|
|
|
HTML: '.html',
|
|
|
|
XML: '.xml',
|
2020-02-28 09:15:21 -08:00
|
|
|
CSS: '.css',
|
|
|
|
SCSS: '.scss',
|
|
|
|
JS: '.js',
|
2020-03-08 14:01:00 -07:00
|
|
|
JSX: '.jsx',
|
2020-02-27 18:57:39 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const {
|
|
|
|
JPG,
|
|
|
|
JPEG,
|
|
|
|
PNG,
|
|
|
|
GIF,
|
|
|
|
MP4,
|
|
|
|
M4V,
|
|
|
|
MD,
|
|
|
|
HBS,
|
|
|
|
HTML,
|
|
|
|
XML,
|
2020-02-28 09:15:21 -08:00
|
|
|
CSS,
|
|
|
|
SCSS,
|
|
|
|
JS,
|
2020-03-08 14:01:00 -07:00
|
|
|
JSX,
|
2020-02-27 18:57:39 -08:00
|
|
|
} = EXT;
|
|
|
|
|
|
|
|
const NORMALIZE_EXT = {
|
|
|
|
[JPG]: JPEG,
|
|
|
|
[M4V]: MP4,
|
|
|
|
[HBS]: HTML,
|
2020-03-08 14:01:00 -07:00
|
|
|
[JSX]: JS,
|
2020-02-27 18:57:39 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const normalizedExt = exports.normalizedExt = (ext) => {
|
|
|
|
if (ext[0] !== '.') ext = '.' + ext.split('.').pop();
|
|
|
|
return NORMALIZE_EXT[ext] || ext;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isVideo = exports.isVideo = is(MP4, M4V);
|
|
|
|
const isImage = exports.isImage = is(JPG, JPEG, PNG, GIF);
|
2020-05-13 11:45:26 -07:00
|
|
|
const isHandybars = exports.isHandybars = is(XML, HBS, HTML);
|
2020-02-27 18:57:39 -08:00
|
|
|
const isMarkdown = exports.isMarkdown = is(MD);
|
2020-05-13 11:45:26 -07:00
|
|
|
const isPage = exports.isPage = is(isHandybars, isMarkdown);
|
2020-02-27 18:57:39 -08:00
|
|
|
const isAsset = exports.isAsset = is(isImage, isVideo);
|
2020-03-08 14:01:00 -07:00
|
|
|
const isArtifact = exports.isArtifact = is(CSS, SCSS, JS, JSX);
|
2020-02-27 20:42:33 -08:00
|
|
|
exports.isCleanUrl = is(HBS, MD);
|
2020-02-27 18:57:39 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const TYPE = exports.TYPE = {
|
2020-03-07 18:04:37 -08:00
|
|
|
IMAGE: 'TYPE_IMAGE',
|
|
|
|
VIDEO: 'TYPE_VIDEO',
|
2020-05-13 11:45:26 -07:00
|
|
|
HANDYBARS: 'TYPE_HANDYBARS',
|
2020-03-07 18:04:37 -08:00
|
|
|
MARKDOWN: 'TYPE_MARKDOWN',
|
|
|
|
SCRIPT: 'TYPE_SCRIPT',
|
|
|
|
STYLE: 'TYPE_STYLE',
|
|
|
|
OTHER: 'TYPE_OTHER',
|
2020-02-27 18:57:39 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.type = dictMatch({
|
|
|
|
[TYPE.IMAGE]: isImage,
|
2020-05-13 11:45:26 -07:00
|
|
|
[TYPE.HANDYBARS]: isHandybars,
|
2020-02-27 18:57:39 -08:00
|
|
|
[TYPE.MARKDOWN]: isMarkdown,
|
|
|
|
[TYPE.VIDEO]: isVideo,
|
2020-03-08 14:01:00 -07:00
|
|
|
[TYPE.SCRIPT]: is(JS, JSX),
|
2020-02-28 09:15:21 -08:00
|
|
|
[TYPE.STYLE]: is(SCSS, CSS),
|
2020-02-27 18:57:39 -08:00
|
|
|
}, TYPE.OTHER);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const KIND = exports.KIND = {
|
2020-03-07 18:04:37 -08:00
|
|
|
PAGE: 'KIND_PAGE',
|
|
|
|
POST: 'KIND_POST',
|
|
|
|
ASSET: 'KIND_ASSET',
|
|
|
|
ARTIFACT: 'KIND_ARTIFACT',
|
|
|
|
OTHER: 'KIND_OTHER',
|
2020-02-27 18:57:39 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.kind = dictMatch({
|
2020-02-28 09:15:21 -08:00
|
|
|
[KIND.ASSET]: isAsset,
|
|
|
|
[KIND.PAGE]: isPage,
|
|
|
|
[KIND.ARTIFACT]: isArtifact,
|
2020-02-27 18:57:39 -08:00
|
|
|
}, KIND.OTHER);
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-07 18:04:37 -08:00
|
|
|
exports.ENGINE = {
|
|
|
|
HTML: 'ENGINE_HTML',
|
|
|
|
PAGE: 'ENGINE_PAGE',
|
|
|
|
POST: 'ENGINE_POST',
|
|
|
|
OTHER: 'ENGINE_OTHER',
|
2020-02-27 18:57:39 -08:00
|
|
|
};
|
|
|
|
|
2020-02-25 19:37:10 -08:00
|
|
|
exports.readFile = function readFile (fpath) {
|
|
|
|
fpath = exports.resolve(fpath);
|
|
|
|
return fs.readFile(fpath).catch((err) => {
|
2020-02-28 09:15:21 -08:00
|
|
|
throw new Error(err.message);
|
2020-02-25 19:37:10 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.resolve = function resolve (...args) {
|
|
|
|
args = args.filter(Boolean);
|
2020-02-28 10:27:52 -08:00
|
|
|
const fpath = args.shift();
|
2020-02-25 19:37:10 -08:00
|
|
|
if (!fpath) return ROOT;
|
|
|
|
return path.resolve(ROOT, fpath, ...args);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.relative = function relative (fpath) {
|
|
|
|
return path.relative(ROOT, fpath);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.ROOT = ROOT;
|