forked from funkensturm/ember-local-storage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (109 loc) · 3.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* eslint-env node */
'use strict';
const stew = require('broccoli-stew');
const VersionChecker = require('ember-cli-version-checker');
const path = require('path');
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
module.exports = {
name: 'ember-localforage',
_warn(message) {
let chalk = require('chalk');
let warning = chalk.yellow('WARNING: ' + message);
if (this.ui && this.ui.writeWarnLine) {
this.ui.writeWarnLine(message);
} else if (this.ui) {
this.ui.writeLine(warning);
} else {
console.log(warning);
}
},
init(app) {
this._super.init && this._super.init.apply(this, arguments);
// determine if ember-data is present
let checker = new VersionChecker(this);
let bowerDep = checker.for('ember-data', 'bower');
let npmDep = checker.for('ember-data', 'npm');
if (
(
bowerDep.version && (
bowerDep.satisfies('>= 1.13.0') ||
bowerDep.satisfies('>= 2.0.0') ||
bowerDep.gt('2.0.0')
)
) ||
(
npmDep.version && (
npmDep.satisfies('>= 1.13.0') ||
npmDep.satisfies('>= 2.0.0') ||
npmDep.gt('2.0.0')
)
)
) {
this.hasEmberData = true;
}
// determine if saveAs and Blob should be imported
let projectConfig = this.project.config(app.env);
let options = {};
if (projectConfig) {
options = projectConfig['ember-localforage'] || {};
if (options.fileExport && this.hasEmberData) {
this.needsFileExport = true;
}
}
// Inform the user about the transition to npm dependencies
if (this.needsFileExport && !options.ignoreBlobWarning) {
let bowerDeps = this.project.bowerDependencies();
if (bowerDeps['blob-polyfill']) {
this._warn('Please remove `blob-polyfill` from `bower.json`. As of ' +
'Ember localStorage 1.4.0 the `blob-polyfill` NPM ' +
'package is a dependency. If other code depends on the ' +
'bower package add `ignoreBlobWarning: true` to ' +
'`ember-localforage` config in `environment.js` to ' +
'ignore this warning.'
);
}
}
},
included: function included(app) {
if (this.needsFileExport) {
app.import('vendor/save-as.js');
app.import('vendor/Blob.js');
}
app.import('node_modules/localforage/dist/localforage.min.js');
app.import('node_modules/localforage-sessionstoragewrapper/src/localforage-sessionstoragewrapper.js');
this._super.included.apply(this, arguments);
},
treeForApp: function(tree) {
if (!this.hasEmberData) {
[
'initializers/local-storage-adapter.js'
].forEach(function(file) {
tree = stew.rm(tree, file);
});
}
return tree;
},
treeForAddon: function(tree) {
if (!this.hasEmberData) {
[
'adapters/adapter.js',
'adapters/base.js',
'adapters/local.js',
'adapters/session.js',
'initializers/local-storage-adapter.js',
'mixins/adapters/import-export.js',
'serializers/serializer.js'
].forEach(function(file) {
tree = stew.rm(tree, file);
});
}
return this._super.treeForAddon.call(this, tree);
},
treeForVendor(vendorTree) {
let blobTree = new Funnel(path.dirname(require.resolve('blob-polyfill')), {
files: ['Blob.js']
});
return mergeTrees([vendorTree, blobTree]);
}
};