Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for other file extensions (.jsx, .ts, and.tsx) #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Run `pixo --help` to see the list of options.
- `index` (boolean) create an `index.js` barrel module
- `iconComponent` (boolean) create an `Icon.js` wrapper component
- `recursive` (boolean) recursively read all SVGs in subdirectories
- `fileType` (string) Specifies a file type to output all generated SVGs, defaulting to .js

**CLI flags**

Expand All @@ -135,6 +136,7 @@ Run `pixo --help` to see the list of options.
-i --index Include index.js barrel module
-c --icon-component Include wrapper Icon.js component
-r --recursive Recursively read all SVGs in subdirectories
-f --fileType Specify JavaScript/jsx or TypeScript/tsx by name or extension. Default JavaScript.
```

**Example `package.json`**
Expand Down
22 changes: 21 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const cli = meow(`
`, {
booleanDefault: undefined,
flags: {
fileType: {
type: 'string',
alias: 'f'
},
outDir: {
type: 'string',
alias: 'd'
Expand Down Expand Up @@ -93,6 +97,22 @@ const readFile = file => {

const ignore = (file, stats) => !stats.isDirectory() && !/\.svg$/.test(file)

const validateFileType = {
'.js': '.js',
'js': '.js',
'javascript': '.js',

'jsx': '.jsx',
'.jsx': '.jsx',

'ts': '.ts',
'.ts': '.ts',
'typescript': '.ts',

'tsx': '.tsx',
'.tsx': '.tsx'
}

const convert = async (opts) => {
const readdir = opts.recursive
? async dirname => recursiveReaddir(dirname, [ ignore ])
Expand All @@ -113,7 +133,7 @@ const convert = async (opts) => {
fs.mkdirSync(opts.outDir)
}
components.forEach(({ name, content }) => {
const filename = path.join(opts.outDir, name + '.js')
const filename = path.join(opts.outDir, name + (validateFileType[opts.fileType] || '.js'))
fs.writeFileSync(filename, content)
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixo",
"version": "1.1.2",
"version": "1.1.4",
"description": "Convert SVG icons into React components",
"main": "index.js",
"bin": {
Expand Down