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
Changes from all 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
53 changes: 50 additions & 3 deletions packages/@vuetify/cli-plugin-utils/index.js
Original file line number Diff line number Diff line change
@@ -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,
5 changes: 4 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,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}`)
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
@@ -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,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%',
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
@@ -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(/<html\s+(.+\s+)?lang=[^\s>]+(\s|>)/))

if (htmlIndex !== -1) {