Skip to content

Commit

Permalink
feat(input): Handle multiple sourceDir
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume MOREAU committed Dec 13, 2019
1 parent 4a104be commit fe9fd84
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
File renamed without changes
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"format:write": "prettier --write 'src/**/*.ts'",
"prebuild": "npm run copy:readme",
"start": "ts-node ./src/bin/svg-to-ts.ts -s ./inputfiles",
"start:multiple-source": "ts-node ./src/bin/svg-to-ts.ts -s ./inputfiles -s ./inputfilesBis",
"start:custom": "ts-node ./src/bin/svg-to-ts.ts -s ./inputfiles -o ./dist -t sampleIcon -i SampleIcon -p sampleIcon -f icons",
"start:help": "ts-node ./src/bin/svg-to-ts.ts -h",
"semantic-release": "semantic-release"
Expand Down
19 changes: 15 additions & 4 deletions src/bin/svg-to-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,39 @@ const DEFAULTS = {
interfaceName: 'MyIcon',
fileName: 'my-icons',
prefix: 'myIcon',
sourceDirectory: '.',
sourceDirectories: ['.'],
outputDirectory: './dist'
};

function collect(value, previous) {
return previous.concat([value]);
}

commander
.version(packgeJSON.version)
.option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULTS.typeName)
.option('-f --fileName <string>', 'name of the generated file', DEFAULTS.fileName)
.option('-p --prefix <string>', 'prefix for the generated svg constants', DEFAULTS.prefix)
.option('-i --interfaceName <string>', 'name for the generated interface', DEFAULTS.interfaceName)
.option('-s --srcDirectory <string>', 'name of the source directory', DEFAULTS.sourceDirectory)
.option('-s --srcDirectory <value>', 'name of the source directory', collect, [])
.option('-o --outputDirectory <string>', 'name of the output directory', DEFAULTS.outputDirectory)
.parse(process.argv);

const { typeName, fileName, prefix, interfaceName, srcDirectory, outputDirectory } = commander;
const { typeName, fileName, prefix, interfaceName, outputDirectory } = commander;

// Because of commander adding default value to params
// See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default
let srcDirectories = commander.srcDirectory;
if (srcDirectories.length === 0) {
srcDirectories = DEFAULTS.sourceDirectories;
}

const convertionOptions = {
typeName,
fileName,
prefix,
interfaceName,
srcDirectory,
srcDirectories,
outputDirectory
};
convert(convertionOptions);
20 changes: 17 additions & 3 deletions src/lib/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import camelCase from 'lodash.camelcase';
import * as prettier from 'prettier/standalone';
import chalk from 'chalk';
import typescriptParser from 'prettier/parser-typescript';
import { Dirent } from 'fs';

const util = require('util');
const path = require('path');
Expand All @@ -19,21 +20,34 @@ export interface ConvertionOptions {
prefix: string;
fileName: string;
interfaceName: string;
srcDirectory: string;
srcDirectories: string[];
outputDirectory: string;
}

export const convert = async (convertionOptions: ConvertionOptions): Promise<void> => {
let svgConstants = '';
const directoryPath = path.join(convertionOptions.srcDirectory);

let types = getTypeDefinition(convertionOptions.typeName);

try {
const files = await readdir(directoryPath, { withFileTypes: true });
const srcDirectories = convertionOptions.srcDirectories;
let files: Dirent[] = [];
let filesDirectoryPath = {};
for (let i = 0; i < srcDirectories.length; i++) {
const directoryPath: string = path.join(srcDirectories[i]);
const directoryContent: Dirent[] = await readdir(directoryPath, { withFileTypes: true });

files.push(...directoryContent);
directoryContent.forEach(file => {
filesDirectoryPath[file.name] = directoryPath;
});
}

for (let i = 0; i < files.length; i++) {
if (files[i].isFile()) {
const fileNameWithEnding = files[i].name;
const filenameWithoutEnding = fileNameWithEnding.split('.')[0];
const directoryPath = filesDirectoryPath[fileNameWithEnding];
const rawSvg = await extractSvgContent(fileNameWithEnding, directoryPath);
const optimizedSvg = await svgo.optimize(rawSvg);
const variableName = getVariableName(convertionOptions, filenameWithoutEnding);
Expand Down

0 comments on commit fe9fd84

Please sign in to comment.