Skip to content

Commit

Permalink
Merge pull request #8 from Boulangerie/chore/refactoring-es6
Browse files Browse the repository at this point in the history
Chore/refactoring es6
  • Loading branch information
firehist committed Mar 2, 2016
2 parents f91b31b + 31d8f8d commit 7e03009
Show file tree
Hide file tree
Showing 64 changed files with 437 additions and 2,069 deletions.
86 changes: 0 additions & 86 deletions Gruntfile.js

This file was deleted.

16 changes: 2 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"commit": "git cz",
"test": "mocha tests -w --compilers js:babel-register",
"test:single": "babel-node node_modules/.bin/babel-istanbul cover _mocha -- tests -R spec",
"check-coverage": "istanbul check-coverage --statements 50 --branches 35 --functions 75 --lines 50",
"check-coverage": "istanbul check-coverage --statements 99 --branches 92 --functions 100 --lines 99",
"report-coverage": "cat ./coverage/lcov.info | coveralls",
"prebuild": "rm -Rf lib && mkdir lib",
"build": "babel -d lib/ src/",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"engines": {
"node": ">= 0.8.0"
"node": ">= 4.0.0"
},
"devDependencies": {
"babel-cli": "6.5.1",
Expand All @@ -25,31 +25,19 @@
"commitizen": "2.5.0",
"coveralls": "2.11.8",
"cz-conventional-changelog": "1.1.5",
"grunt": "~0.4.0",
"grunt-bump": "0.0.13",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-copy": "^0.5.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-markdox": "^0.1.0",
"istanbul": "0.4.2",
"mocha": "2.4.5",
"nodeunit": "^0.9.1",
"semantic-release": "^4.3.5",
"sinon": "1.17.3"
},
"peerDependencies": {
"grunt": ">=0.4.0"
},
"repository": {
"type": "git",
"url": "https://github.com/Boulangerie/angular-translate-extract.git"
},
"keywords": [
"AngularJS",
"angular-translate",
"gruntplugin",
"grunt",
"javascript",
"js",
"translate",
Expand Down
20 changes: 20 additions & 0 deletions src/adapter/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import _ from 'lodash'

export default class Adapter {
constructor(log, basePath) {
this.log = log
this.basePath = basePath
}

init(params) {
this.params = _.defaults(params, this.params)

try {
this.log.debug('Init PodAdapter', this.params.dest, this.params.prefix, this.params.suffix)
} catch(e) {}
}

persist() {
throw new Error('Must be implemented')
}
}
93 changes: 42 additions & 51 deletions src/adapter/json-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,65 @@
*
*/

(function() {
'use strict'

var path = require('path')
var fs = require('fs')
var Utils = require('./../utils.js')
var Translations = require('./../translations.js')

var _log, _basePath, _utils


function JsonAdapter(log, basePath) {
_log = log
_basePath = basePath
_utils = new Utils.Utils({
basePath: _basePath
import path from 'path'
import fs from 'fs'
import Adapter from './adapter'
import {Utils} from './../utils'
import {Translations} from './../translations'

export class JsonAdapter extends Adapter {

constructor(log, basePath) {
super(log, basePath)
this.utils = new Utils({
"basePath": basePath
})

this.params = {
lang: [],
prefix: '',
prefix: '',
suffix: '.json',
source: '',
defaultLang: '.',
stringifyOptions: null
}
}

JsonAdapter.prototype.init = function(params) {
this.dest = params.dest
this.lang = params.lang
this.prefix = params.prefix
this.suffix = params.suffix || '.json'
this.source = params.source
this.defaultLang = params.defaultLang
this.stringifyOptions = params.stringifyOptions
getDestFilename(language) {
return path.resolve(this.basePath, path.join(this.params.dest, this.params.prefix + language + this.params.suffix))
}

JsonAdapter.prototype.persist = function(_translation) {
var lang = this.lang
var dest = this.dest
var prefix = this.prefix
var suffix = this.suffix
var source = this.source || ''
var defaultLang = this.defaultLang || '.'
var stringify_options = this.stringifyOptions || null

// Build all output language files
lang.forEach(function (lang) {

var destFilename = path.resolve(_basePath, path.join(dest, prefix + lang + suffix)),
filename = source,
translations = {},
persist(translations) {
// Build all output language files
this.params.lang.forEach((language) => {


let destFilename = this.getDestFilename(language),
filename = this.params.source,
json = {}

// Test source filename
if (filename === '' || !fs.statSync(filename)) {
filename = destFilename
}

_log.info('Process ' + lang + ' : ' + filename)
this.log.debug('Process ' + language + ' : ' + filename)

var isDefaultLang = (defaultLang === lang)
var isDefaultLang = (this.params.defaultLang === language)
try {
// Test if filename exists
fs.statSync(filename)
_log.debug('File Exists. ' + destFilename)
this.log.debug('File Exists. ' + destFilename)
json = JSON.parse(fs.readFileSync(filename, {encoding: 'utf8'}))
} catch (e) {
_log.debug('Create file: ' + destFilename + (isDefaultLang ? ' (' + lang + ' is the default language)' : ''))
this.log.debug('Create file: ' + destFilename + (isDefaultLang ? ' (' + language + ' is the default language)' : ''))
}

translations = _translation.getMergedTranslations(Translations.Translations.flatten(json), isDefaultLang)
let _translations = translations.getMergedTranslations(Translations.flatten(json), isDefaultLang)

var stats = _translation.getStats()
var statEmptyType = _translation.params.nullEmpty ? "null" : "empty"
var stats = translations.getStats()
var statEmptyType = translations.params.nullEmpty ? "null" : "empty"
var statPercentage = Math.round(stats[statEmptyType] / stats["total"] * 100)
statPercentage = isNaN(statPercentage) ? 100 : statPercentage
var statsString = "Statistics : " +
Expand All @@ -82,13 +74,12 @@
" / Deleted: " + stats["deleted"] +
" / New: " + stats["new"]

_log.info(statsString)
this.log.debug(statsString)

// Write JSON file for lang
fs.writeFileSync(path.resolve(_basePath, destFilename), _utils.customStringify(translations, stringify_options) + '\n')
// Write JSON file for language
fs.writeFileSync(path.resolve(this.basePath, destFilename), this.utils.customStringify(_translations, this.stringifyOptions) + '\n')

})
}

module.exports = JsonAdapter
}())
}
14 changes: 3 additions & 11 deletions src/adapter/pot-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import Po from 'pofile'
import _ from 'lodash'
import Adapter from './adapter'

export class PotObject {

Expand All @@ -20,11 +21,10 @@ msgstr "${PotObject.escapeString(this.msg)}"`

PotObject.escapeString = (str) => (""+str).replace(/"/g, '\\"')

export class PotAdapter {
export class PotAdapter extends Adapter {

constructor(log, basePath) {
this.log = log
this.basePath = basePath
super(log, basePath)

this.params = {
dest: '.',
Expand All @@ -33,14 +33,6 @@ export class PotAdapter {
}
}

init(params) {
this.params = _.defaults(params, this.params)

try {
this.log.debug('Init PodAdapter', this.params.dest, this.params.prefix, this.params.suffix)
} catch(e) {}
}

persist(translations) {
var catalog = new Po()

Expand Down
Loading

0 comments on commit 7e03009

Please sign in to comment.