naive-ui/scripts/read-components.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-09-09 16:09:53 +08:00
const fs = require('fs').promises
const path = require('path')
const _ = require('lodash')
2020-09-09 16:09:53 +08:00
;(async () => {
const files = await fs.readdir(
path.resolve(__dirname, '..', 'src')
)
for (const file of files) {
if ([
'locales'
].includes(file)) continue
if (!file.startsWith('_') && !file.endsWith('.js')) {
console.log(`- [ ] ${file}`)
await generateImportOnDemandTest(file)
}
}
2020-09-09 16:09:53 +08:00
})()
async function generateImportOnDemandTest (name) {
const styleVarName = `${_.camelCase(name)}Light`
2020-12-11 13:32:33 +08:00
const testFileName = `${_.upperFirst(_.camelCase(name))}.spec.js`
const compVarName = `N${_.upperFirst(_.camelCase(name))}`
const script = `import { mount } from '@vue/test-utils'
import create from '../../create'
import { enUS } from '../../locales'
import { ${styleVarName} } from '../styles'
import { ${compVarName} } from '../index'
describe('n-${name}', () => {
const naive = create({
locales: [
enUS
],
styles: [
${styleVarName}
]
})
it('should work with import on demand', () => {
mount(${compVarName}, {
global: {
plugins: [naive]
}
})
})
})
`
if (
await fs.stat(path.resolve(__dirname, '../src/', name, 'tests')).then(() => true).catch(() => false)
) {
} else {
await fs.mkdir(path.resolve(__dirname, '../src/', name, 'tests'))
}
2020-12-11 13:32:33 +08:00
await fs.writeFile(path.resolve(__dirname, '../src/', name, 'tests', testFileName), script)
}