|
3 | 3 |
|
4 | 4 | var path = require('path');
|
5 | 5 | var fs = require('fs');
|
6 |
| -var chalk = require('chalk'); |
7 | 6 | var RSVP = require('rsvp');
|
8 | 7 | var Promise = RSVP.Promise;
|
9 | 8 | var denodeify = RSVP.denodeify;
|
10 | 9 |
|
11 | 10 | var readFile = denodeify(fs.readFile);
|
12 | 11 | var writeFile = denodeify(fs.writeFile);
|
13 | 12 |
|
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'); |
| 13 | +var extractConfigFromHtmlAsJson = require('./lib/utilities/extract-index-config'); |
| 14 | +var DeployPluginBase = require('ember-cli-deploy-plugin'); |
19 | 15 |
|
20 | 16 | module.exports = {
|
21 | 17 | name: 'ember-cli-deploy-json-config',
|
22 | 18 |
|
23 | 19 | createDeployPlugin: function(options) {
|
24 |
| - function _beginMessage(ui, inputPath, outputPath) { |
25 |
| - ui.write(blue('| ')); |
26 |
| - ui.writeLine(blue('- generating `' + outputPath + '` from `' + inputPath + '`')); |
27 |
| - |
28 |
| - return Promise.resolve(); |
29 |
| - } |
30 |
| - |
31 |
| - function _successMessage(ui, outputPath) { |
32 |
| - ui.write(blue('| ')); |
33 |
| - ui.writeLine(blue('- generated: `' + outputPath + '`')); |
34 |
| - |
35 |
| - return Promise.resolve(); |
36 |
| - } |
37 |
| - |
38 |
| - function _errorMessage(ui, error) { |
39 |
| - ui.write(blue('| ')); |
40 |
| - ui.write(red('- ' + error + '`\n')); |
41 |
| - |
42 |
| - return Promise.reject(error); |
43 |
| - } |
44 |
| - |
45 |
| - return { |
| 20 | + var DeployPlugin = DeployPluginBase.extend({ |
46 | 21 | name: options.name,
|
47 | 22 |
|
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] || {}; |
52 |
| - |
53 |
| - return validateConfig(ui, config) |
54 |
| - .then(function() { |
55 |
| - ui.write(blue('| ')); |
56 |
| - ui.writeLine(blue('- config ok')); |
57 |
| - }); |
| 23 | + defaultConfig: { |
| 24 | + fileInputPattern: 'index.html', |
| 25 | + fileOutputPattern: 'index.json', |
| 26 | + projectRoot: function(context) { |
| 27 | + return context.project.root; |
| 28 | + }, |
| 29 | + distDir: function(context) { |
| 30 | + return context.distDir; |
| 31 | + } |
58 | 32 | },
|
59 | 33 |
|
60 | 34 | 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 |
| - |
66 |
| - var root = project.root; |
67 |
| - var distDir = context.distDir; |
68 |
| - var fileInputPattern = config.fileInputPattern; |
69 |
| - var fileOutputPattern = config.fileOutputPattern; |
| 35 | + var root = this.readConfig('projectRoot'); |
| 36 | + var distDir = this.readConfig('distDir'); |
| 37 | + var fileInputPattern = this.readConfig('fileInputPattern'); |
| 38 | + var fileOutputPattern = this.readConfig('fileOutputPattern'); |
70 | 39 | var inputPath = path.join(distDir, fileInputPattern);
|
71 | 40 | var outputPath = path.join(distDir, fileOutputPattern);
|
72 |
| - var absoluteInputPath = path.join(root, inputPath); |
73 |
| - var absoluteOutputPath = path.join(root, outputPath); |
| 41 | + var absoluteInputPath = path.join(root, inputPath); |
| 42 | + var absoluteOutputPath = path.join(root, outputPath); |
| 43 | + |
| 44 | + this.log('generating `' + outputPath + '` from `' + inputPath + '`'); |
74 | 45 |
|
75 |
| - return _beginMessage(ui, inputPath, outputPath) |
76 |
| - .then(readFile.bind(readFile, absoluteInputPath)) |
77 |
| - .then(extractConfig.bind(this)) |
| 46 | + return readFile(absoluteInputPath) |
| 47 | + .then(extractConfigFromHtmlAsJson.bind(this)) |
78 | 48 | .then(writeFile.bind(writeFile, absoluteOutputPath))
|
79 |
| - .then(_successMessage.bind(this, ui, outputPath)) |
| 49 | + .then(this._successMessage.bind(this, outputPath, fileOutputPattern)) |
80 | 50 | .then(function() {
|
81 |
| - ui.write(blue('| ')); |
82 |
| - ui.writeLine(blue('- added `' + fileOutputPattern + '` to `context.distFiles`')); |
83 |
| - |
84 | 51 | return { distFiles: [fileOutputPattern] };
|
85 | 52 | })
|
86 |
| - .catch(_errorMessage.bind(this, ui)); |
| 53 | + .catch(this._errorMessage.bind(this)); |
| 54 | + }, |
| 55 | + |
| 56 | + _successMessage: function(outputPath, fileOutputPattern) { |
| 57 | + this.log('generated: `' + outputPath + '`'); |
| 58 | + this.log('added `' + fileOutputPattern + '` to `context.distFiles`'); |
| 59 | + return Promise.resolve(); |
| 60 | + }, |
| 61 | + |
| 62 | + _errorMessage: function(error) { |
| 63 | + this.log(error, { color: 'red' }); |
| 64 | + if (error) { |
| 65 | + this.log(error.stack, { color: 'red' }); |
| 66 | + } |
| 67 | + return Promise.reject(error); |
87 | 68 | }
|
88 |
| - } |
| 69 | + }); |
| 70 | + |
| 71 | + return new DeployPlugin(); |
89 | 72 | }
|
90 | 73 | };
|
0 commit comments