myst-cli
Version:
Command line tools for MyST
50 lines (49 loc) • 2.56 kB
JavaScript
import fs from 'node:fs';
import { join } from 'node:path';
import { RuleId, TemplateKind } from 'myst-common';
import { createNpmLogger, makeExecutable, tic } from 'myst-cli-utils';
import MystTemplate from 'myst-templates';
import { selectors } from '../../store/index.js';
import { addWarningForFile } from '../../utils/addWarningForFile.js';
import { castSession } from '../../session/cache.js';
const DEFAULT_TEMPLATE = 'book-theme';
const DEFAULT_INSTALL_COMMAND = 'npm install';
export async function getSiteTemplate(session, opts) {
var _a, _b, _c, _d;
const cache = castSession(session);
const state = cache.store.getState();
if (cache.$siteTemplate)
return cache.$siteTemplate;
const siteConfig = selectors.selectCurrentSiteConfig(state);
const file = (_a = selectors.selectCurrentSiteFile(state)) !== null && _a !== void 0 ? _a : session.configFiles[0];
const mystTemplate = new MystTemplate(session, {
kind: TemplateKind.site,
template: (_d = (_c = (_b = opts === null || opts === void 0 ? void 0 : opts.template) !== null && _b !== void 0 ? _b : siteConfig === null || siteConfig === void 0 ? void 0 : siteConfig.template) !== null && _c !== void 0 ? _c : opts === null || opts === void 0 ? void 0 : opts.defaultTemplate) !== null && _d !== void 0 ? _d : DEFAULT_TEMPLATE,
buildDir: session.buildPath(),
errorLogFn: (message) => {
addWarningForFile(session, file, message, 'error', {
ruleId: RuleId.validSiteConfig,
});
},
warningLogFn: (message) => {
addWarningForFile(session, file, message, 'warn', {
ruleId: RuleId.validSiteConfig,
});
},
validateFiles: (opts === null || opts === void 0 ? void 0 : opts.template) ? false : true,
});
await mystTemplate.ensureTemplateExistsOnPath();
cache.$siteTemplate = mystTemplate;
return mystTemplate;
}
export async function installSiteTemplate(session, mystTemplate) {
var _a, _b;
if (fs.existsSync(join(mystTemplate.templatePath, 'node_modules')))
return;
const toc = tic();
session.log.info('⤵️ Installing web libraries (can take up to 60 s)');
await makeExecutable((_b = (_a = mystTemplate.getValidatedTemplateYml().build) === null || _a === void 0 ? void 0 : _a.install) !== null && _b !== void 0 ? _b : DEFAULT_INSTALL_COMMAND, createNpmLogger(session), {
cwd: mystTemplate.templatePath,
})();
session.log.info(toc('📦 Installed web libraries in %s'));
}