Skip to content

Commit

Permalink
Use Path typealias everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Feb 25, 2024
1 parent 23c3951 commit d25b897
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import platform.posix.mkdir
@Suppress("NOTHING_TO_INLINE")
@OptIn(ExperimentalForeignApi::class)
internal actual inline fun fs_platform_chmod(
path: String,
path: Path,
mode: UInt,
): Int = chmod(path, mode.convert()).convert()

@Suppress("NOTHING_TO_INLINE")
@OptIn(ExperimentalForeignApi::class)
internal actual inline fun fs_platform_mkdir(
path: String,
path: Path,
mode: UInt,
): Int = mkdir(path, mode.convert()).convert()
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,55 @@ package io.matthewnelson.kmp.file.internal

/** [docs](https://nodejs.org/api/fs.html#fschmodsyncpath-mode) */
@JsName("chmodSync")
internal external fun fs_chmodSync(path: String, mode: String)
internal external fun fs_chmodSync(path: Path, mode: String)

/** [docs](https://nodejs.org/api/fs.html#fsexistssyncpath) */
@JsName("existsSync")
internal actual external fun fs_exists(path: String): Boolean
internal actual external fun fs_exists(path: Path): Boolean

/** [docs](https://nodejs.org/api/fs.html#fsmkdirsyncpath-options) */
@JsName("mkdirSync")
internal external fun fs_mkdirSync(path: String, options: dynamic): String?
internal external fun fs_mkdirSync(path: Path, options: dynamic): String?

/** [docs](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options) */
@JsName("readFileSync")
internal external fun fs_readFileSync(path: String): buffer_Buffer
internal external fun fs_readFileSync(path: Path): buffer_Buffer

/** [docs](https://nodejs.org/api/fs.html#fsrealpathsyncpath-options) */
@JsName("realpathSync")
internal external fun fs_realpathSync(path: String): String
internal external fun fs_realpathSync(path: Path): String

/** [docs](https://nodejs.org/api/fs.html#fsrmsyncpath-options) */
@JsName("rmSync")
internal external fun fs_rmSync(path: String, options: dynamic)
internal external fun fs_rmSync(path: Path, options: dynamic)

/** [docs](https://nodejs.org/api/fs.html#fsrmdirsyncpath-options) */
@JsName("rmdirSync")
internal external fun fs_rmdirSync(path: String, options: dynamic)
internal external fun fs_rmdirSync(path: Path, options: dynamic)

/** [docs](https://nodejs.org/api/fs.html#fsunlinksyncpath) */
@JsName("unlinkSync")
internal external fun fs_unlinkSync(path: String)
internal external fun fs_unlinkSync(path: Path)

/** [docs](https://nodejs.org/api/fs.html#fswritefilesyncfile-data-options) */
@JsName("writeFileSync")
internal external fun fs_writeFileSync(path: String, data: buffer_Buffer)
internal external fun fs_writeFileSync(path: Path, data: buffer_Buffer)

/** [docs](https://nodejs.org/api/fs.html#fswritefilesyncfile-data-options) */
@JsName("writeFileSync")
internal external fun fs_writeFileSync(path: String, data: ByteArray)
internal external fun fs_writeFileSync(path: Path, data: ByteArray)

/** [docs](https://nodejs.org/api/fs.html#fswritefilesyncfile-data-options) */
@JsName("writeFileSync")
internal external fun fs_writeFileSync(path: String, data: String)
internal external fun fs_writeFileSync(path: Path, data: String)

/** [docs](https://nodejs.org/api/fs.html#fslstatsyncpath-options) */
@JsName("lstatSync")
internal external fun fs_lstatSync(path: String): fs_Stats
internal external fun fs_lstatSync(path: Path): fs_Stats

/** [docs](https://nodejs.org/api/fs.html#fsstatsyncpath-options) */
@JsName("statSync")
internal external fun fs_statSync(path: String): fs_Stats
internal external fun fs_statSync(path: Path): fs_Stats

/** [docs](https://nodejs.org/api/fs.html#class-fsstats) */
@JsName("Stats")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ internal external val path_sep: String

/** [docs](https://nodejs.org/api/path.html#pathbasenamepath-suffix) **/
@JsName("basename")
internal external fun path_basename(path: String): String
internal external fun path_basename(path: Path): String

/** [docs](https://nodejs.org/api/path.html#pathdirnamepath) **/
@JsName("dirname")
internal external fun path_dirname(path: String): String
internal external fun path_dirname(path: Path): String

/** [docs](https://nodejs.org/api/path.html#pathisabsolutepath) **/
@JsName("isAbsolute")
internal external fun path_isAbsolute(path: String): Boolean
internal external fun path_isAbsolute(path: Path): Boolean
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal actual inline fun Path.isAbsolute(): Boolean {
}

// @Throws(IOException::class)
internal actual fun fs_chmod(path: String, mode: String) {
internal actual fun fs_chmod(path: Path, mode: String) {
try {
fs_chmodSync(path, mode)
} catch (t: Throwable) {
Expand All @@ -129,7 +129,7 @@ internal actual fun fs_chmod(path: String, mode: String) {
}

// @Throws(IOException::class)
internal actual fun fs_remove(path: String): Boolean {
internal actual fun fs_remove(path: Path): Boolean {
try {
fs_unlinkSync(path)
return true
Expand All @@ -154,7 +154,7 @@ internal actual fun fs_remove(path: String): Boolean {
}
}

internal actual fun fs_mkdir(path: String): Boolean {
internal actual fun fs_mkdir(path: Path): Boolean {
return try {
val options = js("{}")
options["recursive"] = false
Expand All @@ -168,7 +168,7 @@ internal actual fun fs_mkdir(path: String): Boolean {
}

// @Throws(IOException::class)
internal actual fun fs_realpath(path: String): String {
internal actual fun fs_realpath(path: Path): Path {
return try {
fs_realpathSync(path)
} catch (t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import platform.posix.*
@Suppress("NOTHING_TO_INLINE")
@OptIn(ExperimentalForeignApi::class)
internal actual inline fun fs_platform_chmod(
path: String,
path: Path,
mode: UInt,
): Int = chmod(path, mode.convert()).convert()

@Suppress("NOTHING_TO_INLINE")
@OptIn(ExperimentalForeignApi::class)
internal actual inline fun fs_platform_mkdir(
path: String,
path: Path,
mode: UInt,
): Int = mkdir(path, mode.convert()).convert()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import platform.posix.remove
import platform.posix.rmdir

@Throws(IOException::class)
internal actual fun fs_chmod(path: String, mode: String) { /* no-op */ }
internal actual fun fs_chmod(path: Path, mode: String) { /* no-op */ }

@Throws(IOException::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_remove(path: String): Boolean {
internal actual fun fs_remove(path: Path): Boolean {
if (remove(path) == 0) return true

val err = errno
Expand All @@ -55,14 +55,12 @@ internal actual fun fs_remove(path: String): Boolean {
}

internal actual fun fs_platform_mkdir(
path: String,
path: Path,
): Int = mkdir(path)

@Throws(IOException::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_realpath(
path: String,
): String {
internal actual fun fs_realpath(path: Path): Path {
val real = _fullpath(null, path, PATH_MAX.toULong())
?: throw errnoToIOException(errno)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import platform.posix.FILE
import platform.posix.access
import platform.posix.errno

internal actual fun fs_exists(path: String): Boolean {
internal actual fun fs_exists(path: Path): Boolean {
val result = access(path, 0)
return if (result != 0 && errno == ENOENT) {
false
Expand All @@ -34,11 +34,11 @@ internal actual fun fs_exists(path: String): Boolean {
}
}

internal actual fun fs_mkdir(path: String): Boolean {
internal actual fun fs_mkdir(path: Path): Boolean {
return fs_platform_mkdir(path) == 0
}

internal expect fun fs_platform_mkdir(path: String): Int
internal expect fun fs_platform_mkdir(path: Path): Int

@ExperimentalForeignApi
@Suppress("NOTHING_TO_INLINE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package io.matthewnelson.kmp.file.internal
import io.matthewnelson.kmp.file.IOException

@Throws(IOException::class)
internal fun fs_canonicalize(path: String): String {
internal fun fs_canonicalize(path: Path): Path {
if (path.isEmpty()) return fs_realpath(".")

val resolved = path.absolute().normalize()
Expand All @@ -37,13 +37,13 @@ internal fun fs_canonicalize(path: String): String {
}

@Throws(IOException::class)
internal expect fun fs_chmod(path: String, mode: String)
internal expect fun fs_chmod(path: Path, mode: String)

internal expect fun fs_exists(path: String): Boolean
internal expect fun fs_exists(path: Path): Boolean

internal expect fun fs_mkdir(path: String): Boolean
internal expect fun fs_mkdir(path: Path): Boolean

internal fun fs_mkdirs(path: String): Boolean {
internal fun fs_mkdirs(path: Path): Boolean {
if (fs_mkdir(path)) return true

val dirsToMake = try {
Expand All @@ -70,7 +70,7 @@ internal fun fs_mkdirs(path: String): Boolean {
}

@Throws(IOException::class)
internal expect fun fs_remove(path: String): Boolean
internal expect fun fs_remove(path: Path): Boolean

@Throws(IOException::class)
internal expect fun fs_realpath(path: String): String
internal expect fun fs_realpath(path: Path): Path
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import platform.posix.*

@Throws(IOException::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_chmod(path: String, mode: String) {
internal actual fun fs_chmod(path: Path, mode: String) {
val modeT = try {
Mode(value = mode).toModeT()
} catch (e: IllegalArgumentException) {
Expand All @@ -40,7 +40,7 @@ internal actual fun fs_chmod(path: String, mode: String) {

@Throws(IOException::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_remove(path: String): Boolean {
internal actual fun fs_remove(path: Path): Boolean {
val result = remove(path)
if (result != 0) {
if (errno == ENOENT) return false
Expand All @@ -51,7 +51,7 @@ internal actual fun fs_remove(path: String): Boolean {

@Throws(IOException::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun fs_realpath(path: String): String {
internal actual fun fs_realpath(path: Path): Path {
val real = realpath(path, null)
?: throw errnoToIOException(errno)

Expand All @@ -63,18 +63,18 @@ internal actual fun fs_realpath(path: String): String {
}

internal actual fun fs_platform_mkdir(
path: String,
path: Path,
): Int = fs_platform_mkdir(path, Mode("775").toModeT())

@Suppress("NOTHING_TO_INLINE")
internal expect inline fun fs_platform_chmod(
path: String,
path: Path,
mode: UInt,
): Int

@Suppress("NOTHING_TO_INLINE")
internal expect inline fun fs_platform_mkdir(
path: String,
path: Path,
mode: UInt,
): Int

Expand Down

0 comments on commit d25b897

Please sign in to comment.