diff --git a/core/server/adapters/storage/LocalFileStorage.js b/core/server/adapters/storage/LocalFileStorage.js index 8baef307b5..f8b8ae7ae7 100644 --- a/core/server/adapters/storage/LocalFileStorage.js +++ b/core/server/adapters/storage/LocalFileStorage.js @@ -39,9 +39,9 @@ class LocalFileStore extends StorageBase { return this.getUniqueFileName(image, targetDir).then(function (filename) { targetFilename = filename; - return Promise.promisify(fs.mkdirs)(targetDir); + return fs.mkdirs(targetDir); }).then(function () { - return Promise.promisify(fs.copy)(image.path, targetFilename); + return fs.copy(image.path, targetFilename); }).then(function () { // The src for the image must be in URI format, not a file system path, which in Windows uses \ // For local file system storage can use relative path so add a slash @@ -60,12 +60,13 @@ class LocalFileStore extends StorageBase { exists(fileName, targetDir) { var filePath = path.join(targetDir || this.storagePath, fileName); - return new Promise(function (resolve) { - fs.stat(filePath, function (err) { - var exists = !err; - resolve(exists); + return fs.stat(filePath) + .then(function () { + return true; + }) + .catch(function () { + return false; }); - }); } /** diff --git a/core/server/api/db.js b/core/server/api/db.js index 5b6a2548ff..8c99e0aaa3 100644 --- a/core/server/api/db.js +++ b/core/server/api/db.js @@ -38,7 +38,7 @@ db = { .then(function successMessage(exportResult) { var filename = path.resolve(urlService.utils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename)); - return Promise.promisify(fs.writeFile)(filename, JSON.stringify(exportResult.data)) + return fs.writeFile(filename, JSON.stringify(exportResult.data)) .then(function () { return filename; }); diff --git a/core/server/api/redirects.js b/core/server/api/redirects.js index 570675ac77..5e45fb750b 100644 --- a/core/server/api/redirects.js +++ b/core/server/api/redirects.js @@ -16,7 +16,7 @@ let redirectsAPI, _private.readRedirectsFile = function readRedirectsFile(customRedirectsPath) { let redirectsPath = customRedirectsPath || path.join(config.getContentPath('data'), 'redirects.json'); - return Promise.promisify(fs.readFile)(redirectsPath, 'utf-8') + return fs.readFile(redirectsPath, 'utf-8') .then(function serveContent(content) { try { content = JSON.parse(content); @@ -56,29 +56,29 @@ redirectsAPI = { return apiUtils.handlePermissions('redirects', 'upload')(options) .then(function backupOldRedirectsFile() { - return Promise.promisify(fs.pathExists)(redirectsPath) + return fs.pathExists(redirectsPath) .then(function (exists) { if (!exists) { return null; } - return Promise.promisify(fs.pathExists)(backupRedirectsPath) + return fs.pathExists(backupRedirectsPath) .then(function (exists) { if (!exists) { return null; } - return Promise.promisify(fs.unlink)(backupRedirectsPath); + return fs.unlink(backupRedirectsPath); }) .then(function () { - return Promise.promisify(fs.move)(redirectsPath, backupRedirectsPath); + return fs.move(redirectsPath, backupRedirectsPath); }); }) .then(function overrideFile() { return _private.readRedirectsFile(options.path) .then(function (content) { globalUtils.validateRedirects(content); - return Promise.promisify(fs.writeFile)(redirectsPath, JSON.stringify(content), 'utf-8'); + return fs.writeFile(redirectsPath, JSON.stringify(content), 'utf-8'); }) .then(function () { // CASE: trigger that redirects are getting re-registered diff --git a/core/server/api/subscribers.js b/core/server/api/subscribers.js index e4450e664a..772afbc9d5 100644 --- a/core/server/api/subscribers.js +++ b/core/server/api/subscribers.js @@ -336,7 +336,7 @@ subscribers = { }; }).finally(function () { // Remove uploaded file from tmp location - return Promise.promisify(fs.unlink)(filePath); + return fs.unlink(filePath); }); } diff --git a/core/server/api/themes.js b/core/server/api/themes.js index 9c701dbce6..060cb44c37 100644 --- a/core/server/api/themes.js +++ b/core/server/api/themes.js @@ -143,7 +143,7 @@ themes = { // @TODO we should probably do this as part of saving the theme // remove zip upload from multer // happens in background - Promise.promisify(fs.remove)(zip.path) + fs.remove(zip.path) .catch(function (err) { common.logging.error(new common.errors.GhostError({err: err})); }); @@ -152,7 +152,7 @@ themes = { // remove extracted dir from gscan // happens in background if (checkedTheme) { - Promise.promisify(fs.remove)(checkedTheme.path) + fs.remove(checkedTheme.path) .catch(function (err) { common.logging.error(new common.errors.GhostError({err: err})); }); diff --git a/core/server/api/upload.js b/core/server/api/upload.js index 8e200da385..eeb9e5becc 100644 --- a/core/server/api/upload.js +++ b/core/server/api/upload.js @@ -1,6 +1,5 @@ var Promise = require('bluebird'), fs = require('fs-extra'), - pUnlink = Promise.promisify(fs.unlink), storage = require('../adapters/storage'), upload; @@ -23,7 +22,7 @@ upload = { return store.save(options).finally(function () { // Remove uploaded file from tmp location - return pUnlink(options.path); + return fs.unlink(options.path); }); }) }; diff --git a/core/server/data/db/backup.js b/core/server/data/db/backup.js index 4a552c38cc..8a377c5033 100644 --- a/core/server/data/db/backup.js +++ b/core/server/data/db/backup.js @@ -14,7 +14,7 @@ var fs = require('fs-extra'), writeExportFile = function writeExportFile(exportResult) { var filename = path.resolve(urlService.utils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename)); - return Promise.promisify(fs.writeFile)(filename, JSON.stringify(exportResult.data)).return(filename); + return fs.writeFile(filename, JSON.stringify(exportResult.data)).return(filename); }; /** diff --git a/core/server/data/importer/handlers/json.js b/core/server/data/importer/handlers/json.js index 685f6dc010..321d12c7f1 100644 --- a/core/server/data/importer/handlers/json.js +++ b/core/server/data/importer/handlers/json.js @@ -14,8 +14,9 @@ JSONHandler = { // @TODO: Handle multiple JSON files var filePath = files[0].path; - return Promise.promisify(fs.readFile)(filePath).then(function (fileData) { + return fs.readFile(filePath).then(function (fileData) { var importData; + try { importData = JSON.parse(fileData); diff --git a/core/server/data/importer/handlers/markdown.js b/core/server/data/importer/handlers/markdown.js index 5dd09dfdda..e694bfcb77 100644 --- a/core/server/data/importer/handlers/markdown.js +++ b/core/server/data/importer/handlers/markdown.js @@ -1,7 +1,7 @@ -var _ = require('lodash'), +var _ = require('lodash'), Promise = require('bluebird'), - fs = require('fs-extra'), - moment = require('moment'), + fs = require('fs-extra'), + moment = require('moment'), featuredImageRegex = /^(!\[]\(([^)]*?)\)\s+)(?=#)/, titleRegex = /^#\s?([\w\W]*?)(?=\n)/, @@ -92,7 +92,7 @@ MarkdownHandler = { ops = []; _.each(files, function (file) { - ops.push(Promise.promisify(fs.readFile)(file.path).then(function (content) { + ops.push(fs.readFile(file.path).then(function (content) { // normalize the file name file.name = file.name.replace(startDirRegex, ''); // don't include deleted posts diff --git a/core/server/mail/utils.js b/core/server/mail/utils.js index abe7d39f11..b9ca374ff4 100644 --- a/core/server/mail/utils.js +++ b/core/server/mail/utils.js @@ -1,6 +1,5 @@ var _ = require('lodash').runInContext(), fs = require('fs-extra'), - Promise = require('bluebird'), path = require('path'), htmlToText = require('html-to-text'), urlService = require('../services/url'), @@ -19,7 +18,7 @@ exports.generateContent = function generateContent(options) { data = _.defaults(defaults, options.data); // read the proper email body template - return Promise.promisify(fs.readFile)(path.join(templatesDir, options.template + '.html'), 'utf8') + return fs.readFile(path.join(templatesDir, options.template + '.html'), 'utf8') .then(function (content) { var compiled, htmlContent, diff --git a/core/server/themes/Storage.js b/core/server/themes/Storage.js index cb99eaff59..01a668ec72 100644 --- a/core/server/themes/Storage.js +++ b/core/server/themes/Storage.js @@ -6,8 +6,7 @@ var fs = require('fs-extra'), Promise = require('bluebird'), config = require('../config'), globalUtils = require('../utils'), - LocalFileStorage = require('../adapters/storage/LocalFileStorage'), - remove = Promise.promisify(fs.remove); + LocalFileStorage = require('../adapters/storage/LocalFileStorage'); /** * @TODO: combine with loader.js? @@ -35,7 +34,7 @@ class ThemeStorage extends LocalFileStorage { zipPath = path.join(zipBasePath, zipName), stream; - Promise.promisify(fs.ensureDir)(zipBasePath) + fs.ensureDir(zipBasePath) .then(function () { return Promise.promisify(globalUtils.zipFolder)(themePath, zipPath); }) @@ -53,13 +52,13 @@ class ThemeStorage extends LocalFileStorage { next(err); }) .finally(function () { - remove(zipBasePath); + return fs.remove(zipBasePath); }); }; } delete(fileName) { - return remove(path.join(this.storagePath, fileName)); + return fs.remove(path.join(this.storagePath, fileName)); } } diff --git a/core/server/utils/packages/parse-package-json.js b/core/server/utils/packages/parse-package-json.js index 44069fd0dc..a4f0d5ffce 100644 --- a/core/server/utils/packages/parse-package-json.js +++ b/core/server/utils/packages/parse-package-json.js @@ -4,8 +4,7 @@ var Promise = require('bluebird'), fs = require('fs-extra'), - common = require('../../lib/common'), - readFile = Promise.promisify(fs.readFile); + common = require('../../lib/common'); /** * Parse package.json and validate it has @@ -13,7 +12,7 @@ var Promise = require('bluebird'), */ function parsePackageJson(path) { - return readFile(path) + return fs.readFile(path) .catch(function () { var err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); err.context = path; diff --git a/core/server/utils/packages/read-packages.js b/core/server/utils/packages/read-packages.js index 738b41c66e..a189cfa6fe 100644 --- a/core/server/utils/packages/read-packages.js +++ b/core/server/utils/packages/read-packages.js @@ -11,9 +11,6 @@ var Promise = require('bluebird'), notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i, packageJSONPath = 'package.json', - statFile = Promise.promisify(fs.stat), - readDir = Promise.promisify(fs.readdir), - readPackage, readPackages, processPackage; @@ -43,7 +40,7 @@ processPackage = function processPackage(absolutePath, packageName) { readPackage = function readPackage(packagePath, packageName) { var absolutePath = join(packagePath, packageName); - return statFile(absolutePath) + return fs.stat(absolutePath) .then(function (stat) { if (!stat.isDirectory()) { return {}; @@ -67,14 +64,14 @@ readPackage = function readPackage(packagePath, packageName) { }; readPackages = function readPackages(packagePath) { - return readDir(packagePath) + return fs.readdir(packagePath) .filter(function (packageName) { // Filter out things which are not packages by regex if (packageName.match(notAPackageRegex)) { return; } // Check the remaining items to ensure they are a directory - return statFile(join(packagePath, packageName)).then(function (stat) { + return fs.stat(join(packagePath, packageName)).then(function (stat) { return stat.isDirectory(); }); }) diff --git a/core/test/functional/routes/api/db_spec.js b/core/test/functional/routes/api/db_spec.js index 21d0a7ffc2..c8de36b060 100644 --- a/core/test/functional/routes/api/db_spec.js +++ b/core/test/functional/routes/api/db_spec.js @@ -1,5 +1,6 @@ var should = require('should'), supertest = require('supertest'), + Promise = require('bluebird'), testUtils = require('../../../utils'), path = require('path'), sinon = require('sinon'), @@ -107,7 +108,7 @@ describe('DB API', function () { it('export can be triggered by backup client', function (done) { backupQuery = '?client_id=' + backupClient.slug + '&client_secret=' + backupClient.secret; - fsStub = sandbox.stub(fs, 'writeFile').yields(); + fsStub = sandbox.stub(fs, 'writeFile').resolves(); request.post(testUtils.API.getApiQuery('db/backup' + backupQuery)) .expect('Content-Type', /json/) .expect(200) @@ -124,7 +125,7 @@ describe('DB API', function () { it('export can be triggered by backup client', function (done) { schedulerQuery = '?client_id=' + schedulerClient.slug + '&client_secret=' + schedulerClient.secret; - fsStub = sandbox.stub(fs, 'writeFile').yields(); + fsStub = sandbox.stub(fs, 'writeFile').resolves(); request.post(testUtils.API.getApiQuery('db/backup' + schedulerQuery)) .expect('Content-Type', /json/) .expect(403) diff --git a/core/test/integration/api/api_subscribers_spec.js b/core/test/integration/api/api_subscribers_spec.js index 84af30ee8f..65123c879d 100644 --- a/core/test/integration/api/api_subscribers_spec.js +++ b/core/test/integration/api/api_subscribers_spec.js @@ -279,9 +279,7 @@ describe('Subscribers API', function () { stub; beforeEach(function () { - sandbox.stub(fs, 'unlink').callsFake(function (path, cb) { - cb(); - }); + sandbox.stub(fs, 'unlink').resolves(); sandbox.stub(apiUtils, 'checkFileExists').returns(true); stub = sandbox.stub(apiUtils, 'checkFileIsValid').returns(true); sandbox.stub(globalUtils, 'readCSV').callsFake(function () { diff --git a/core/test/unit/adapters/storage/LocalFileStorage_spec.js b/core/test/unit/adapters/storage/LocalFileStorage_spec.js index 80d7f0bab5..94566a1d91 100644 --- a/core/test/unit/adapters/storage/LocalFileStorage_spec.js +++ b/core/test/unit/adapters/storage/LocalFileStorage_spec.js @@ -2,6 +2,7 @@ var should = require('should'), // jshint ignore:line sinon = require('sinon'), fs = require('fs-extra'), moment = require('moment'), + Promise = require('bluebird'), path = require('path'), common = require('../../../../server/lib/common'), LocalFileStore = require('../../../../server/adapters/storage/LocalFileStorage'), @@ -39,10 +40,10 @@ describe('Local File System Storage', function () { }); beforeEach(function () { - sandbox.stub(fs, 'mkdirs').yields(); - sandbox.stub(fs, 'copy').yields(); - sandbox.stub(fs, 'stat').yields(true); - sandbox.stub(fs, 'unlink').yields(); + sandbox.stub(fs, 'mkdirs').resolves(); + sandbox.stub(fs, 'copy').resolves(); + sandbox.stub(fs, 'stat').rejects(); + sandbox.stub(fs, 'unlink').resolves(); image = { path: 'tmp/123456.jpg', @@ -111,13 +112,13 @@ describe('Local File System Storage', function () { }); it('can upload two different images with the same name without overwriting the first', function (done) { - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE.jpg')).yields(false); - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-1.jpg')).yields(true); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE.jpg')).resolves(); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-1.jpg')).rejects(); // if on windows need to setup with back slashes // doesn't hurt for the test to cope with both - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE.jpg')).yields(false); - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-1.jpg')).yields(true); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE.jpg')).resolves(); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-1.jpg')).rejects(); localFileStore.save(image).then(function (url) { url.should.equal('/content/images/2013/09/IMAGE-1.jpg'); @@ -127,18 +128,18 @@ describe('Local File System Storage', function () { }); it('can upload five different images with the same name without overwriting the first', function (done) { - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE.jpg')).yields(false); - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-1.jpg')).yields(false); - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-2.jpg')).yields(false); - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-3.jpg')).yields(false); - fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-4.jpg')).yields(true); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE.jpg')).resolves(); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-1.jpg')).resolves(); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-2.jpg')).resolves(); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-3.jpg')).resolves(); + fs.stat.withArgs(path.resolve('./content/images/2013/09/IMAGE-4.jpg')).rejects(); // windows setup - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE.jpg')).yields(false); - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-1.jpg')).yields(false); - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-2.jpg')).yields(false); - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-3.jpg')).yields(false); - fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-4.jpg')).yields(true); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE.jpg')).resolves(); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-1.jpg')).resolves(); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-2.jpg')).resolves(); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-3.jpg')).resolves(); + fs.stat.withArgs(path.resolve('.\\content\\images\\2013\\Sep\\IMAGE-4.jpg')).rejects(); localFileStore.save(image).then(function (url) { url.should.equal('/content/images/2013/09/IMAGE-4.jpg'); diff --git a/core/test/unit/migration_spec.js b/core/test/unit/migration_spec.js index 51a2b0903c..e0abaf73f0 100644 --- a/core/test/unit/migration_spec.js +++ b/core/test/unit/migration_spec.js @@ -65,9 +65,9 @@ describe('Migrations', function () { var exportStub, filenameStub, fsStub; beforeEach(function () { - exportStub = sandbox.stub(exporter, 'doExport').returns(new Promise.resolve()); - filenameStub = sandbox.stub(exporter, 'fileName').returns(new Promise.resolve('test')); - fsStub = sandbox.stub(fs, 'writeFile').yields(); + exportStub = sandbox.stub(exporter, 'doExport').resolves(); + filenameStub = sandbox.stub(exporter, 'fileName').resolves('test'); + fsStub = sandbox.stub(fs, 'writeFile').resolves(); }); it('should create a backup JSON file', function (done) { diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 35f8151c1f..6467e0acfe 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -344,10 +344,9 @@ fixtures = { loadExportFixture: function loadExportFixture(filename, options) { options = options || {lts: false}; - var filePath = this.getExportFixturePath(filename, options), - readFile = Promise.promisify(fs.readFile); + var filePath = this.getExportFixturePath(filename, options); - return readFile(filePath).then(function (fileContents) { + return fs.readFile(filePath).then(function (fileContents) { var data; // Parse the json data