UNPKG

ring-websites-toolbelt

Version:

Ring Publishing Platform tool to work with Ring Websites

122 lines (95 loc) 4.71 kB
const walkSync = require('walk-sync'); const path = require("path"); const fs = require("fs"); const Builder = require(`./../../Builder`); const ThemeScriptAbstract = require(`./../../ThemeScriptAbstract`); const ExternalApiProvider = require(`./../../providers/ExternalApiProvider`); const Watchers = require(`./../../Watchers`); class Start extends ThemeScriptAbstract { constructor(options) { super(options); this.validateThemeJson(); this.loadThemeVarriables(); this.setUpProviders(); this.buildExcludeFilters = []; if (this.themeJson.development && this.themeJson.development.buildExcludeFilters) { this.buildExcludeFilters = this.themeJson.development.buildExcludeFilters; } this._builder = new Builder(this.paths, this.buildExcludeFilters); this._themePath = this.paths.theme; if (this.themeJson.modules && this.themeJson.modules.length) { this._themePath = this.paths.build; } } async execute() { await this._builder.build(); await this.purgeTheme(); await this.initialDeploy(); // must be before start watchers await this.startWatchers(); } async initialDeploy() { console.info('Starting initial deploy of theme'); const cdfTemplates = walkSync.entries(path.join(this._themePath, Start.DIRECTORIES.CDF_TEMPLATES), {directories: false}); await this.api.insertFiles(cdfTemplates, ExternalApiProvider.CODES.CDF_TEMPLATES); const teTemplates = walkSync.entries(path.join(this._themePath, Start.DIRECTORIES.TE_TEMPLATES), {directories: false}); await this.api.insertFiles(teTemplates, ExternalApiProvider.CODES.TE_TEMPLATES); const viewHelpers = walkSync.entries(path.join(this._themePath, Start.DIRECTORIES.VIEW_HELPERS), {directories: false}); await this.api.insertFiles(viewHelpers, ExternalApiProvider.CODES.VIEW_HELPERS); const translations = walkSync.entries(path.join(this._themePath, Start.DIRECTORIES.TRANSLATIONS), {directories: false}); await this.api.insertFiles(translations, ExternalApiProvider.CODES.TRANSLATIONS); if (!fs.existsSync(path.join(this._themePath, Start.DIRECTORIES.STATIC))) { fs.mkdirSync(path.join(this._themePath, Start.DIRECTORIES.STATIC)); } await this.files.compressDir(path.join(this._themePath, Start.DIRECTORIES.STATIC), Start.DIRECTORIES.STATIC, Start.FILES.STATICS_ZIP); this.ocdnData = await this.s3.uploadCompressedFile(Start.FILES.STATICS_ZIP); if (!this.ocdnData || !this.ocdnData.Location) { throw new Error("There is no key 'Location'!"); } console.info(`Static files uploaded to: ${this.ocdnData.Location}`); await this.files.removeCompressedFile(Start.FILES.STATICS_ZIP); let configPath = path.join(this._themePath, Start.FILES.CONFIG_JSON); let partialPath = path.join(this._themePath, Start.FILES.PARTIALS_JSON); let schemesPath = path.join(this._themePath, Start.DIRECTORIES.SCHEMES); let ocdnLocation = this.ocdnData.Location + Start.DIRECTORIES.STATIC; let configJson = this.files.getConfigJson(configPath, partialPath, schemesPath, ocdnLocation); this.api.insertThemeConfig(configJson); } async startWatchers() { this._watchers = new Watchers({ paths: { ...this.paths, build: this._themePath }, dbCodes: ExternalApiProvider.CODES, directories: Start.DIRECTORIES, files: Start.FILES, ocdnData: this.ocdnData, providers: { api: this.api, files: this.files, builder: this._builder, s3: this.s3 } }); console.info('Starting watchers'); if (this.themeJson.modules && this.themeJson.modules.length) { this._watchers.startBuildingTheme(); } this._watchers.startSavingConfig(); this._watchers.startSavingTheme(); this._watchers.startSavingStatic(); this._watchers.startGetLogs(this.options.verbose); } } Start.DIRECTORIES = {}; Start.DIRECTORIES.CDF_TEMPLATES = '/cdf/'; Start.DIRECTORIES.TE_TEMPLATES = '/templates/'; Start.DIRECTORIES.VIEW_HELPERS = '/helpers/view/'; Start.DIRECTORIES.TRANSLATIONS = '/translations/'; Start.DIRECTORIES.STATIC = '/static/'; Start.DIRECTORIES.SCHEMES = '/dynamic/schemes'; Start.FILES = {}; Start.FILES.CONFIG_JSON = 'config.json'; Start.FILES.PARTIALS_JSON = 'partials.json'; Start.FILES.STATICS_ZIP = 'static.zip'; module.exports = Start;