Skip to content

Commit

Permalink
Merge pull request #243 from JoA-MoS/adds-type-keyword-to-import
Browse files Browse the repository at this point in the history
feat: updates generateTypeHelper to use type keyword and updates prettier
  • Loading branch information
nivekcode authored Feb 18, 2024
2 parents d08f56a + 2f3d9ee commit 606097e
Show file tree
Hide file tree
Showing 31 changed files with 1,254 additions and 924 deletions.
1,758 changes: 1,039 additions & 719 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@
"lodash.kebabcase": "^4.1.1",
"lodash.snakecase": "^4.1.1",
"ora": "^5.1.0",
"prettier": "^1.19.1",
"prettier": "^3.2.5",
"svgo": "^3.0.2",
"typescript": "^4.9.4"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
"@semantic-release/changelog": "^3.0.1",
"@semantic-release/exec": "^3.3.2",
"@semantic-release/git": "^7.0.6",
Expand All @@ -116,7 +116,7 @@
"husky": "^3.1.0",
"import-conductor": "^2.0.3",
"jest": "^24.9.0",
"pretty-quick": "^2.0.1",
"pretty-quick": "^4.0.0",
"replace-json-property": "^1.4.1",
"semantic-release": "^15.13.31",
"ts-jest": "^24.2.0",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/compiler/typescript-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const compileToEsNext = (filePaths: string[], outputDir: string): void =>
outDir: outputDir,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
target: ts.ScriptTarget.ES2020,
module: ts.ModuleKind.ESNext
module: ts.ModuleKind.ESNext,
};

ts.createProgram(filePaths, compilerOptionsNext).emit();
Expand All @@ -23,7 +23,7 @@ export const addMJsExtensionToImportStatements = (outputDir: string): void => {
return;
}

children.forEach(file => {
children.forEach((file) => {
const path = `${outputDir}/${file}`;
if (lstatSync(path).isDirectory()) {
addMJsExtensionToImportStatements(path);
Expand All @@ -46,7 +46,7 @@ export const renameJsFilesToMJs = (outputDir: string) => {
return;
}

children.forEach(file => {
children.forEach((file) => {
const path = `${outputDir}/${file}`;
if (lstatSync(path).isDirectory()) {
renameJsFilesToMJs(path);
Expand All @@ -64,7 +64,7 @@ export const compileToUMD = (filePaths: string[], outputDir: string): void => {
outDir: outputDir,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
target: ts.ScriptTarget.ES2020,
module: ts.ModuleKind.UMD
module: ts.ModuleKind.UMD,
};
ts.createProgram(filePaths, compilerOptionsUMD).emit();
};
26 changes: 13 additions & 13 deletions src/lib/converters/constants.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
generateSvgConstant,
generateTSXConstant,
generateTypeDefinition,
generateTypeHelper
generateTypeHelper,
} from '../generators/code-snippet-generators';
import { generateExportSection } from '../helpers/complete-icon-set.helper';
import { writeFile } from '../helpers/file-helpers';
Expand All @@ -20,7 +20,7 @@ export async function convertToConstants(conversionOptions: ConstantsConversionO

const svgDefinitions = await callAndMonitorAsync<SvgDefinition[]>(
filesProcessor.bind({}, conversionOptions),
'Processing SVG files'
'Processing SVG files',
);

if (!svgDefinitions.length) {
Expand All @@ -43,7 +43,7 @@ async function convertToTSConstants(conversionOptions: ConstantsConversionOption
exportCompleteIconSet,
completeIconSetName,
generateType,
generateEnum
generateEnum,
} = conversionOptions;
let exportAllStatement = '';

Expand All @@ -52,13 +52,13 @@ async function convertToTSConstants(conversionOptions: ConstantsConversionOption
const typeDefinition = generateType
? callAndMonitor<string>(
generateTypeDefinition.bind({}, conversionOptions, svgDefinitions),
'Generate type definitions'
'Generate type definitions',
)
: '';

const interfaceDefinition = callAndMonitor<string>(
generateInterfaceDefinition.bind({}, conversionOptions),
'Generate Interface Definition'
'Generate Interface Definition',
);

const enumDefinition = generateEnum
Expand All @@ -68,15 +68,15 @@ async function convertToTSConstants(conversionOptions: ConstantsConversionOption
if (exportCompleteIconSet) {
exportAllStatement = callAndMonitor<string>(
generateExportSection.bind({}, svgDefinitions, completeIconSetName),
'Exporting all constants'
'Exporting all constants',
);
}

const typeHelper = callAndMonitor<string>(generateTypeHelper.bind({}, interfaceName), 'Generate Type Helper');
const fileContent = `${svgContants}${typeDefinition}${enumDefinition}${interfaceDefinition}${typeHelper}${exportAllStatement}`;
await callAndMonitorAsync<void>(
writeFile.bind({}, outputDirectory, fileName, fileContent),
`Writing files to ${outputDirectory}`
`Writing files to ${outputDirectory}`,
);
Logger.generationSuccess(outputDirectory);
}
Expand All @@ -90,29 +90,29 @@ async function convertToTSXConstants(conversionOptions: ConstantsConversionOptio
if (exportCompleteIconSet) {
exportAllStatement = callAndMonitor<string>(
generateExportSection.bind({}, svgDefinitions, completeIconSetName, FILE_TYPE.TSX),
'Exporting all TSX constants as ${completeIconSetName}'
'Exporting all TSX constants as ${completeIconSetName}',
);
}

const fileContent = `${tsxContants}${exportAllStatement}`;

await callAndMonitorAsync<void>(
writeFile.bind({}, outputDirectory, fileName, fileContent, FILE_TYPE.TSX),
`Writing files to ${outputDirectory}`
`Writing files to ${outputDirectory}`,
);
Logger.generationSuccess(outputDirectory);
}

function getTSConstants(svgDefinitions): string {
const svgConstants = svgDefinitions.map(svgDefinition =>
generateSvgConstant(svgDefinition.variableName, svgDefinition.typeName, svgDefinition.data)
const svgConstants = svgDefinitions.map((svgDefinition) =>
generateSvgConstant(svgDefinition.variableName, svgDefinition.typeName, svgDefinition.data),
);
return svgConstants.join('');
}

function getTSXConstants(svgDefinitions): string {
const tsxConstants = svgDefinitions.map(svgDefinition =>
generateTSXConstant(svgDefinition.variableName, svgDefinition.data)
const tsxConstants = svgDefinitions.map((svgDefinition) =>
generateTSXConstant(svgDefinition.variableName, svgDefinition.data),
);
return tsxConstants.join('');
}
64 changes: 32 additions & 32 deletions src/lib/converters/files.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
generateSvgConstant,
generateTypeDefinition,
generateTypeHelperWithImport,
generateTSXConstant
generateTSXConstant,
} from '../generators/code-snippet-generators';
import { generateCompleteIconSetContent } from '../helpers/complete-icon-set.helper';
import { deleteFiles, deleteFolder, writeFile } from '../helpers/file-helpers';
Expand All @@ -15,7 +15,7 @@ import { callAndMonitor, callAndMonitorAsync } from '../helpers/monitor';
import { getFilePathsFromRegex } from '../helpers/regex-helpers';
import {
FilesConversionOptions,
SVG_TO_TS_COMPILATION_OUTPUT
SVG_TO_TS_COMPILATION_OUTPUT,
} from '../options/conversion-options/files-conversion-options';
import { FILE_TYPE } from '../shared/file-type.model';

Expand All @@ -39,17 +39,17 @@ async function generateTSXFiles(conversionOptions: FilesConversionOptions) {
barrelFileName,
interfaceName,
generateType,
modelFileName
modelFileName,
} = conversionOptions;

const svgDefinitions = await callAndMonitorAsync<SvgDefinition[]>(
filesProcessor.bind({}, conversionOptions),
'Processing SVG files'
'Processing SVG files',
);

const generatedFileNames = await callAndMonitorAsync<string[]>(
generateTSXFileConstants.bind({}, svgDefinitions, outputDirectory, iconsFolderName),
'Generate TSX constants'
'Generate TSX constants',
);

if (exportCompleteIconSet) {
Expand All @@ -62,9 +62,9 @@ async function generateTSXFiles(conversionOptions: FilesConversionOptions) {
completeIconSetName,
interfaceName,
generateType,
modelFileName
modelFileName,
),
'Export complete icon set'
'Export complete icon set',
);
generatedFileNames.push(completeIconSetName);
}
Expand All @@ -75,7 +75,7 @@ async function generateTSXFiles(conversionOptions: FilesConversionOptions) {

await callAndMonitorAsync<void>(
writeFile.bind({}, outputDirectory, barrelFileName, indexFileContent),
'Generate barrel file'
'Generate barrel file',
);

Logger.generationSuccess(outputDirectory);
Expand All @@ -93,20 +93,20 @@ async function generateTSFiles(conversionOptions: FilesConversionOptions) {
completeIconSetName,
compilationOutput,
barrelFileName,
generateType
generateType,
} = conversionOptions;
await callAndMonitorAsync<void>(
deleteFolder.bind({}, `${outputDirectory}/${iconsFolderName}`),
'Deleting the output folder'
'Deleting the output folder',
);
const svgDefinitions = await callAndMonitorAsync<SvgDefinition[]>(
filesProcessor.bind({}, conversionOptions),
'Processing SVG files'
'Processing SVG files',
);

const generatedFileNames = await callAndMonitorAsync<string[]>(
generateSVGConstants.bind({}, svgDefinitions, outputDirectory, iconsFolderName),
'Generate SVG constants'
'Generate SVG constants',
);

if (exportCompleteIconSet) {
Expand All @@ -119,16 +119,16 @@ async function generateTSFiles(conversionOptions: FilesConversionOptions) {
completeIconSetName,
interfaceName,
modelFileName,
generateType
generateType,
),
'Export complete icon set'
'Export complete icon set',
);
generatedFileNames.push(completeIconSetName);
}

let indexFileContent = callAndMonitor<string>(
generateTypeHelperWithImport.bind({}, interfaceName, iconsFolderName, modelFileName),
'Generate Type Helper'
'Generate Type Helper',
);

indexFileContent += generatedFileNames
Expand All @@ -138,27 +138,27 @@ async function generateTSFiles(conversionOptions: FilesConversionOptions) {
indexFileContent += generateExportStatement(modelFileName, iconsFolderName);
await callAndMonitorAsync<void>(
writeFile.bind({}, outputDirectory, barrelFileName, indexFileContent),
'Generate barrel file'
'Generate barrel file',
);

if (modelFileName) {
const modelFile = await callAndMonitorAsync<void>(
generateModelFile.bind({}, conversionOptions, svgDefinitions),
'Generate model file'
'Generate model file',
);

if (additionalModelOutputPath) {
await callAndMonitorAsync<void>(
writeFile.bind({}, `${additionalModelOutputPath}`, modelFileName, modelFile),
'Write model file to additional output path'
'Write model file to additional output path',
);
}
}

if (compileSources) {
await callAndMonitorAsync<void>(
compileTypeScriptToJS.bind({}, outputDirectory, iconsFolderName, barrelFileName, compilationOutput),
'Compile TypeScript to JavaScript'
'Compile TypeScript to JavaScript',
);
}
Logger.generationSuccess(outputDirectory);
Expand All @@ -167,35 +167,35 @@ async function generateTSFiles(conversionOptions: FilesConversionOptions) {
const generateSVGConstants = async (
svgDefinitions: SvgDefinition[],
outputDirectory: string,
iconsFolderName: string
iconsFolderName: string,
): Promise<string[]> => {
const generatedFileNames: string[] = [];
await Promise.all(
svgDefinitions.map(async svgDefinition => {
svgDefinitions.map(async (svgDefinition) => {
const svgConstant = generateSvgConstant(svgDefinition.variableName, svgDefinition.typeName, svgDefinition.data);
const generatedFileName = `${svgDefinition.prefix}-${svgDefinition.filenameWithoutEnding}.icon`;
generatedFileNames.push(generatedFileName);
await writeFile(`${outputDirectory}/${iconsFolderName}`, generatedFileName, svgConstant);
Logger.verboseInfo(`write file svg: ${outputDirectory}/${iconsFolderName}/${generatedFileName}.ts`);
})
}),
);
return generatedFileNames;
};

const generateTSXFileConstants = async (
svgDefinitions: SvgDefinition[],
outputDirectory: string,
iconsFolderName: string
iconsFolderName: string,
): Promise<string[]> => {
const generatedFileNames: string[] = [];
await Promise.all(
svgDefinitions.map(async svgDefinition => {
svgDefinitions.map(async (svgDefinition) => {
const tsxConstant = generateTSXConstant(svgDefinition.variableName, svgDefinition.data);
const generatedFileName = `${svgDefinition.prefix}-${svgDefinition.filenameWithoutEnding}.icon`;
generatedFileNames.push(generatedFileName);
await writeFile(`${outputDirectory}/${iconsFolderName}`, generatedFileName, tsxConstant, FILE_TYPE.TSX);
Logger.verboseInfo(`write file svg: ${outputDirectory}/${iconsFolderName}/${generatedFileName}.tsx`);
})
}),
);
return generatedFileNames;
};
Expand All @@ -207,21 +207,21 @@ const generateCompleteIconSet = async (
completeIconSetName: string,
interfaceName?: string,
modelFileName?: string,
generateType?: boolean
generateType?: boolean,
): Promise<void> => {
const completeIconSetContent = generateCompleteIconSetContent(
svgDefinitions,
completeIconSetName,
interfaceName,
modelFileName,
generateType
generateType,
);
await writeFile(`${outputDirectory}/${iconsFolderName}`, completeIconSetName, completeIconSetContent);
};

const generateModelFile = async (
conversionOptions: FilesConversionOptions,
svgDefinitions: SvgDefinition[]
svgDefinitions: SvgDefinition[],
): Promise<string> => {
const { outputDirectory, modelFileName, additionalModelOutputPath, iconsFolderName } = conversionOptions;

Expand All @@ -231,13 +231,13 @@ const generateModelFile = async (
const modelFile = `${typeDefinition}${interfaceDefinition}${enumDefinition}`;
await writeFile(`${outputDirectory}/${iconsFolderName}`, modelFileName, modelFile);
Logger.verboseInfo(
`model-file successfully generated under ${outputDirectory}/${iconsFolderName}/${modelFileName}.ts`
`model-file successfully generated under ${outputDirectory}/${iconsFolderName}/${modelFileName}.ts`,
);

if (additionalModelOutputPath) {
await writeFile(`${additionalModelOutputPath}`, modelFileName, modelFile);
Logger.verboseInfo(
`additional model-file successfully generated under ${additionalModelOutputPath}/${modelFileName}.ts`
`additional model-file successfully generated under ${additionalModelOutputPath}/${modelFileName}.ts`,
);
}
return modelFile;
Expand All @@ -247,11 +247,11 @@ const compileTypeScriptToJS = async (
outputDirectory: string,
iconsFolderName: string,
barrelFileName: string,
compilationOutput: SVG_TO_TS_COMPILATION_OUTPUT
compilationOutput: SVG_TO_TS_COMPILATION_OUTPUT,
): Promise<void> => {
const generatedTypeScriptFilePaths = await getFilePathsFromRegex([
`${outputDirectory}/${iconsFolderName}/*.ts`,
`${outputDirectory}/${barrelFileName}.ts`
`${outputDirectory}/${barrelFileName}.ts`,
]);
switch (compilationOutput) {
case SVG_TO_TS_COMPILATION_OUTPUT.ESM:
Expand Down
Loading

0 comments on commit 606097e

Please sign in to comment.