Aurélien Georget bd91a2ef93 Add 'packages/strapi-plugin-settings-manager/' from commit 'cd241c14c6a6239bca279e7accd709ba58e87cc8'
git-subtree-dir: packages/strapi-plugin-settings-manager
git-subtree-mainline: 80aa83d8460c95366547e143c74bf79ea6ae69f8
git-subtree-split: cd241c14c6a6239bca279e7accd709ba58e87cc8
2017-01-15 20:14:40 +01:00

97 lines
2.9 KiB
JavaScript

'use strict';
/**
* Schema languages dependencies
*/
// Public node modules
var _ = require('lodash');
var path = require('path');
// Local services
var SettingService = require('../SettingsService');
// var SocketService = require('../SocketService');
var SchemaLanguages = function(app) {
var schema = {
defaultLocale: {
type: 'string',
path: 'config/i18n.json',
nested: 'i18n',
resolver: function(rootValue, value, scope, cb) {
if (_.includes(_.union(app.config.i18n.locales, rootValue.locales), value)) {
return cb(null, value);
}
return cb('This locale doesn\'t exist', null);
}
},
locales: {
update: false,
type: 'array',
path: 'config/i18n.json',
resolver: function(rootValue, value, scope, cb) {
var arrayOfFiles = [];
var localesToRemove = _.difference(app.config.i18n.locales, value);
var localesToAdd = _.difference(value, _.difference(app.config.i18n.locales, localesToRemove));
var defaultLocale = _.includes(app.config.i18n.locales, rootValue.defaultLocale) ? rootValue.defaultLocale : app.config.i18n.defaultLocale;
SettingService.getFiles(app, [{
path: path.resolve(app.config.appPath, 'config', 'locales', defaultLocale + '.json')}
])
.then(function(files) {
var arrayOfPromises = [];
_.forEach(localesToAdd, function(locale) {
var localePath = path.resolve(app.config.appPath, 'config', 'locales', locale + '.json');
arrayOfPromises.push(SettingService.generateSetting(app, localePath, locale + '.json', files[0].value));
});
// Create new locale based on default locale
return Promise.all(arrayOfPromises);
})
.then(function(files) {
arrayOfFiles = _.union(arrayOfFiles, files);
var arrayOfPathToRemove = [];
_.forEach(localesToRemove, function(locale) {
arrayOfPathToRemove.push({
path: path.resolve(app.config.appPath, 'config', 'locales', locale + '.json')
});
});
// Remove locales from local machine
return SocketService.todo({
action: 'removeFileOrFolder',
from: app.token,
to: app.config.studio.appId,
toRemove: arrayOfPathToRemove
});
})
.then(function() {
return SocketService.zip(app.token, arrayOfFiles);
})
.then(function() {
return SocketService.todo({
from: app.token,
to: app.config.studio.appId,
files: arrayOfFiles
});
})
.then(function() {
cb(null, value);
})
.catch(function(errors) {
cb(errors, null);
});
}
}
};
return schema;
};
module.exports = SchemaLanguages;