|
1 | 1 | /* jshint node: true */ |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | | -var cheerio = require('cheerio'); |
5 | 4 | var path = require('path'); |
6 | 5 | var fs = require('fs'); |
| 6 | +var chalk = require('chalk'); |
7 | 7 | var RSVP = require('rsvp'); |
| 8 | +var Promise = RSVP.Promise; |
8 | 9 | var denodeify = RSVP.denodeify; |
9 | 10 |
|
10 | 11 | var readFile = denodeify(fs.readFile); |
11 | 12 | var writeFile = denodeify(fs.writeFile); |
12 | 13 |
|
| 14 | +var blue = chalk.blue; |
| 15 | +var red = chalk.red; |
| 16 | + |
| 17 | +var validateConfig = require('./lib/utilities/validate-config'); |
| 18 | +var extractConfig = require('./lib/utilities/extract-index-config'); |
| 19 | + |
13 | 20 | module.exports = { |
14 | 21 | name: 'ember-cli-deploy-json-config', |
15 | 22 |
|
16 | 23 | createDeployPlugin: function(options) { |
17 | | - return { |
18 | | - name: options.name, |
19 | | - |
20 | | - didBuild: function(context) { |
21 | | - var deployment = context.deployment; |
22 | | - var project = deployment.project; |
23 | | - var root = project.root; |
24 | | - var indexPath = path.join(root, (context.indexPath || 'dist/index.html')); |
25 | | - var outputPath = path.join(path.dirname(indexPath), 'index.json'); |
| 24 | + function _beginMessage(ui, inputPattern, outputPattern) { |
| 25 | + ui.write(blue('| ')); |
| 26 | + ui.writeLine(blue('- generating `' + outputPattern + '` from `' + inputPattern + '`')); |
26 | 27 |
|
27 | | - return readFile(indexPath) |
28 | | - .then(this._extractConfig.bind(this), this._handleMissingFile) |
29 | | - .then(writeFile.bind(this, outputPath)) |
30 | | - .then(function() { |
31 | | - return { indexPath: outputPath }; |
32 | | - }); |
33 | | - }.bind(this) |
| 28 | + return Promise.resolve(); |
34 | 29 | } |
35 | | - }, |
36 | 30 |
|
37 | | - _extractConfig: function(data) { |
38 | | - var $ = cheerio.load(data.toString()); |
39 | | - var json = { |
40 | | - base: this._get($, 'base', ['href']), |
41 | | - meta: this._get($, 'meta[name*="/config/environment"]', ['name', 'content']), |
42 | | - link: this._get($, 'link', ['rel', 'href']), |
43 | | - script: this._get($, 'script', ['src']) |
44 | | - }; |
| 31 | + function _successMessage(ui, outputPattern) { |
| 32 | + ui.write(blue('| ')); |
| 33 | + ui.writeLine(blue('- generated: `' + outputPattern + '`')); |
| 34 | + |
| 35 | + return Promise.resolve(outputPattern); |
| 36 | + } |
45 | 37 |
|
46 | | - return RSVP.resolve(JSON.stringify(json)); |
47 | | - }, |
| 38 | + function _errorMessage(ui, error) { |
| 39 | + ui.write(blue('| ')); |
| 40 | + ui.write(red('- ' + error + '`\n')); |
48 | 41 |
|
49 | | - _handleMissingFile: function() { |
50 | | - return RSVP.resolve(); |
51 | | - }, |
| 42 | + return Promise.reject(error); |
| 43 | + } |
52 | 44 |
|
53 | | - _get: function($, selector, attributes) { |
54 | | - attributes = attributes || []; |
55 | | - var config = []; |
56 | | - var $tags = $(selector); |
| 45 | + return { |
| 46 | + name: options.name, |
57 | 47 |
|
58 | | - $tags.each(function() { |
59 | | - var $tag = $(this); |
| 48 | + willDeploy: function(context) { |
| 49 | + var deployment = context.deployment; |
| 50 | + var ui = deployment.ui; |
| 51 | + var config = deployment.config[this.name] = deployment.config[this.name] || {}; |
60 | 52 |
|
61 | | - var data = attributes.reduce(function(data, attribute) { |
62 | | - data[attribute] = $tag.attr(attribute); |
| 53 | + return validateConfig(ui, config) |
| 54 | + .then(function() { |
| 55 | + ui.write(blue('| ')); |
| 56 | + ui.writeLine(blue('- config ok')); |
| 57 | + }); |
| 58 | + }, |
63 | 59 |
|
64 | | - return data; |
65 | | - }, {}); |
| 60 | + didBuild: function(context) { |
| 61 | + var deployment = context.deployment; |
| 62 | + var ui = deployment.ui; |
| 63 | + var config = deployment.config[this.name]; |
| 64 | + var project = deployment.project; |
| 65 | + var root = project.root; |
66 | 66 |
|
67 | | - config.push(data); |
68 | | - }); |
| 67 | + var fileInputPattern = config.fileInputPattern; |
| 68 | + var fileOutputPattern = config.fileOutputPattern; |
| 69 | + var inputPath = path.join(root, fileInputPattern); |
| 70 | + var outputPath = path.join(root, fileOutputPattern); |
69 | 71 |
|
70 | | - return config; |
| 72 | + return _beginMessage(ui, fileInputPattern, fileOutputPattern) |
| 73 | + .then(readFile.bind(readFile, inputPath)) |
| 74 | + .then(extractConfig.bind(this)) |
| 75 | + .then(writeFile.bind(writeFile, outputPath)) |
| 76 | + .then(_successMessage.bind(this, ui, fileOutputPattern)) |
| 77 | + .then(function(outputPattern) { |
| 78 | + return { distFiles: [outputPattern] }; |
| 79 | + }) |
| 80 | + .catch(_errorMessage.bind(this, ui)); |
| 81 | + } |
| 82 | + } |
71 | 83 | } |
72 | 84 | }; |
0 commit comments