1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| const fs = require('fs'); const path = require('path'); const ignoreItem = [];
function checkOrCreateDir(dirPath, dir, create = true) { let target = path.resolve(dirPath, './' + dir); let dirStatus = fs.existsSync(target); if (!dirStatus && create) { fs.mkdir(target, () => { console.log(dir, '文件夹创建成功!') }); return true; } else { return dirStatus; } }
function modifyProjectConfig(targetPath, config) { let item = path.resolve(targetPath, './project.config.json'); fs.readFile(item, (err, data) => { let projectConfig = data.toString(); projectConfig = JSON.parse(projectConfig); projectConfig['appid'] = config.appid; projectConfig['projectname'] = config.projectname;
var str = JSON.stringify(projectConfig); fs.writeFile(item, str, (err) => { if (err) { console.error(err); } console.log('project.config.json 修改成功!'); }) }) };
function modifyConfig(targetPath, config) { let item = path.resolve(targetPath, './config.js'); fs.writeFile(item, `export const shop_id = ${config.shopid};export const qqMapKey = 'SWLBZ-WKWCJ-SKLFP-FBAGY-2M3WE-SBFAI';`, (err) => { if (err) { console.error(err); } console.log('config.js 操作成功!'); }) }
function checkTemplate(templatePath, targetPath) { let arr = fs.readdirSync(templatePath); for (let i = 0, l = arr.length; i < l; i++) { let itemPath = path.resolve(templatePath, './' + arr[i]); let targetItemPath = path.resolve(targetPath, './' + arr[i]); let fileStatus = fs.statSync(itemPath).isFile(); if (ignoreItem.includes(arr[i])) continue; if (fileStatus) { copyFile(itemPath, targetItemPath) } else { let status = checkOrCreateDir(targetPath, arr[i], true); if (status) { checkTemplate(itemPath, targetItemPath) }; } } }
function copyFile(src, target) { fs.copyFileSync(src, target); console.log(target, '文件创建成功!') } const templateFolder = './template'; const targetFolder = './miniprogram';
module.exports = { checkOrCreateDir, modifyProjectConfig, modifyConfig, checkTemplate, copyFile, templateFolder, targetFolder }
|