UNPKG

@viewdo/dxp-story-cli

Version:
239 lines (186 loc) 8.94 kB
const mkdirp = require('mkdirp'); const fs = require('fs'); class FolderCreator { constructor(_console = console) { Object.assign(this, { console: _console, mkdirp, fs }); } createRootConfigFolders(root_path, page_elements_path, metadata_path, static_data_path) { this.createRootConfigFolder(root_path) this.createPageElementsFolder(page_elements_path); this.createMetadataFolder(metadata_path); this.createStaticDataFolder(static_data_path); } createRootConfigFolder(root_path) { const { mkdirp, fs } = this; if (!root_path) { if(this.console.debug) this.console.debug(`Can't create folder since there is no path.`); return; } if (fs.existsSync(root_path)) { if(this.console.debug) this.console.debug(`Can't create folder for the root because it already exists.`); return; } if(this.console.debug) this.console.debug(`Creating root folder at: ${root_path}`); mkdirp.sync(root_path); if(this.console.debug) this.console.debug(`Finished creating root folder at: ${root_path}`); } createStatesFolder(states_path, common_states_path) { const { mkdirp, fs } = this; if (!states_path) { if(this.console.debug) this.console.debug(`Can't create folder since there is no states path.`); return; } if (fs.existsSync(states_path) && fs.existsSync(common_states_path)) { if(this.console.debug) this.console.debug(`Can't create folder for the states because it already exists.`); return; } if(this.console.debug) this.console.debug(`Creating states folder at: ${states_path}`); mkdirp.sync(states_path); mkdirp.sync(common_states_path); if(this.console.debug) this.console.debug(`Finished creating states folder at: ${states_path}`); } createPageElementsFolder(page_elements_path) { const { mkdirp, fs } = this; if (!page_elements_path) { if(this.console.debug) this.console.debug(`Can't create folder since there is no page elemeents path.`); return; } if (fs.existsSync(page_elements_path)) { if(this.console.debug) this.console.debug(`Can't create folder for the page elements because it already exists.`); return; } if(this.console.debug) this.console.debug(`Creating page elements folder: ${page_elements_path}`); mkdirp.sync(page_elements_path); if(this.console.debug) this.console.debug(`Finished creating page elements folder at: ${page_elements_path}`); } createStateFolder(state, state_path, state_events_path, state_templates_path, state_embedded_view_paths = []) { const { mkdirp, fs } = this; const { type: state_type } = state; if (fs.existsSync(state_path) && fs.existsSync(state_events_path)) { if(this.console.debug) this.console.debug(`Can't create folders for the state ${state.name} (id: ${state.id}) because the folder already exists!`); return; } if(this.console.debug) this.console.debug(`Creating state folders at: ${state_path}`); mkdirp.sync(state_path); mkdirp.sync(state_events_path); if (state_type !== 'html' && !fs.existsSync(state_templates_path)) mkdirp.sync(state_templates_path); state_embedded_view_paths.forEach(state_embedded_view_path_info => { const { path: state_embedded_view_path } = state_embedded_view_path_info; mkdirp.sync(state_embedded_view_path); }) if(this.console.debug) this.console.debug(`Finished creating state folders at: ${state_path}`); } createTracksFolder(tracks_folder_path) { const { mkdirp, fs } = this; if (fs.existsSync(tracks_folder_path)) { if(this.console.debug) this.console.debug(`Can't create tracks folder at ${tracks_folder_path} because the folder already exists!`); return; } mkdirp.sync(tracks_folder_path); } createStateLinksFolder(state, state_links_path) { const { mkdirp, fs } = this; if (fs.existsSync(state_links_path)) { if(this.console.debug) this.console.debug(`Can't create links folders for the state ${state.name} (id: ${state.id}) because the folder already exists!`); return; } mkdirp.sync(state_links_path); } createNavigationLinkFolder(link_path) { const { mkdirp } = this; if (fs.existsSync(link_path)) { if(this.console.debug) this.console.debug(`Can't create link folders at ${link_path} because the folder already exists!`); return; } mkdirp.sync(link_path); } createStateInputsFolder(state_inputs_path) { const { mkdirp } = this; if (fs.existsSync(state_inputs_path)) { if(this.console.debug) this.console.debug(`Can't create link folders at ${state_inputs_path} because the folder already exists!`); return; } mkdirp.sync(state_inputs_path); } createInputFolder(input_path, input_template_path) { const { mkdirp } = this; if (fs.existsSync(input_path)) { if(this.console.debug) this.console.debug(`Can't create link folders at ${input_path} because the folder already exists!`); return; } mkdirp.sync(input_path); mkdirp.sync(input_template_path); } createPageElementFolder(page_element_path) { const { mkdirp } = this; if (fs.existsSync(page_element_path)) { if(this.console.debug) this.console.debug(`Can't create page element path folders at ${page_element_path} because the folder already exists!`); return; } mkdirp.sync(page_element_path); } createCtaOverlayPageElement(page_element_templates_path, page_element_events_path) { this._createPageElementTemplatePath(page_element_templates_path); this._createPageElementEventsPath(page_element_events_path); } _createPageElementTemplatePath(page_element_templates_path) { const { mkdirp } = this; if (fs.existsSync(page_element_templates_path)) { if(this.console.debug) this.console.debug(`Can't create page element templates folders at ${page_element_templates_path} because the folder already exists!`); return; } mkdirp.sync(page_element_templates_path); } _createPageElementEventsPath(page_element_events_path) { const { mkdirp } = this; if (fs.existsSync(page_element_events_path)) { if(this.console.debug) this.console.debug(`Can't create page element evemts folders at ${page_element_events_path} because the folder already exists!`); return; } mkdirp.sync(page_element_events_path); } createMetadataFolder(metadata_path) { const { mkdirp } = this; if (!metadata_path) { if(this.console.debug) this.console.debug(`Can't create metadata folder since there is no path.`); return; } if (fs.existsSync(metadata_path)) { if(this.console.debug) this.console.debug(`Can't create folder for the metadata because it already exists.`); return; } if(this.console.debug) this.console.debug(`Creating metadata folder at: ${metadata_path}`); mkdirp.sync(metadata_path); if(this.console.debug) this.console.debug(`Finished creating metadata folder at: ${metadata_path}`); } createStaticDataFolder(static_data_path) { const { mkdirp } = this; if (!static_data_path) { if(this.console.debug) this.console.debug(`Can't create static folder since there is no path.`); return; } if (fs.existsSync(static_data_path)) { if(this.console.debug) this.console.debug(`Can't create folder for the static-data because it already exists.`); return; } if(this.console.debug) this.console.debug(`Creating static data folder at: ${static_data_path}`); mkdirp.sync(static_data_path); if(this.console.debug) this.console.debug(`Finished creating static data folder. at: ${static_data_path}`); } createComplexStaticDataFolder(static_data_complex_paths = []) { const self = this; const { mkdirp } = this; static_data_complex_paths.forEach(({ path }) => { if (fs.existsSync(path)) { self.console.debug(`Can't create folder for the static-data complex data (path: ${path}) because it already exists.`); return; } mkdirp.sync(path); }); } } module.exports = FolderCreator;