Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task(helpers): Abstract helpers and remove duplicates #226

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions packages/@vuetify/cli-plugin-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,11 +104,49 @@ function mergeSassVariables (opt, file) {
return opt
}

// 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)) {
ElijahKotyluk marked this conversation as resolved.
Show resolved Hide resolved
configPath = rcPath;
config = callback(require(rcPath));
} else if (fileExists(api, pkgPath)) {
configPath = pkgPath;
config = JSON.parse(fs.readFileSync(pkgPath, { encoding: "utf8" }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it would be reused a lot, maybe can go into utils?


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)
let content = fileExists(api, file)
? fs.readFileSync(file, { encoding: 'utf8' })
: ''

Expand Down Expand Up @@ -189,6 +227,8 @@ module.exports = {
injectHtmlLink,
injectSassVariables,
mergeSassVariables,
resolve,
updateBabelConfig,
updateFile,
updateVuetifyObject,
VuetifyPresetGenerator,
Expand Down
4 changes: 3 additions & 1 deletion packages/vue-cli-plugin-vuetify/generator/tools/fonts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { updateFile } = require("@vuetify/cli-plugin-utils")
ElijahKotyluk marked this conversation as resolved.
Show resolved Hide resolved

const helpers = require('./helpers')
const fonts = {
mdi: {
Expand Down Expand Up @@ -52,7 +54,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}`)
Expand Down
12 changes: 0 additions & 12 deletions packages/vue-cli-plugin-vuetify/generator/tools/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
4 changes: 3 additions & 1 deletion packages/vue-cli-plugin-vuetify/generator/tools/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const helpers = require('./helpers')
ElijahKotyluk marked this conversation as resolved.
Show resolved Hide resolved
const fs = require('fs')

ElijahKotyluk marked this conversation as resolved.
Show resolved Hide resolved
const { updateFile } = require("@vuetify/cli-plugin-utils")

function addDependencies (api) {
api.extendPackage({
dependencies: {
Expand Down Expand Up @@ -55,7 +57,7 @@ function updateBrowsersList (api) {
{ encoding: 'utf8' },
)
} else {
helpers.updateFile(api, './.browserslistrc', lines => {
updateFile(api, './.browserslistrc', lines => {
if (!lines.length) {
return [
'> 1%',
Expand Down
4 changes: 3 additions & 1 deletion packages/vue-cli-plugin-vuetify/generator/tools/vuetify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const fs = require('fs')
ElijahKotyluk marked this conversation as resolved.
Show resolved Hide resolved
const helpers = require('./helpers')

ElijahKotyluk marked this conversation as resolved.
Show resolved Hide resolved
const { updateFile } = require("@vuetify/cli-plugin-utils")

function addDependencies (api) {
api.extendPackage({
dependencies: {
Expand Down Expand Up @@ -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(/<html\s+(.+\s+)?lang=[^\s>]+(\s|>)/))

if (htmlIndex !== -1) {
Expand Down