export const packageJSON = require('../package.json'); export const fs = require('fs'); export const path = require('path'); export const resolve = (pathStr: any) => { return path.resolve(__dirname, pathStr); } const mdFileList = fs .readdirSync(resolve('../'), { withFileTypes: true, encoding: 'utf8' }) .reduce((t: any[], c: { isFile: () => any; name: string; }) => { if (c.isFile() && /\.md$/.test(c.name)) { t.push(path.resolve(c.name)); } return t; }, []); const genMainFile = () => { const pathStr = resolve('../assets/packageJSON/') fs .readdirSync(pathStr, { withFileTypes: true, encoding: 'utf8'}) .forEach((item: any) => { if (!item.isFile() || !/.json$/.test(item.name)) { return null; } const obj = require(path.resolve(pathStr, item.name)); Object.entries(obj).forEach(([k, v]) => { if (v === '$VAR$') { obj[k] = packageJSON[k]; } }) const [, pathName] = item.name.match(/^(.*)\.json$/); console.log(`created package.json from ${item.name}`); mdFileList.forEach((mdFilePath: any) => { const [, fileName] = mdFilePath.match(/^.*\\(.+)$/); // windows下用该命令 // const [, fileName] = mdFilePath.match(/^.*\/(.+)$/); // unix下用该命令 fs.copyFileSync(mdFilePath, resolve(`../dist/${fileName}`)); }) fs.writeFileSync(resolve(`../dist/package.json`), JSON.stringify(obj, null, '\t')); }) } genMainFile();