|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const glob = require('glob'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Simple Promiseify function that takes a Node API and return a version that supports promises. |
| 10 | + * We use promises instead of synchronized functions to make the process less I/O bound and |
| 11 | + * faster. It also simplify the code. |
| 12 | + */ |
| 13 | +function promiseify(fn) { |
| 14 | + return function() { |
| 15 | + const args = [].slice.call(arguments, 0); |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + fn.apply(this, args.concat([function (err, value) { |
| 18 | + if (err) { |
| 19 | + reject(err); |
| 20 | + } else { |
| 21 | + resolve(value); |
| 22 | + } |
| 23 | + }])); |
| 24 | + }); |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +const readFile = promiseify(fs.readFile); |
| 29 | +const writeFile = promiseify(fs.writeFile); |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * For every argument, inline the templates and styles under it and write the new file. |
| 34 | + */ |
| 35 | +for (let arg of process.argv.slice(2)) { |
| 36 | + if (arg.indexOf('*') < 0) { |
| 37 | + // Argument is a directory target, add glob patterns to include every files. |
| 38 | + arg = path.join(arg, '**', '*'); |
| 39 | + } |
| 40 | + |
| 41 | + const files = glob.sync(arg, {}) |
| 42 | + .filter(name => /\.js$/.test(name)); // Matches only JavaScript files. |
| 43 | + |
| 44 | + // Generate all files content with inlined templates. |
| 45 | + files.forEach(filePath => { |
| 46 | + readFile(filePath, 'utf-8') |
| 47 | + .then(content => inlineTemplate(filePath, content)) |
| 48 | + .then(content => inlineStyle(filePath, content)) |
| 49 | + .then(content => writeFile(filePath, content)) |
| 50 | + .catch(err => { |
| 51 | + console.error('An error occured: ', err); |
| 52 | + }); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +/** |
| 58 | + * Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and |
| 59 | + * replace with `template: ...` (with the content of the file included). |
| 60 | + * @param filePath {string} The path of the source file. |
| 61 | + * @param content {string} The source file's content. |
| 62 | + * @return {string} The content with all templates inlined. |
| 63 | + */ |
| 64 | +function inlineTemplate(filePath, content) { |
| 65 | + return content.replace(/templateUrl:\s*'([^']+\.html)'/g, function(m, templateUrl) { |
| 66 | + const templateFile = path.join(path.dirname(filePath), templateUrl); |
| 67 | + const templateContent = fs.readFileSync(templateFile, 'utf-8'); |
| 68 | + const shortenedTemplate = templateContent |
| 69 | + .replace(/([\n\r]\s*)+/gm, ' ') |
| 70 | + .replace(/"/g, '\\"'); |
| 71 | + return `template: "${shortenedTemplate}"`; |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +/** |
| 77 | + * Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and |
| 78 | + * replace with `styles: [...]` (with the content of the file included). |
| 79 | + * @param filePath {string} The path of the source file. |
| 80 | + * @param content {string} The source file's content. |
| 81 | + * @return {string} The content with all styles inlined. |
| 82 | + */ |
| 83 | +function inlineStyle(filePath, content) { |
| 84 | + return content.replace(/styleUrls:\s*(\[[\s\S]*?\])/gm, function(m, styleUrls) { |
| 85 | + const urls = eval(styleUrls); |
| 86 | + return 'styles: [' |
| 87 | + + urls.map(styleUrl => { |
| 88 | + const styleFile = path.join(path.dirname(filePath), styleUrl); |
| 89 | + const styleContent = fs.readFileSync(styleFile, 'utf-8'); |
| 90 | + const shortenedStyle = styleContent |
| 91 | + .replace(/([\n\r]\s*)+/gm, ' ') |
| 92 | + .replace(/"/g, '\\"'); |
| 93 | + return `"${shortenedStyle}"`; |
| 94 | + }) |
| 95 | + .join(',\n') |
| 96 | + + ']'; |
| 97 | + }); |
| 98 | +} |
0 commit comments