ztxkutils 2.10.66-15 → 2.10.66-16
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/dist/index.js +1 -1
- package/dist/{request-ba8abf99.js → request-588c90ec.js} +92 -18
- package/dist/{request-0cc024b1.js → request-dc69f021.js} +68 -13
- package/dist/request.js +1 -1
- package/package.json +1 -1
- package/zti18n-cli/bin/index.js +3 -3
- package/zti18n-cli/index.js +23 -23
- package/zti18n-cli/src/command/convert.js +17 -17
- package/zti18n-cli/src/command/convert2.js +35 -35
- package/zti18n-cli/src/command/initFileConf.js +133 -133
- package/zti18n-cli/src/command/publish.js +24 -24
- package/zti18n-cli/src/conf/BaseConf.js +21 -21
- package/zti18n-cli/src/conf/FileConf.js +116 -116
- package/zti18n-cli/src/index.js +75 -75
- package/zti18n-cli/src/translate/google.js +6 -6
- package/zti18n-cli/src/utils/isChinese.js +3 -3
- package/zti18n-cli/src/utils/log.js +8 -8
- package/zti18n-cli/src/utils/mergeOptions.js +45 -45
- package/zti18n-cli/src/utils/reactOptions.js +73 -73
- package/zti18n-cli/src/utils/vueOptions.js +69 -69
- package/zti18n-core/index.js +1 -1
- package/zti18n-core/src/index.js +5 -5
- package/zti18n-core/src/plugin/reactIntlToReactIntlUniversal.js +224 -224
- package/zti18n-core/src/plugin/reactIntlUniversalToDi18n.js +64 -64
- package/zti18n-core/src/transform/defaultPkMap.js +79 -79
- package/zti18n-core/src/transform/transformHtml.js +271 -271
- package/zti18n-core/src/transform/transformPug.js +272 -272
- package/zti18n-core/src/transform/transformReactIntlToReactIntlUniversal.js +96 -96
- package/zti18n-core/src/transform/transformReactIntlUniveralToDi18n.js +90 -90
- package/zti18n-core/src/transform/transformToDi18n.js +22 -22
- package/zti18n-core/src/transform/transformTs.js +41 -41
- package/zti18n-core/src/transform/transformVue.js +126 -126
- package/zti18n-core/src/translate/google.js +6 -6
- package/zti18n-core/src/utils/constants.js +3 -3
- package/zti18n-core/src/utils/getIgnoreLines.js +14 -14
- package/zti18n-core/src/utils/isChinese.js +3 -3
- package/zti18n-core/src/utils/log.js +8 -8
- package/dist/authority-e6bde99f.js +0 -423
- package/dist/reqUrl-3792afcd.js +0 -82
- package/dist/reqUrl-787dd9e5.js +0 -82
- package/dist/request-ef290b9a.js +0 -2838
- package/dist/tools-09a4d620.js +0 -2445
- package/dist/tools-0ca888cd.js +0 -2473
- package/dist/validate-6e735536.js +0 -79
- package/dist/zti18n-cli/bin/index.d.ts +0 -2
- package/dist/zti18n-cli/index.d.ts +0 -1
- package/dist/zti18n-cli/src/command/collect.d.ts +0 -2
- package/dist/zti18n-cli/src/command/convert.d.ts +0 -2
- package/dist/zti18n-cli/src/command/convert2.d.ts +0 -2
- package/dist/zti18n-cli/src/command/initFileConf.d.ts +0 -2
- package/dist/zti18n-cli/src/command/publish.d.ts +0 -2
- package/dist/zti18n-cli/src/conf/BaseConf.d.ts +0 -8
- package/dist/zti18n-cli/src/conf/FileConf.d.ts +0 -6
- package/dist/zti18n-cli/src/index.d.ts +0 -1
- package/dist/zti18n-cli/src/translate/google.d.ts +0 -2
- package/dist/zti18n-cli/src/utils/isChinese.d.ts +0 -2
- package/dist/zti18n-cli/src/utils/log.d.ts +0 -4
- package/dist/zti18n-cli/src/utils/mergeOptions.d.ts +0 -24
- package/dist/zti18n-cli/src/utils/reactOptions.d.ts +0 -21
- package/dist/zti18n-cli/src/utils/vueOptions.d.ts +0 -17
- package/dist/zti18n-core/index.d.ts +0 -2
- package/dist/zti18n-core/src/index.d.ts +0 -8
- package/dist/zti18n-core/src/plugin/reactIntlToReactIntlUniversal.d.ts +0 -19
- package/dist/zti18n-core/src/plugin/reactIntlUniversalToDi18n.d.ts +0 -9
- package/dist/zti18n-core/src/transform/defaultPkMap.d.ts +0 -75
- package/dist/zti18n-core/src/transform/transformHtml.d.ts +0 -5
- package/dist/zti18n-core/src/transform/transformJs.d.ts +0 -5
- package/dist/zti18n-core/src/transform/transformPug.d.ts +0 -5
- package/dist/zti18n-core/src/transform/transformReactIntlToReactIntlUniversal.d.ts +0 -2
- package/dist/zti18n-core/src/transform/transformReactIntlUniveralToDi18n.d.ts +0 -2
- package/dist/zti18n-core/src/transform/transformToDi18n.d.ts +0 -5
- package/dist/zti18n-core/src/transform/transformTs.d.ts +0 -5
- package/dist/zti18n-core/src/transform/transformVue.d.ts +0 -5
- package/dist/zti18n-core/src/transform/transformZeroToDi18n.d.ts +0 -2
- package/dist/zti18n-core/src/translate/google.d.ts +0 -2
- package/dist/zti18n-core/src/utils/constants.d.ts +0 -3
- package/dist/zti18n-core/src/utils/getIgnoreLines.d.ts +0 -2
- package/dist/zti18n-core/src/utils/isChinese.d.ts +0 -2
- package/dist/zti18n-core/src/utils/log.d.ts +0 -4
@@ -1,133 +1,133 @@
|
|
1
|
-
const glob = require('glob');
|
2
|
-
const fs = require('fs');
|
3
|
-
const path = require('path');
|
4
|
-
const inquirer = require('inquirer');
|
5
|
-
const prettier = require('prettier');
|
6
|
-
const FileConf = require('../conf/FileConf');
|
7
|
-
const reactOptions = require('../utils/reactOptions');
|
8
|
-
const vueOptions = require('../utils/vueOptions');
|
9
|
-
const log = require('../utils/log');
|
10
|
-
|
11
|
-
function getLocaleFiles({ path, exclude }) {
|
12
|
-
return glob.sync(`${path}/**/*.json`, {
|
13
|
-
ignore: (exclude || []).map((e) => `${path}/${e}`),
|
14
|
-
});
|
15
|
-
}
|
16
|
-
|
17
|
-
async function doInquire() {
|
18
|
-
// 1. whether overwrite?
|
19
|
-
let configExist = true;
|
20
|
-
try {
|
21
|
-
fs.accessSync('./zti18n.config.js');
|
22
|
-
} catch (e) {
|
23
|
-
configExist = false;
|
24
|
-
}
|
25
|
-
|
26
|
-
if (configExist) {
|
27
|
-
const ans = await inquirer.prompt([
|
28
|
-
{
|
29
|
-
name: 'overwrite',
|
30
|
-
type: 'confirm',
|
31
|
-
message: '配置文件 zti18n.config.js 已存在,是否覆盖?',
|
32
|
-
},
|
33
|
-
]);
|
34
|
-
|
35
|
-
if (!ans.overwrite) process.exit(0);
|
36
|
-
}
|
37
|
-
|
38
|
-
// 2. first i18n?
|
39
|
-
let ans = await inquirer.prompt([
|
40
|
-
{
|
41
|
-
name: 'firstI18n',
|
42
|
-
type: 'confirm',
|
43
|
-
message: '是否初次国际化?',
|
44
|
-
},
|
45
|
-
{
|
46
|
-
name: 'localePath',
|
47
|
-
type: 'input',
|
48
|
-
message: '请输入现有国际化资源路径:',
|
49
|
-
when(answers) {
|
50
|
-
return !answers.firstI18n;
|
51
|
-
},
|
52
|
-
},
|
53
|
-
]);
|
54
|
-
|
55
|
-
return ans;
|
56
|
-
}
|
57
|
-
|
58
|
-
module.exports = async function initFileConf(isVue) {
|
59
|
-
const answers = await doInquire();
|
60
|
-
const { localePath = 'locales', firstI18n } = answers;
|
61
|
-
|
62
|
-
const defaultOptions = isVue ? vueOptions : reactOptions;
|
63
|
-
const options = {
|
64
|
-
...defaultOptions,
|
65
|
-
localeConf: { type: 'file', folder: localePath },
|
66
|
-
};
|
67
|
-
|
68
|
-
// 配置信息写入文件
|
69
|
-
fs.writeFileSync(
|
70
|
-
'./zti18n.config.js',
|
71
|
-
prettier.format('module.exports = ' + JSON.stringify(options), {
|
72
|
-
parser: 'babel',
|
73
|
-
singleQuote: true,
|
74
|
-
trailingComma: 'es5',
|
75
|
-
}),
|
76
|
-
'utf8'
|
77
|
-
);
|
78
|
-
|
79
|
-
let createTasks = [];
|
80
|
-
const confService = new FileConf(options.localeConf.folder);
|
81
|
-
|
82
|
-
if (!firstI18n) {
|
83
|
-
// 非首次国际化,本地代码中已有国际化资源
|
84
|
-
const locales = getLocaleFiles({ path: localePath });
|
85
|
-
|
86
|
-
// 读取国际化资源
|
87
|
-
const data = locales.map((element) => {
|
88
|
-
// TODO: 支持国际化资源为 js 文件的情况,目前只支持为 json 文件。
|
89
|
-
const json = fs.readFileSync(element, {
|
90
|
-
encoding: 'utf-8',
|
91
|
-
});
|
92
|
-
|
93
|
-
// 使用现有文件名为语言 key
|
94
|
-
const key = path.parse(element).name;
|
95
|
-
|
96
|
-
return {
|
97
|
-
key,
|
98
|
-
value: JSON.parse(json),
|
99
|
-
confName: key,
|
100
|
-
};
|
101
|
-
});
|
102
|
-
|
103
|
-
createTasks = data.map(({ confName, value, key }) => {
|
104
|
-
let commentValue = {};
|
105
|
-
if (key !== options.primaryLocale) {
|
106
|
-
commentValue = data.find((d) => d.key === options.primaryLocale).value;
|
107
|
-
}
|
108
|
-
return confService.createConf(confName, value, commentValue, key);
|
109
|
-
});
|
110
|
-
} else {
|
111
|
-
// 首次国际化,只需要创建配置
|
112
|
-
createTasks = options.supportedLocales.map((key) => {
|
113
|
-
const value = { TBD: 'TBD' }; // 创建配置时,必须要有一个 k/V
|
114
|
-
const confName = key; // 初始化时,先创建测试环境的配置
|
115
|
-
return confService.createConf(confName, value, {}, key);
|
116
|
-
});
|
117
|
-
}
|
118
|
-
|
119
|
-
const createTaskResults = await Promise.all(createTasks);
|
120
|
-
log.success('[done] conf created');
|
121
|
-
console.log(createTaskResults);
|
122
|
-
|
123
|
-
// 将已有国际化资源的配置发布
|
124
|
-
if (!firstI18n) {
|
125
|
-
const publishTasks = options.supportedLocales.map((key) => {
|
126
|
-
return confService.publishConf(`${key}-test`);
|
127
|
-
});
|
128
|
-
|
129
|
-
const publishTaskResults = await Promise.all(publishTasks);
|
130
|
-
log.success('[done] conf published');
|
131
|
-
console.log(publishTaskResults);
|
132
|
-
}
|
133
|
-
};
|
1
|
+
const glob = require('glob');
|
2
|
+
const fs = require('fs');
|
3
|
+
const path = require('path');
|
4
|
+
const inquirer = require('inquirer');
|
5
|
+
const prettier = require('prettier');
|
6
|
+
const FileConf = require('../conf/FileConf');
|
7
|
+
const reactOptions = require('../utils/reactOptions');
|
8
|
+
const vueOptions = require('../utils/vueOptions');
|
9
|
+
const log = require('../utils/log');
|
10
|
+
|
11
|
+
function getLocaleFiles({ path, exclude }) {
|
12
|
+
return glob.sync(`${path}/**/*.json`, {
|
13
|
+
ignore: (exclude || []).map((e) => `${path}/${e}`),
|
14
|
+
});
|
15
|
+
}
|
16
|
+
|
17
|
+
async function doInquire() {
|
18
|
+
// 1. whether overwrite?
|
19
|
+
let configExist = true;
|
20
|
+
try {
|
21
|
+
fs.accessSync('./zti18n.config.js');
|
22
|
+
} catch (e) {
|
23
|
+
configExist = false;
|
24
|
+
}
|
25
|
+
|
26
|
+
if (configExist) {
|
27
|
+
const ans = await inquirer.prompt([
|
28
|
+
{
|
29
|
+
name: 'overwrite',
|
30
|
+
type: 'confirm',
|
31
|
+
message: '配置文件 zti18n.config.js 已存在,是否覆盖?',
|
32
|
+
},
|
33
|
+
]);
|
34
|
+
|
35
|
+
if (!ans.overwrite) process.exit(0);
|
36
|
+
}
|
37
|
+
|
38
|
+
// 2. first i18n?
|
39
|
+
let ans = await inquirer.prompt([
|
40
|
+
{
|
41
|
+
name: 'firstI18n',
|
42
|
+
type: 'confirm',
|
43
|
+
message: '是否初次国际化?',
|
44
|
+
},
|
45
|
+
{
|
46
|
+
name: 'localePath',
|
47
|
+
type: 'input',
|
48
|
+
message: '请输入现有国际化资源路径:',
|
49
|
+
when(answers) {
|
50
|
+
return !answers.firstI18n;
|
51
|
+
},
|
52
|
+
},
|
53
|
+
]);
|
54
|
+
|
55
|
+
return ans;
|
56
|
+
}
|
57
|
+
|
58
|
+
module.exports = async function initFileConf(isVue) {
|
59
|
+
const answers = await doInquire();
|
60
|
+
const { localePath = 'locales', firstI18n } = answers;
|
61
|
+
|
62
|
+
const defaultOptions = isVue ? vueOptions : reactOptions;
|
63
|
+
const options = {
|
64
|
+
...defaultOptions,
|
65
|
+
localeConf: { type: 'file', folder: localePath },
|
66
|
+
};
|
67
|
+
|
68
|
+
// 配置信息写入文件
|
69
|
+
fs.writeFileSync(
|
70
|
+
'./zti18n.config.js',
|
71
|
+
prettier.format('module.exports = ' + JSON.stringify(options), {
|
72
|
+
parser: 'babel',
|
73
|
+
singleQuote: true,
|
74
|
+
trailingComma: 'es5',
|
75
|
+
}),
|
76
|
+
'utf8'
|
77
|
+
);
|
78
|
+
|
79
|
+
let createTasks = [];
|
80
|
+
const confService = new FileConf(options.localeConf.folder);
|
81
|
+
|
82
|
+
if (!firstI18n) {
|
83
|
+
// 非首次国际化,本地代码中已有国际化资源
|
84
|
+
const locales = getLocaleFiles({ path: localePath });
|
85
|
+
|
86
|
+
// 读取国际化资源
|
87
|
+
const data = locales.map((element) => {
|
88
|
+
// TODO: 支持国际化资源为 js 文件的情况,目前只支持为 json 文件。
|
89
|
+
const json = fs.readFileSync(element, {
|
90
|
+
encoding: 'utf-8',
|
91
|
+
});
|
92
|
+
|
93
|
+
// 使用现有文件名为语言 key
|
94
|
+
const key = path.parse(element).name;
|
95
|
+
|
96
|
+
return {
|
97
|
+
key,
|
98
|
+
value: JSON.parse(json),
|
99
|
+
confName: key,
|
100
|
+
};
|
101
|
+
});
|
102
|
+
|
103
|
+
createTasks = data.map(({ confName, value, key }) => {
|
104
|
+
let commentValue = {};
|
105
|
+
if (key !== options.primaryLocale) {
|
106
|
+
commentValue = data.find((d) => d.key === options.primaryLocale).value;
|
107
|
+
}
|
108
|
+
return confService.createConf(confName, value, commentValue, key);
|
109
|
+
});
|
110
|
+
} else {
|
111
|
+
// 首次国际化,只需要创建配置
|
112
|
+
createTasks = options.supportedLocales.map((key) => {
|
113
|
+
const value = { TBD: 'TBD' }; // 创建配置时,必须要有一个 k/V
|
114
|
+
const confName = key; // 初始化时,先创建测试环境的配置
|
115
|
+
return confService.createConf(confName, value, {}, key);
|
116
|
+
});
|
117
|
+
}
|
118
|
+
|
119
|
+
const createTaskResults = await Promise.all(createTasks);
|
120
|
+
log.success('[done] conf created');
|
121
|
+
console.log(createTaskResults);
|
122
|
+
|
123
|
+
// 将已有国际化资源的配置发布
|
124
|
+
if (!firstI18n) {
|
125
|
+
const publishTasks = options.supportedLocales.map((key) => {
|
126
|
+
return confService.publishConf(`${key}-test`);
|
127
|
+
});
|
128
|
+
|
129
|
+
const publishTaskResults = await Promise.all(publishTasks);
|
130
|
+
log.success('[done] conf published');
|
131
|
+
console.log(publishTaskResults);
|
132
|
+
}
|
133
|
+
};
|
@@ -1,24 +1,24 @@
|
|
1
|
-
const path = require('path');
|
2
|
-
const mergeOptions = require('../utils/mergeOptions');
|
3
|
-
const log = require('../utils/log');
|
4
|
-
|
5
|
-
module.exports = async function publish(programOption, env = '') {
|
6
|
-
const options = mergeOptions(programOption, ['locales']);
|
7
|
-
|
8
|
-
let confService = null;
|
9
|
-
if (options.localeConf.type === 'file') {
|
10
|
-
const FileConf = require('../conf/FileConf');
|
11
|
-
confService = new FileConf(options.locales);
|
12
|
-
} else {
|
13
|
-
const Conf = require(path.join(process.cwd(), options.localeConf.path));
|
14
|
-
confService = new Conf(options.localeConf);
|
15
|
-
}
|
16
|
-
|
17
|
-
const publishTasks = options.supportedLocales.map((key) => {
|
18
|
-
return confService.publishConf(`${key}-${env}`);
|
19
|
-
});
|
20
|
-
|
21
|
-
const publishTaskResults = await Promise.all(publishTasks);
|
22
|
-
log.success('[done] conf published');
|
23
|
-
console.log(publishTaskResults);
|
24
|
-
};
|
1
|
+
const path = require('path');
|
2
|
+
const mergeOptions = require('../utils/mergeOptions');
|
3
|
+
const log = require('../utils/log');
|
4
|
+
|
5
|
+
module.exports = async function publish(programOption, env = '') {
|
6
|
+
const options = mergeOptions(programOption, ['locales']);
|
7
|
+
|
8
|
+
let confService = null;
|
9
|
+
if (options.localeConf.type === 'file') {
|
10
|
+
const FileConf = require('../conf/FileConf');
|
11
|
+
confService = new FileConf(options.locales);
|
12
|
+
} else {
|
13
|
+
const Conf = require(path.join(process.cwd(), options.localeConf.path));
|
14
|
+
confService = new Conf(options.localeConf);
|
15
|
+
}
|
16
|
+
|
17
|
+
const publishTasks = options.supportedLocales.map((key) => {
|
18
|
+
return confService.publishConf(`${key}-${env}`);
|
19
|
+
});
|
20
|
+
|
21
|
+
const publishTaskResults = await Promise.all(publishTasks);
|
22
|
+
log.success('[done] conf published');
|
23
|
+
console.log(publishTaskResults);
|
24
|
+
};
|
@@ -1,21 +1,21 @@
|
|
1
|
-
module.exports = class BaseConf {
|
2
|
-
createService() {
|
3
|
-
return Promise.resolve(true);
|
4
|
-
}
|
5
|
-
|
6
|
-
createConf() {
|
7
|
-
throw new Error('`createConf` must be overrided');
|
8
|
-
}
|
9
|
-
|
10
|
-
updateConf() {
|
11
|
-
throw new Error('`updateConf` must be overrided');
|
12
|
-
}
|
13
|
-
|
14
|
-
getConf() {
|
15
|
-
throw new Error('`getConf` must be overrided');
|
16
|
-
}
|
17
|
-
|
18
|
-
publishConf() {
|
19
|
-
return Promise.resolve(true);
|
20
|
-
}
|
21
|
-
};
|
1
|
+
module.exports = class BaseConf {
|
2
|
+
createService() {
|
3
|
+
return Promise.resolve(true);
|
4
|
+
}
|
5
|
+
|
6
|
+
createConf() {
|
7
|
+
throw new Error('`createConf` must be overrided');
|
8
|
+
}
|
9
|
+
|
10
|
+
updateConf() {
|
11
|
+
throw new Error('`updateConf` must be overrided');
|
12
|
+
}
|
13
|
+
|
14
|
+
getConf() {
|
15
|
+
throw new Error('`getConf` must be overrided');
|
16
|
+
}
|
17
|
+
|
18
|
+
publishConf() {
|
19
|
+
return Promise.resolve(true);
|
20
|
+
}
|
21
|
+
};
|
@@ -1,116 +1,116 @@
|
|
1
|
-
const fs = require('fs');
|
2
|
-
const path = require('path');
|
3
|
-
const BaseConf = require('./BaseConf');
|
4
|
-
const log = require('../utils/log');
|
5
|
-
|
6
|
-
const cwdPath = process.cwd();
|
7
|
-
|
8
|
-
module.exports = class FileConf extends BaseConf {
|
9
|
-
constructor(folder) {
|
10
|
-
super();
|
11
|
-
this.localesDir = folder;
|
12
|
-
}
|
13
|
-
|
14
|
-
/**
|
15
|
-
* 创建一个服务
|
16
|
-
*/
|
17
|
-
createService() {
|
18
|
-
log.info('No need to create service in FileConf');
|
19
|
-
return super.createService();
|
20
|
-
}
|
21
|
-
|
22
|
-
/**
|
23
|
-
* 创建一个配置
|
24
|
-
* @param {string} confName 配置名称
|
25
|
-
* @param {object} values KV值
|
26
|
-
* @param {object} refValues 参数备注
|
27
|
-
* @param {string} key locales标识
|
28
|
-
*/
|
29
|
-
createConf(confName, values, refValues, key) {
|
30
|
-
const folder = this.localesDir.startsWith('/')
|
31
|
-
? this.localesDir
|
32
|
-
: path.join(cwdPath, this.localesDir);
|
33
|
-
|
34
|
-
try {
|
35
|
-
fs.accessSync(folder);
|
36
|
-
} catch (e) {
|
37
|
-
fs.mkdirSync(folder);
|
38
|
-
}
|
39
|
-
|
40
|
-
const configFilePath = path.join(folder, `${key}.json`);
|
41
|
-
return new Promise((resolve, reject) => {
|
42
|
-
fs.writeFile(configFilePath, JSON.stringify(values, null, 2), (err) => {
|
43
|
-
if (err) {
|
44
|
-
reject(err);
|
45
|
-
} else {
|
46
|
-
resolve(configFilePath);
|
47
|
-
}
|
48
|
-
});
|
49
|
-
});
|
50
|
-
}
|
51
|
-
|
52
|
-
/**
|
53
|
-
* 更新一个配置
|
54
|
-
* @param {string} confName 配置名称
|
55
|
-
* @param {object} values KV值
|
56
|
-
* @param {object} refValues 参数备注
|
57
|
-
* @param {string} key locales标识
|
58
|
-
*/
|
59
|
-
updateConf(confName, values, refValues, key) {
|
60
|
-
const configFilePath = path.join(cwdPath, this.localesDir, `${key}.json`);
|
61
|
-
return new Promise((resolve) => {
|
62
|
-
fs.writeFile(configFilePath, JSON.stringify(values, null, 2), (err) => {
|
63
|
-
if (err) {
|
64
|
-
resolve({
|
65
|
-
code: -1,
|
66
|
-
message: err.message,
|
67
|
-
});
|
68
|
-
} else {
|
69
|
-
resolve({
|
70
|
-
code: 0,
|
71
|
-
data: configFilePath,
|
72
|
-
});
|
73
|
-
}
|
74
|
-
});
|
75
|
-
});
|
76
|
-
}
|
77
|
-
|
78
|
-
/**
|
79
|
-
* 获取配置值
|
80
|
-
* @param {string} _ 配置名称
|
81
|
-
* @param {string} key key
|
82
|
-
*/
|
83
|
-
getConf(confName, key) {
|
84
|
-
const configFilePath = path.join(cwdPath, this.localesDir, `${key}.json`);
|
85
|
-
return new Promise((resolve, reject) => {
|
86
|
-
if (fs.existsSync(configFilePath)) {
|
87
|
-
let data = {};
|
88
|
-
try {
|
89
|
-
const content = fs.readFileSync(configFilePath);
|
90
|
-
data = content.length > 0 ? JSON.parse(content) : {};
|
91
|
-
resolve({
|
92
|
-
code: 0,
|
93
|
-
data: Object.keys(data).map((k) => ({
|
94
|
-
key: k,
|
95
|
-
value: data[k],
|
96
|
-
})),
|
97
|
-
});
|
98
|
-
} catch (err) {
|
99
|
-
reject(
|
100
|
-
new Error(`请检查 ${configFilePath} 资源文件 JSON 格式是否正确`)
|
101
|
-
);
|
102
|
-
}
|
103
|
-
} else {
|
104
|
-
reject(new Error(`资源文件 ${configFilePath} 不存在`));
|
105
|
-
}
|
106
|
-
});
|
107
|
-
}
|
108
|
-
|
109
|
-
/**
|
110
|
-
* 发布指定的配置
|
111
|
-
*/
|
112
|
-
publishConf() {
|
113
|
-
log.info('No need to publish in FileConf');
|
114
|
-
return super.publishConf();
|
115
|
-
}
|
116
|
-
};
|
1
|
+
const fs = require('fs');
|
2
|
+
const path = require('path');
|
3
|
+
const BaseConf = require('./BaseConf');
|
4
|
+
const log = require('../utils/log');
|
5
|
+
|
6
|
+
const cwdPath = process.cwd();
|
7
|
+
|
8
|
+
module.exports = class FileConf extends BaseConf {
|
9
|
+
constructor(folder) {
|
10
|
+
super();
|
11
|
+
this.localesDir = folder;
|
12
|
+
}
|
13
|
+
|
14
|
+
/**
|
15
|
+
* 创建一个服务
|
16
|
+
*/
|
17
|
+
createService() {
|
18
|
+
log.info('No need to create service in FileConf');
|
19
|
+
return super.createService();
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* 创建一个配置
|
24
|
+
* @param {string} confName 配置名称
|
25
|
+
* @param {object} values KV值
|
26
|
+
* @param {object} refValues 参数备注
|
27
|
+
* @param {string} key locales标识
|
28
|
+
*/
|
29
|
+
createConf(confName, values, refValues, key) {
|
30
|
+
const folder = this.localesDir.startsWith('/')
|
31
|
+
? this.localesDir
|
32
|
+
: path.join(cwdPath, this.localesDir);
|
33
|
+
|
34
|
+
try {
|
35
|
+
fs.accessSync(folder);
|
36
|
+
} catch (e) {
|
37
|
+
fs.mkdirSync(folder);
|
38
|
+
}
|
39
|
+
|
40
|
+
const configFilePath = path.join(folder, `${key}.json`);
|
41
|
+
return new Promise((resolve, reject) => {
|
42
|
+
fs.writeFile(configFilePath, JSON.stringify(values, null, 2), (err) => {
|
43
|
+
if (err) {
|
44
|
+
reject(err);
|
45
|
+
} else {
|
46
|
+
resolve(configFilePath);
|
47
|
+
}
|
48
|
+
});
|
49
|
+
});
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* 更新一个配置
|
54
|
+
* @param {string} confName 配置名称
|
55
|
+
* @param {object} values KV值
|
56
|
+
* @param {object} refValues 参数备注
|
57
|
+
* @param {string} key locales标识
|
58
|
+
*/
|
59
|
+
updateConf(confName, values, refValues, key) {
|
60
|
+
const configFilePath = path.join(cwdPath, this.localesDir, `${key}.json`);
|
61
|
+
return new Promise((resolve) => {
|
62
|
+
fs.writeFile(configFilePath, JSON.stringify(values, null, 2), (err) => {
|
63
|
+
if (err) {
|
64
|
+
resolve({
|
65
|
+
code: -1,
|
66
|
+
message: err.message,
|
67
|
+
});
|
68
|
+
} else {
|
69
|
+
resolve({
|
70
|
+
code: 0,
|
71
|
+
data: configFilePath,
|
72
|
+
});
|
73
|
+
}
|
74
|
+
});
|
75
|
+
});
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* 获取配置值
|
80
|
+
* @param {string} _ 配置名称
|
81
|
+
* @param {string} key key
|
82
|
+
*/
|
83
|
+
getConf(confName, key) {
|
84
|
+
const configFilePath = path.join(cwdPath, this.localesDir, `${key}.json`);
|
85
|
+
return new Promise((resolve, reject) => {
|
86
|
+
if (fs.existsSync(configFilePath)) {
|
87
|
+
let data = {};
|
88
|
+
try {
|
89
|
+
const content = fs.readFileSync(configFilePath);
|
90
|
+
data = content.length > 0 ? JSON.parse(content) : {};
|
91
|
+
resolve({
|
92
|
+
code: 0,
|
93
|
+
data: Object.keys(data).map((k) => ({
|
94
|
+
key: k,
|
95
|
+
value: data[k],
|
96
|
+
})),
|
97
|
+
});
|
98
|
+
} catch (err) {
|
99
|
+
reject(
|
100
|
+
new Error(`请检查 ${configFilePath} 资源文件 JSON 格式是否正确`)
|
101
|
+
);
|
102
|
+
}
|
103
|
+
} else {
|
104
|
+
reject(new Error(`资源文件 ${configFilePath} 不存在`));
|
105
|
+
}
|
106
|
+
});
|
107
|
+
}
|
108
|
+
|
109
|
+
/**
|
110
|
+
* 发布指定的配置
|
111
|
+
*/
|
112
|
+
publishConf() {
|
113
|
+
log.info('No need to publish in FileConf');
|
114
|
+
return super.publishConf();
|
115
|
+
}
|
116
|
+
};
|