statigen
Version:
A static site generator that supports html, ejs, and markdown source files
105 lines • 3.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestVersion = exports.replacePath = exports.toUnixPath = exports.getRelativeUrl = exports.getTitleFromFilePath = exports.getEjsError = exports.createRange = exports.log = exports.s = exports.standardizePath = void 0;
const path = require("path");
const moment = require("moment");
const chalk = require("chalk");
const child_process_1 = require("child_process");
const semver = require("semver");
/**
*
*/
function standardizePath(...parts) {
//remove null or empty parts
parts = parts.filter(x => !!x);
return path.normalize(path.resolve(...parts)).replace(/[\\\/]/g, path.sep);
}
exports.standardizePath = standardizePath;
/**
* Normalize, resolve, and standardize path.sep for a chunk of path parts.A tagged template literal function for standardizing the path. This has to be defined as standalone function since it's a tagged template literal function,
* we can't use `object.tag` syntax.
*/
function s(stringParts, ...expressions) {
let parts = [];
for (let i = 0; i < stringParts.length; i++) {
parts.push(stringParts[i], expressions[i]);
}
return path.normalize(parts.filter(x => !!x).join(path.sep)).replace(/[\\\/]/g, path.sep);
}
exports.s = s;
function log(...messages) {
console.log('[' + chalk.grey(moment().format(`hh:mm:ss:SSSS A`)) + ']', ...messages);
}
exports.log = log;
function createRange(startLine, startCharacter, endLine, endCharacter) {
return {
start: {
line: startLine,
character: startCharacter
},
end: {
line: endLine,
character: endCharacter
}
};
}
exports.createRange = createRange;
/**
* Given an ejs error message, parse it and find the actual error information
*/
function getEjsError(error) {
var _a;
const [, lineMatch] = (_a = />>\s*(\d+)\s*\|/g.exec(error.message)) !== null && _a !== void 0 ? _a : [];
const lineNumber = parseInt(lineMatch);
return {
message: error.message.split(/\r?\n/).pop(),
line: isNaN(lineNumber) ? undefined : lineNumber
};
}
exports.getEjsError = getEjsError;
/**
* Given a path to a file, convert it into a title
* (remove extension, replace dashes with spaces, upper case first letter of each word)
*/
function getTitleFromFilePath(filePath) {
//derive a title from the filename
const filename = path.basename(filePath).replace(/\.html$/i, '');
//remove dashes
return filename.split('-').join(' ');
}
exports.getTitleFromFilePath = getTitleFromFilePath;
/**
* Get a relative url based on the position of the template file and the host file
* @param url the URL relative to the `templateOutPath`
* @param templateOutPath the output path of the template
* @param hostOutPath the outPath of the host file (the file being published)
*/
function getRelativeUrl(url, templateOutPath, hostOutPath) {
const relativePath = path.relative(path.dirname(hostOutPath), path.join(path.dirname(templateOutPath), url));
return relativePath.replace(/[\\\/]/g, '/');
}
exports.getRelativeUrl = getRelativeUrl;
function toUnixPath(thePath) {
return thePath.replace(/[\/\\]/g, '/');
}
exports.toUnixPath = toUnixPath;
function replacePath(subject, search, replace) {
const idx = toUnixPath(subject).indexOf(toUnixPath(search));
if (idx > -1) {
return replace + subject.substring(idx + search.length);
}
else {
return subject;
}
}
exports.replacePath = replacePath;
/**
*
* @param packageName Find the latest-published version of an npm package
*/
function getLatestVersion(packageName) {
const versions = JSON.parse((0, child_process_1.execSync)(`npm view ${packageName} versions --json`).toString());
return semver.maxSatisfying(versions, '*');
}
exports.getLatestVersion = getLatestVersion;
//# sourceMappingURL=util.js.map
;