Skip to content

Commit 017e7b9

Browse files
author
Guillaume Chau
committed
fix(ui): config: create first file if no file exists
1 parent bafcaae commit 017e7b9

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

packages/@vue/cli-ui/src/graphql-api/connectors/configurations.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const fs = require('fs')
1+
const fs = require('fs-extra')
22
const path = require('path')
33
const yaml = require('js-yaml')
44
const clone = require('clone')
5+
const stringifyJS = require('javascript-stringify')
56
// Connectors
67
const cwd = require('./cwd')
78
const plugins = require('./plugins')
@@ -52,6 +53,25 @@ function findFile (config, context) {
5253
}
5354
}
5455

56+
function getDefaultFile (config, context) {
57+
if (!config.files) {
58+
return null
59+
}
60+
61+
const keys = Object.keys(config.files)
62+
if (keys.length) {
63+
for (const key of keys) {
64+
if (key !== 'package') {
65+
const file = config.files[key][0]
66+
return {
67+
type: key,
68+
path: path.resolve(cwd.get(), file)
69+
}
70+
}
71+
}
72+
}
73+
}
74+
5575
function readData (config, context) {
5676
const file = findFile(config, context)
5777
config.file = file
@@ -74,9 +94,15 @@ function readData (config, context) {
7494
}
7595

7696
function writeData ({ config, data, changedFields }, context) {
77-
const file = findFile(config, context)
97+
let file = findFile(config, context)
98+
99+
if (!file) {
100+
file = getDefaultFile(config, context)
101+
}
102+
78103
if (file) {
79-
log('Config write', config.id, data, changedFields)
104+
log('Config write', config.id, data, changedFields, file.path)
105+
fs.ensureFileSync(file.path)
80106
let rawContent
81107
if (file.type === 'package') {
82108
const pkg = folders.readPackage(cwd.get(), context)
@@ -88,12 +114,16 @@ function writeData ({ config, data, changedFields }, context) {
88114
} else if (file.type === 'yaml') {
89115
rawContent = yaml.safeDump(data)
90116
} else if (file.type === 'js') {
91-
const changedData = changedFields.reduce((obj, field) => {
92-
obj[field] = data[field]
93-
return obj
94-
}, {})
95-
const source = fs.readFileSync(file.path, { encoding: 'utf8' })
96-
rawContent = extendJSConfig(changedData, source)
117+
let source = fs.readFileSync(file.path, { encoding: 'utf8' })
118+
if (!source.trim()) {
119+
rawContent = `module.exports = ${stringifyJS(data, null, 2)}`
120+
} else {
121+
const changedData = changedFields.reduce((obj, field) => {
122+
obj[field] = data[field]
123+
return obj
124+
}, {})
125+
rawContent = extendJSConfig(changedData, source)
126+
}
97127
}
98128
}
99129
fs.writeFileSync(file.path, rawContent, { encoding: 'utf8' })

0 commit comments

Comments
 (0)