Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit ff460e9

Browse files
stalniyPatrickJS
authored andcommitted
refactor(file): adds isFileExists function (#14)
In order to get rid of code duplication
1 parent 80ed46e commit ff460e9

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

index.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -147,32 +147,24 @@ function Angular2ConventionsLoader(source, sourcemap) {
147147
var _hasHtmlFile;
148148
// if selector and filename are the same
149149
if (hasSameFileSelector) {
150-
try {
151-
_hasHtmlFile = fs.statSync(path.join(self.context, lastFileName + htmlExtension));
152-
} catch(e) {}
150+
_hasHtmlFile = isFileExist(path.join(self.context, lastFileName + htmlExtension))
153151
} else {
154-
try {
155-
_hasHtmlFile = fs.statSync(path.join(self.context, relativePathStart + __selector + htmlExtension));
156-
} catch(e) {
157-
metadata = 'template: "",' + metadata;
158-
}
152+
_hasHtmlFile = isFileExist(path.join(self.context, relativePathStart + __selector + htmlExtension))
159153
}
160154
// set file in metadata
161155
if (_hasHtmlFile) {
162156
metadata = 'template: require("' + relativePathStart + lastFileName + htmlExtension + '"),\n' + metadata;
157+
} else {
158+
metadata = 'template: "",' + metadata;
163159
}
164160
}
165161
// do the same for styles
166162
if (decorator === 'Component' && !(/styles\s*:(\s*\[[\s\S]*?\])/g.test(metadata))) {
167163
var _hasCssFile;
168164
if (hasSameFileSelector) {
169-
try {
170-
_hasCssFile = fs.statSync(path.join(self.context, lastFileName + cssExtension));
171-
} catch(e) {}
165+
_hasCssFile = isFileExist(path.join(self.context, lastFileName + cssExtension))
172166
} else {
173-
try {
174-
_hasCssFile = fs.statSync(path.join(self.context, relativePathStart + __selector + cssExtension));
175-
} catch(e) {}
167+
_hasCssFile = isFileExist(path.join(self.context, relativePathStart + __selector + cssExtension));
176168
}
177169

178170
if (_hasCssFile) {
@@ -195,4 +187,12 @@ function Angular2ConventionsLoader(source, sourcemap) {
195187
};
196188
Angular2ConventionsLoader.default = Angular2ConventionsLoader;
197189

190+
function isFileExist(path) {
191+
try {
192+
return !!fs.statSync(path);
193+
} catch (e) {
194+
return false;
195+
}
196+
}
197+
198198
module.exports = Angular2ConventionsLoader

0 commit comments

Comments
 (0)