UNPKG

ring-websites-toolbelt

Version:

Ring Publishing Platform tool to work with Ring Websites

96 lines (84 loc) 3.95 kB
const fs = require('fs'); const rimraf = require('rimraf'); const mkdirp = require('mkdirp'); const { compareSync } = require('dir-compare'); const ucsthemebuilder = require('ring-ucs-theme-builder'); const minimatch = require('minimatch'); class Builder { constructor(paths, excludeFilters) { this.paths = paths; if(Array.isArray(excludeFilters)) { this.excludeFilters = Builder.IGNORED_PATHS.concat(excludeFilters) } else { this.excludeFilters = Builder.IGNORED_PATHS; } } async build() { const buildDestination = !fs.existsSync(this.paths.build) ? this.paths.build : this.paths.temp_build; await new Promise((resolve, reject) => { ucsthemebuilder.build(this.paths.theme, this.paths.modules, buildDestination, function (err, modules) { if (err) { reject(err); } resolve(modules); }); }); if (buildDestination !== this.paths.build) { const filesCompare = compareSync(this.paths.temp_build, this.paths.build, { compareContent: true, excludeFilter: this.excludeFilters.join() }); filesCompare.diffSet.forEach((diff) => { if (diff.state !== 'equal') { const sourcePath = buildDestination + diff.relativePath + '/' + diff.name1; const destinationPath = this.paths.build + diff.relativePath + '/' + diff.name1; const removePath = this.paths.build + diff.relativePath + '/' + diff.name2; let relPath = diff.name1 || diff.name2; relPath = diff.relativePath + '/' + relPath; for(let i=0; i < this.excludeFilters.length; i++) { if (minimatch(relPath, this.excludeFilters[i], {dot: true})) { //console.warn(`ignore: ${relPath}`); return } } //console.info(`ok: ${relPath}`); if (diff.state === 'distinct') { fs.unlinkSync(destinationPath); fs.appendFileSync(destinationPath, fs.readFileSync(sourcePath)); } else if (diff.state === 'left') { if (diff.type1 === 'directory') { mkdirp.sync(destinationPath); } else { fs.appendFileSync(destinationPath, fs.readFileSync(sourcePath)); } } else if (diff.state === 'right') { if (diff.type2 === 'directory') { if (fs.readdirSync(removePath).length === 0){ // only null directories! fs.rmdirSync(removePath); } } else { fs.unlinkSync(removePath); } } else { console.warn(`Unsupported change: "${diff.state}". Contact Support Team.`); } } }); rimraf(this.paths.temp_build, () => {}); } const themeNodeModules = this.paths.theme + '/node_modules'; const buildNodeModules = this.paths.build + '/node_modules'; if (fs.existsSync(themeNodeModules) && fs.existsSync(this.paths.build) && !fs.existsSync(buildNodeModules)) { try { fs.symlinkSync(themeNodeModules, buildNodeModules, 'dir'); } catch (error) { console.error('Unable to create symlink in your environment. Reason:'); throw error; } } } } Builder.IGNORED_PATHS = ["/node_modules", "/node_modules/**"]; module.exports = Builder;