UNPKG

@viewdo/dxp-story-cli

Version:
223 lines (161 loc) 6.61 kB
const path = require('path'); const isUndefined = require('lodash/isUndefined'); const isObject = require('lodash/isObject'); const isEmpty = require('lodash/isEmpty'); const kebabCase = require('lodash/kebabCase'); class PathManager { constructor(source, output, opts = {}) { const { story_key, episode_key } = opts; Object.assign(this, { story_key, episode_key, path, isUndefined, isObject, isEmpty, kebabCase }); this._setUp(source, output) } _setUp(source, output) { Object.assign(this, { output: this._resolve(output), source: this._resolve(source) }); } getSource() { return this.source; } getStoryOutputPath() { const { output, story_key, episode_key } = this; if (!story_key) return output; return this._resolve(`${output}/${story_key}${episode_key ? "/episodes/" + episode_key : ''}`); } getStoryOutputJson() { const storyPath = this.getStoryOutputPath(); return this._resolve(`${storyPath}/story.json`); } getStoryConfigSourcePath() { const { source, story_key, episode_key = "default" } = this; const has_story_key = !isUndefined(story_key); if (!has_story_key) return source; const story_path = `/${story_key}`; const episode_path = `/${episode_key}`; return this._resolve(`${source}${story_path}/config${episode_path}`); } getStatesFolderPath() { const src = this.getStoryConfigSourcePath(); return this._resolve(`${src}/states`); } getCommonStatesFolderPath() { const src = this.getStatesFolderPath(); return this._resolve(`${src}/_common`); } getStateFolderPath(state, opts = {}) { const { isEmpty, isUndefined } = this; const { parent_state = {}, embedded_view = {}, embedded_parent_states = [] } = opts; const { id: state_id, embedded } = state; const hasOneParent = embedded_parent_states.length == 1; const hasMultipleParents = embedded_parent_states.length > 1; let state_path = this.getStatesFolderPath(); if (hasMultipleParents) { state_path = this.getCommonStatesFolderPath(); } if (hasOneParent) { const { id: parent_state_id } = embedded_parent_states[0]; const { id: embedded_view_id } = embedded_view; state_path = `${state_path}/${parent_state_id}/embedded-views/${embedded_view_id}`; } state_path = `${state_path}/${state_id}`; return this._resolve(state_path); } getEmbeddedViewsFolderPath(state, opts = {}) { const state_path = this.getStateFolderPath(state, opts); return this._resolve(`${state_path}/embedded-views`); } getEmbeddedViewFolderPaths(state, opts = {}) { const embedded_views_path = this.getEmbeddedViewsFolderPath(state, opts); const { embeddedViews: embedded_views = [] } = state; const self = this; return embedded_views.map((embedded_view, index) => { const { id: embedded_view_id } = embedded_view; return { id: embedded_view_id, path: self._resolve(`${embedded_views_path}/${embedded_view_id}`) }; }) } getNavigationLinksFolderPath(navigation_state, opts) { const state_path = this.getStateFolderPath(navigation_state, opts); return this._resolve(`${state_path}/links`); } getNavigationLinkFolderPath(link = {}, navigation_state, opts) { const navigation_links_path = this.getNavigationLinksFolderPath(navigation_state, opts); const { id: link_id = `link-${opts.index}` } = link; return this._resolve(`${navigation_links_path}/${link_id}`); } getInputsFolderPath(input_state, opts) { const state_path = this.getStateFolderPath(input_state, opts); return this._resolve(`${state_path}/inputs`); } getInputFolderPath(input, input_state, opts) { const state_path = this.getStateFolderPath(input_state, opts); const { id: input_id = `input-${opts.index}` } = input; return this._resolve(`${state_path}/inputs/${input_id}`); } getEventsFolderPath(source_path) { return this._resolve(`${source_path}/events`); } getTemplatesFolderPath(source_path) { return this._resolve(`${source_path}/templates`); } getTracksFolderPath(source_path) { return this._resolve(`${source_path}/tracks`); } getPageElementsFolderPath() { const src = this.getStoryConfigSourcePath(); return this._resolve(`${src}/page-elements`); } getPageElementFolderPath(page_element, opts = {}) { const { id: page_element_id = `page-element-${opts.index}` } = page_element; const page_elements_folder_path = this.getPageElementsFolderPath(); return this._resolve(`${page_elements_folder_path}/${page_element_id}`); } getMetadataFolderPath() { const src = this.getStoryConfigSourcePath(); return this._resolve(`${src}/metadata`); } getStaticDataFolderPath() { const src = this.getStoryConfigSourcePath(); return this._resolve(`${src}/static-data`); } getStaticDataPaths(static_data = {}) { const { kebabCase, isObject } = this; const self = this; const static_data_path = this.getStaticDataFolderPath(); const complex_data_paths = Object.keys(static_data) .reduce((current_complex_data_paths, key) => { const current_data = static_data[key]; const is_complex_data = isObject(current_data) || Array.isArray(current_data); if (is_complex_data) { const path = self._resolve(`${static_data_path}/${kebabCase(key)}`); current_complex_data_paths = [ ...current_complex_data_paths, { data: current_data, static_data, key, path } ]; } return current_complex_data_paths }, []); return complex_data_paths; } _resolve(current_path) { const { path } = this; return path.resolve(current_path); } } module.exports = PathManager;