Last active
June 4, 2019 13:14
-
-
Save Igloczek/b5b313cbcdeef6922f665568d88f00fd to your computer and use it in GitHub Desktop.
Tool for moving components imports from JS to Vue SFC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs-extra') | |
const path = require('path') | |
const glob = require('glob') | |
const prettier = require('prettier') | |
const components = glob | |
.sync('src/*/*/*.js', { ignore: '**/*.{stories,stories_,spec}.js' }) | |
.map(file => ({ | |
src: file, | |
name: path.basename(file) | |
})) | |
components.forEach(component => { | |
const file = fs.readFileSync(component.src, 'utf8') | |
const name = path.basename(component.src).replace('.js', '') | |
const imports = file.match(/(import.+)/g) | |
const obj = file.match(/(components:\s{.*?})/gs) | |
const js = prettier.format( | |
file | |
.replace(/(import.+)/g, '') | |
.replace(/(components:\s{.*?},?)/gs, ''), | |
{ parser: 'babel', semi: false, singleQuote: true } | |
) | |
fs.writeFileSync(component.src, js) | |
const vue = fs.readFileSync(component.src.replace('.js', '.vue'), 'utf8') | |
.replace(/<script.*\/>/g, ` | |
<script> | |
${imports ? imports.join('\n') : ''} | |
import A${name} from './${name}.js' | |
export default { | |
name: 'Alpaca ${name}', | |
${obj ? obj[0] + ',' : ''} | |
mixins: [ A${name} ] | |
} | |
</script> | |
`) | |
fs.writeFileSync( | |
component.src.replace('.js', '.vue'), | |
prettier.format(vue, { parser: 'vue', semi: false, singleQuote: true }) | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment