Я на самом деле полностью переписал все, способ Glup, и теперь он работает просто отлично!
Я больше не использую gulp-require-tasks
и вот мой код.
gulpfile.js
:
const awspublish = require('gulp-awspublish')
const env = require('gulp-env')
const gulp = require('gulp')
const oneSkyPull = require('./gulp-plugins/onesky-pull')
const rename = require('gulp-rename')
/*
** Pull translations from OneSky to the locale disk, then upload them to an
* Amazon S3 bucket.
*/
gulp.task('i18n-deploy', function() {
env({ file: '.gulp-env.json' }) // Load environment variables
// Prepare the AWS S3 object
var publisher = awspublish.create({
accessKeyId: process.env.AMAZON_S3_ACCESS_KEY_ID,
secretAccessKey: process.env.AMAZON_S3_SECRET_ACCESS_KEY,
region: process.env.AMAZON_S3_REGION,
params: {
Bucket: 'companion-assets'
}
}, {
cacheFileName: 'aws-s3-cache'
})
// Walk through all the base language translation files
// The { base: 'companion' } here is used in order to keep the folder
// strucutre on S3, otherwise the files were uploaded at the root of
// the S3 bucket.
gulp.src('./locales/' + process.env.ONESKY_BASE_LOCALE + '/*.json', { base: 'companion' })
.pipe(oneSkyPull({
baseLocale: process.env.ONESKY_BASE_LOCALE,
locales: ['fr-BE'], // All translation languages
oneskyApiKey: process.env.ONESKY_API_KEY,
oneskySecretKey: process.env.ONESKY_SECRET_KEY,
oneskyProjectId: process.env.ONESKY_PROJECT_ID
})) // Fetch translation files from OneSky
.pipe(rename(function(path) {
// Remove the `../` part from the absolute path avoiding AWS error
path.dirname = path.dirname.replace('../', '')
}))
.pipe(publisher.publish()) // Push to Amazon S3
})
А вот мой файл плагина для OneSky:
const fs = require('fs')
const gutil = require('gulp-util')
const onesky = require('onesky-utils')
const path = require('path')
const through = require('through2')
const PLUGIN_NAME = 'onesky-pull'
// OneSky function to pull the translation file for the given locale
// and given file.
function oneSkyPullFile(file, locale, options) {
console.log('Pulling the file', path.basename(file), 'with locale', locale, '...')
return onesky.getFile({
apiKey: options.oneskyApiKey,
secret: options.oneskySecretKey,
projectId: options.oneskyProjectId,
language: locale,
fileName: path.basename(file)
})
}
// Write the translation files on the disk, creating the folders if needed.
// Returns a gutil.File to be pushed in the stream.
function writeFile(file, baseLocale, locale, content) {
var languageFolder = path.dirname(file)
languageFolder = languageFolder.replace(baseLocale, locale)
if (!fs.existsSync(languageFolder)) {
console.log('Creating the folder', languageFolder)
fs.mkdirSync(languageFolder);
}
const filePath = languageFolder + '/' + path.basename(file)
fs.writeFileSync(filePath, content, 'utf-8');
console.log('File', filePath, 'successfully updated localy for locale', locale, '.')
return new gutil.File({
path: filePath,
contents: new Buffer.from(content)
})
}
// Plugin main functiomn
module.exports = function(options) {
options = options || {}
if (!options.baseLocale)
throw new gutil.PluginError(PLUGIN_NAME, 'please specify the base locale (baseLocale)')
if (!options.locales)
throw new gutil.PluginError(PLUGIN_NAME, 'please specify the locales (locales)')
if (!options.oneskyApiKey)
throw new gutil.PluginError(PLUGIN_NAME, 'please specify your OneSky API Key (oneskyApiKey)')
if (!options.oneskySecretKey)
throw new gutil.PluginError(PLUGIN_NAME, 'please specify your OneSky Secret Key (oneskySecretKey)')
if (!options.oneskyProjectId)
throw new gutil.PluginError(PLUGIN_NAME, 'please specify your OneSky Project ID (oneskyProjectId)')
return through.obj(function (file, encoding, callback) {
if (file.isNull()) {
this.push(file) // Do nothing if no contents
return callback()
}
if (file.isBuffer()) {
for(var locale of options.locales) {
oneSkyPullFile(file.path, locale, options).then(content => {
// OneSky build a JSON file based on the base language file with
// the target locale translations.
// So we replace the base locale with the current one.
content = content.replace(options.baseLocale, locale)
// Write the file on the disk
const translationFile = writeFile(file.path, options.baseLocale, locale, content)
// Add back the vynil file to the stream
this.push(file)
// Add the created translation file to the stream (so that it will be
// available for the next plugins).
this.push(translationFile)
return callback()
}).catch(error => {
if (error.message === 'Invalid source file') {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'The file', file, 'is not present on OneSky'))
} else {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'ERROR:' + error))
}
return callback()
})
}
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'ERROR: Streams are not supported'))
return callback()
}
})
}
При запуске задачи выдается следующий вывод:
[12:15:45] Using gulpfile ~/Developments/companion/gulpfile.js
[12:15:45] Starting 'i18n-deploy'...
[12:15:45] Finished 'i18n-deploy' after 11 ms
Pulling the file screens.login.json with locale fr-BE ...
file /Users/zedtux/Developments/companion/locales/fr-FR/screens.login.json
path.dirname(file) /Users/zedtux/Developments/companion/locales/fr-FR
File /Users/zedtux/Developments/companion/locales/fr-BE/screens.login.json successfully updated localy for locale fr-BE .