diff --git a/packages/@vuetify/cli-plugin-utils/index.js b/packages/@vuetify/cli-plugin-utils/index.js index 9e9b58dc..514332da 100644 --- a/packages/@vuetify/cli-plugin-utils/index.js +++ b/packages/@vuetify/cli-plugin-utils/index.js @@ -67,7 +67,7 @@ function generatePreset (api, preset, onCreateComplete) { const file = 'src/plugins/vuetify.js' const plugin = api.resolve(file) - if (!fs.existsSync(plugin)) { + if (!fileExists(api, plugin)) { console.warn('Unable to locate `vuetify.js` plugin file in `src/plugins`.') return @@ -104,12 +104,56 @@ function mergeSassVariables (opt, file) { return opt } +// JSON-ify the contents of the supplied file +function parseFile(filePath) { + return JSON.parse(fs.readFileSync(filePath, { encoding: "utf8" })); +} + +// Resolve the supplied file +function resolve(file) { + return path.resolve(__dirname, file); +} + +// Update Babel config file with supplied callback +function updateBabelConfig(api, callback) { + let config, configPath; + + const rcPath = api.resolve("./babel.config.js"); + const pkgPath = api.resolve("./package.json"); + + if (fileExists(api, rcPath)) { + configPath = rcPath; + config = callback(require(rcPath)); + } else if (fileExists(api, pkgPath)) { + configPath = pkgPath; + config = parseFile(pkgPath); + + if (config.babel) { + config.babel = callback(config.babel); + } else { + // TODO: error handling here? + } + } + + if (configPath) { + const moduleExports = configPath !== pkgPath ? "module.exports = " : ""; + + fs.writeFileSync( + configPath, + `${moduleExports}${JSON.stringify(config, null, 2)}`, + { encoding: "utf8" } + ); + } else { + // TODO: handle if babel config doesn't exist + } +} + // Update local file with supplied callback function updateFile (api, file, callback) { const { EOL } = require('os') file = api.resolve(file) - let content = fs.existsSync(file) - ? fs.readFileSync(file, { encoding: 'utf8' }) + let content = fileExists(api, file) + ? parseFile(file) : '' content = callback(content.split(/\r?\n/g)).join(EOL) @@ -189,6 +233,9 @@ module.exports = { injectHtmlLink, injectSassVariables, mergeSassVariables, + parseFile, + resolve, + updateBabelConfig, updateFile, updateVuetifyObject, VuetifyPresetGenerator, diff --git a/packages/vue-cli-plugin-vuetify/generator/tools/fonts.js b/packages/vue-cli-plugin-vuetify/generator/tools/fonts.js index 1d91978d..35f91a66 100644 --- a/packages/vue-cli-plugin-vuetify/generator/tools/fonts.js +++ b/packages/vue-cli-plugin-vuetify/generator/tools/fonts.js @@ -1,3 +1,6 @@ +// Utilities +const { updateFile } = require("@vuetify/cli-plugin-utils") + const helpers = require('./helpers') const fonts = { mdi: { @@ -52,7 +55,7 @@ function addImports (api, iconFont) { } function addLinks (api, iconFont) { - helpers.updateFile(api, './public/index.html', lines => { + updateFile(api, './public/index.html', lines => { const lastLink = lines.reverse().findIndex(line => line.match(/^\s*<\/head>/)) lines.splice(lastLink + 1, 0, ` ${fonts.roboto.link}`) diff --git a/packages/vue-cli-plugin-vuetify/generator/tools/helpers.js b/packages/vue-cli-plugin-vuetify/generator/tools/helpers.js index 55a99376..7908caa1 100644 --- a/packages/vue-cli-plugin-vuetify/generator/tools/helpers.js +++ b/packages/vue-cli-plugin-vuetify/generator/tools/helpers.js @@ -32,18 +32,6 @@ function updateBabelConfig (api, callback) { } } -function updateFile (api, file, callback) { - file = api.resolve(file) - let content = fs.existsSync(file) - ? fs.readFileSync(file, { encoding: 'utf8' }) - : '' - - content = callback(content.split(/\r?\n/g)).join('\n') - - fs.writeFileSync(file, content, { encoding: 'utf8' }) -} - module.exports = { updateBabelConfig, - updateFile, } diff --git a/packages/vue-cli-plugin-vuetify/generator/tools/polyfill.js b/packages/vue-cli-plugin-vuetify/generator/tools/polyfill.js index c3d71078..145aff12 100644 --- a/packages/vue-cli-plugin-vuetify/generator/tools/polyfill.js +++ b/packages/vue-cli-plugin-vuetify/generator/tools/polyfill.js @@ -1,5 +1,7 @@ +// Utilities const helpers = require('./helpers') const fs = require('fs') +const { updateFile } = require("@vuetify/cli-plugin-utils") function addDependencies (api) { api.extendPackage({ @@ -55,7 +57,7 @@ function updateBrowsersList (api) { { encoding: 'utf8' }, ) } else { - helpers.updateFile(api, './.browserslistrc', lines => { + updateFile(api, './.browserslistrc', lines => { if (!lines.length) { return [ '> 1%', diff --git a/packages/vue-cli-plugin-vuetify/generator/tools/vuetify.js b/packages/vue-cli-plugin-vuetify/generator/tools/vuetify.js index 7774a24c..60b07e64 100644 --- a/packages/vue-cli-plugin-vuetify/generator/tools/vuetify.js +++ b/packages/vue-cli-plugin-vuetify/generator/tools/vuetify.js @@ -1,6 +1,8 @@ // Imports +// Utilities const fs = require('fs') const helpers = require('./helpers') +const { updateFile } = require("@vuetify/cli-plugin-utils") function addDependencies (api) { api.extendPackage({ @@ -53,7 +55,7 @@ function addImports (api) { } function setHtmlLang (api, locale) { - helpers.updateFile(api, './public/index.html', lines => { + updateFile(api, './public/index.html', lines => { const htmlIndex = lines.findIndex(line => line.match(/]+(\s|>)/)) if (htmlIndex !== -1) {