diff --git a/dist/yata.js b/dist/yata.js index ebaaa00..8ad7f6b 100644 --- a/dist/yata.js +++ b/dist/yata.js @@ -83,11 +83,11 @@ module.exports = { const filePath = path.join(process.cwd(), `${this.outputPath}/${fileName}`); const url = `${this.apiHost}/api/v1/project/${this.project}/${locale}/${this.format}?apiToken=${this.token}&root=${this.root}`; - let fileSize = 0; + let bufferFile; // if file exist we grab it's size if (fs.existsSync(filePath)) { - fileSize = fs.statSync(filePath).size; + bufferFile = fs.readFileSync(filePath); } // we start stream @@ -103,9 +103,9 @@ module.exports = { response.pipe(file); file.on('finish', () => { - const newFileSize = fs.statSync(filePath).size; + const newBufferFile = fs.readFileSync(filePath); - if (newFileSize === fileSize) { + if (bufferFile.equals(newBufferFile)) { log('yellow', `Generating "${locale}" translation. Skipped.`); } else { log('green', `Generating "${locale}" translation. Done.`); diff --git a/dist/yata.js.map b/dist/yata.js.map index 178be97..ac3f4df 100644 --- a/dist/yata.js.map +++ b/dist/yata.js.map @@ -1 +1 @@ -{"version":3,"sources":["yata.js"],"names":["http","require","fs","path","log","module","exports","config","defaultConfigPath","configPath","token","project","locales","format","root","outputPath","apiHost","getConfigPath","validateConfig","Error","Array","isArray","length","normalizeLocale","locale","toLowerCase","replace","downloadTranslation","normalizedLocale","existsSync","mkdirSync","fileName","filePath","join","process","cwd","url","fileSize","statSync","size","file","createWriteStream","Promise","resolve","reject","get","response","statusCode","pipe","on","newFileSize","e"],"mappings":"AAAA,MAAMA,OAAOC,QAAQ,MAAR,CAAb;AACA,MAAMC,KAAKD,QAAQ,IAAR,CAAX;AACA,MAAME,OAAOF,QAAQ,MAAR,CAAb;AACA,MAAMG,MAAMH,QAAQ,OAAR,CAAZ;;AAEAI,OAAOC,OAAP,GAAiB;AACfC,UAAmB,IADJ;AAEfC,qBAAmB,aAFJ;AAGfC,cAAmB,IAHJ;AAIfC,SAAmB,IAJJ;AAKfC,WAAmB,IALJ;AAMfC,WAAmB,EANJ;AAOfC,UAAmB,KAPJ;AAQf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cAAchD,GAAGoC,QAAH,CAAYN,QAAZ,EAAsBO,IAA1C;;AAEA,cAAIW,gBAAgBb,QAApB,EAA8B;AAC5BjC,gBAAI,QAAJ,EAAe,eAAcoB,MAAO,yBAApC;AACD,WAFD,MAEO;AACLpB,gBAAI,OAAJ,EAAc,eAAcoB,MAAO,sBAAnC;AACD;AACDmB;AACD,SATD;AAUD,OAlBD,EAkBGM,EAlBH,CAkBM,OAlBN,EAkBeE,KAAK;AAClB/C,YAAI,KAAJ,EAAW+C,CAAX;AACD,OApBD;AAqBD,KAtBM,CAAP;AAuBD;AA7Gc,CAAjB","file":"yata.js","sourcesContent":["const http = require('http');\nconst fs = require('fs');\nconst path = require('path');\nconst log = require('./log');\n\nmodule.exports = {\n config: null,\n defaultConfigPath: './yata.json',\n configPath: null,\n token: null,\n project: null,\n locales: [],\n format: 'yml',\n root: false,\n outputPath: 'translations',\n apiHost: null,\n\n getConfigPath(configPath) {\n if (configPath) {\n this.configPath = configPath;\n } else {\n this.configPath = this.defaultConfigPath;\n }\n\n return this.configPath;\n },\n\n validateConfig(token, project, locales, format, root, outputPath) {\n if (!token) {\n throw new Error('No `token` in ENV');\n } else {\n this.token = token;\n }\n\n if (!project) {\n throw new Error('No `project` in config file');\n } else {\n this.project = project;\n }\n\n if (!Array.isArray(locales) || locales.length === 0) {\n throw new Error('No `locales` in config file');\n } else {\n this.locales = locales;\n }\n\n if (format && typeof format === 'string') {\n this.format = format;\n }\n\n if (root && typeof root === 'boolean') {\n this.root = root;\n }\n\n if (outputPath && typeof outputPath === 'string') {\n this.outputPath = outputPath;\n }\n\n return true;\n },\n\n normalizeLocale(locale) {\n if (!locale) { return; }\n\n return locale.toLowerCase().replace('_', '-');\n },\n\n downloadTranslation(locale) {\n const normalizedLocale = this.normalizeLocale(locale);\n\n if (!normalizedLocale) { throw new Error('No locale passed to download function'); }\n\n // if output folder doesn't exist we create it\n if (!fs.existsSync(this.outputPath)) {\n fs.mkdirSync(this.outputPath);\n }\n\n const fileName = `${normalizedLocale}.${this.format}`;\n const filePath = path.join(process.cwd(), `${this.outputPath}/${fileName}`);\n const url = `${this.apiHost}/api/v1/project/${this.project}/${locale}/${this.format}?apiToken=${this.token}&root=${this.root}`;\n\n let fileSize = 0;\n\n // if file exist we grab it's size\n if (fs.existsSync(filePath)) {\n fileSize = fs.statSync(filePath).size;\n }\n\n // we start stream\n const file = fs.createWriteStream(filePath);\n\n return new Promise((resolve, reject) => {\n http.get(url, response => {\n const { statusCode } = response;\n\n if (statusCode !== 200) {\n return reject(`Request Failed.\\nStatus Code: ${statusCode}`);\n }\n\n response.pipe(file);\n file.on('finish', () => {\n const newFileSize = fs.statSync(filePath).size;\n\n if (newFileSize === fileSize) {\n log('yellow', `Generating \"${locale}\" translation. Skipped.`);\n } else {\n log('green', `Generating \"${locale}\" translation. Done.`);\n }\n resolve();\n });\n }).on('error', e => {\n log('red', e);\n });\n });\n }\n};\n"]} \ No newline at end of file +{"version":3,"sources":["yata.js"],"names":["http","require","fs","path","log","module","exports","config","defaultConfigPath","configPath","token","project","locales","format","root","outputPath","apiHost","getConfigPath","validateConfig","Error","Array","isArray","length","normalizeLocale","locale","toLowerCase","replace","downloadTranslation","normalizedLocale","existsSync","mkdirSync","fileName","filePath","join","process","cwd","url","bufferFile","readFileSync","file","createWriteStream","Promise","resolve","reject","get","response","statusCode","pipe","on","newBufferFile","equals","e"],"mappings":"AAAA,MAAMA,OAAOC,QAAQ,MAAR,CAAb;AACA,MAAMC,KAAKD,QAAQ,IAAR,CAAX;AACA,MAAME,OAAOF,QAAQ,MAAR,CAAb;AACA,MAAMG,MAAMH,QAAQ,OAAR,CAAZ;;AAEAI,OAAOC,OAAP,GAAiB;AACfC,UAAmB,IADJ;AAEfC,qBAAmB,aAFJ;AAGfC,cAAmB,IAHJ;AAIfC,SAAmB,IAJJ;AAKfC,WAAmB,IALJ;AAMfC,WAAmB,EANJ;AAOfC,UAAmB,KAPJ;AAQfC,QAAmB,KARJ;AASfC,cAAmB,cAT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B;;AAEA,cAAIK,WAAWa,MAAX,CAAkBD,aAAlB,CAAJ,EAAsC;AACpC7C,gBAAI,QAAJ,EAAe,eAAcoB,MAAO,yBAApC;AACD,WAFD,MAEO;AACLpB,gBAAI,OAAJ,EAAc,eAAcoB,MAAO,sBAAnC;AACD;AACDkB;AACD,SATD;AAUD,OAlBD,EAkBGM,EAlBH,CAkBM,OAlBN,EAkBeG,KAAK;AAClB/C,YAAI,KAAJ,EAAW+C,CAAX;AACD,OApBD;AAqBD,KAtBM,CAAP;AAuBD;AA7Gc,CAAjB","file":"yata.js","sourcesContent":["const http = require('http');\nconst fs = require('fs');\nconst path = require('path');\nconst log = require('./log');\n\nmodule.exports = {\n config: null,\n defaultConfigPath: './yata.json',\n configPath: null,\n token: null,\n project: null,\n locales: [],\n format: 'yml',\n root: false,\n outputPath: 'translations',\n apiHost: null,\n\n getConfigPath(configPath) {\n if (configPath) {\n this.configPath = configPath;\n } else {\n this.configPath = this.defaultConfigPath;\n }\n\n return this.configPath;\n },\n\n validateConfig(token, project, locales, format, root, outputPath) {\n if (!token) {\n throw new Error('No `token` in ENV');\n } else {\n this.token = token;\n }\n\n if (!project) {\n throw new Error('No `project` in config file');\n } else {\n this.project = project;\n }\n\n if (!Array.isArray(locales) || locales.length === 0) {\n throw new Error('No `locales` in config file');\n } else {\n this.locales = locales;\n }\n\n if (format && typeof format === 'string') {\n this.format = format;\n }\n\n if (root && typeof root === 'boolean') {\n this.root = root;\n }\n\n if (outputPath && typeof outputPath === 'string') {\n this.outputPath = outputPath;\n }\n\n return true;\n },\n\n normalizeLocale(locale) {\n if (!locale) { return; }\n\n return locale.toLowerCase().replace('_', '-');\n },\n\n downloadTranslation(locale) {\n const normalizedLocale = this.normalizeLocale(locale);\n\n if (!normalizedLocale) { throw new Error('No locale passed to download function'); }\n\n // if output folder doesn't exist we create it\n if (!fs.existsSync(this.outputPath)) {\n fs.mkdirSync(this.outputPath);\n }\n\n const fileName = `${normalizedLocale}.${this.format}`;\n const filePath = path.join(process.cwd(), `${this.outputPath}/${fileName}`);\n const url = `${this.apiHost}/api/v1/project/${this.project}/${locale}/${this.format}?apiToken=${this.token}&root=${this.root}`;\n\n let bufferFile;\n\n // if file exist we grab it's size\n if (fs.existsSync(filePath)) {\n bufferFile = fs.readFileSync(filePath);\n }\n\n // we start stream\n const file = fs.createWriteStream(filePath);\n\n return new Promise((resolve, reject) => {\n http.get(url, response => {\n const { statusCode } = response;\n\n if (statusCode !== 200) {\n return reject(`Request Failed.\\nStatus Code: ${statusCode}`);\n }\n\n response.pipe(file);\n file.on('finish', () => {\n const newBufferFile = fs.readFileSync(filePath);\n\n if (bufferFile.equals(newBufferFile)) {\n log('yellow', `Generating \"${locale}\" translation. Skipped.`);\n } else {\n log('green', `Generating \"${locale}\" translation. Done.`);\n }\n resolve();\n });\n }).on('error', e => {\n log('red', e);\n });\n });\n }\n};\n"]} \ No newline at end of file diff --git a/src/yata.js b/src/yata.js index 4cd5d83..e1d1f87 100644 --- a/src/yata.js +++ b/src/yata.js @@ -79,11 +79,11 @@ module.exports = { const filePath = path.join(process.cwd(), `${this.outputPath}/${fileName}`); const url = `${this.apiHost}/api/v1/project/${this.project}/${locale}/${this.format}?apiToken=${this.token}&root=${this.root}`; - let fileSize = 0; + let bufferFile; // if file exist we grab it's size if (fs.existsSync(filePath)) { - fileSize = fs.statSync(filePath).size; + bufferFile = fs.readFileSync(filePath); } // we start stream @@ -99,9 +99,9 @@ module.exports = { response.pipe(file); file.on('finish', () => { - const newFileSize = fs.statSync(filePath).size; + const newBufferFile = fs.readFileSync(filePath); - if (newFileSize === fileSize) { + if (bufferFile.equals(newBufferFile)) { log('yellow', `Generating "${locale}" translation. Skipped.`); } else { log('green', `Generating "${locale}" translation. Done.`);