Skip to content

Commit

Permalink
fix: use ts files when necessary with --bare option (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
cexbrayat authored Jan 8, 2025
1 parent 9c9a356 commit f5291af
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
38 changes: 22 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import generateReadme from './utils/generateReadme'
import getCommand from './utils/getCommand'
import getLanguage from './utils/getLanguage'
import renderEslint from './utils/renderEslint'
import trimBoilerplate from './utils/trimBoilerplate'
import { trimBoilerplate, removeCSSImport, emptyRouterConfig } from './utils/trimBoilerplate'

import cliPackageJson from './package.json'

Expand Down Expand Up @@ -560,6 +560,24 @@ async function init() {
},
)

if (argv.bare) {
trimBoilerplate(root)
render('bare/base')
// TODO: refactor the `render` utility to avoid this kind of manual mapping?
if (needsTypeScript) {
render('bare/typescript')
}
if (needsVitest) {
render('bare/vitest')
}
if (needsCypressCT) {
render('bare/cypress-ct')
}
if (needsNightwatchCT) {
render('bare/nightwatch-ct')
}
}

// Cleanup.

// We try to share as many files between TypeScript and JavaScript as possible.
Expand Down Expand Up @@ -610,21 +628,9 @@ async function init() {
}

if (argv.bare) {
trimBoilerplate(root, { needsTypeScript, needsRouter })
render('bare/base')

// TODO: refactor the `render` utility to avoid this kind of manual mapping?
if (needsTypeScript) {
render('bare/typescript')
}
if (needsVitest) {
render('bare/vitest')
}
if (needsCypressCT) {
render('bare/cypress-ct')
}
if (needsNightwatchCT) {
render('bare/nightwatch-ct')
removeCSSImport(root, needsTypeScript)
if (needsRouter) {
emptyRouterConfig(root, needsTypeScript)
}
}

Expand Down
2 changes: 1 addition & 1 deletion template/base/vite.config.js.data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function getData() {
id: 'vite-plugin-vue-devtools',
importer: "import vueDevTools from 'vite-plugin-vue-devtools'",
initializer: 'vueDevTools()',
}
},
],
}
}
24 changes: 13 additions & 11 deletions utils/trimBoilerplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ function replaceContent(filepath: string, replacer: (content: string) => string)
fs.writeFileSync(filepath, replacer(content))
}

export default function trimBoilerplate(rootDir: string, features: Record<string, boolean>) {
const isTs = features.needsTypeScript
export function trimBoilerplate(rootDir: string) {
const srcDir = path.resolve(rootDir, 'src')

for (const filename of fs.readdirSync(srcDir)) {
Expand All @@ -19,18 +18,21 @@ export default function trimBoilerplate(rootDir: string, features: Record<string
const fullpath = path.resolve(srcDir, filename)
fs.rmSync(fullpath, { recursive: true })
}
}

export function removeCSSImport(rootDir: string, needsTypeScript: boolean) {
// Remove CSS import in the entry file
const entryPath = path.resolve(rootDir, isTs ? 'src/main.ts' : 'src/main.js')
const entryPath = path.resolve(rootDir, needsTypeScript ? 'src/main.ts' : 'src/main.js')
replaceContent(entryPath, (content) => content.replace("import './assets/main.css'\n\n", ''))
}

export function emptyRouterConfig(rootDir: string, needsTypeScript: boolean) {
const srcDir = path.resolve(rootDir, 'src')
// If `router` feature is selected, use an empty router configuration
if (features.needsRouter) {
const routerEntry = path.resolve(srcDir, isTs ? 'router/index.ts' : 'router/index.js')
replaceContent(routerEntry, (content) =>
content
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
)
}
const routerEntry = path.resolve(srcDir, needsTypeScript ? 'router/index.ts' : 'router/index.js')
replaceContent(routerEntry, (content) =>
content
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
)
}

0 comments on commit f5291af

Please sign in to comment.