Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 5b28285

Browse files
refactor: more generic updateFile instead of updateMain
1 parent a9b5c00 commit 5b28285

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

generator/helpers.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,12 @@ module.exports = function (api) {
4040
}
4141
},
4242

43-
updateMain (callback) {
44-
const tsPath = api.resolve('./src/main.ts')
45-
const jsPath = api.resolve('./src/main.js')
43+
updateFile (file, callback) {
44+
let content = fs.readFileSync(file, { encoding: 'utf8' })
4645

47-
const mainPath = fs.existsSync(tsPath) ? tsPath : jsPath
48-
let content = fs.readFileSync(mainPath, { encoding: 'utf8' })
46+
content = callback(content.split(/\r?\n/g)).join('\n')
4947

50-
let lines = content.split(/\r?\n/g)
51-
52-
lines = callback(lines)
53-
54-
content = lines.join('\n')
55-
fs.writeFileSync(mainPath, content, { encoding: 'utf8' })
48+
fs.writeFileSync(file, content, { encoding: 'utf8' })
5649
}
5750
}
5851
}

generator/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ module.exports = (api, opts, rootOpts) => {
7171
// adapted from https://github.com/Akryum/vue-cli-plugin-apollo/blob/master/generator/index.js#L68-L91
7272
api.onCreateComplete(() => {
7373
// Modify main.js
74-
helpers.updateMain(src => {
75-
const vueImportIndex = src.findIndex(line => line.match(/^import Vue/))
74+
helpers.updateFile(helpers.getMain(), lines => {
75+
const vueImportIndex = lines.findIndex(line => line.match(/^import Vue/))
7676

77-
src.splice(vueImportIndex + 1, 0, 'import \'./plugins/vuetify\'')
77+
lines.splice(vueImportIndex + 1, 0, 'import \'./plugins/vuetify\'')
7878

79-
return src
79+
return lines
8080
})
8181

8282
// Add polyfill
@@ -103,12 +103,12 @@ module.exports = (api, opts, rootOpts) => {
103103
return cfg
104104
})
105105

106-
helpers.updateMain(src => {
107-
if (!src.find(l => l.match(/^(import|require).*@babel\/polyfill.*$/))) {
108-
src.unshift('import \'@babel/polyfill\'')
106+
helpers.updateFile(helpers.getMain(), lines => {
107+
if (!lines.find(l => l.match(/^(import|require).*@babel\/polyfill.*$/))) {
108+
lines.unshift('import \'@babel/polyfill\'')
109109
}
110110

111-
return src
111+
return lines
112112
})
113113
}
114114

0 commit comments

Comments
 (0)