|
| 1 | +const sass = require('node-sass'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const path = require('path'); |
| 4 | +const { exec } = require('child_process'); |
| 5 | + |
| 6 | +const tempFolder = '.tmp'; |
| 7 | +const componentPath = `./${tempFolder}/image-viewer.component.ts`; |
| 8 | +const scssPath = `./${tempFolder}/image-viewer.scss`; |
| 9 | + |
| 10 | +process(); |
| 11 | + |
| 12 | +function process() { |
| 13 | + console.log('Running build...'); |
| 14 | + deleteFolderRecursiveSync(tempFolder); |
| 15 | + copyRecursiveSync('src', tempFolder); |
| 16 | + |
| 17 | + const component = getFileContent(componentPath); |
| 18 | + const css = getCss(getFileContent(scssPath)); |
| 19 | + |
| 20 | + console.log('Inlining styles...'); |
| 21 | + const newComponent = component.replace('styles: []', `styles: ['${css}']`); |
| 22 | + |
| 23 | + writeFile(componentPath, newComponent, () => { |
| 24 | + console.log('About to run ngc async...'); |
| 25 | + |
| 26 | + runNgc('tsconfig.json'); |
| 27 | + runNgc('tsconfig.umd.json'); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function runNgc(tsConfigPath) { |
| 32 | + console.log('Started for', tsConfigPath); |
| 33 | + |
| 34 | + const ngc = path.resolve('node_modules', '.bin', 'ngc'); |
| 35 | + exec(`${ngc} -p ${tsConfigPath}`, (err, stdout, stdeer) => { |
| 36 | + if (err) { |
| 37 | + console.log('Error !', err); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + console.log('Done for', tsConfigPath); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function getCss(scss_content) { |
| 46 | + const style = sass.renderSync({ |
| 47 | + data: scss_content |
| 48 | + }); |
| 49 | + |
| 50 | + return style.css |
| 51 | + .toString() |
| 52 | + .replace(/([\n\r]\s*)+/gm, ' ') |
| 53 | + .replace(/"/g, '\\"'); |
| 54 | +} |
| 55 | + |
| 56 | +function getFileContent(path) { |
| 57 | + return fs.readFileSync(path, 'utf8'); |
| 58 | +} |
| 59 | + |
| 60 | +function writeFile(path, data, callback) { |
| 61 | + return fs.writeFile(path, data, err => { |
| 62 | + if (err) { |
| 63 | + console.log('Error while writing file !', err); |
| 64 | + } |
| 65 | + |
| 66 | + callback(); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +function copyRecursiveSync(src, dest) { |
| 71 | + const exists = fs.existsSync(src); |
| 72 | + const stats = exists && fs.statSync(src); |
| 73 | + const isDirectory = exists && stats.isDirectory(); |
| 74 | + if (exists && isDirectory) { |
| 75 | + fs.mkdirSync(dest); |
| 76 | + fs.readdirSync(src).forEach(function(childItemName) { |
| 77 | + copyRecursiveSync( |
| 78 | + path.join(src, childItemName), |
| 79 | + path.join(dest, childItemName) |
| 80 | + ); |
| 81 | + }); |
| 82 | + } else { |
| 83 | + fs.copySync(src, dest); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function deleteFolderRecursiveSync(path) { |
| 88 | + if (fs.existsSync(path)) { |
| 89 | + fs.readdirSync(path).forEach(function(file, index) { |
| 90 | + var curPath = path + '/' + file; |
| 91 | + if (fs.lstatSync(curPath).isDirectory()) { |
| 92 | + // recurse |
| 93 | + deleteFolderRecursive(curPath); |
| 94 | + } else { |
| 95 | + // delete file |
| 96 | + fs.removeSync(curPath); |
| 97 | + } |
| 98 | + }); |
| 99 | + fs.rmdirSync(path); |
| 100 | + } |
| 101 | +} |
0 commit comments