1- import fs from "fs"
2- import fse from "fs-extra"
1+ import {
2+ chmodSync ,
3+ ensureDirSync ,
4+ existsSync ,
5+ moveSync ,
6+ readFileSync ,
7+ statSync ,
8+ unlinkSync ,
9+ writeFileSync ,
10+ } from "fs-extra"
311import { dirname , join , relative , resolve } from "path"
4- import { ParsedPatchFile , FilePatch , Hunk } from "./parse"
512import { assertNever } from "../assertNever"
13+ import { FilePatch , Hunk , ParsedPatchFile } from "./parse"
614
715export const executeEffects = (
816 effects : ParsedPatchFile ,
@@ -19,7 +27,7 @@ export const executeEffects = (
1927 switch ( eff . type ) {
2028 case "file deletion" :
2129 if ( dryRun ) {
22- if ( ! fs . existsSync ( inCwd ( eff . path ) ) ) {
30+ if ( ! existsSync ( inCwd ( eff . path ) ) ) {
2331 throw new Error (
2432 "Trying to delete file that doesn't exist: " +
2533 humanReadable ( eff . path ) ,
@@ -28,7 +36,7 @@ export const executeEffects = (
2836 } else {
2937 // TODO: integrity checks
3038 try {
31- fs . unlinkSync ( inCwd ( eff . path ) )
39+ unlinkSync ( inCwd ( eff . path ) )
3240 } catch ( e ) {
3341 if ( bestEffort ) {
3442 errors ?. push ( `Failed to delete file ${ eff . path } ` )
@@ -41,15 +49,15 @@ export const executeEffects = (
4149 case "rename" :
4250 if ( dryRun ) {
4351 // TODO: see what patch files look like if moving to exising path
44- if ( ! fs . existsSync ( inCwd ( eff . fromPath ) ) ) {
52+ if ( ! existsSync ( inCwd ( eff . fromPath ) ) ) {
4553 throw new Error (
4654 "Trying to move file that doesn't exist: " +
4755 humanReadable ( eff . fromPath ) ,
4856 )
4957 }
5058 } else {
5159 try {
52- fse . moveSync ( inCwd ( eff . fromPath ) , inCwd ( eff . toPath ) )
60+ moveSync ( inCwd ( eff . fromPath ) , inCwd ( eff . toPath ) )
5361 } catch ( e ) {
5462 if ( bestEffort ) {
5563 errors ?. push (
@@ -63,7 +71,7 @@ export const executeEffects = (
6371 break
6472 case "file creation" :
6573 if ( dryRun ) {
66- if ( fs . existsSync ( inCwd ( eff . path ) ) ) {
74+ if ( existsSync ( inCwd ( eff . path ) ) ) {
6775 throw new Error (
6876 "Trying to create file that already exists: " +
6977 humanReadable ( eff . path ) ,
@@ -77,8 +85,8 @@ export const executeEffects = (
7785 : ""
7886 const path = inCwd ( eff . path )
7987 try {
80- fse . ensureDirSync ( dirname ( path ) )
81- fs . writeFileSync ( path , fileContents , { mode : eff . mode } )
88+ ensureDirSync ( dirname ( path ) )
89+ writeFileSync ( path , fileContents , { mode : eff . mode } )
8290 } catch ( e ) {
8391 if ( bestEffort ) {
8492 errors ?. push ( `Failed to create new file ${ eff . path } ` )
@@ -92,7 +100,7 @@ export const executeEffects = (
92100 applyPatch ( eff , { dryRun, cwd, bestEffort, errors } )
93101 break
94102 case "mode change" :
95- const currentMode = fs . statSync ( inCwd ( eff . path ) ) . mode
103+ const currentMode = statSync ( inCwd ( eff . path ) ) . mode
96104 if (
97105 ( ( isExecutable ( eff . newMode ) && isExecutable ( currentMode ) ) ||
98106 ( ! isExecutable ( eff . newMode ) && ! isExecutable ( currentMode ) ) ) &&
@@ -102,7 +110,7 @@ export const executeEffects = (
102110 `Mode change is not required for file ${ humanReadable ( eff . path ) } ` ,
103111 )
104112 }
105- fs . chmodSync ( inCwd ( eff . path ) , eff . newMode )
113+ chmodSync ( inCwd ( eff . path ) , eff . newMode )
106114 break
107115 default :
108116 assertNever ( eff )
@@ -153,8 +161,8 @@ function applyPatch(
153161) : void {
154162 path = cwd ? resolve ( cwd , path ) : path
155163 // modifying the file in place
156- const fileContents = fs . readFileSync ( path ) . toString ( )
157- const mode = fs . statSync ( path ) . mode
164+ const fileContents = readFileSync ( path ) . toString ( )
165+ const mode = statSync ( path ) . mode
158166
159167 const fileLines : string [ ] = fileContents . split ( / \n / )
160168
@@ -220,7 +228,7 @@ function applyPatch(
220228 }
221229
222230 try {
223- fs . writeFileSync ( path , fileLines . join ( "\n" ) , { mode } )
231+ writeFileSync ( path , fileLines . join ( "\n" ) , { mode } )
224232 } catch ( e ) {
225233 if ( bestEffort ) {
226234 errors ?. push ( `Failed to write file ${ path } ` )
0 commit comments