UNPKG

fy-convertor

Version:

Convert excel/xml/json/bin/protocl/ts/as ...

159 lines 4.58 kB
import crypto from 'crypto'; import fg from 'fast-glob'; import fs from 'fs-extra'; import path from 'path'; import chardet from 'chardet'; import iconv from 'iconv-lite'; import { spawn } from 'child_process'; export function gb2byte(gb) { return gb * 1024 * 1024 * 1024; } export function getValues(o, keys) { if (keys.length == 0) return [o]; const out = []; const key = keys[0]; let so = o[key]; if (keys.length == 1) { if (so instanceof Array) { out.push(...so); } else { out.push(so); } } else { const leftKeys = keys.slice(1); if (so instanceof Array) { for (const sov of so) { out.push(...getValues(sov, leftKeys)); } } else { out.push(...getValues(so, leftKeys)); } } return out; } export function joinValues(o, keys) { const values = []; for (const key of keys) { values.push(o[key]); } return values.join('_'); } export function makeComments(comments) { if (comments.length == 1) { return `/**${comments[0]}*/`; } let c = '/**\n'; for (const s of comments) { c += ` * ${s}\n`; } c += '*/'; return c; } export function findFiles(root, exts, mode) { let out = []; if (exts) { for (let i = 0, len = exts.length; i < len; i++) { exts[i] = exts[i].toLowerCase(); } } findFilesInternal(root, exts, mode, out); return out; } function findFilesInternal(root, exts, mode, out) { const files = fs.readdirSync(root); for (let f of files) { let file = path.join(root, f); if (fs.statSync(file).isDirectory()) { findFilesInternal(file, exts, mode, out); } else if (!exts || (mode == '+') == exts.includes(path.extname(f).toLowerCase())) { out.push(file); } } } export async function copyFiles(src, dst, pattern, ignore) { const files = await fg(pattern, { cwd: src, ignore }); for (let f of files) { await fs.copyFile(path.join(src, f), path.join(dst, f)); } } export function readGB2312(file) { let content = fs.readFileSync(file); return iconv.decode(content, 'gbk'); } export async function readXml(file) { const content = await fs.readFile(file); const encoding = chardet.detect(content); if (encoding == 'UTF-8') { return await fs.readFile(file, 'utf-8'); } return iconv.decode(content, 'gbk'); } export function md5(str) { const md5 = crypto.createHash('md5'); md5.update(str); str = md5.digest('hex'); return str; } export function rsubstr(str, from, length) { let slen = str.length; let r = slen - from; let l = -1; if (length) { l = r - length; } if (l < 0) { l = 0; } return str.substring(l, r); } export function writeUTF8withBom(file, content) { fs.writeFileSync(file, '\ufeff' + content, 'utf-8'); } export function indent(s, blanks = ' ', startLine = 0) { let lines = s.split(/\r?\n/); for (let i = startLine, len = lines.length; i < len; i++) { let l = lines[i]; if (l) { lines[i] = blanks + l; } } return lines.join('\n'); } export function count(s, subs) { let count = 0; let idx = s.indexOf(subs); while (idx >= 0) { count++; s = s.substring(idx + subs.length); idx = s.indexOf(subs); } return count; } export function runCommand(command, args = [], options = {}) { return new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: options.cwd, env: options.env ? { ...process.env, ...options.env } : process.env, shell: options.shell ?? false, windowsHide: true }); let output = ''; child.stdout.on('data', (data) => output += data.toString()); child.stderr.on('data', (data) => output += data.toString()); child.on('error', reject); child.on('close', (code) => { if (code == 0) { resolve(output); return; } const fullCommand = [command, ...args].join(' '); reject(new Error(`${fullCommand} failed with code ${code}\n${output}`)); }); }); } //# sourceMappingURL=vendor.js.map