@ainc/script
Version:
Script compiler for typescript
36 lines (28 loc) • 989 B
text/typescript
/**
*****************************************
* Created by edonet@163.com
* Created on 2022-01-01 20:45:29
*****************************************
*/
'use strict';
/**
*****************************************
* 加载依赖
*****************************************
*/
import * as fs from 'fs';
import { stripBOM } from '@ainc/fs/dist/helpers/stripBOM';
import { stripComments } from '@ainc/fs/dist/helpers/stripComments';
/**
*****************************************
* 解析【JSON】文件
*****************************************
*/
export function json(script: { exports?: unknown }, filename: string): void {
const names = ['tsconfig.json', 'jsconfig.json', '.config.json', 'rc.json'];
const jsonc = !!names.find(name => filename.endsWith(name));
const code = fs.readFileSync(filename, 'utf8');
const normalized = jsonc ? stripComments(code) : stripBOM(code);
// 解析内容
script.exports = JSON.parse(normalized.trim() || '{}');
}