|
| 1 | +import { execa } from 'execa'; |
| 2 | +import { Options } from '../types.js'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as ejs from 'ejs'; |
| 5 | +import * as path from 'path'; |
| 6 | +import { stringCamelCase } from '@dedot/utils'; |
| 7 | +import { IS_IGNORE_FILES, IS_TEMPLATE_FILE } from '../utils/index.js'; |
| 8 | + |
| 9 | +export async function copyTemplateFiles(options: Options, templatesDir: string, targetDir: string) { |
| 10 | + const { projectName, noGit, template } = options; |
| 11 | + |
| 12 | + const templateDir = `${templatesDir}/${template}`; |
| 13 | + |
| 14 | + if (!fs.existsSync(templateDir)) { |
| 15 | + throw new Error(`Template directory not found: ${templateDir}`); |
| 16 | + } |
| 17 | + |
| 18 | + await fs.promises.cp(templateDir, targetDir, { recursive: true }); |
| 19 | + |
| 20 | + const packageJsonPath = `${targetDir}/package.json`; |
| 21 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 22 | + |
| 23 | + packageJson.name = projectName; |
| 24 | + await fs.promises.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 25 | + |
| 26 | + processPresetContract(options, targetDir); |
| 27 | + processTemplateFiles(options, targetDir); |
| 28 | + |
| 29 | + if (!noGit) { |
| 30 | + await execa('git', ['init'], { cwd: targetDir }); |
| 31 | + await execa('git', ['checkout', '-b', 'main'], { cwd: targetDir }); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export async function processPresetContract(options: Options, targetDir: string) { |
| 36 | + const dirsToCheck = [`${targetDir}/contracts/artifacts`, `${targetDir}/contracts/types`]; |
| 37 | + |
| 38 | + dirsToCheck.forEach(async (dir) => { |
| 39 | + for (const file of await fs.promises.readdir(dir, { withFileTypes: true })) { |
| 40 | + if (file.name === options.presetContract) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + await fs.promises.rm(path.join(dir, file.name), { recursive: true }); |
| 45 | + } |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +export async function processTemplateFiles(rawOptions: Options, targetDir: string) { |
| 50 | + const options = { |
| 51 | + ...rawOptions, |
| 52 | + networks: rawOptions.networks?.map(stringCamelCase), |
| 53 | + }; |
| 54 | + |
| 55 | + await processTemplateFilesRecursive(options, targetDir); |
| 56 | +} |
| 57 | + |
| 58 | +async function processTemplateFilesRecursive(options: any, dir: string) { |
| 59 | + if (IS_IGNORE_FILES.test(dir)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const files = await fs.promises.readdir(dir, { withFileTypes: true }); |
| 64 | + |
| 65 | + for (const file of files) { |
| 66 | + const filePath = path.join(dir, file.name); |
| 67 | + |
| 68 | + if (file.isDirectory()) { |
| 69 | + await processTemplateFilesRecursive(options, filePath); |
| 70 | + } else { |
| 71 | + if (IS_TEMPLATE_FILE.test(filePath)) { |
| 72 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 73 | + const result = ejs.render(content, { options }); |
| 74 | + |
| 75 | + if (result.trim() !== '') { |
| 76 | + await fs.promises.writeFile(filePath.replace('.template.ejs', ''), result); |
| 77 | + } |
| 78 | + |
| 79 | + await fs.promises.rm(filePath); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments