-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(converter): convert to multiple files
- Loading branch information
Showing
4 changed files
with
66 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import chalk from 'chalk'; | ||
import * as path from 'path'; | ||
import { svgo } from '../svgo'; | ||
|
||
import { | ||
generateInterfaceDefinition, | ||
generateSvgConstant, | ||
generateTypeDefinition, | ||
generateTypeName, | ||
generateVariableName | ||
} from '../generators/generators'; | ||
import { getFilePathsFromRegex } from '../regex-helpers'; | ||
import { extractSvgContent, writeIconsFile } from '../file-helpers'; | ||
import { ConvertionOptions } from '../../bin/svg-to-ts'; | ||
|
||
const typesDelimitor = ' | '; | ||
|
||
export const convertToMultipleFiles = async (convertionOptions: ConvertionOptions): Promise<void> => { | ||
const { typeName, prefix, delimiter, interfaceName, outputDirectory, srcFiles, fileName } = convertionOptions; | ||
let svgConstants = ''; | ||
let types = generateTypeDefinition(typeName); | ||
|
||
try { | ||
const filePaths = await getFilePathsFromRegex(srcFiles); | ||
for (let i = 0; i < filePaths.length; i++) { | ||
const fileNameWithEnding = path.basename(filePaths[i]); | ||
const [filenameWithoutEnding, extension] = fileNameWithEnding.split('.'); | ||
|
||
if (extension === 'svg') { | ||
const rawSvg = await extractSvgContent(filePaths[i]); | ||
const optimizedSvg = await svgo.optimize(rawSvg); | ||
const variableName = generateVariableName(prefix, filenameWithoutEnding); | ||
const typeName = generateTypeName(filenameWithoutEnding, delimiter); | ||
const svgConstant = generateSvgConstant(variableName, interfaceName, typeName, optimizedSvg.data); | ||
|
||
await writeIconsFile(outputDirectory, `${prefix}-${filenameWithoutEnding}.icon`, svgConstant); | ||
|
||
svgConstants += svgConstant; | ||
types += i === filePaths.length - 1 ? `'${typeName}';` : `'${typeName}'${typesDelimitor}`; | ||
} | ||
} | ||
|
||
/* | ||
if (svgConstants !== '') { | ||
const fileContent = (svgConstants += types += generateInterfaceDefinition(interfaceName, typeName)); | ||
await writeIconsFile(outputDirectory, fileName, fileContent); | ||
console.log( | ||
chalk.blue.bold('svg-to-ts:'), | ||
chalk.green('Icons file successfully generated under'), | ||
chalk.green.underline(outputDirectory) | ||
); | ||
} | ||
*/ | ||
} catch (error) { | ||
console.log(chalk.blue.bold('svg-to-ts:'), chalk.red('Something went wrong', error)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters