Skip to content

Commit cfbeb3d

Browse files
Charles Lydingfilipesilva
authored andcommitted
refactor(@angular/cli): simplify ember-cli promise usage
1 parent 3c685a1 commit cfbeb3d

File tree

13 files changed

+31
-126
lines changed

13 files changed

+31
-126
lines changed

packages/@angular/cli/ember-cli/lib/ext/promise.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

packages/@angular/cli/ember-cli/lib/models/blueprint.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
@module ember-cli
55
*/
66
var FileInfo = require('./file-info');
7-
var Promise = require('../ext/promise');
7+
var RSVP = require('rsvp');
8+
var Promise = RSVP.Promise;
89
var chalk = require('chalk');
910
var printableProperties = require('../utilities/printable-properties').blueprint;
1011
var sequence = require('../utilities/sequence');
@@ -13,7 +14,7 @@ var fs = require('fs-extra');
1314
var inflector = require('inflection');
1415
var minimatch = require('minimatch');
1516
var path = require('path');
16-
var stat = Promise.denodeify(fs.stat);
17+
var stat = RSVP.denodeify(fs.stat);
1718
var stringUtils = require('ember-cli-string-utils');
1819
var compact = require('lodash/compact');
1920
var intersect = require('lodash/intersection');
@@ -26,8 +27,8 @@ var keys = require('lodash/keys');
2627
var merge = require('lodash/merge');
2728
var values = require('lodash/values');
2829
var walkSync = require('walk-sync');
29-
var writeFile = Promise.denodeify(fs.outputFile);
30-
var removeFile = Promise.denodeify(fs.remove);
30+
var writeFile = RSVP.denodeify(fs.outputFile);
31+
var removeFile = RSVP.denodeify(fs.remove);
3132
var SilentError = require('silent-error');
3233
var CoreObject = require('core-object');
3334
var EOL = require('os').EOL;
@@ -493,7 +494,8 @@ Blueprint.prototype._process = function(options, beforeHook, process, afterHook)
493494
return this._locals(options).then(function (locals) {
494495
return Promise.resolve()
495496
.then(beforeHook.bind(self, options, locals))
496-
.then(process.bind(self, intoDir, locals)).map(self._commit.bind(self))
497+
.then(process.bind(self, intoDir, locals))
498+
.then(promises => RSVP.map(promises, self._commit.bind(self)))
497499
.then(afterHook.bind(self, options));
498500
});
499501
};
@@ -774,9 +776,9 @@ Blueprint.prototype.processFiles = function(intoDir, templateVariables) {
774776

775777
this._ignoreUpdateFiles();
776778

777-
return Promise.filter(fileInfos, isValidFile).
778-
map(prepareConfirm).
779-
then(finishProcessingForInstall);
779+
return RSVP.filter(fileInfos, isValidFile)
780+
.then(promises => RSVP.map(promises, prepareConfirm))
781+
.then(finishProcessingForInstall);
780782
};
781783

782784
/**
@@ -789,7 +791,7 @@ Blueprint.prototype.processFilesForUninstall = function(intoDir, templateVariabl
789791

790792
this._ignoreUpdateFiles();
791793

792-
return Promise.filter(fileInfos, isValidFile).
794+
return RSVP.filter(fileInfos, isValidFile).
793795
then(finishProcessingForUninstall);
794796
};
795797

@@ -1374,7 +1376,7 @@ function gatherConfirmationMessages(collection, info) {
13741376
@return {Boolean}
13751377
*/
13761378
function isFile(info) {
1377-
return stat(info.inputPath).invoke('isFile');
1379+
return stat(info.inputPath).then(it => it.isFile());
13781380
}
13791381

13801382
/**

packages/@angular/cli/ember-cli/lib/models/command.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const path = require('path');
66
const camelize = require('ember-cli-string-utils').camelize;
77
const getCallerFile = require('get-caller-file');
88
const printCommand = require('../utilities/print-command');
9-
const Promise = require('rsvp').Promise;
109
const _ = require('lodash');
1110
const EOL = require('os').EOL;
1211
const CoreObject = require('core-object');

packages/@angular/cli/ember-cli/lib/models/edit-file-diff.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
'use strict';
22

3-
const fs = require('fs');
3+
const fs = require('fs-extra');
44
const RSVP = require('rsvp');
55
const jsdiff = require('diff');
66
const temp = require('temp').track();
77
const path = require('path');
88
const SilentError = require('silent-error');
99
const openEditor = require('../utilities/open-editor');
1010

11-
const readFile = RSVP.denodeify(fs.readFile);
12-
const writeFile = RSVP.denodeify(fs.writeFile);
13-
1411
class EditFileDiff {
1512
constructor(options) {
1613
this.info = options.info;
@@ -19,7 +16,7 @@ class EditFileDiff {
1916
edit() {
2017
return RSVP.hash({
2118
input: this.info.render(),
22-
output: readFile(this.info.outputPath),
19+
output: fs.readFile(this.info.outputPath),
2320
})
2421
.then(this.invokeEditor.bind(this))
2522
.then(this.applyPatch.bind(this))
@@ -32,8 +29,8 @@ class EditFileDiff {
3229

3330
applyPatch(resultHash) {
3431
return RSVP.hash({
35-
diffString: readFile(resultHash.diffPath),
36-
currentString: readFile(resultHash.outputPath),
32+
diffString: fs.readFile(resultHash.diffPath),
33+
currentString: fs.readFile(resultHash.outputPath),
3734
}).then(result => {
3835
let appliedDiff = jsdiff.applyPatch(result.currentString.toString(), result.diffString.toString());
3936

@@ -43,7 +40,7 @@ class EditFileDiff {
4340
throw new SilentError(message);
4441
}
4542

46-
return writeFile(resultHash.outputPath, appliedDiff);
43+
return fs.writeFile(resultHash.outputPath, appliedDiff);
4744
});
4845
}
4946

@@ -52,7 +49,7 @@ class EditFileDiff {
5249
let diff = jsdiff.createPatch(info.outputPath, result.output.toString(), result.input);
5350
let diffPath = path.join(temp.mkdirSync(), 'currentDiff.diff');
5451

55-
return writeFile(diffPath, diff)
52+
return fs.writeFile(diffPath, diff)
5653
.then(() => openEditor(diffPath))
5754
.then(() => ({ outputPath: info.outputPath, diffPath }));
5855
}

packages/@angular/cli/ember-cli/lib/models/file-info.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const fs = require('fs');
3+
const fs = require('fs-extra');
44
const RSVP = require('rsvp');
55
const chalk = require('chalk');
66
const EditFileDiff = require('./edit-file-diff');
@@ -9,10 +9,6 @@ const rxEOL = new RegExp(EOL, 'g');
99
const isBinaryFile = require('isbinaryfile').sync;
1010
const canEdit = require('../utilities/open-editor').canEdit;
1111

12-
const Promise = RSVP.Promise;
13-
const readFile = RSVP.denodeify(fs.readFile);
14-
const lstat = RSVP.denodeify(fs.stat);
15-
1612
function processTemplate(content, context) {
1713
let options = {
1814
evaluate: /<%([\s\S]+?)%>/g,
@@ -84,7 +80,7 @@ class FileInfo {
8480
jsdiff = require('diff');
8581
return RSVP.hash({
8682
input: this.render(),
87-
output: readFile(info.outputPath),
83+
output: fs.readFile(info.outputPath),
8884
}).then(result => {
8985
let diff = jsdiff.createPatch(
9086
info.outputPath, result.output.toString().replace(rxEOL, '\n'), result.input.replace(rxEOL, '\n')
@@ -103,8 +99,8 @@ class FileInfo {
10399
let path = this.inputPath,
104100
context = this.templateVariables;
105101
if (!this.rendered) {
106-
this.rendered = readFile(path)
107-
.then(content => lstat(path)
102+
this.rendered = fs.readFile(path)
103+
.then(content => fs.stat(path)
108104
.then(fileStat => {
109105
if (isBinaryFile(content, fileStat.size)) {
110106
return content;
@@ -133,7 +129,7 @@ class FileInfo {
133129
if (doesExist) {
134130
result = RSVP.hash({
135131
input: this.render(),
136-
output: readFile(this.outputPath),
132+
output: fs.readFile(this.outputPath),
137133
}).then(result => {
138134
let type;
139135
if (result.input.toString().replace(rxEOL, '\n') === result.output.toString().replace(rxEOL, '\n')) {

packages/@angular/cli/ember-cli/lib/tasks/install-blueprint.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
var Blueprint = require('../models/blueprint');
44
var Task = require('../models/task');
5-
var Promise = require('../ext/promise');
6-
var temp = require('temp');
7-
var childProcess = require('child_process');
85
var path = require('path');
96
var merge = require('lodash/merge');
107

11-
var mkdir = Promise.denodeify(temp.mkdir);
12-
var exec = Promise.denodeify(childProcess.exec);
13-
148
module.exports = Task.extend({
159
run: function(options) {
1610
var cwd = process.cwd();

packages/@angular/cli/ember-cli/lib/tasks/npm-task.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
var chalk = require('chalk');
66
var Task = require('../models/task');
7-
var Promise = require('../ext/promise');
7+
var RSVP = require('rsvp');
88

9-
var spawn = Promise.denodeify(require('child_process').spawn);
9+
var spawn = RSVP.denodeify(require('child_process').spawn);
1010

1111

1212
module.exports = Task.extend({

packages/@angular/cli/ember-cli/lib/ui/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var Promise = require('../ext/promise');
43
var EOL = require('os').EOL;
54
var chalk = require('chalk');
65
var writeError = require('./write-error');

packages/@angular/cli/ember-cli/lib/utilities/insert-into-file.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
const fs = require('fs-extra');
44
const EOL = require('os').EOL;
5-
const RSVP = require('rsvp');
6-
7-
const Promise = RSVP.Promise;
8-
const writeFile = RSVP.denodeify(fs.outputFile);
95

106
/**
117
Inserts the given content into a file. If the `contentsToInsert` string is already
@@ -88,7 +84,7 @@ function insertIntoFile(fullPath, contentsToInsert, providedOptions) {
8884
if (contentsToWrite !== originalContents) {
8985
returnValue.inserted = true;
9086

91-
return writeFile(fullPath, contentsToWrite)
87+
return fs.writeFile(fullPath, contentsToWrite)
9288
.then(() => returnValue);
9389

9490
} else {

packages/@angular/cli/ember-cli/lib/utilities/mk-tmp-dir-in.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22

33
const fs = require('fs-extra');
44
const temp = require('temp');
5-
const RSVP = require('rsvp');
5+
const denodeify = require('denodeify');
66

7-
const Promise = RSVP.Promise;
8-
const mkdir = RSVP.denodeify(fs.mkdir);
9-
const mkdirTemp = RSVP.denodeify(temp.mkdir);
10-
11-
function exists(dir) {
12-
return new Promise(resolve => {
13-
fs.exists(dir, resolve);
14-
});
15-
}
7+
const mkdirTemp = denodeify(temp.mkdir);
168

179
function mkTmpDirIn(dir) {
18-
return exists(dir).then(doesExist => {
10+
return fs.pathExists(dir).then(doesExist => {
1911
if (!doesExist) {
20-
return mkdir(dir);
12+
return fs.mkdir(dir);
2113
}
2214
}).then(() => mkdirTemp({ dir }));
2315
}

0 commit comments

Comments
 (0)