Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore .js, cjs and other build output #2

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ declare module 'tsconfig-utils' {
}

export interface Config {
loaders?: string[]
ignored?: string[]
converters?: string[]
}

export async function build(cwd: string, args: string[] = []) {
Expand All @@ -22,7 +23,8 @@ export async function build(cwd: string, args: string[] = []) {
const emitDeclarationOnly = config.get('emitDeclarationOnly')
if (!outDir || !rootDir || noEmit || emitDeclarationOnly) return

const loaders = config.atsc?.loaders || ['.yaml', '.yml']
const converters = config.atsc?.converters || ['.yaml', '.yml']
const ignored = config.atsc?.ignored || ['.ts', '.mts', '.cts', '.tsx', '.mjs', '.cjs', '.js', '.jsx']

const files = await globby(['**'], {
cwd: resolve(cwd, rootDir),
Expand All @@ -31,15 +33,16 @@ export async function build(cwd: string, args: string[] = []) {

await Promise.all(files.map(async (file) => {
const ext = extname(file)
if (['.ts', '.mts', '.cts', '.tsx'].includes(ext)) return
if (ignored.includes(ext)) return

const src = resolve(cwd, rootDir, file)
const dest = resolve(cwd, outDir, file)
await fs.mkdir(dirname(dest), { recursive: true })
if (!loaders.includes(ext)) {
await fs.copyFile(src, dest)
} else if (['.yaml', '.yml'].includes(ext)) {
if (converters.includes(ext) && ['.yml', '.yaml'].includes(ext)) {
const data = yaml.load(await fs.readFile(src, 'utf8'))
await fs.writeFile(dest.slice(0, -ext.length) + '.json', JSON.stringify(data) || '')
} else {
await fs.copyFile(src, dest)
}
}))
}