1 Star 6 Fork 1

xavier/node-hiprint-transit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
init.js 3.96 KB
一键复制 编辑 原始数据 按行查看 历史
xavier 提交于 2024-12-17 19:55 +08:00 . chore: add prettier and prettier project
/*
* @Date: 2023-09-29 13:52:41
* @LastEditors: admin@54xavier.cn
* @LastEditTime: 2024-07-22 15:02:09
* @FilePath: /node-hiprint-transit/init.js
*/
import path from 'node:path';
import readline from 'node:readline';
import inquirer from 'inquirer';
import { fileURLToPath } from 'node:url';
import { writeConfig } from './src/config.js';
import { I18n } from 'i18n';
// ES Module need use fileURLToPath to get __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Setup readline
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Default config
const CONFIG = {
port: 17521, // port
token: 'vue-plugin-hiprint', // TOKEN
useSSL: false, // SSL on/off
lang: 'en', // language
};
// Setup i18n
const i18n = new I18n({
locales: ['en', 'zh'],
directory: path.join(__dirname, './src/locales'),
defaultLocale: 'en',
});
/**
* @description: Set language
* @return {Promise}
*/
function setLang() {
return new Promise((resolve) => {
inquirer
.prompt([
{
name: 'lang',
type: 'list',
message: 'Set language 设置语言 ',
choices: [
{
name: 'English',
value: 'en',
},
{
name: '简体中文',
value: 'zh',
},
],
},
])
.then((answers) => {
CONFIG.lang = answers.lang;
i18n.setLocale(CONFIG.lang);
resolve();
});
});
}
/**
* @description: Set port
* @return {Promise}
*/
function setPort() {
return new Promise((resolve) => {
inquirer
.prompt([
{
name: 'port',
type: 'input',
message: i18n.__('Set serve port %s:', '10000~65535'),
default: 17521,
validate: (input) => {
if (
input &&
/^\d+$/.test(input) &&
input >= 10000 &&
input <= 65535
) {
return true;
} else if (!input) {
return true;
} else {
return i18n.__('Port must be set between %s', '10000 and 65535');
}
},
},
])
.then((answers) => {
CONFIG.port = answers.port * 1;
resolve();
});
});
}
/**
* @description: Set token
* @return {Promise}
*/
function setToken() {
return new Promise((resolve) => {
inquirer
.prompt([
{
name: 'token',
type: 'input',
message: i18n.__(
'Set service TOKEN (Use the wildcard character (*) to match any character):',
),
default: 'vue-plugin-hiprint',
validate: (input) => {
if (input && input.length >= 6) {
return true;
} else if (!input) {
return true;
} else {
return i18n.__(
'For security reasons, the TOKEN length must be greater than 5',
);
}
},
},
])
.then((answers) => {
CONFIG.token = answers.token;
resolve();
});
});
}
/**
* @description: Set SSL
* @return {Promise}
*/
function setSSL() {
return new Promise((resolve) => {
inquirer
.prompt([
{
name: 'ssl',
type: 'confirm',
message: i18n.__('Use SSL:'),
default: false,
},
])
.then((answers) => {
CONFIG.useSSL = answers.ssl;
resolve();
});
});
}
setLang().then(() => {
setPort().then(() => {
setToken().then(() => {
setSSL().then(() => {
writeConfig(CONFIG)
.then(() => {
console.log(i18n.__('Configuration file written successfully'));
})
.catch(() => {
console.error(i18n.__('Configuration file write failed'));
})
.finally(() => {
rl.close();
});
});
});
});
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/xavier9896/node-hiprint-transit.git
git@gitee.com:xavier9896/node-hiprint-transit.git
xavier9896
node-hiprint-transit
node-hiprint-transit
main

搜索帮助