Skip to content

Commit 820c7d2

Browse files
committed
add createService
1 parent bf5bd19 commit 820c7d2

File tree

13 files changed

+1796
-155
lines changed

13 files changed

+1796
-155
lines changed

.eslintrc.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
"env": {
3+
"commonjs": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": ["eslint:recommended", "airbnb-base"],
8+
"globals": {
9+
"Atomics": "readonly",
10+
"SharedArrayBuffer": "readonly"
11+
},
12+
"parserOptions": {
13+
"ecmaVersion": 2018
14+
},
15+
"rules": {
16+
"indent": ["warn", 2],
17+
"no-unused-vars": "warn",
18+
"no-console": "warn",
19+
"linebreak-style": "off",
20+
"global-require": "warn"
21+
}
22+
};

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 fratzinger
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

generator/createService.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const fs = require('fs');
2+
3+
module.exports = (api, options) => {
4+
const { createServicesFolder } = require('./utils')(api);
5+
6+
if (options.actionType !== 'service') throw 'service not defined!';
7+
8+
const feathersClientPath = options.service.feathersClientPath || '@/store/feathers-client.js';
9+
const globalClientPath = api.resolve(feathersClientPath);
10+
if (fs.existsSync(globalClientPath)) {
11+
throw `feathers-client file at ${globalClientPath} !`;
12+
}
13+
14+
createServicesFolder();
15+
16+
const file = require.resolve('./templates/service.js');
17+
18+
let service = fs.readFileSync(file, { encoding: 'utf-8' });
19+
20+
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);
44+
};

generator/index.js

Lines changed: 6 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,14 @@
1-
const fs = require('fs');
2-
31
module.exports = (api, options) => {
4-
api.extendPackage({
5-
dependencies: {
6-
"@feathersjs/authentication-client": "^1.0.11",
7-
"@feathersjs/feathers": "^3.3.1",
8-
"@feathersjs/socketio-client": "^1.2.1",
9-
"feathers-vuex": "^1.7.0",
10-
"socket.io-client": "^2.2.0"
11-
}
12-
});
13-
14-
const hasVuex = api.hasPlugin("vuex");
2+
const hasVuex = api.hasPlugin('vuex');
153
if (!hasVuex) {
16-
api.extendPackage({
17-
dependencies: {
18-
"vuex": "^3.0.1"
19-
}
20-
})
21-
}
22-
23-
api.onCreateComplete(() => {
24-
moveStorePlainToFolderIndex();
25-
copyFeathersClientFile();
26-
modifyStoreFile();
27-
createServicesFolder();
28-
});
29-
30-
const moveStorePlainToFolderIndex = () => {
31-
const storePlainFile = api.resolve(`./src/store.js`);
32-
const storeFolder = api.resolve(`./src/store/`);
33-
const storeIndexFile = api.resolve(`./src/store/index.js`);
34-
35-
try {
36-
if (fs.existsSync(storePlainFile) && !fs.existsSync(storeIndexFile)) {
37-
if (!fs.existsSync(storeFolder)) fs.mkdirSync(storeFolder);
38-
39-
fs.rename(storePlainFile, storeIndexFile, (err) => {
40-
if (err) throw err;
41-
});
42-
43-
}
44-
} catch(err) {
45-
console.error(err)
46-
}
47-
}
48-
49-
function copyFeathersClientFile() {
50-
const file = api.resolve(`./src/store/feathers-client.js`);
51-
if (!fs.existsSync(file)) {
52-
fs.writeFileSync(file, feathersClientFile(), function(err) {
53-
if (err) console.log(err);
54-
})
55-
}
56-
}
57-
58-
function modifyStoreFile() {
59-
const storeFile = api.resolve(`./src/store/index.js`);
60-
let store = fs.readFileSync(storeFile, { encoding: 'utf-8' });
61-
let storeLines = store.split(/\r?\n/g);
62-
63-
storeLines = insertStoreImports(storeLines);
64-
storeLines = insertStorePlugins(storeLines);
65-
66-
// modify app
67-
store = storeLines.join('\n');
68-
fs.writeFileSync(storeFile, store, { encoding: 'utf-8' });
69-
}
70-
71-
function createServicesFolder() {
72-
try {
73-
const servicesFolder = api.resolve(`./src/store/services/`);
74-
if (!fs.existsSync(servicesFolder)) fs.mkdirSync(servicesFolder);
75-
} catch(ex) {
76-
console.log(ex);
77-
}
4+
throw "Vuex not installed! Please run 'vue add vuex' first!";
785
}
796

80-
function insertStoreImports(storeLines) {
81-
if (storeLines.findIndex(line => line.includes("feathers-vuex")) >= 0) { return storeLines; }
7+
const isInit = options.actionType == "init";
828

83-
const imports = () => {
84-
const authIdField = options.authIdField || "id";
85-
let clientLine = "";
86-
if (options.isAuth) {
87-
clientLine = `const { auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '${authIdField}' });`;
88-
} else {
89-
clientLine = `const { auth, FeathersVuex } = feathersVuex(feathersClient)`
90-
}
9+
if (isInit) require("./init.js")(api, options);
9110

92-
return ["",
93-
"import feathersVuex from 'feathers-vuex';",
94-
"import feathersClient from './feathers-client';",
95-
clientLine,
96-
"Vue.use(FeathersVuex);",
97-
"const requireModule = require.context('./services', false, /.js$/);",
98-
"const servicePlugins = requireModule.keys().map(modulePath => requireModule(modulePath).default);"
99-
];
100-
}
11+
const isService = options.actionType == "service";
10112

102-
// inject import
103-
const vueUseVuexLine = storeLines.findIndex(line => line.includes("Vue.use(Vuex)"));
104-
storeLines.splice(vueUseVuexLine + 1, 0, ...imports());
105-
106-
return storeLines;
107-
}
108-
109-
function insertStorePlugins(storeLines) {
110-
function indexPluginsArray() {
111-
return storeLines.findIndex(line => line.includes("plugins: ["));
112-
}
113-
114-
const indexServicePlugins = storeLines.findIndex(line => line.includes("...servicePlugins,"));
115-
const indexAuthPlugin = storeLines.findIndex(line => line.includes("auth({ userService: 'users' })"));
116-
117-
if (indexPluginsArray() < 0) {
118-
const indexExportVuex = storeLines.findIndex(line => line.includes("export default new Vuex.Store({"));
119-
if (indexExportVuex < 0) throw new Error("No export default new Vuex.Store - Line!");
120-
storeLines.splice(indexExportVuex + 1, 0, " plugins: [", " ],");
121-
}
122-
123-
if (indexServicePlugins < 0) {
124-
storeLines.splice(indexPluginsArray() + 1, 0, " ...servicePlugins,")
125-
}
126-
127-
if (indexAuthPlugin < 0 && options.isAuth) {
128-
const userService = options.authUserService || "users";
129-
storeLines.splice(indexPluginsArray() + 1, 0, ` auth({ userService: '${userService}' }),`)
130-
}
131-
132-
return storeLines;
133-
}
134-
135-
136-
137-
const feathersClientFile = () => {
138-
const url = options.serverUrl || "http://localhost:3030";
139-
140-
const array = ["import feathers from '@feathersjs/feathers';",
141-
"import socketio from '@feathersjs/socketio-client';",
142-
"import auth from '@feathersjs/authentication-client';",
143-
"import io from 'socket.io-client';",
144-
"",
145-
`const socket = io('${url}', {transports: ['websocket']});`,
146-
"",
147-
"const feathersClient = feathers()",
148-
" .configure(socketio(socket))",
149-
" .configure(auth({ storage: window.localStorage }));",
150-
"",
151-
"export default feathersClient;"]
152-
153-
return array.join('\n');
154-
}
13+
if (isService) require("./createService.js")(api, options);
15514
};

generator/init.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
const fs = require('fs');
2+
3+
module.exports = (api, options) => {
4+
const { createServicesFolder } = require('./utils')(api);
5+
6+
console.log('Hallo');
7+
8+
api.extendPackage({
9+
dependencies: {
10+
'@feathersjs/authentication-client': '^1.0.11',
11+
'@feathersjs/feathers': '^3.3.1',
12+
'@feathersjs/socketio-client': '^1.2.1',
13+
'feathers-vuex': '^1.7.0',
14+
'socket.io-client': '^2.2.0',
15+
},
16+
});
17+
18+
const moveStorePlainToFolderIndex = () => {
19+
const storePlainFile = api.resolve('./src/store.js');
20+
const storeFolder = api.resolve('./src/store/');
21+
const storeIndexFile = api.resolve('./src/store/index.js');
22+
23+
try {
24+
if (fs.existsSync(storePlainFile) && !fs.existsSync(storeIndexFile)) {
25+
if (!fs.existsSync(storeFolder)) fs.mkdirSync(storeFolder);
26+
27+
fs.rename(storePlainFile, storeIndexFile, (err) => {
28+
if (err) throw err;
29+
});
30+
}
31+
} catch (err) {
32+
console.error(err);
33+
}
34+
};
35+
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');
57+
if (!fs.existsSync(file)) {
58+
fs.writeFileSync(file, feathersClientFile(), (err) => {
59+
if (err) console.log(err);
60+
});
61+
}
62+
}
63+
64+
function insertStoreImports(storeLines) {
65+
if (storeLines.findIndex(line => line.includes('feathers-vuex')) >= 0) { return storeLines; }
66+
67+
const imports = () => {
68+
let clientLine = '';
69+
if (options.init.isAuth) {
70+
const authIdField = options.init.authIdField || 'id';
71+
clientLine = `const { auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '${authIdField}' });`;
72+
} else {
73+
clientLine = 'const { auth, FeathersVuex } = feathersVuex(feathersClient)';
74+
}
75+
76+
return ['',
77+
"import feathersVuex from 'feathers-vuex';",
78+
"import feathersClient from './feathers-client';",
79+
clientLine,
80+
'Vue.use(FeathersVuex);',
81+
"const requireModule = require.context('./services', false, /.js$/);",
82+
'const servicePlugins = requireModule.keys().map(modulePath => requireModule(modulePath).default);',
83+
];
84+
};
85+
86+
// inject import
87+
const vueUseVuexLine = storeLines.findIndex(line => line.includes('Vue.use(Vuex)'));
88+
storeLines.splice(vueUseVuexLine + 1, 0, ...imports());
89+
90+
return storeLines;
91+
}
92+
93+
function insertStorePlugins(storeLines) {
94+
function indexPluginsArray() {
95+
return storeLines.findIndex(line => line.includes('plugins: ['));
96+
}
97+
98+
const indexServicePlugins = storeLines.findIndex(line => line.includes('...servicePlugins,'));
99+
const indexAuthPlugin = storeLines.findIndex(line => line.includes("auth({ userService: 'users' })"));
100+
101+
if (indexPluginsArray() < 0) {
102+
const indexExportVuex = storeLines.findIndex(line => line.includes('export default new Vuex.Store({'));
103+
if (indexExportVuex < 0) throw new Error('No export default new Vuex.Store - Line!');
104+
storeLines.splice(indexExportVuex + 1, 0, ' plugins: [', ' ],');
105+
}
106+
107+
if (indexServicePlugins < 0) {
108+
storeLines.splice(indexPluginsArray() + 1, 0, ' ...servicePlugins,');
109+
}
110+
111+
if (indexAuthPlugin < 0 && options.init.isAuth) {
112+
const userService = options.init.authUserService || 'users';
113+
storeLines.splice(indexPluginsArray() + 1, 0, ` auth({ userService: '${userService}' }),`);
114+
}
115+
116+
return storeLines;
117+
}
118+
119+
function modifyStoreFile() {
120+
const storeFile = api.resolve('./src/store/index.js');
121+
const store = fs.readFileSync(storeFile, { encoding: 'utf-8' });
122+
let storeLines = store.split(/\r?\n/g);
123+
124+
storeLines = insertStoreImports(storeLines);
125+
storeLines = insertStorePlugins(storeLines);
126+
127+
fs.writeFileSync(storeFile, storeLines.join('\n'), { encoding: 'utf-8' });
128+
}
129+
130+
api.onCreateComplete(() => {
131+
moveStorePlainToFolderIndex();
132+
copyFeathersClientFile();
133+
modifyStoreFile();
134+
createServicesFolder();
135+
});
136+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
instanceDefaults(data, { store, Model, Models }) {
2+
return {
3+
};
4+
},

0 commit comments

Comments
 (0)