@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
125 lines • 6.03 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const globby_1 = __importDefault(require("@sprucelabs/globby"));
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const SpruceError_1 = __importDefault(require("../../../errors/SpruceError"));
const AbstractWriter_1 = __importDefault(require("../../../writers/AbstractWriter"));
class ViewWriter extends AbstractWriter_1.default {
writeSkillViewController(cwd, options) {
const { path } = this.buildViewControllerPath(cwd, 'skillView', options.namePascal);
return this.writeController(path, { ...options, viewType: 'skillView' });
}
async writeCombinedViewsFile(cwd, options) {
let { vcTemplateItems, svcTemplateItems, viewPluginItems, appTemplateItem, ...rest } = options;
const destinationDir = spruce_skill_utils_1.diskUtil.resolveHashSprucePath(cwd, 'views');
const destination = spruce_skill_utils_1.diskUtil.resolvePath(destinationDir, 'views.ts');
vcTemplateItems = this.removeFileExtensionsFromTemplateItems(vcTemplateItems, destinationDir);
svcTemplateItems = this.removeFileExtensionsFromTemplateItems(svcTemplateItems, destinationDir);
viewPluginItems = this.removeFileExtensionsFromTemplateItems(viewPluginItems, destinationDir);
if (appTemplateItem) {
appTemplateItem = this.makePathRelative(appTemplateItem, destinationDir);
}
const contents = this.templates.views({
vcTemplateItems,
svcTemplateItems,
viewPluginItems,
appTemplateItem,
...rest,
});
const results = await this.writeFileIfChangedMixinResults(destination, contents, 'Used to export your controllers to Heartwood.');
return results;
}
removeFileExtensionsFromTemplateItems(vcTemplateItems, destinationDir) {
return vcTemplateItems.map((i) => this.makePathRelative(i, destinationDir));
}
makePathRelative(i, destinationDir) {
return {
...i,
path: spruce_skill_utils_1.diskUtil
.resolveRelativePath(destinationDir, i.path)
.replace('.ts', ''),
};
}
writeViewController(cwd, options) {
const { path } = this.buildViewControllerPath(cwd, 'view', options.namePascal);
return this.writeController(path, options);
}
async writeController(path, options) {
const { namePascal, viewModel, viewType, nameKebab } = options;
if (spruce_skill_utils_1.diskUtil.doesFileExist(path)) {
throw new SpruceError_1.default({
code: 'SKILL_VIEW_EXISTS',
name: namePascal,
});
}
const contents = viewType === 'skillView'
? this.templates.skillViewController({ namePascal, nameKebab })
: this.templates.viewController({
namePascal,
viewModel,
nameKebab,
});
const results = this.writeFileIfChangedMixinResults(path, contents, 'Your new view controller!');
return results;
}
async doesRootControllerExist(cwd) {
const matches = await (0, globby_1.default)('**/Root.svc.ts', { cwd });
return matches.length > 0;
}
async writeViewControllerPlugin(options) {
const { nameCamel, namePascal, cwd } = options;
const destination = spruce_skill_utils_1.diskUtil.resolvePath(cwd, 'src', 'viewPlugins', `${nameCamel}.view.plugin.ts`);
const contents = this.templates.viewControllerPlugin({
nameCamel,
namePascal,
});
if (spruce_skill_utils_1.diskUtil.doesFileExist(destination)) {
throw new SpruceError_1.default({
code: 'VIEW_PLUGIN_ALREADY_EXISTS',
name: nameCamel,
});
}
return this.writeFileIfChangedMixinResults(destination, contents, `Your new view plugin!`);
}
writePlugin(cwd) {
const destination = spruce_skill_utils_1.diskUtil.resolveHashSprucePath(cwd, 'features', 'view.plugin.ts');
const pluginContents = this.templates.viewPlugin();
const results = this.writeFileIfChangedMixinResults(destination, pluginContents, 'Supports your skill with rendering views.');
return results;
}
writeTheme(cwd) {
const destination = this.buildThemePath(cwd);
const contents = this.templates.theme();
const results = this.writeFileIfChangedMixinResults(destination, contents, 'Your brand new theme file!');
return results;
}
buildThemePath(cwd) {
return spruce_skill_utils_1.diskUtil.resolvePath(cwd, 'src', 'themes', 'skill.theme.ts');
}
doesThemeFileExist(cwd) {
const destination = this.buildThemePath(cwd);
return spruce_skill_utils_1.diskUtil.doesFileExist(destination);
}
buildViewControllerPath(cwd, viewType, namePascal) {
const ext = viewType === 'skillView' ? '.svc.ts' : '.vc.ts';
const filename = namePascal + ext;
const path = spruce_skill_utils_1.diskUtil.resolvePath(cwd, 'src', viewType + 'Controllers', filename);
return { path, filename };
}
async writeAppController(cwd, id, namespacePascal) {
const match = await (0, globby_1.default)(cwd + '/**/App.ac.ts');
if (match.length > 0) {
throw new SpruceError_1.default({
code: 'APP_CONTROLLER_ALREADY_EXISTS',
});
}
const destination = spruce_skill_utils_1.diskUtil.resolvePath(cwd, 'src', 'App.ac.ts');
const contents = this.templates.appController({ id, namespacePascal });
return this.writeFileIfChangedMixinResults(destination, contents, 'Your new app view controller!');
}
}
exports.default = ViewWriter;
//# sourceMappingURL=ViewWriter.js.map