66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
export function showWarn(message: string) {
|
|
console.error('\x1b[33m%s\x1b[0m', `Warn: ${message}`);
|
|
}
|
|
|
|
export function showInfo(message: string) {
|
|
console.info('\x1b[32m%s\x1b[0m', message);
|
|
}
|
|
|
|
const GLTF_EXTENSIONS = new Set(['.gltf', '.glb']);
|
|
|
|
export function isGLTFFile(filePath: string) {
|
|
return GLTF_EXTENSIONS.has(path.extname(filePath).toLowerCase());
|
|
}
|
|
|
|
export async function readFileJson(filePath: string): Promise<object> {
|
|
return new Promise((resolve, reject) => {
|
|
fs.readFile(filePath, { encoding: 'utf8' }, (error, content) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
|
|
resolve(JSON.parse(content));
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function readFileBuffer(filePath: string): Promise<Buffer> {
|
|
return new Promise((resolve, reject) => {
|
|
fs.readFile(filePath, (error, content) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
|
|
resolve(content);
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function writeFile(filePath: string, buffer: Buffer | string): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const dirPath = path.dirname(filePath);
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
|
|
fs.writeFile(
|
|
filePath,
|
|
buffer,
|
|
typeof buffer === 'string' ? { encoding: 'utf8' } : {},
|
|
(error) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
|
|
resolve(filePath);
|
|
}
|
|
);
|
|
});
|
|
}
|