2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Use Promise support which was added in fs-extra 3.x

no issue

- now that we use bluebird globally, we can use the promise support from fs-extra
This commit is contained in:
kirrg001 2017-12-13 20:03:07 +01:00 committed by Katharina Irrgang
parent d83f474ff8
commit 485c264c69
18 changed files with 64 additions and 70 deletions

View file

@ -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;
});
});
}
/**

View file

@ -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;
});

View file

@ -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

View file

@ -336,7 +336,7 @@ subscribers = {
};
}).finally(function () {
// Remove uploaded file from tmp location
return Promise.promisify(fs.unlink)(filePath);
return fs.unlink(filePath);
});
}

View file

@ -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}));
});

View file

@ -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);
});
})
};

View file

@ -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);
};
/**

View file

@ -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);

View file

@ -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

View file

@ -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,

View file

@ -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));
}
}

View file

@ -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;

View file

@ -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();
});
})

View file

@ -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)

View file

@ -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 () {

View file

@ -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');

View file

@ -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) {

View file

@ -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