@viewdo/dxp-story-cli
Version:
README.md
167 lines (133 loc) • 8.35 kB
JavaScript
const { iVXjsReader, FileCreator, FolderCreator } = require('../services');
const fs = require('fs');
const isEmpty = require('lodash/isEmpty');
class Unpack {
constructor(source, _console = console, opts = {}) {
const { story_key, episode_key } = opts;
Object.assign(this, {
source,
fs,
story_key,
episode_key,
console: _console
});
}
unpackFromFile(file_path, cb = () => {}) {
const { fs } = this;
try {
if (fs.existsSync(file_path)) {
const file_contents = fs.readFileSync(file_path, 'utf8');
if (isEmpty(file_contents) || file_contents === "{}") return;
const ivx_js_config = JSON.parse(fs.readFileSync(file_path, 'utf8'));
return this.unpack(ivx_js_config, cb);
}
} catch (err) {
this.console.error(err);
this.console.log('Unpacking was not successful.'.red);
process.exit();
}
}
unpack(ivx_js_config, cb = () => { }) {
const { source, story_key, episode_key } = this;
const ivx_js_reader = new iVXjsReader(ivx_js_config, source, { story_key, episode_key });
const folder_creator = new FolderCreator(this.console);
const file_creator = new FileCreator(this.console);
ivx_js_reader
.handleiVXjsConfig((ivx_js_config, opts) => {
const { config_path, page_elements_path, metadata_path, static_data_path } = opts;
folder_creator.createRootConfigFolders(config_path, page_elements_path, metadata_path, static_data_path);
file_creator.createConfigFiles(ivx_js_config, config_path, page_elements_path);
})
.handleStates((states, opts) => {
const { states_folder_path, common_states_folder_path, common_states = [] } = opts;
folder_creator.createStatesFolder(states_folder_path, common_states_folder_path);
file_creator.createStatesFiles(states, states_folder_path, common_states, common_states_folder_path);
})
.handleState((state, opts) => {
const { state_path, state_events_path, state_templates_path, state_embedded_view_paths } = opts;
folder_creator.createStateFolder(state, state_path, state_events_path, state_templates_path, state_embedded_view_paths);
})
.handleHtmlState((html_state, opts) => {
const { state_path, state_events_path, state_embedded_view_paths = [], state_embedded_views_path, common_states = [] } = opts;
const { embedded, embeddedViews: embedded_views = [] } = html_state;
file_creator.createHtmlStateFiles(html_state, state_path, state_events_path);
if (!embedded && !isEmpty(embedded_views))
file_creator.createEmbeddedViews(html_state, state_embedded_views_path, state_embedded_view_paths, common_states);
})
.handleNavigationState((navigation_state, opts) => {
const { state_path, state_events_path, state_templates_path, state_links_path, state_embedded_view_paths = [], state_embedded_views_path, common_states = [] } = opts;
const { embedded, embeddedViews: embedded_views = [], links = [] } = navigation_state;
folder_creator.createStateLinksFolder(navigation_state, state_links_path);
file_creator.createNavigationStateFiles(navigation_state, state_path, state_events_path, state_templates_path);
file_creator.createLinksStateFiles(links, state_links_path);
if (!embedded && !isEmpty(embedded_views))
file_creator.createEmbeddedViews(navigation_state, state_embedded_views_path, state_embedded_view_paths, common_states);
})
.handleNavigationLink((link, navigation_state, opts) => {
const { link_path, index } = opts;
folder_creator.createNavigationLinkFolder(link_path);
file_creator.createNavigationLinkFiles(link, navigation_state, link_path, index);
})
.handleVideoState((video_state, opts) => {
const { state_path, state_events_path, state_templates_path, tracks_folder_path, state_embedded_view_paths = [], state_embedded_views_path, common_states = [] } = opts;
const { embedded, embeddedViews: embedded_views = [], playerSettings: player_settings = {} } = video_state;
const { tracks = [] } = player_settings;
file_creator.createVideoStateFiles(video_state, state_path, state_events_path, state_templates_path);
if (!isEmpty(tracks)) {
folder_creator.createTracksFolder(tracks_folder_path);
file_creator.createVideoTracksFiles(tracks, tracks_folder_path);
}
if (!embedded && !isEmpty(embedded_views))
file_creator.createEmbeddedViews(video_state, state_embedded_views_path, state_embedded_view_paths, common_states);
})
.handleInputState((input_state, opts) => {
const { state_path, state_events_path, state_templates_path, state_inputs_path, state_embedded_view_paths = [], state_embedded_views_path, common_states = [] } = opts;
const { embedded, embeddedViews: embedded_views = [], inputs = [] } = input_state;
file_creator.createInputStateFiles(input_state, state_path, state_events_path, state_templates_path);
folder_creator.createStateInputsFolder(state_inputs_path);
file_creator.createInputStateInputsFiles(inputs, state_inputs_path);
if (!embedded && !isEmpty(embedded_views))
file_creator.createEmbeddedViews(video_state, state_embedded_views_path, state_embedded_view_paths, common_states);
})
.handleInput((input, input_state, opts) => {
const { input_path, input_template_path, index } = opts;
folder_creator.createInputFolder(input_path, input_template_path);
file_creator.createInputFiles(input, input_path, input_template_path, index);
})
.handlePageElement((page_element, opts) => {
const { page_element_path } = opts;
folder_creator.createPageElementFolder(page_element_path)
})
.handleCtaOverlayPageElement((page_element, opts) => {
const { page_element_path, page_element_templates_path, page_element_events_path } = opts;
folder_creator.createCtaOverlayPageElement(page_element_templates_path, page_element_events_path)
file_creator.createCtaOverlayPageElementFiles(page_element, page_element_path, page_element_templates_path, page_element_events_path);
})
.handleCtaLinkPageElement((page_element, opts) => {
const { page_element_path } = opts;
file_creator.createCtaLinkPageElementFiles(page_element, page_element_path);
})
.handleHtmlPageElement((page_element, opts) => {
const { page_element_path } = opts;
file_creator.createHtmlPageElementFiles(page_element, page_element_path);
})
.handleImagePageElement((page_element, opts) => {
const { page_element_path } = opts;
file_creator.createImagePageElementsFiles(page_element, page_element_path);
})
.handleMetadata((metadata, opts) => {
const { metadata_path } = opts;
file_creator.createMetadataFiles(metadata, metadata_path);
})
.handleStaticData((static_data, opts) => {
const { static_data_complex_paths = [], static_data_path, basic_data = {} } = opts;
folder_creator.createComplexStaticDataFolder(static_data_complex_paths);
file_creator.createStaticDataFiles(static_data, static_data_path, basic_data, static_data_complex_paths);
})
.read(() => {
cb()
});
return this;
}
}
module.exports = Unpack;