@@ -4,14 +4,42 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
} ;
5
5
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6
6
const path_1 = __importDefault ( require ( "path" ) ) ;
7
+ const child_process_1 = __importDefault ( require ( "child_process" ) ) ;
8
+ const OperatingSystemException_1 = require ( "./Exception/OperatingSystemException" ) ;
7
9
const ImageSqueezerCommonException_1 = require ( "./Exception/ImageSqueezerCommonException" ) ;
8
10
class ImageSqueezerCommon {
9
11
constructor ( ) {
12
+ this . operatingSystem = '' ;
10
13
this . subClassType = '' ;
11
14
this . bin = '' ;
12
15
this . sourceFilePath = '' ;
13
16
this . outputFilePath = '' ;
14
17
this . isAllowedEmptyOutputFilePath = false ;
18
+ this . commandStatement = '' ;
19
+ this . isExecuteChildProcess = true ;
20
+ }
21
+ load ( ) {
22
+ this . verifySupportedOperatingSystem ( ) ;
23
+ }
24
+ verifySupportedOperatingSystem ( ) {
25
+ switch ( this . getOperatingSystem ( ) ) {
26
+ case ImageSqueezerCommon . WINDOWS_OS :
27
+ case ImageSqueezerCommon . LINUX_OS :
28
+ case ImageSqueezerCommon . UNIX_OS :
29
+ break ;
30
+ case ImageSqueezerCommon . MACOSX_OS :
31
+ default :
32
+ throw OperatingSystemException_1 . OperatingSystemException . isNotSupported ( ) ;
33
+ }
34
+ }
35
+ setOperatingSystem ( operatingSystem ) {
36
+ this . operatingSystem = operatingSystem ;
37
+ }
38
+ getOperatingSystem ( ) {
39
+ if ( this . operatingSystem ) {
40
+ return this . operatingSystem ;
41
+ }
42
+ return process . platform ;
15
43
}
16
44
setSubClassType ( subClassType ) {
17
45
this . subClassType = subClassType ;
@@ -28,6 +56,28 @@ class ImageSqueezerCommon {
28
56
allowEmptyOutputFilePath ( ) {
29
57
this . isAllowedEmptyOutputFilePath = true ;
30
58
}
59
+ setCommandStatement ( commandStatement ) {
60
+ this . commandStatement = commandStatement ;
61
+ }
62
+ getCommandStatement ( ) {
63
+ return this . commandStatement ;
64
+ }
65
+ disableChildProcessExecution ( ) {
66
+ this . isExecuteChildProcess = false ;
67
+ }
68
+ build ( ) {
69
+ this . transferSouceFilePathToOutputFilePath ( ) ;
70
+ this . validateRequiredProperties ( ) ;
71
+ this . setCommandStatement ( this . command ( ) ) ;
72
+ }
73
+ compress ( ) {
74
+ return this . executeChildProcess ( ) ;
75
+ }
76
+ transferSouceFilePathToOutputFilePath ( ) {
77
+ if ( this . isAllowedEmptyOutputFilePath ) {
78
+ this . outputFilePath = this . sourceFilePath ;
79
+ }
80
+ }
31
81
validateRequiredProperties ( ) {
32
82
if ( ! this . sourceFilePath ) {
33
83
throw ImageSqueezerCommonException_1 . ImageSqueezerCommonException . emptySourceFilePath ( ) ;
@@ -36,11 +86,6 @@ class ImageSqueezerCommon {
36
86
throw ImageSqueezerCommonException_1 . ImageSqueezerCommonException . emptyOutputFilePath ( ) ;
37
87
}
38
88
}
39
- transferSouceFilePathToOutputFilePath ( ) {
40
- if ( this . isAllowedEmptyOutputFilePath ) {
41
- this . outputFilePath = this . sourceFilePath ;
42
- }
43
- }
44
89
handleOutputFilePath ( ) {
45
90
if ( this . isAllowedEmptyOutputFilePath ) {
46
91
return this . generateTemporaryOutputFilePath ( ) ;
@@ -54,11 +99,45 @@ class ImageSqueezerCommon {
54
99
let splittedFilename = filename . split ( '.' ) ;
55
100
let newFilename = splittedFilename [ 0 ] + '-compressed-' + this . subClassType + '.' + splittedFilename [ 1 ] ;
56
101
let newBasename = this . escapeShellArg ( this . outputFilePath . replace ( filename , newFilename ) ) ;
57
- return newBasename + ' && mv ' +
58
- newBasename + ' ' + this . escapeShellArg ( this . outputFilePath ) ;
102
+ return newBasename + this . renameCommandWithCompatibilityChecking ( newBasename ) ;
103
+ }
104
+ renameCommandWithCompatibilityChecking ( newBasename ) {
105
+ let cmd = '' ;
106
+ switch ( this . getOperatingSystem ( ) ) {
107
+ case ImageSqueezerCommon . WINDOWS_OS :
108
+ cmd = ' && move -y ' + newBasename + ' ' + this . escapeShellArg ( this . outputFilePath ) ;
109
+ break ;
110
+ case ImageSqueezerCommon . LINUX_OS :
111
+ case ImageSqueezerCommon . UNIX_OS :
112
+ cmd = ' && mv ' + newBasename + ' ' + this . escapeShellArg ( this . outputFilePath ) ;
113
+ break ;
114
+ }
115
+ return cmd ;
59
116
}
60
117
escapeShellArg ( arg ) {
61
118
return `'${ arg . replace ( / ' / g, `'\\''` ) } '` ;
62
119
}
120
+ executeChildProcess ( ) {
121
+ if ( ! this . isExecuteChildProcess ) {
122
+ return Promise . reject ( ImageSqueezerCommon . DISABLED_CHILD_PROC_MSG ) ;
123
+ }
124
+ return new Promise ( ( resolve , reject ) => {
125
+ child_process_1 . default . exec ( this . commandStatement , ( error ) => {
126
+ ( error ? reject ( error ) : resolve ( true ) ) ;
127
+ } ) ;
128
+ } ) ;
129
+ }
130
+ /**
131
+ * This is a abstract or no-op class method.
132
+ * The subclass is expected to override this method.
133
+ */
134
+ command ( ) {
135
+ return '' ;
136
+ }
63
137
}
138
+ ImageSqueezerCommon . WINDOWS_OS = 'win32' ;
139
+ ImageSqueezerCommon . LINUX_OS = 'linux' ;
140
+ ImageSqueezerCommon . UNIX_OS = 'freebsd' ;
141
+ ImageSqueezerCommon . MACOSX_OS = 'darwin' ;
142
+ ImageSqueezerCommon . DISABLED_CHILD_PROC_MSG = 'The Child Process Execution was disabled.' ;
64
143
exports . ImageSqueezerCommon = ImageSqueezerCommon ;
0 commit comments