11import crypto from "node:crypto" ;
2- import { homedir } from "node:os" ;
2+ import * as os from "node:os" ;
33import path from "node:path" ;
44import fsPromise from "node:fs/promises" ;
55import fs from "node:fs" ;
6- import Logger from "../logger" ;
6+ import child_process from "node:child_process" ;
7+ import { promisify } from "node:util" ;
8+ const exec = promisify ( child_process . exec ) ;
79
810export const checkFileOrFolderExists = async ( pathToCheck : string ) => {
911 try {
@@ -15,7 +17,7 @@ export const checkFileOrFolderExists = async (pathToCheck: string) => {
1517} ;
1618
1719export const getSaveFolder = async ( ) => {
18- const pathSaveFolder = path . join ( homedir ( ) , ".firecoder" ) ;
20+ const pathSaveFolder = path . join ( os . homedir ( ) , ".firecoder" ) ;
1921
2022 const folderIsExist = await checkFileOrFolderExists ( pathSaveFolder ) ;
2123
@@ -27,7 +29,33 @@ export const getSaveFolder = async () => {
2729} ;
2830
2931export const getChecksum = async ( path : string ) => {
30- // TODO: Use sha256sum
32+ const osplatform = os . platform ( ) ;
33+ const isLinux = osplatform === "linux" ;
34+ const isWindows = osplatform === "win32" ;
35+ try {
36+ if ( isLinux ) {
37+ const hashOutput = await exec ( `sha256sum ${ path } | awk '{ print $1 }'` ) ;
38+
39+ return hashOutput . stdout . trim ( ) ;
40+ }
41+
42+ if ( isWindows ) {
43+ const hashOutput = await exec (
44+ `$(CertUtil -hashfile ${ path } SHA256)[1] -replace " ",""` ,
45+ { shell : "powershell.exe" }
46+ ) ;
47+
48+ return hashOutput . stdout . replace ( "\r\n" , "" ) . trim ( ) ;
49+ }
50+
51+ // fallback to default method
52+ return await getCheckSumNode ( path ) ;
53+ } catch ( error ) {
54+ return await getCheckSumNode ( path ) ;
55+ }
56+ } ;
57+
58+ const getCheckSumNode = async ( path : string ) => {
3159 return await new Promise ( ( res , rej ) => {
3260 try {
3361 const fsStream = fs . createReadStream ( path ) ;
0 commit comments