|
| 1 | +package tutorial.webapp |
| 2 | + |
| 3 | +import typings.node.childProcessMod as node_child_process |
| 4 | +import typings.node.utilMod as node_util |
| 5 | +import typings.node.fsMod as node_fs |
| 6 | +import typings.node.osMod as node_os |
| 7 | +import typings.node.pathMod as node_path |
| 8 | +import typings.node.bufferMod.global.BufferEncoding |
| 9 | + |
| 10 | +object Node { |
| 11 | + |
| 12 | + def readFile(filePath: String): String = |
| 13 | + val opts = BufferEncoding.utf8 |
| 14 | + val read = node_fs.readFileSync(filePath, opts) |
| 15 | + read |
| 16 | + |
| 17 | + def writeFile(filePath: String, contents: String): Unit = |
| 18 | + node_fs.writeFileSync(filePath, contents) |
| 19 | + |
| 20 | + def rename(oldPath: String, newPath: String): Unit = |
| 21 | + node_fs.renameSync(oldPath, newPath) |
| 22 | + |
| 23 | + def cp(filePath1: String, filePath2: String): Unit = |
| 24 | + node_fs.copyFileSync(filePath1, filePath2) |
| 25 | + |
| 26 | + def makeDir(dirPath: String): Unit = |
| 27 | + try { |
| 28 | + if (!node_fs.existsSync(dirPath)) { |
| 29 | + val opts = node_fs.MakeDirectoryOptions() |
| 30 | + opts.recursive = true |
| 31 | + node_fs.mkdirSync(dirPath, opts) |
| 32 | + } |
| 33 | + } catch { |
| 34 | + case e: Exception => |
| 35 | + throw Exception(s"Couldn't create a new directory: ${dirPath}", e) |
| 36 | + } |
| 37 | + |
| 38 | + def makeTmpDir(): String = |
| 39 | + try { |
| 40 | + val tmpDir = node_fs.mkdtempSync(node_path.join(node_os.tmpdir(), "appimage2deb-")); |
| 41 | + tmpDir |
| 42 | + } catch { |
| 43 | + case e: Exception => |
| 44 | + throw Exception(s"Couldn't create a new temporary directory", e) |
| 45 | + } |
| 46 | + |
| 47 | + def chmod(filePath: String, mode: Int): Unit = |
| 48 | + try { |
| 49 | + node_fs.chmodSync(filePath, mode.toDouble); |
| 50 | + } catch { |
| 51 | + case e: Exception => |
| 52 | + throw Exception(s"Couldn't chmod ${filePath} on ${filePath}") |
| 53 | + } |
| 54 | + |
| 55 | + def filesInDir(dirPath: String): Array[String] = |
| 56 | + val res = node_fs.readdirSync(dirPath) |
| 57 | + Interop.toScalaArray(res) |
| 58 | + |
| 59 | + def filesInDirRecursive(dirPath: String): List[String] = |
| 60 | + val filesInDirectory = node_fs.readdirSync(dirPath); |
| 61 | + val a1 = |
| 62 | + filesInDirectory |
| 63 | + .flatMap(file => |
| 64 | + val nextPath = node_path.resolve(dirPath, file); |
| 65 | + val stats = node_fs.statSync(nextPath).getOrElse(scala.sys.error(s"Couldn't read directory ${nextPath}")) |
| 66 | + if (stats.isDirectory()) { |
| 67 | + filesInDirRecursive(nextPath); |
| 68 | + } else { |
| 69 | + List(nextPath); |
| 70 | + } |
| 71 | + ) |
| 72 | + .toList |
| 73 | + a1 |
| 74 | + |
| 75 | + // private def filesInDirRecursiveAux(directory: String, collected: ArrayBuffer[String]): Unit = |
| 76 | + // val filesInDirectory = node_fs.readdirSync(directory); |
| 77 | + // filesInDirectory.foreach(file => |
| 78 | + // val absolute = node_path.join(directory, file); |
| 79 | + // if (node_fs.statSync(absolute).isDirectory()) { |
| 80 | + // filesInDirRecursiveAux(absolute, collected); |
| 81 | + // } else { |
| 82 | + // collected.push(absolute); |
| 83 | + // } |
| 84 | + // ) |
| 85 | + |
| 86 | + def desktopFileInDir(dirPath: String): Option[String] = |
| 87 | + filesInDir(dirPath) |
| 88 | + .filter(path => path.endsWith(".desktop")) |
| 89 | + .map(node_path.resolve(dirPath, _)) |
| 90 | + .headOption |
| 91 | + |
| 92 | + def appIconInDir(dirPath: String): Option[String] = |
| 93 | + try { |
| 94 | + val linkVal = node_fs.readlinkSync(node_path.resolve(dirPath, ".DirIcon")) |
| 95 | + Some(node_path.resolve(dirPath, linkVal)) |
| 96 | + } catch { |
| 97 | + case e: Exception => |
| 98 | + filesInDir(dirPath) |
| 99 | + .filter(path => path.endsWith(".png") || path.endsWith(".svg")) |
| 100 | + .map(node_path.resolve(dirPath, _)) |
| 101 | + .headOption |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments