Skip to content

Commit ed89d33

Browse files
committed
fix blueprints
1 parent de6e3b8 commit ed89d33

2 files changed

Lines changed: 103 additions & 3 deletions

File tree

blueprints/ember-cli-admin-auth/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var processTextContent = require('node_modules/ember-cli-admin/lib/proccess-text-content').processTextContent;
1+
var processTextContent = require('ember-cli-admin-auth/lib/proccess-text-content').processTextContent;
22

33
module.exports = {
44
description: 'Generates ember-simple-auth components and settings',
@@ -24,13 +24,21 @@ module.exports = {
2424
process('app/router.js',
2525
{
2626
replace: {
27-
"Router.map(function() {": "Router.map(function() {\n\treturn this.route(\"login\"});\n"
27+
"Router.map(function() {": "Router.map(function() {\n\tthis.route(\"login\");\n"
2828
}
2929
});
3030
process('config/environment.js',
3131
{
3232
insert: {
33-
"\t};": "\n\tENV['simple-auth'] = {\n\t\tauthorizer: 'simple-auth-authorizer:devise',\n\t\trouteAfterAuthentication: '/'\n\t};\n"
33+
"if (environment === 'development') {": {content: "ENV['simple-auth'] = {\n\t\tauthorizer: 'simple-auth-authorizer:devise',\n\t\trouteAfterAuthentication: '/'\n\t};\n\n\t", before: true},
34+
"if (environment === 'development') {": {content: "\n\t\tENV['simple-auth-devise'] = {\n\t\t\tresourceName: 'user',\n\t\t\tserverTokenEndpoint: '/api/v1/users/sign_in'\n\t\t};\n\t\tENV['simple-auth'].crossOriginWhitelist = ['*'];\n", after: true}
35+
}
36+
});
37+
process('app/routes/main.js',
38+
{
39+
insert: {
40+
"'ember-cli-admin/mixins/routes/base';": {content: "\nimport ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';\n\nBaseAdminRouteMixin.reopen(ApplicationRouteMixin);\n", after: true},
41+
"extend(BaseAdminRouteMixin": {content: ", {\n\tredirect: function(){\n\t\tif(!this.get('session.isAuthenticated')){\n\t\t\tthis.transitionTo('login');\n\t\t}\n\t}\n}" , after: true}
3442
}
3543
});
3644
}

lib/proccess-text-content.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var Promise = require('ember-cli/lib/ext/promise');
2+
var fs = require('ember-cli/node_modules/fs-extra');
3+
var path = require('path');
4+
var writeFile = Promise.denodeify(fs.outputFile);
5+
6+
var replacePhrases = function(content, phrases){
7+
var contentsToWrite = content;
8+
9+
return Object.keys(phrases).reduce(function(prev, next){
10+
var contentMarkerIndex = contentsToWrite.indexOf(next);
11+
if (contentMarkerIndex !== -1) {
12+
var replaceIndex = contentMarkerIndex;
13+
14+
contentsToWrite = contentsToWrite.slice(0, replaceIndex) +
15+
phrases[next] +
16+
contentsToWrite.slice(replaceIndex + next.length);
17+
return contentsToWrite;
18+
}
19+
}, "");
20+
};
21+
22+
var insertPhrases = function(contents, phrases){
23+
var contentsToWrite = contents;
24+
25+
return Object.keys(phrases).reduce(function(prev, next){
26+
var contentMarkerIndex = contentsToWrite.indexOf(next);
27+
if (contentMarkerIndex !== -1) {
28+
var insertIndex;
29+
if(phrases[next].after){
30+
insertIndex = contentMarkerIndex + next.length;
31+
}
32+
33+
if(phrases[next].before){
34+
insertIndex = contentMarkerIndex;
35+
}
36+
37+
contentsToWrite = contentsToWrite.slice(0, insertIndex) +
38+
phrases[next].content +
39+
contentsToWrite.slice(insertIndex);
40+
return contentsToWrite;
41+
}
42+
}, "");
43+
};
44+
module.exports = {
45+
processTextContent: function(pathRelativeToProjectRoot, commands) {
46+
var fullPath = path.join(this.project.root, pathRelativeToProjectRoot);
47+
var originalContents = '';
48+
49+
if (fs.existsSync(fullPath)) {
50+
originalContents = fs.readFileSync(fullPath, {
51+
encoding: 'utf8'
52+
});
53+
}
54+
55+
var contentsToWrite = originalContents;
56+
if (commands.replace) {
57+
contentsToWrite = replacePhrases(contentsToWrite, commands.replace);
58+
}
59+
if (commands.insert) {
60+
contentsToWrite = insertPhrases(contentsToWrite, commands.insert);
61+
}
62+
63+
var returnValue = {
64+
path: fullPath,
65+
originalContents: originalContents,
66+
contents: contentsToWrite,
67+
inserted: false
68+
};
69+
70+
if (contentsToWrite !== originalContents) {
71+
returnValue.inserted = true;
72+
73+
return writeFile(fullPath, contentsToWrite)
74+
.then(function() {
75+
return returnValue;
76+
77+
});
78+
} else {
79+
return Promise.resolve(returnValue);
80+
}
81+
82+
},
83+
renameFile: function(pathOld, pathNew){
84+
var fullPathOld = path.join(this.project.root, pathOld);
85+
var fullPathNew = path.join(this.project.root, pathNew);
86+
fs.rename(fullPathOld, fullPathNew, function(error){
87+
if(error) {
88+
return;
89+
}
90+
});
91+
}
92+
};

0 commit comments

Comments
 (0)