Skip to content

Commit

Permalink
feat: show error if filename is longer than 255 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
marsidev committed Sep 4, 2022
1 parent b1af9d1 commit 856b2d9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LogOptions } from './types'
import { join, parse } from 'path'
import { join, parse } from 'node:path'
import pc from 'picocolors'
import {
createEmptyFile,
Expand Down Expand Up @@ -44,6 +44,11 @@ export const init = () => {

const parsed = parse(filenamePath)

if (filename.length > 255) {
error('Filename is too long!', logOptions)
return
}

if (dirExists(filename)) {
warn(`File ${pc.cyan(filename)} already exists`, logOptions)
return
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs'
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'

export const ensureDir = (path: string) => {
mkdirSync(path, { recursive: true })
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SyncOptions } from 'execa'
import { join, parse } from 'node:path'
import { readdirSync } from 'node:fs'
import { randomBytes } from 'crypto'
import { afterAll, beforeAll, describe, expect, it, test } from 'vitest'
import { execa } from 'execa'
import pkgJson from '../package.json'
Expand Down Expand Up @@ -187,4 +188,12 @@ describe('creating files', () => {
expect(removeColors(stdout)).toContain(`${file} is not a valid filename`)
expect(dirExists(file)).toBe(false)
})

it('shows a proper error if the filename is too long', async () => {
const file = randomBytes(128).toString('hex') // each byte encoded to hex is 2 characters

const { stdout } = await run([file])
expect(removeColors(stdout)).toContain('Filename is too long!')
expect(dirExists(file)).toBe(false)
})
})

0 comments on commit 856b2d9

Please sign in to comment.