-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from kreuzerk/feature/multiFileGeneration
Feature/multi file generation
- Loading branch information
Showing
13 changed files
with
238 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,37 @@ | ||
import * as ts from 'typescript'; | ||
import { error, info } from '../helpers/log-helper'; | ||
|
||
const compile = (filePaths: string[], compilerOptions: ts.CompilerOptions): void => { | ||
let program = ts.createProgram(filePaths, compilerOptions); | ||
let emitResult = program.emit(); | ||
if (emitResult.emitSkipped) { | ||
error('Error during compilation of Typesript files'); | ||
reportDiagnostics(ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)); | ||
} else { | ||
info('Typescript files successfully compiled'); | ||
} | ||
}; | ||
|
||
const reportDiagnostics = (diagnostics: ts.Diagnostic[]): void => { | ||
diagnostics.forEach(diagnostic => { | ||
let message = 'Error'; | ||
if (diagnostic.file) { | ||
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); | ||
message += ` ${diagnostic.file.fileName} (${line + 1},${character + 1})`; | ||
} | ||
message += ': ' + ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | ||
info(message); | ||
}); | ||
}; | ||
|
||
export const compileSources = (filePaths: string[]): void => { | ||
const tsOptions = { | ||
noEmitOnError: true, | ||
noImplicitAny: true, | ||
declaration: true, | ||
moduleResolution: ts.ModuleResolutionKind.NodeJs, | ||
target: ts.ScriptTarget.ESNext, | ||
module: ts.ModuleKind.ESNext | ||
}; | ||
compile(filePaths, tsOptions); | ||
}; |
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,63 @@ | ||
import * as path from 'path'; | ||
|
||
import { ConvertionOptions } from '../../bin/svg-to-ts'; | ||
|
||
import { | ||
generateExportStatement, | ||
generateTypeName, | ||
generateUntypedSvgConstant, | ||
generateVariableName | ||
} from '../generators/code-snippet-generators'; | ||
import { getFilePathsFromRegex } from '../helpers/regex-helpers'; | ||
import { deleteFiles, deleteFolder, extractSvgContent, writeFile } from '../helpers/file-helpers'; | ||
import { compileSources } from '../compiler/typescript-compiler'; | ||
import { info, success } from '../helpers/log-helper'; | ||
import { svgOptimizer } from '../helpers/svg-optimization'; | ||
|
||
export const convertToMultipleFiles = async (convertionOptions: ConvertionOptions): Promise<void> => { | ||
const { prefix, delimiter, outputDirectory, srcFiles } = convertionOptions; | ||
let indexFileContent = ''; | ||
|
||
try { | ||
const filePaths = await getFilePathsFromRegex(srcFiles); | ||
await deleteFolder(`${outputDirectory}/icons`); | ||
info(`deleting output directory: ${outputDirectory}`); | ||
|
||
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]); | ||
info(`optimize svg: ${fileNameWithEnding}`); | ||
const optimizedSvg = await svgOptimizer.optimize(rawSvg); | ||
const variableName = generateVariableName(prefix, filenameWithoutEnding); | ||
const typeName = generateTypeName(filenameWithoutEnding, delimiter); | ||
const svgConstant = generateUntypedSvgConstant(variableName, typeName, optimizedSvg.data); | ||
const generatedFileName = `${prefix}-${filenameWithoutEnding}.icon`; | ||
indexFileContent += generateExportStatement(generatedFileName); | ||
await writeFile(`${outputDirectory}/icons`, generatedFileName, svgConstant); | ||
info(`write file svg: ${outputDirectory}/icons/${generatedFileName}.ts`); | ||
} | ||
} | ||
await writeFile(outputDirectory, 'index', indexFileContent); | ||
info(`write index.ts`); | ||
const generatedTypeScriptFilePaths = await getFilePathsFromRegex([ | ||
`${outputDirectory}/icons/*.ts`, | ||
`${outputDirectory}/index.ts` | ||
]); | ||
compileSources(generatedTypeScriptFilePaths); | ||
info(`compile Typescript - generate JS and d.ts`); | ||
deleteFiles(generatedTypeScriptFilePaths); | ||
info(`delete Typescript files`); | ||
|
||
success('========================================================'); | ||
success(`your files were successfully created under: ${outputDirectory}`); | ||
success( | ||
`don't forget to copy this folder to your dist in a post build script - enjoy your tree-shakable icon library 😎` | ||
); | ||
success('========================================================'); | ||
} catch (error) { | ||
error('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 was deleted.
Oops, something went wrong.
File renamed without changes.
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
Oops, something went wrong.