1- const fs = require ( 'fs' ) ;
2-
31module . 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} ;
0 commit comments