Skip to content

Commit

Permalink
DRY the 'if exist' functions to a module
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jun 6, 2024
1 parent c34c3da commit c8e0a5e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
14 changes: 5 additions & 9 deletions src/build-commonjs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import chalk from 'chalk'
import { spawnSync } from 'node:child_process'
import { existsSync, renameSync, unlinkSync } from 'node:fs'
import { relative, resolve } from 'node:path/posix'
import buildFail from './build-fail.js'
import config from './config.js'
import * as console from './console.js'
import ifExist from './if-exist.js'
import polyfills from './polyfills.js'
import setFolderDialect from './set-folder-dialect.js'
import './tsconfig.js'
Expand All @@ -13,10 +13,6 @@ import tsc from './which-tsc.js'
const node = process.execPath
const { commonjsDialects = [] } = config

const unlinkIfExist = (f: string) => existsSync(f) && unlinkSync(f)
const renameIfExist = (f: string, to: string) =>
existsSync(f) && renameSync(f, to)

export const buildCommonJS = () => {
setFolderDialect('src', 'commonjs')
for (const d of ['commonjs', ...commonjsDialects]) {
Expand All @@ -41,10 +37,10 @@ export const buildCommonJS = () => {
).replace(/\.tsx?$/, '')
const stemToPath = `${stemTo}.js.map`
const stemToDtsPath = `${stemTo}.d.ts.map`
unlinkIfExist(stemToPath)
unlinkIfExist(stemToDtsPath)
renameIfExist(`${stemFrom}.cjs`, `${stemTo}.js`)
renameIfExist(`${stemFrom}.d.cts`, `${stemTo}.d.ts`)
ifExist.unlink(stemToPath)
ifExist.unlink(stemToDtsPath)
ifExist.rename(`${stemFrom}.cjs`, `${stemTo}.js`)
ifExist.rename(`${stemFrom}.d.cts`, `${stemTo}.d.ts`)
}
console.error(chalk.cyan.bold('built commonjs'))
}
Expand Down
14 changes: 5 additions & 9 deletions src/build-esm.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import chalk from 'chalk'
import { spawnSync } from 'node:child_process'
import { existsSync, renameSync, unlinkSync } from 'node:fs'
import { relative, resolve } from 'node:path'
import buildFail from './build-fail.js'
import config from './config.js'
import * as console from './console.js'
import ifExist from './if-exist.js'
import polyfills from './polyfills.js'
import setFolderDialect from './set-folder-dialect.js'
import './tsconfig.js'
import tsc from './which-tsc.js'

const unlinkIfExist = (f: string) => existsSync(f) && unlinkSync(f)
const renameIfExist = (f: string, to: string) =>
existsSync(f) && renameSync(f, to)

const node = process.execPath
const { esmDialects = [] } = config

Expand All @@ -39,10 +35,10 @@ export const buildESM = () => {
`.tshy-build/${d}`,
relative(resolve('src'), resolve(orig))
).replace(/\.tsx?$/, '')
unlinkIfExist(`${stemTo}.js.map`)
unlinkIfExist(`${stemTo}.d.ts.map`)
renameIfExist(`${stemFrom}.mjs`, `${stemTo}.js`)
renameIfExist(`${stemFrom}.d.mts`, `${stemTo}.d.ts`)
ifExist.unlink(`${stemTo}.js.map`)
ifExist.unlink(`${stemTo}.d.ts.map`)
ifExist.rename(`${stemFrom}.mjs`, `${stemTo}.js`)
ifExist.rename(`${stemFrom}.d.mts`, `${stemTo}.d.ts`)
}
console.error(chalk.cyan.bold('built ' + d))
}
Expand Down
10 changes: 10 additions & 0 deletions src/if-exist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { existsSync, renameSync, unlinkSync } from 'fs'

const unlink = (f: string) => existsSync(f) && unlinkSync(f)
const rename = (f: string, to: string) =>
existsSync(f) && renameSync(f, to)

export default {
unlink,
rename,
}
18 changes: 18 additions & 0 deletions test/if-exist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readdirSync } from 'fs'
import t from 'tap'
import ifExist from '../src/if-exist.js'

const dir = t.testdir({
a: 'a',
b: 'b',
})

ifExist.unlink(dir + '/x')
ifExist.rename(dir + '/x', dir + '/z')

t.strictSame(new Set(readdirSync(dir)), new Set(['a', 'b']))

ifExist.unlink(dir + '/a')
ifExist.rename(dir + '/b', dir + '/c')

t.strictSame(readdirSync(dir), ['c'])

0 comments on commit c8e0a5e

Please sign in to comment.