Skip to content

Commit 3eed013

Browse files
committed
add ejs and api.render
1 parent f5d18b7 commit 3eed013

File tree

8 files changed

+103
-125
lines changed

8 files changed

+103
-125
lines changed

generator/createService.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,23 @@ module.exports = (api, options) => {
55

66
if (options.actionType !== 'service') throw 'service not defined!';
77

8-
const feathersClientPath = options.service.feathersClientPath || '@/store/feathers-client.js';
9-
const globalClientPath = api.resolve(feathersClientPath);
8+
const clientPath = options.service.feathersClientPath || '@/store/feathers-client.js';
9+
const globalClientPath = api.resolve(clientPath);
1010
if (fs.existsSync(globalClientPath)) {
1111
throw `feathers-client file at ${globalClientPath} !`;
1212
}
1313

1414
createServicesFolder();
1515

16-
const file = require.resolve('./templates/service.js');
17-
18-
let service = fs.readFileSync(file, { encoding: 'utf-8' });
19-
2016
const servicePath = options.service.path;
21-
const serviceIdField = options.service.idField;
22-
23-
if (!servicePath) throw 'service-path not defined!';
24-
25-
const serviceFilePath = api.resolve(`./src/store/services/${servicePath}.js`);
26-
27-
if (fs.existsSync(serviceFilePath)) throw 'service already exists!';
28-
29-
service = service.replace('%CLIENTPATH%', feathersClientPath);
30-
service = service.replace('%SERVICEPATH%', servicePath);
31-
service = service.replace('%IDFIELD%', serviceIdField);
32-
33-
let instanceDefaultsFile = require.resolve('./templates/instanceDefaultsFn.js');
34-
35-
if (options.service && options.service.instanceDefaults === 'obj') {
36-
instanceDefaultsFile = require.resolve('./templates/instanceDefaultsObj.js');
37-
}
38-
39-
const instanceDefaults = fs.readFileSync(instanceDefaultsFile, { encoding: 'utf-8' });
40-
41-
service = service.replace('%INSTANCEDEFAULTS%', instanceDefaults);
42-
43-
fs.writeFileSync(serviceFilePath, service);
17+
const { idField, instanceDefaults } = options.service;
18+
19+
api.render({
20+
[`./src/store/services/${servicePath}.js`]: './templates/store/services/service.js',
21+
}, {
22+
clientPath,
23+
idField,
24+
servicePath,
25+
instanceDefaults,
26+
});
4427
};

generator/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module.exports = (api, options) => {
44
throw "Vuex not installed! Please run 'vue add vuex' first!";
55
}
66

7-
const isInit = options.actionType == "init";
7+
const isInit = options.actionType == 'init';
88

9-
if (isInit) require("./init.js")(api, options);
9+
if (isInit) require('./init.js')(api, options);
1010

11-
const isService = options.actionType == "service";
11+
const isService = options.actionType == 'service';
1212

13-
if (isService) require("./createService.js")(api, options);
14-
};
13+
if (isService) require('./createService.js')(api, options);
14+
};

generator/init.js

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const fs = require('fs');
33
module.exports = (api, options) => {
44
const { createServicesFolder } = require('./utils')(api);
55

6-
console.log('Hallo');
6+
const storePlainFile = api.resolve('./src/store.js');
7+
const storeFolder = api.resolve('./src/store/');
78

89
api.extendPackage({
910
dependencies: {
@@ -16,8 +17,6 @@ module.exports = (api, options) => {
1617
});
1718

1819
const moveStorePlainToFolderIndex = () => {
19-
const storePlainFile = api.resolve('./src/store.js');
20-
const storeFolder = api.resolve('./src/store/');
2120
const storeIndexFile = api.resolve('./src/store/index.js');
2221

2322
try {
@@ -33,30 +32,14 @@ module.exports = (api, options) => {
3332
}
3433
};
3534

36-
const feathersClientFile = () => {
37-
const url = options.init.serverUrl || 'http://localhost:3030';
38-
39-
const array = ["import feathers from '@feathersjs/feathers';",
40-
"import socketio from '@feathersjs/socketio-client';",
41-
"import auth from '@feathersjs/authentication-client';",
42-
"import io from 'socket.io-client';",
43-
'',
44-
`const socket = io('${url}', {transports: ['websocket']});`,
45-
'',
46-
'const feathersClient = feathers()',
47-
' .configure(socketio(socket))',
48-
' .configure(auth({ storage: window.localStorage }));',
49-
'',
50-
'export default feathersClient;'];
51-
52-
return array.join('\n');
53-
};
54-
55-
function copyFeathersClientFile() {
56-
const file = api.resolve('./src/store/feathers-client.js');
35+
function createFeathersClientFile() {
36+
const clientFilePath = './src/store/feathers-client.js';
37+
const file = api.resolve(clientFilePath);
5738
if (!fs.existsSync(file)) {
58-
fs.writeFileSync(file, feathersClientFile(), (err) => {
59-
if (err) console.log(err);
39+
api.render({
40+
clientFilePath: './templates/store/feathers-client.js',
41+
}, {
42+
serverUrl: options.init.serverUrl,
6043
});
6144
}
6245
}
@@ -127,10 +110,8 @@ module.exports = (api, options) => {
127110
fs.writeFileSync(storeFile, storeLines.join('\n'), { encoding: 'utf-8' });
128111
}
129112

130-
api.onCreateComplete(() => {
131-
moveStorePlainToFolderIndex();
132-
copyFeathersClientFile();
133-
modifyStoreFile();
134-
createServicesFolder();
135-
});
113+
moveStorePlainToFolderIndex();
114+
createFeathersClientFile();
115+
modifyStoreFile();
116+
createServicesFolder();
136117
};

generator/templates/instanceDefaultsFn.js

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

generator/templates/instanceDefaultsObj.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import feathers from '@feathersjs/feathers';
2+
import socketio from '@feathersjs/socketio-client';
3+
import auth from '@feathersjs/authentication-client';
4+
import io from 'socket.io-client';
5+
6+
const socket = io('<%= serverUrl %>', { transports: ['websocket'] });
7+
8+
const feathersClient = feathers()
9+
.configure(socketio(socket))
10+
.configure(auth({ storage: window.localStorage }));
11+
12+
export default feathersClient;

generator/templates/service.js renamed to generator/templates/store/services/service.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
/* eslint-disable no-unused-vars */
22

33
import feathersVuex from 'feathers-vuex';
4-
import feathersClient from '%CLIENTPATH%';
4+
import feathersClient from '<%= clientPath %>';
55

6-
const { service } = feathersVuex(feathersClient, { idField: '%IDFIELD%' });
6+
const { service } = feathersVuex(feathersClient, { idField: '<%= idField %>' });
77

8-
const servicePath = '%SERVICEPATH%';
8+
const servicePath = '<%= servicePath %>';
99
const servicePlugin = service(servicePath, {
10-
%INSTANCEDEFAULTS%
10+
<% if ( instanceDefaults == "obj" ) { -%>
11+
instanceDefaults: {
12+
13+
},
14+
<%_ } else { -%>
15+
instanceDefaults(data, { store, Model, Models }) {
16+
return {
17+
};
18+
},
19+
<%_ } -%>
1120
});
1221

1322
feathersClient.service(servicePath)

prompts.js

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,86 @@
11
module.exports = [
22
{
3-
name: `actionType`,
3+
name: 'actionType',
44
type: 'list',
55
message: 'What do you want to do?',
66
choices: [
7-
{
8-
name: "init feathers-vuex",
9-
value: "init"
7+
{
8+
name: 'init feathers-vuex',
9+
value: 'init',
1010
}, {
11-
name: "add service",
12-
value: "service"
13-
}
11+
name: 'add service',
12+
value: 'service',
13+
},
1414
],
1515
default: 0,
1616
},
17-
//#region init
17+
// #region init
1818
{
19-
when: answers => answers.actionType == "init",
20-
name: `init.serverUrl`,
19+
when: answers => answers.actionType === 'init',
20+
name: 'init.serverUrl',
2121
type: 'input',
2222
message: 'What is the url of your feathers server instance?',
23-
default: "http://localhost:3030",
23+
default: 'http://localhost:3030',
2424
},
2525
{
26-
when: answers => answers.actionType == "init",
27-
name: `init.isAuth`,
26+
when: answers => answers.actionType === 'init',
27+
name: 'init.isAuth',
2828
type: 'confirm',
2929
message: 'Setup authentication (highly recommened)?',
3030
default: true,
3131
},
3232
{
33-
when: answers => answers.isAuth == true,
34-
name: `init.authIdField`,
33+
when: answers => answers.actionType === 'init' && answers.init.isAuth === true,
34+
name: 'init.authIdField',
3535
type: 'input',
3636
message: 'What is the id-key of your auth service?',
37-
default: "id",
37+
default: 'id',
3838
},
3939
{
40-
when: answers => answers.isAuth == true,
41-
name: `init.authUserService`,
40+
when: answers => answers.actionType === 'init' && answers.init.isAuth === true,
41+
name: 'init.authUserService',
4242
type: 'input',
4343
message: 'What is the name of your users-service for authentication?',
44-
default: "users",
44+
default: 'users',
4545
},
46-
//#endregion
47-
//#region generate service
46+
// #endregion
47+
// #region generate service
4848
{
49-
when: answers => answers.actionType == "service",
50-
name: "service.path",
51-
type: "input",
52-
message: "What is the name of the service?",
53-
default: ""
49+
when: answers => answers.actionType === 'service',
50+
name: 'service.path',
51+
type: 'input',
52+
message: 'What is the name of the service?',
53+
default: '',
5454
},
5555
{
56-
when: answers => answers.actionType == "service",
57-
name: "service.idField",
58-
type: "input",
59-
message: "What is the id-key of the service?",
60-
default: "id"
56+
when: answers => answers.actionType === 'service',
57+
name: 'service.idField',
58+
type: 'input',
59+
message: 'What is the id-key of the service?',
60+
default: 'id',
6161
},
6262
{
63-
when: answers => answers.actionType == "service",
64-
name: "service.feathersClientPath",
65-
type: "input",
66-
message: "Where is your feathers-client file located?",
67-
default: "@/store/feathers-client.js"
63+
when: answers => answers.actionType === 'service',
64+
name: 'service.feathersClientPath',
65+
type: 'input',
66+
message: 'Where is your feathers-client file located?',
67+
default: '@/store/feathers-client.js',
6868
},
6969
{
70-
when: answers => answers.actionType == "service",
71-
name: "service.instanceDefaults",
70+
when: answers => answers.actionType === 'service',
71+
name: 'service.instanceDefaults',
7272
type: 'list',
73-
message: "Use instanceDefaults as function or object? (function recommended)",
73+
message: 'Use instanceDefaults as function or object? (function recommended)',
7474
choices: [
75-
{
76-
name: "function",
77-
value: "fn"
75+
{
76+
name: 'function',
77+
value: 'fn',
7878
}, {
79-
name: "object",
80-
value: "obj"
81-
}
79+
name: 'object',
80+
value: 'obj',
81+
},
8282
],
8383
default: 0,
84-
}
85-
//#endregion
86-
];
84+
},
85+
// #endregion
86+
];

0 commit comments

Comments
 (0)