Skip to content

Commit

Permalink
chore: add test for initial config generation
Browse files Browse the repository at this point in the history
  • Loading branch information
joris-gallot committed May 8, 2024
1 parent eedfbd1 commit 92221a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import { defineCommand } from 'citty'
import { parseTscOutput } from '../index.js'
import { writeTypestepConfig } from '../utils.js'
import { CONFIG_FILE_NAME } from '../constants.js'
import type { TypestepConfig } from '../types.js'

async function init(tscOutputFile: string) {
export async function generateInitialConfig(tscOutputFile: string): Promise<TypestepConfig> {
const tscOutput = await readFile(tscOutputFile, 'utf8')
const parsedTscOutput = parseTscOutput(tscOutput)

const ignoredFiles = [...new Set(parsedTscOutput.map(({ path }) => path))]
const configFileContent = writeTypestepConfig({ ignoredFiles })

return writeFile(CONFIG_FILE_NAME, configFileContent)
return { ignoredFiles }
}

async function initConfig(tscOutputFile: string) {
const config = await generateInitialConfig(tscOutputFile)
return writeFile(CONFIG_FILE_NAME, writeTypestepConfig(config))
}

export default defineCommand({
Expand All @@ -37,6 +42,6 @@ export default defineCommand({
if (existsSync(CONFIG_FILE_NAME))
throw new Error('Typestep config file already exists')

init(resolve(process.cwd(), args.tsc_output_file))
initConfig(resolve(process.cwd(), args.tsc_output_file))
},
})
31 changes: 31 additions & 0 deletions tests/init-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { resolve } from 'node:path'
import { expect, test } from 'vitest'
import { generateInitialConfig } from '../src/commands/init.js'

const tscOutputPath = resolve(__dirname, 'fixtures', 'tsc-output.log')

test('init config with ignoredFiles', async () => {
const config = await generateInitialConfig(tscOutputPath)

expect(config).toStrictEqual({
ignoredFiles: [
'node_modules/.pnpm/@[email protected]/node_modules/@splidejs/vue-splide/src/js/plugin/plugin.ts',
'src/App.vue',
'src/components/__tests__/SortingButton.spec.ts',
'src/components/__tests__/MyForm.spec.ts',
'src/components/__tests__/MyModal.spec.ts',
'src/components/MyActionsGroup.vue',
'src/components/Comp1.vue',
'src/components/Comp2.vue',
'src/components/MyNav.vue',
'src/components/MyDocumentUploader.vue',
'src/components/MyForm2.vue',
'src/components/MyModal2.vue',
'src/components/Comp3.vue',
'src/components/Comp4.vue',
'src/components/Comp5.vue',
'src/components/Comp6.vue',
'src/components/Comp7.vue',
],
})
})

0 comments on commit 92221a6

Please sign in to comment.