Skip to content

Commit bf5bd19

Browse files
committed
init
0 parents  commit bf5bd19

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
lib
2+
coverage
3+
node_modules
4+
test/e2e/report
5+
.DS_Store
6+
*.log
7+
*.swp
8+
*~

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.*
2+
*.log
3+
*.swp
4+
*.yml
5+
bower.json
6+
coverage
7+
config
8+
dist/*.map
9+
lib
10+
test

README.md

Whitespace-only changes.

generator/index.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
const fs = require('fs');
2+
3+
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");
15+
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+
}
78+
}
79+
80+
function insertStoreImports(storeLines) {
81+
if (storeLines.findIndex(line => line.includes("feathers-vuex")) >= 0) { return storeLines; }
82+
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+
}
91+
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+
}
101+
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+
}
155+
};

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = (api, options) => {
2+
3+
};

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "vue-cli-plugin-feathers-vuex",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
},
8+
"author": "fratzinger",
9+
"license": "ISC"
10+
}

prompts.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = [
2+
{
3+
name: `serverUrl`,
4+
type: 'input',
5+
message: 'What is the url of your feathers server instance?',
6+
default: "http://localhost:3030",
7+
},
8+
{
9+
name: `isAuth`,
10+
type: 'confirm',
11+
message: 'Setup authentication (highly recommened)?',
12+
default: true,
13+
},
14+
{
15+
when: answers => answers.isAuth == true,
16+
name: `authIdField`,
17+
type: 'input',
18+
message: 'What is the id-key of your auth service?',
19+
default: "id",
20+
},
21+
{
22+
when: answers => answers.isAuth == true,
23+
name: `authUserService`,
24+
type: 'input',
25+
message: 'What is the name of your users-service for authentication?',
26+
default: "users",
27+
}
28+
];

0 commit comments

Comments
 (0)