Skip to content

Commit

Permalink
feat(converter): convert to multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Mar 3, 2020
1 parent 4b5e9e5 commit fe13a4c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/bin/svg-to-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as packgeJSON from '../../package.json';
import commander from 'commander';
import { Delimiter } from '../lib/generators/generators';
import { convertToSingleFile } from '../lib/converters/single-file.converter';
import { convertToMultipleFiles } from '../lib/converters/multiple-files.converter';

export interface ConvertionOptions {
delimiter: Delimiter;
Expand Down Expand Up @@ -62,4 +63,5 @@ const convertionOptions = {
outputDirectory
};

convertToSingleFile(convertionOptions);
// convertToSingleFile(convertionOptions);
convertToMultipleFiles(convertionOptions);
57 changes: 57 additions & 0 deletions src/lib/converters/multiple-files.converter.ts
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));
}
};
4 changes: 2 additions & 2 deletions src/lib/converters/single-file.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ConvertionOptions } from '../../bin/svg-to-ts';
const typesDelimitor = ' | ';

export const convertToSingleFile = async (convertionOptions: ConvertionOptions): Promise<void> => {
const { typeName, prefix, delimiter, interfaceName, outputDirectory, srcFiles } = convertionOptions;
const { typeName, prefix, delimiter, interfaceName, outputDirectory, srcFiles, fileName } = convertionOptions;
let svgConstants = '';
let types = generateTypeDefinition(typeName);

Expand All @@ -40,7 +40,7 @@ export const convertToSingleFile = async (convertionOptions: ConvertionOptions):

if (svgConstants !== '') {
const fileContent = (svgConstants += types += generateInterfaceDefinition(interfaceName, typeName));
await writeIconsFile(convertionOptions, fileContent);
await writeIconsFile(outputDirectory, fileName, fileContent);
console.log(
chalk.blue.bold('svg-to-ts:'),
chalk.green('Icons file successfully generated under'),
Expand Down
11 changes: 4 additions & 7 deletions src/lib/file-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ export const extractSvgContent = async (filePath: string): Promise<string> => {
return fileContentRaw.replace(/\r?\n|\r/g, ' ');
};

export const writeIconsFile = async (convertionOptions: ConvertionOptions, fileContent: string): Promise<void> => {
export const writeIconsFile = async (outputDirectory: string, fileName: string, fileContent: string): Promise<void> => {
const formatedFileContent = formatContent(fileContent);
if (!fs.existsSync(convertionOptions.outputDirectory)) {
fs.mkdirSync(convertionOptions.outputDirectory);
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory);
}
await writeFile(
path.join(convertionOptions.outputDirectory, `${convertionOptions.fileName}.ts`),
formatedFileContent
);
await writeFile(path.join(outputDirectory, `${fileName}.ts`), formatedFileContent);
};

const formatContent = (fileContent: string) =>
Expand Down

0 comments on commit fe13a4c

Please sign in to comment.