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"
3
11
import { dirname , join , relative , resolve } from "path"
4
- import { ParsedPatchFile , FilePatch , Hunk } from "./parse"
5
12
import { assertNever } from "../assertNever"
13
+ import { FilePatch , Hunk , ParsedPatchFile } from "./parse"
6
14
7
15
export const executeEffects = (
8
16
effects : ParsedPatchFile ,
@@ -19,7 +27,7 @@ export const executeEffects = (
19
27
switch ( eff . type ) {
20
28
case "file deletion" :
21
29
if ( dryRun ) {
22
- if ( ! fs . existsSync ( inCwd ( eff . path ) ) ) {
30
+ if ( ! existsSync ( inCwd ( eff . path ) ) ) {
23
31
throw new Error (
24
32
"Trying to delete file that doesn't exist: " +
25
33
humanReadable ( eff . path ) ,
@@ -28,7 +36,7 @@ export const executeEffects = (
28
36
} else {
29
37
// TODO: integrity checks
30
38
try {
31
- fs . unlinkSync ( inCwd ( eff . path ) )
39
+ unlinkSync ( inCwd ( eff . path ) )
32
40
} catch ( e ) {
33
41
if ( bestEffort ) {
34
42
errors ?. push ( `Failed to delete file ${ eff . path } ` )
@@ -41,15 +49,15 @@ export const executeEffects = (
41
49
case "rename" :
42
50
if ( dryRun ) {
43
51
// 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 ) ) ) {
45
53
throw new Error (
46
54
"Trying to move file that doesn't exist: " +
47
55
humanReadable ( eff . fromPath ) ,
48
56
)
49
57
}
50
58
} else {
51
59
try {
52
- fse . moveSync ( inCwd ( eff . fromPath ) , inCwd ( eff . toPath ) )
60
+ moveSync ( inCwd ( eff . fromPath ) , inCwd ( eff . toPath ) )
53
61
} catch ( e ) {
54
62
if ( bestEffort ) {
55
63
errors ?. push (
@@ -63,7 +71,7 @@ export const executeEffects = (
63
71
break
64
72
case "file creation" :
65
73
if ( dryRun ) {
66
- if ( fs . existsSync ( inCwd ( eff . path ) ) ) {
74
+ if ( existsSync ( inCwd ( eff . path ) ) ) {
67
75
throw new Error (
68
76
"Trying to create file that already exists: " +
69
77
humanReadable ( eff . path ) ,
@@ -77,8 +85,8 @@ export const executeEffects = (
77
85
: ""
78
86
const path = inCwd ( eff . path )
79
87
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 } )
82
90
} catch ( e ) {
83
91
if ( bestEffort ) {
84
92
errors ?. push ( `Failed to create new file ${ eff . path } ` )
@@ -92,7 +100,7 @@ export const executeEffects = (
92
100
applyPatch ( eff , { dryRun, cwd, bestEffort, errors } )
93
101
break
94
102
case "mode change" :
95
- const currentMode = fs . statSync ( inCwd ( eff . path ) ) . mode
103
+ const currentMode = statSync ( inCwd ( eff . path ) ) . mode
96
104
if (
97
105
( ( isExecutable ( eff . newMode ) && isExecutable ( currentMode ) ) ||
98
106
( ! isExecutable ( eff . newMode ) && ! isExecutable ( currentMode ) ) ) &&
@@ -102,7 +110,7 @@ export const executeEffects = (
102
110
`Mode change is not required for file ${ humanReadable ( eff . path ) } ` ,
103
111
)
104
112
}
105
- fs . chmodSync ( inCwd ( eff . path ) , eff . newMode )
113
+ chmodSync ( inCwd ( eff . path ) , eff . newMode )
106
114
break
107
115
default :
108
116
assertNever ( eff )
@@ -153,8 +161,8 @@ function applyPatch(
153
161
) : void {
154
162
path = cwd ? resolve ( cwd , path ) : path
155
163
// 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
158
166
159
167
const fileLines : string [ ] = fileContents . split ( / \n / )
160
168
@@ -220,7 +228,7 @@ function applyPatch(
220
228
}
221
229
222
230
try {
223
- fs . writeFileSync ( path , fileLines . join ( "\n" ) , { mode } )
231
+ writeFileSync ( path , fileLines . join ( "\n" ) , { mode } )
224
232
} catch ( e ) {
225
233
if ( bestEffort ) {
226
234
errors ?. push ( `Failed to write file ${ path } ` )
0 commit comments