import {getConfig} from "../config"; import {stat, writeFile} from "fs"; import {extname, join} from "path"; import {TemplateGenerator} from "./template_generator"; import {checkFolders} from "../shared/check_folder"; import {importHandler} from "./import_handler"; export class Make { static page(params): void { if (params.length < 1) { throw new Error(`Please supply a name for the new page`); } const {src, ext} = getConfig(); params.map((name: string) => { const pageDir: string = join(src, 'pages'); const dir: string = join(pageDir, name); const files: { name: string, content: string }[] = [ {name: 'index.js', content: TemplateGenerator.page(name, ext)}, {name: `${name}.${ext.templates}`, content: ''}, ]; checkFolders([src, pageDir, dir]); files.map((file) => { const filePath: string = join(dir, file.name); stat(filePath, (error) => { if (error === null) { throw new Error(`The page ${name} already exists at ${dir}`); } else if (error.code === 'ENOENT') { writeFile(filePath, file.content, (error) => { if (error) throw error; }); } else { throw error; } }); }); }); } static item(type, params): void { if (params.length < 1) { throw new Error(`Please supply a name for the new ${type}`); } const {src, ext} = getConfig(); const dir: string = join(src, `${type}s`); params.map((name: string) => { const files: { name: string, folder: string, content: string }[] = [ {name: 'index.js', folder: '.', content: TemplateGenerator.item(name, ext)}, {name: 'README.md', folder: '.', content: `# ${name}`}, {name: `${name}.${ext.templates}`, folder: '.', content: ''}, ]; if (ext.styles) { files.push({name: `${name}.${ext.styles}`, folder: './styles', content: ''}); } if (ext.scripts) { files.push({name: `${name}.${ext.scripts}`, folder: './scripts', content: ''}); } files.map((file) => { const fileFolder: string = join(dir, name, file.folder); const filePath: string = join(dir, name, file.folder, file.name); checkFolders([src, dir, fileFolder]); const fileExt: string = extname(file.name).replace(/\./, ''); if (fileExt === ext.styles || fileExt === ext.scripts) { importHandler(file.name, type, name); } stat(filePath, (error) => { if (error === null) { throw new Error(`The ${type} ${name} already exists at ${dir}`); } else if (error.code === 'ENOENT') { writeFile(filePath, file.content, (error) => { if (error) throw error; }); } else { throw error; } }); }); }); } }