This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.js
184 lines (164 loc) · 6.14 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Ignite CLI plugin for ReduxPersist
// ----------------------------------------------------------------------------
const NPM_MODULE_NAME = 'redux-persist'
const NPM_MODULE_VERSION = '^5.10.0'
const PLUGIN_PATH = __dirname
const APP_PATH = process.cwd()
const add = async function(context) {
const { ignite, filesystem } = context
// install an NPM module and link it
await ignite.addModule(NPM_MODULE_NAME, { version: NPM_MODULE_VERSION })
// ensure supporting modules
await ignite.addModule('ramda')
await ignite.addModule('seamless-immutable')
// add immutable persistence transform service
if (!filesystem.exists(`${APP_PATH}/App/Services/ImmutablePersistenceTransform.js`)) {
filesystem.copy(
`${PLUGIN_PATH}/templates/ImmutablePersistenceTransform.js`,
`${APP_PATH}/App/Services/ImmutablePersistenceTransform.js`
)
}
// add rehydration service
if (!filesystem.exists(`${APP_PATH}/App/Services/Rehydration.js`)) {
filesystem.copy(
`${PLUGIN_PATH}/templates/Rehydration.js`,
`${APP_PATH}/App/Services/Rehydration.js`
)
}
// add config
if (!filesystem.exists(`${APP_PATH}/Config/ReduxPersist.js`)) {
filesystem.copy(
`${PLUGIN_PATH}/templates/ReduxPersist.js`,
`${APP_PATH}/App/Config/ReduxPersist.js`
)
}
// patch CreateStore.js
ignite.patchInFile(`${APP_PATH}/App/Redux/CreateStore.js`, {
insert: `import ReduxPersist from '../Config/ReduxPersist'`,
after: `from 'redux'`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/CreateStore.js`, {
insert: `import Rehydration from '../Services/Rehydration'`,
after: `from 'redux'`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/CreateStore.js`, {
insert: `
// configure persistStore and check reducer version number
if (ReduxPersist.active) {
Rehydration.updateReducers(store)
}`,
after: `const store`
})
// patch Redux/index.js
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
insert: `import { persistReducer } from 'redux-persist'`,
after: `from 'redux'`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
insert: `import ReduxPersist from '../Config/ReduxPersist'`,
after: `from '../Sagas/'`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
insert: ` let finalReducers = reducers
// If rehydration is on use persistReducer otherwise default combineReducers
if (ReduxPersist.active) {
const persistConfig = ReduxPersist.storeConfig
finalReducers = persistReducer(persistConfig, reducers)
}\n`,
after: `export default`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
replace: `let { store, sagasManager, sagaMiddleware } = configureStore(reducers, rootSaga)`,
insert: `let { store, sagasManager, sagaMiddleware } = configureStore(finalReducers, rootSaga)`
})
// patch RootContainer.js
ignite.patchInFile(`${APP_PATH}/App/Containers/RootContainer.js`, {
insert: `import ReduxPersist from '../Config/ReduxPersist'`,
after: `from '../Redux/StartupRedux'`
})
ignite.patchInFile(`${APP_PATH}/App/Containers/RootContainer.js`, {
insert: ` componentDidMount () {
// if redux persist is not active fire startup action
if (!ReduxPersist.active) {
this.props.startup()
}
}`,
replace: ` componentDidMount () {
this.props.startup()
}`
})
}
const remove = async function(context) {
const { ignite, filesystem } = context
// remove the npm module
await ignite.removeModule(NPM_MODULE_NAME)
// remove immutable persistence transform service
const removeConfig = await context.prompt.confirm('Do you want to remove Config/ReduxPersist.js?')
if (removeConfig) {
filesystem.remove(`${APP_PATH}/App/Config/ReduxPersist.js`)
}
// remove immutable persistence transform service
const removePersistenceTransform = await context.prompt.confirm(
'Do you want to remove App/Services/ImmutablePersistenceTransform.js?'
)
if (removePersistenceTransform) {
filesystem.remove(`${APP_PATH}/App/Services/ImmutablePersistenceTransform.js`)
}
// remove rehydration service
const removeRehydration = await context.prompt.confirm(
'Do you want to remove App/Services/Rehydration.js?'
)
if (removeRehydration) {
filesystem.remove(`${APP_PATH}/App/Services/Rehydration.js`)
}
// unpatch CreateStore.js
ignite.patchInFile(`${APP_PATH}/App/Redux/CreateStore.js`, {
delete: `import ReduxPersist from '../Config/ReduxPersist'\n`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/CreateStore.js`, {
delete: `import Rehydration from '../Services/Rehydration'\n`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/CreateStore.js`, {
delete: `
// configure persistStore and check reducer version number
if (ReduxPersist.active) {
Rehydration.updateReducers(store)
}\n`
})
// unpatch Redux/index.js
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
delete: `import { persistReducer } from 'redux-persist'\n`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
delete: `import ReduxPersist from '../Config/ReduxPersist'\n`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
delete: ` let finalReducers = reducers
// If rehydration is on use persistReducer otherwise default combineReducers
if (ReduxPersist.active) {
const persistConfig = ReduxPersist.storeConfig
finalReducers = persistReducer(persistConfig, reducers)
}\n`
})
ignite.patchInFile(`${APP_PATH}/App/Redux/index.js`, {
replace: `let { store, sagasManager, sagaMiddleware } = configureStore(finalReducers, rootSaga)`,
insert: `let { store, sagasManager, sagaMiddleware } = configureStore(reducers, rootSaga)`
})
// unpatch RootContainer.js
ignite.patchInFile(`${APP_PATH}/App/Containers/RootContainer.js`, {
delete: `import ReduxPersist from '../Config/ReduxPersist'\n`
})
ignite.patchInFile(`${APP_PATH}/App/Containers/RootContainer.js`, {
replace: ` componentDidMount () {
// if redux persist is not active fire startup action
if (!ReduxPersist.active) {
this.props.startup()
}
}`,
insert: ` componentDidMount () {
this.props.startup()
}`
})
}
// Required in all Ignite CLI plugins
module.exports = { add, remove }