Browse Source

Fix #19, better to check if a file exist before trying to copy it 😂

Godzil 7 years ago
parent
commit
ed4f398062
1 changed files with 19 additions and 1 deletions
  1. 19 1
      src/series.ts

+ 19 - 1
src/series.ts

@@ -9,6 +9,21 @@ import url = require('url');
 import log  = require('./log');
 const persistent = '.crpersistent';
 
+/**
+ * Check if a file exist..
+ */
+function fileExist(path: string)
+{
+  try
+  {
+    fs.statSync(path);
+    return true;
+  } catch (e)
+  {
+    return false;
+  }
+}
+
 /**
  * Streams the series to disk.
  */
@@ -17,7 +32,10 @@ export default function(config: IConfig, address: string, done: (err: Error) =>
   const persistentPath = path.join(config.output || process.cwd(), persistent);
 
   /* Make a backup of the persistent file in case of */
-  fse.copySync(persistentPath, persistentPath + '.backup');
+  if (fileExist(persistentPath))
+  {
+    fse.copySync(persistentPath, persistentPath + '.backup');
+  }
 
   fs.readFile(persistentPath, 'utf8', (err: Error, contents: string) =>
   {