import {appendFileSync, existsSync, writeFileSync} from "fs"; import {extname, join} from "path"; import {getConfig} from "../config"; function styleImport(type: string, name: string, ext: string): string { return `@import './${type}/${name}/styles/${name + ext}';\n`; } function scriptImport(name: string, ext: string): string { return `module.exports.${name.replace(' ', '-')} = require('./${name}/scripts/${name + ext}');\n`; } function appendImport(file: string, text: string): void { if (existsSync(file)) { appendFileSync(file, text); } else { writeFileSync(file, text); } } export function importHandler(fileName: string, type: string, name: string): void { const ext: string = extname(fileName); const {src} = getConfig(); const file: string = join(src, type + ext); if (ext === '.scss' || ext === '.less') { appendImport(file, styleImport(`${type}s`, name, ext)); } else if (fileName === `${name}.js`) { appendImport(file, scriptImport(name, ext)); } }