t-comm
Version:
专业、稳定、纯粹的工具库
111 lines (108 loc) • 3.94 kB
JavaScript
import { statSync, readFileSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
var kMinutes = 601000;
var kDataFile = 't-comm-install.data';
function getNpmTipTemplate(_a) {
var prefix = _a.prefix,
link = _a.link,
postfix = _a.postfix,
feedbackTitle = _a.feedbackTitle,
feedbackList = _a.feedbackList;
return "".concat(colorFactory(96)).concat(prefix, " (").concat(colorFactory(94), " ").concat(link, " ").concat(colorFactory(96), ") ").concat(postfix).concat(colorFactory(), "\n\n") + "".concat(colorFactory(96)).concat(feedbackTitle).concat(colorFactory(), "\n") + "".concat(feedbackList.map(function (feedback) {
return "".concat(colorFactory(96), ">").concat(colorFactory(94), " ").concat(feedback, " ").concat(colorFactory());
}).join('\n'), "\n");
}
function getNpmTips(_a) {
var packageName = _a.packageName,
packageLink = _a.packageLink,
packagePostfix = _a.packagePostfix,
packagePostfixEn = _a.packagePostfixEn,
feedbackList = _a.feedbackList;
var content = isUtf8Encoding() ? getNpmTipTemplate({
prefix: "\u611F\u8C22\u60A8\u4F7F\u7528".concat(packageName),
link: packageLink,
postfix: "".concat(packagePostfix, "\uFF01"),
feedbackTitle: '如有任何疑问或建议,可通过下述渠道向我们反馈:',
feedbackList: feedbackList
}) : getNpmTipTemplate({
prefix: "Thank you for using ".concat(packageName),
link: packageLink,
postfix: "".concat(packagePostfixEn, "!"),
feedbackTitle: 'If you have any issue or advice, you can give us feedback:',
feedbackList: feedbackList
});
return content;
}
function not(it) {
return !it || it === '0' || it === 'false';
}
function colorFactory(color) {
if (!process.stdout.hasColors()) {
return '';
}
return "\x1B[".concat(color !== null && color !== void 0 ? color : 0, "m");
}
function isUtf8Encoding() {
var _a;
var _b = process.env,
LANG = _b.LANG,
LC_CTYPE = _b.LC_CTYPE;
var language = ((_a = LANG !== null && LANG !== void 0 ? LANG : LC_CTYPE) !== null && _a !== void 0 ? _a : '').toLowerCase();
if (language.includes('utf-8') || language.includes('utf8')) {
return true;
}
if (process.platform === 'win32') {
return true;
}
return false;
}
function isShowInfo(kContent) {
// eslint-disable-next-line @typescript-eslint/naming-convention
var _a = process.env,
ADBLOCK = _a.ADBLOCK,
CI = _a.CI,
DISABLE_OPENCOLLECTIVE = _a.DISABLE_OPENCOLLECTIVE,
OPEN_SOURCE_CONTRIBUTOR = _a.OPEN_SOURCE_CONTRIBUTOR,
npm_config_loglevel = _a.npm_config_loglevel;
var isSilent = typeof npm_config_loglevel === 'string' && ['silent', 'error', 'warn'].includes(npm_config_loglevel);
if (not(ADBLOCK) && not(CI) && not(DISABLE_OPENCOLLECTIVE) && not(OPEN_SOURCE_CONTRIBUTOR) && !isSilent) {
var file = join(tmpdir(), kDataFile);
var contents = [];
try {
var delta = Date.now() - statSync(file).mtime.getTime();
if (delta >= 0 && delta < 3 * kMinutes) {
contents = JSON.parse(readFileSync(file, 'utf8'));
// if (contents.includes(kContent)) {
// return false;
// }
}
} catch (_b) {
contents = [];
}
try {
contents.push(kContent);
writeFileSync(file, JSON.stringify(contents), 'utf8');
} catch (/* (empty function) */_c) {/* (empty function) */}
return true;
}
return false;
}
function npmInstallTip(_a) {
var packageName = _a.packageName,
packageLink = _a.packageLink,
packagePostfix = _a.packagePostfix,
packagePostfixEn = _a.packagePostfixEn,
feedbackList = _a.feedbackList;
var content = getNpmTips({
packageName: packageName,
packageLink: packageLink,
packagePostfix: packagePostfix,
packagePostfixEn: packagePostfixEn,
feedbackList: feedbackList
});
if (isShowInfo(content)) {
console.log(content);
}
}
export { npmInstallTip };