Skip to content

Commit f8c44d7

Browse files
committed
refact: remove updateMain & updateApp helpers
they are replaced with updateFile method
1 parent d53155a commit f8c44d7

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

generator/helpers.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,14 @@ module.exports = function (api) {
3535
}
3636
},
3737

38-
updateMain (callback) {
39-
let content = fs.readFileSync(api.resolve(api.entryFile), { encoding: 'utf8' })
38+
updateFile(filepath, callback){
39+
let content = fs.readFileSync(filepath, 'utf-8')
4040

41-
let lines = content.split(/\r?\n/g)
42-
43-
lines = callback(lines)
41+
const lines = content.split(/\r?\n/)
42+
callback(lines)
4443

4544
content = lines.join('\n')
46-
fs.writeFileSync(api.resolve(api.entryFile), content, { encoding: 'utf8' })
45+
fs.writeFileSync(filepath, content, { encoding: 'utf-8' })
4746
},
48-
49-
//TODO: refactor since is equal to updateMain
50-
updateApp(callback){
51-
const appPath = api.resolve('./src/App.vue')
52-
53-
let content = fs.readFileSync(appPath, { encoding: 'utf8' })
54-
let lines = content.split(/\r?\n/g)
55-
lines = callback(lines)
56-
57-
content = lines.join('\n')
58-
fs.writeFileSync(appPath, content, { encoding: 'utf8' })
59-
}
6047
}
6148
}

generator/index.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ module.exports = (api, opts, rootOpts) => {
3131
// adapted from https://github.com/Akryum/vue-cli-plugin-apollo/blob/master/generator/index.js#L68-L91
3232
api.onCreateComplete(() => {
3333
// Modify main.js
34-
helpers.updateMain(src => {
35-
const vueImportIndex = src.findIndex(line => line.match(/^import Vue/))
36-
37-
src.splice(vueImportIndex + 1, 0, 'import \'./plugins/bootstrap-vue\'')
38-
39-
return src
34+
helpers.updateFile(api.resolve(api.entryFile), srcLines => {
35+
const vueImportIndex = srcLines.findIndex(line => line.match(/^import Vue/))
36+
srcLines.splice(vueImportIndex + 1, 0, 'import \'./plugins/bootstrap-vue\'')
4037
})
4138

4239
if(opts.useScss){
4340
//Modify App.vue (import bootstrap styles)
44-
helpers.updateApp(src => {
45-
let styleBlockIndex = src.findIndex(line => line.match(/^<style/))
41+
helpers.updateFile(api.resolve('./src/App.vue'), srcLines => {
42+
let styleBlockIndex = srcLines.findIndex(line => line.match(/^<style/))
4643

4744
if(styleBlockIndex === -1){ //no style block found
4845
//create it with lang scss
@@ -59,10 +56,8 @@ module.exports = (api, opts, rootOpts) => {
5956
}
6057
}
6158

62-
const bootstrapImportString = `@import "~@/assets/scss/vendors/bootstrap-vue/index";\n`
59+
const bootstrapImportString = `@import "~@/assets/scss/vendors/bootstrap-vue/index";`
6360
src.splice(styleBlockIndex + 1, 0, bootstrapImportString)
64-
65-
return src
6661
})
6762
}
6863

0 commit comments

Comments
 (0)