i18nexus-cli 1.0.0 → 2.1.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/README.md +13 -11
- package/bin/index.js +16 -8
- package/package.json +4 -2
- package/pull.js +32 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
## Who is this CLI meant for?
|
|
10
10
|
|
|
11
11
|
- Developers using SSR/SSG or libraries such as [next-i18next](https://github.com/isaachinman/next-i18next)
|
|
12
|
-
- Developers who prefer to bundle their translation files with their
|
|
12
|
+
- Developers who prefer to bundle their translation files with their app
|
|
13
13
|
|
|
14
14
|
The primary motivation for this CLI is to ease i18nexus integration with SSR/SSG frameworks such as NextJS. It is the best way to integrate i18nexus with [next-i18next](https://github.com/isaachinman/next-i18next).
|
|
15
15
|
|
|
@@ -47,21 +47,23 @@ If you wish to download your files to a different directory, you can use the `--
|
|
|
47
47
|
|
|
48
48
|
`i18nexus pull`
|
|
49
49
|
|
|
50
|
-
| Option
|
|
51
|
-
|
|
|
52
|
-
| `--
|
|
53
|
-
| `--path` or `-p`
|
|
54
|
-
| `--ver` or `-v`
|
|
55
|
-
| `--clean`
|
|
50
|
+
| Option | Default value |
|
|
51
|
+
| ------------------- | ------------------ |
|
|
52
|
+
| `--api-key` or `-k` | |
|
|
53
|
+
| `--path` or `-p` | `./public/locales` |
|
|
54
|
+
| `--ver` or `-v` | `latest` |
|
|
55
|
+
| `--clean` | `false` |
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
#### Details
|
|
58
|
+
|
|
59
|
+
`--api-key`
|
|
60
|
+
Your project API key (Can also be set using environment variable `I18NEXUS_API_KEY`)
|
|
59
61
|
|
|
60
62
|
`--path`
|
|
61
63
|
The path to the destination folder in which translation files will be downloaded
|
|
62
64
|
|
|
63
65
|
`--ver`
|
|
64
|
-
The version of your project's translations to be downloaded
|
|
66
|
+
The version of your project's translations to be downloaded (Can also be set using environment variable `I18NEXUS_VERSION`)
|
|
65
67
|
|
|
66
68
|
`--clean`
|
|
67
|
-
|
|
69
|
+
Before download, clears your destination folder specified in --path. As a safety precaution, this only deletes folders with names that match a simple language code regex. You should still ensure you are not storing any files in your destination folder that you do not want deleted.
|
package/bin/index.js
CHANGED
|
@@ -4,18 +4,28 @@ const pkg = require('../package.json');
|
|
|
4
4
|
const program = require('commander');
|
|
5
5
|
const pull = require('../pull');
|
|
6
6
|
|
|
7
|
+
// Using Next's env variable loader because
|
|
8
|
+
// Next supports more than just one .env file
|
|
9
|
+
const { loadEnvConfig } = require('@next/env');
|
|
10
|
+
|
|
11
|
+
loadEnvConfig(process.cwd());
|
|
12
|
+
|
|
7
13
|
program.version(pkg.version);
|
|
8
14
|
|
|
9
15
|
program
|
|
10
16
|
.command('pull')
|
|
11
17
|
.description('Download all translations as .json files')
|
|
12
|
-
.requiredOption(
|
|
13
|
-
|
|
18
|
+
.requiredOption(
|
|
19
|
+
'-k, --api-key <apiKey>',
|
|
20
|
+
'The API key for your project',
|
|
21
|
+
process.env.I18NEXUS_API_KEY
|
|
22
|
+
)
|
|
23
|
+
.requiredOption(
|
|
14
24
|
'-v, --ver <version>',
|
|
15
25
|
'The version of the translations to be downloaded.',
|
|
16
|
-
'latest'
|
|
26
|
+
process.env.I18NEXUS_VERSION || 'latest'
|
|
17
27
|
)
|
|
18
|
-
.
|
|
28
|
+
.requiredOption(
|
|
19
29
|
'-p, --path <path>',
|
|
20
30
|
'The path to the destination folder in which translation files will be downloaded',
|
|
21
31
|
`${process.cwd()}/public/locales`
|
|
@@ -26,11 +36,9 @@ program
|
|
|
26
36
|
false
|
|
27
37
|
)
|
|
28
38
|
.action(options => {
|
|
29
|
-
console.log(options.clean, typeof options.clean);
|
|
30
|
-
|
|
31
39
|
pull({
|
|
32
|
-
apiKey: options.apiKey
|
|
33
|
-
version: options.ver
|
|
40
|
+
apiKey: options.apiKey,
|
|
41
|
+
version: options.ver,
|
|
34
42
|
path: options.path,
|
|
35
43
|
clean: options.clean
|
|
36
44
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i18nexus-cli",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Command line interface (CLI) for accessing the i18nexus API",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -14,8 +14,10 @@
|
|
|
14
14
|
"author": "i18nexus",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
+
"@next/env": "^11.0.1",
|
|
17
18
|
"colors": "^1.4.0",
|
|
18
19
|
"commander": "^7.2.0",
|
|
19
|
-
"
|
|
20
|
+
"https-proxy-agent": "^5.0.0",
|
|
21
|
+
"node-fetch": "^2.6.7"
|
|
20
22
|
}
|
|
21
23
|
}
|
package/pull.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const fetch = require('node-fetch');
|
|
3
3
|
const colors = require('colors');
|
|
4
|
+
const HttpsProxyAgent = require('https-proxy-agent');
|
|
4
5
|
|
|
5
6
|
const handleError = async (response, usingVersion) => {
|
|
6
7
|
let json, message;
|
|
@@ -24,6 +25,23 @@ const handleError = async (response, usingVersion) => {
|
|
|
24
25
|
process.exit(1);
|
|
25
26
|
};
|
|
26
27
|
|
|
28
|
+
const cleanDirectory = path => {
|
|
29
|
+
if (!fs.existsSync(path)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// as safety precaution, only delete folders that match regex
|
|
34
|
+
const regex = /^[a-z]{2}(-[A-Z]{2,4})?$/;
|
|
35
|
+
|
|
36
|
+
const contents = fs.readdirSync(path);
|
|
37
|
+
|
|
38
|
+
contents.forEach(name => {
|
|
39
|
+
if (regex.test(name)) {
|
|
40
|
+
fs.rmdirSync(`${path}/${name}`, { recursive: true });
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
27
45
|
const pull = async opt => {
|
|
28
46
|
const usingVersion = opt.version !== 'latest';
|
|
29
47
|
|
|
@@ -35,7 +53,19 @@ const pull = async opt => {
|
|
|
35
53
|
|
|
36
54
|
console.log(`Downloading translations to ${opt.path}...`);
|
|
37
55
|
|
|
38
|
-
const
|
|
56
|
+
const proxy =
|
|
57
|
+
process.env.http_proxy ||
|
|
58
|
+
process.env.HTTP_PROXY ||
|
|
59
|
+
process.env.https_proxy ||
|
|
60
|
+
process.env.HTTPS_PROXY;
|
|
61
|
+
|
|
62
|
+
const fetchOptions = {};
|
|
63
|
+
|
|
64
|
+
if (proxy) {
|
|
65
|
+
fetchOptions.agent = new HttpsProxyAgent(proxy);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const response = await fetch(url, fetchOptions);
|
|
39
69
|
|
|
40
70
|
if (response.status !== 200) {
|
|
41
71
|
return handleError(response, usingVersion);
|
|
@@ -44,7 +74,7 @@ const pull = async opt => {
|
|
|
44
74
|
const translations = await response.json();
|
|
45
75
|
|
|
46
76
|
if (opt.clean) {
|
|
47
|
-
|
|
77
|
+
cleanDirectory(opt.path);
|
|
48
78
|
}
|
|
49
79
|
|
|
50
80
|
for (let lng in translations) {
|