yata-fetch 1.3.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli.js DELETED
@@ -1,42 +0,0 @@
1
- const yata = require("./yata");
2
- const nconf = require("nconf");
3
- const log = require("./log");
4
-
5
- module.exports = async function () {
6
- // read argv for potential custom config path
7
- nconf.argv();
8
-
9
- // read ENV for token
10
- nconf.env();
11
-
12
- // load config path
13
- nconf.file({ file: yata.getConfigPath(nconf.get("config")) });
14
-
15
- // setup API host
16
- yata.apiHost = nconf.get("YATA_API_HOST") || "https://api.yatapp.net";
17
-
18
- try {
19
- if (
20
- yata.validateConfig(
21
- nconf.get(nconf.get("token")),
22
- nconf.get("project"),
23
- nconf.get("locales"),
24
- nconf.get("format"),
25
- nconf.get("root"),
26
- nconf.get("outputPath"),
27
- nconf.get("strip_empty")
28
- )
29
- ) {
30
- // if passed locale explicit download just one
31
- if (nconf.get("locale")) {
32
- await yata.downloadTranslation(nconf.get("locale"));
33
- } else {
34
- for (let locale of yata.locales) {
35
- await yata.downloadTranslation(locale);
36
- }
37
- }
38
- }
39
- } catch (e) {
40
- log("red", e);
41
- }
42
- };
package/src/log.js DELETED
@@ -1,20 +0,0 @@
1
- module.exports = function (color, message) {
2
- let code;
3
-
4
- switch (color) {
5
- case "red":
6
- code = "\x1b[31m";
7
- break;
8
- case "green":
9
- code = "\x1b[32m";
10
- break;
11
- case "yellow":
12
- code = "\x1b[33m";
13
- break;
14
-
15
- default:
16
- code = "\x1b[37m"; // white
17
- }
18
-
19
- return console.log(`${code}%s\x1b[0m`, message);
20
- };
package/src/yata.js DELETED
@@ -1,144 +0,0 @@
1
- const https = require("https");
2
- const fs = require("fs");
3
- const path = require("path");
4
- const log = require("./log");
5
-
6
- module.exports = {
7
- config: null,
8
- defaultConfigPath: "./yata.json",
9
- configPath: null,
10
- token: null,
11
- project: null,
12
- locales: [],
13
- format: "yml",
14
- root: false,
15
- outputPath: "translations",
16
- stripEmpty: false,
17
- apiHost: null,
18
-
19
- getConfigPath(configPath) {
20
- if (configPath) {
21
- this.configPath = configPath;
22
- } else {
23
- this.configPath = this.defaultConfigPath;
24
- }
25
-
26
- return this.configPath;
27
- },
28
-
29
- validateConfig(
30
- token,
31
- project,
32
- locales,
33
- format,
34
- root,
35
- outputPath,
36
- stripEmpty
37
- ) {
38
- if (!token) {
39
- throw new Error("No `token` in ENV");
40
- } else {
41
- this.token = token;
42
- }
43
-
44
- if (!project) {
45
- throw new Error("No `project` in config file");
46
- } else {
47
- this.project = project;
48
- }
49
-
50
- if (!Array.isArray(locales) || locales.length === 0) {
51
- throw new Error("No `locales` in config file");
52
- } else {
53
- this.locales = locales;
54
- }
55
-
56
- if (format && typeof format === "string") {
57
- this.format = format;
58
- }
59
-
60
- if (root && typeof root === "boolean") {
61
- this.root = root;
62
- }
63
-
64
- if (outputPath && typeof outputPath === "string") {
65
- this.outputPath = outputPath;
66
- }
67
-
68
- if (stripEmpty && typeof stripEmpty === "boolean") {
69
- this.stripEmpty = stripEmpty;
70
- }
71
-
72
- return true;
73
- },
74
-
75
- normalizeLocale(locale) {
76
- if (!locale) {
77
- return;
78
- }
79
-
80
- const localeSegments = locale.replace("-", "_").split("_");
81
- let newLocale = [];
82
- newLocale.push(localeSegments[0].toLowerCase());
83
-
84
- // two segment locale
85
- if (localeSegments[1]) {
86
- newLocale.push(localeSegments[1].toUpperCase());
87
- }
88
-
89
- return newLocale.join("_");
90
- },
91
-
92
- downloadTranslation(locale) {
93
- const normalizedLocale = this.normalizeLocale(locale);
94
-
95
- if (!normalizedLocale) {
96
- throw new Error("No locale passed to download function");
97
- }
98
-
99
- // if output folder doesn't exist we create it
100
- if (!fs.existsSync(this.outputPath)) {
101
- fs.mkdirSync(this.outputPath);
102
- }
103
-
104
- const fileName = `${normalizedLocale}.${this.format}`;
105
- const filePath = path.join(process.cwd(), `${this.outputPath}/${fileName}`);
106
- const url = `${this.apiHost}/api/v1/project/${this.project}/${locale}/${this.format}?apiToken=${this.token}&root=${this.root}&strip_empty=${this.stripEmpty}`;
107
-
108
- let bufferFile;
109
-
110
- // if file exist we grab it's size
111
- if (fs.existsSync(filePath)) {
112
- bufferFile = fs.readFileSync(filePath);
113
- }
114
-
115
- // we start stream
116
- const file = fs.createWriteStream(filePath);
117
-
118
- return new Promise((resolve, reject) => {
119
- https
120
- .get(url, (response) => {
121
- const { statusCode } = response;
122
-
123
- if (statusCode !== 200) {
124
- return reject(`Request Failed.\nStatus Code: ${statusCode}`);
125
- }
126
-
127
- response.pipe(file);
128
- file.on("finish", () => {
129
- const newBufferFile = fs.readFileSync(filePath);
130
-
131
- if (bufferFile && bufferFile.equals(newBufferFile)) {
132
- log("yellow", `Generating "${locale}" translation. Skipped.`);
133
- } else {
134
- log("green", `Generating "${locale}" translation. Done.`);
135
- }
136
- resolve();
137
- });
138
- })
139
- .on("error", (e) => {
140
- log("red", e);
141
- });
142
- });
143
- },
144
- };
@@ -1,20 +0,0 @@
1
- module.exports = {
2
- globals: {
3
- it: true,
4
- describe: true,
5
- afterEach: true,
6
- beforeEach: true
7
- },
8
- rules: {
9
- 'camelcase': 0,
10
-
11
- // JSHint "expr", disabled due to chai expect assertions
12
- 'no-unused-expressions': 0,
13
-
14
- // disabled for easier asserting of file contents
15
- 'quotes': 0,
16
-
17
- // disabled because describe(), it(), etc. should not use arrow functions
18
- 'prefer-arrow-callback': 0
19
- }
20
- };
@@ -1,103 +0,0 @@
1
- const expect = require('chai').expect;
2
- const yata = require('../../dist/yata');
3
-
4
- describe('yata library', function() {
5
-
6
- describe('getConfigPath function', function() {
7
- it('return default config path', function() {
8
- expect(yata.getConfigPath()).to.equal('./yata.json');
9
- });
10
-
11
- it('return custom config path', function() {
12
- expect(yata.getConfigPath('./custom-yata.json')).to.equal('./custom-yata.json');
13
- });
14
- });
15
-
16
- describe('validateConfig function', function() {
17
- it('return exception if no token in ENV', function() {
18
- expect(() => {
19
- yata.validateConfig();
20
- }).to.throw('token');
21
- });
22
-
23
- it('return exception if no project', function() {
24
- expect(() => {
25
- yata.validateConfig('token');
26
- }).to.throw('project');
27
- });
28
-
29
- it('return exception if empty locales', function() {
30
- expect(() => {
31
- yata.validateConfig('token', 'project', []);
32
- }).to.throw('locales');
33
- });
34
-
35
- it('return exception if no locales', function() {
36
- expect(() => {
37
- yata.validateConfig('token', 'project');
38
- }).to.throw('locales');
39
- });
40
-
41
- it('return exception if locales as string', function() {
42
- expect(() => {
43
- yata.validateConfig('token', 'project', 'pl_PL');
44
- }).to.throw('locales');
45
- });
46
-
47
- it('passes validaiton when using only required params', function() {
48
- const validation = yata.validateConfig('token', 'project', ['pl_PL']);
49
-
50
- expect(validation).to.be.true;
51
- expect(yata.format).to.equal('yml');
52
- expect(yata.root).to.be.false;
53
- expect(yata.stripEmpty).to.be.false;
54
- expect(yata.outputPath).to.be.equal('translations');
55
- });
56
-
57
- it('passes validaiton when passing all params', function() {
58
- const validation = yata.validateConfig('token', 'project', ['pl_PL'], 'json', true, 'locales', true);
59
-
60
- expect(validation).to.be.true;
61
- expect(yata.token).to.equal('token');
62
- expect(yata.project).to.equal('project');
63
- expect(yata.locales).to.deep.equal(['pl_PL']);
64
- expect(yata.format).to.equal('json');
65
- expect(yata.root).to.be.true;
66
- expect(yata.stripEmpty).to.be.true;
67
- expect(yata.outputPath).to.be.equal('locales');
68
- });
69
- });
70
-
71
- describe('downloadTranslation function', function() {
72
- it('throw exception if no locale', function() {
73
- expect(() => yata.downloadTranslation()).to.throw('locale');
74
- });
75
- });
76
-
77
- describe('normalizeLocale function', function() {
78
- it('work with undefined value', function() {
79
- expect(() => yata.normalizeLocale()).to.not.throw();
80
- });
81
-
82
- it('work with null value', function() {
83
- expect(() => yata.normalizeLocale(null)).to.not.throw();
84
- });
85
-
86
- it('work with empty string value', function() {
87
- expect(yata.normalizeLocale('')).to.equal();
88
- });
89
-
90
-
91
- it('work ISO notation', function() {
92
- expect(yata.normalizeLocale('en-us')).to.equal('en_US');
93
- });
94
-
95
- it('work with simple notation', function() {
96
- expect(yata.normalizeLocale('EN')).to.equal('en');
97
- });
98
-
99
- it('work with already correct version', function() {
100
- expect(yata.normalizeLocale('en_US')).to.equal('en_US');
101
- });
102
- });
103
- });