Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
👕 refactor(generator): lang checking
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Jun 24, 2018
1 parent 58f7d9e commit 2430f91
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions generator/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const debug = require('debug')('vue-cli-plugin-i18n:generator')
const {
checkInstalled,
exists,
Expand Down Expand Up @@ -28,23 +29,15 @@ function buildEnvContent (values) {

module.exports = (api, options, rootOptions) => {
const { locale, fallbackLocale, enableInSFC } = options
debug('options', options)
debug('rootOptions', rootOptions)

try {
const tsPath = api.resolve('src/main.ts')
const jsPath = api.resolve('src/main.js')
const tsExists = exists(tsPath)
const jsExists = exists(jsPath)

if (!tsExists && !jsExists) {
api.exitLog('No entry found', 'error')
return
}

const lang = tsExists && api.hasPlugin('typescript') ? 'ts' : 'js'
const classComponent = tsExists &&
checkInstalled('./node_modules/vue-class-component/package.json') &&
const lang = api.hasPlugin('typescript') ? 'ts' : 'js'
const classComponent = checkInstalled('./node_modules/vue-class-component/package.json') &&
checkInstalled('./node_modules/vue-property-decorator/package.json')
const additionalOptions = { ...options, ...{ lang, classComponent } }
debug('additionalOptions', additionalOptions)

/*
* extend packages
Expand Down Expand Up @@ -75,6 +68,7 @@ module.exports = (api, options, rootOptions) => {
}
pkg.devDependencies = { ...pkg.devDependencies, ...devTsDependencies }
}
debug('pkg', pkg)

api.extendPackage(pkg)

Expand All @@ -94,6 +88,7 @@ module.exports = (api, options, rootOptions) => {
* Modify main.(js|ts)
*/
const file = lang === 'ts' ? 'src/main.ts' : 'src/main.js'
debug('target file', file)
api.injectImports(file, `import i18n from './i18n'`)
api.injectRootOptions(file, `i18n,`)
} catch (e) {
Expand All @@ -102,6 +97,7 @@ module.exports = (api, options, rootOptions) => {
}

api.onCreateComplete(() => {
debug('onCreateComplete called')
const defaultLocaleMessages = JSON.stringify({
message: 'hello i18n !!'
}, null, 2)
Expand All @@ -117,17 +113,17 @@ module.exports = (api, options, rootOptions) => {
}
}

const localesDirPath = api.resolve('./src/locales')
const localesDirPath = api.resolve('src/locales')
if (!exists(localesDirPath)) {
if (!mkdir(localesDirPath)) {
api.exitLog(`cannot make ${localesDirPath}`, 'error')
return
}
}

writeLocaleFile(api.resolve(`./src/locales/${locale}.json`))
writeLocaleFile(api.resolve(`src/locales/${locale}.json`))
if (locale !== fallbackLocale) {
writeLocaleFile(api.resolve(`./src/locales/${fallbackLocale}.json`))
writeLocaleFile(api.resolve(`src/locales/${fallbackLocale}.json`))
}

const envPath = api.resolve('.env')
Expand All @@ -150,6 +146,7 @@ module.exports = (api, options, rootOptions) => {
})

api.postProcessFiles(files => {
debug('postProcessFiles called')
if (!api.hasPlugin('typescript')) { return }
const tsConfigRaw = files['tsconfig.json']
const tsConfig = JSON.parse(tsConfigRaw)
Expand Down

0 comments on commit 2430f91

Please sign in to comment.