-
Notifications
You must be signed in to change notification settings - Fork 4
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 #22 from obytes/feat/add-cli
Feat/add cli
- Loading branch information
Showing
8 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
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,4 @@ | ||
#!/usr/bin/env node | ||
import { parseArgument } from './parse-arguments'; | ||
|
||
parseArgument(); |
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,51 @@ | ||
import z from 'zod'; | ||
import path from 'path'; | ||
import { fromZodError } from 'zod-validation-error'; | ||
import chalk from 'chalk'; | ||
import type { Params } from '../types'; | ||
|
||
const HEX_COLOR_REGEX = /^#[0-9a-fA-F]{6}$/; | ||
|
||
const Badge = z.discriminatedUnion('type', [ | ||
z.object({ | ||
type: z.literal('banner'), | ||
text: z.string(), | ||
position: z.union([z.literal('top'), z.literal('bottom')]).optional(), | ||
color: z.union([z.literal('white'), z.literal('black')]).optional(), | ||
background: z.string().regex(HEX_COLOR_REGEX).optional(), | ||
}), | ||
z.object({ | ||
type: z.literal('ribbon'), | ||
text: z.string(), | ||
position: z.union([z.literal('left'), z.literal('right')]).optional(), | ||
color: z.union([z.literal('white'), z.literal('black')]).optional(), | ||
background: z | ||
.string({ | ||
invalid_type_error: 'background color should be in hex format', | ||
}) | ||
.regex(HEX_COLOR_REGEX) | ||
.optional(), | ||
}), | ||
]); | ||
|
||
const configParamsSchema = z.object({ | ||
icon: z.string(), | ||
dstPath: z.string().optional(), | ||
badges: z.array(Badge), | ||
isAdaptiveIcon: z.boolean().optional(), | ||
}); | ||
|
||
const badgesArray = z.array(configParamsSchema); | ||
|
||
export const validateConfig = (configs: Params[]) => { | ||
try { | ||
badgesArray.parse(configs); | ||
} catch (err) { | ||
if (err instanceof z.ZodError) { | ||
const validationError = fromZodError(err); | ||
console.error(chalk.red(validationError.message)); | ||
} | ||
return false; | ||
} | ||
return true; | ||
}; |
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,15 @@ | ||
import { Badge, Params } from '../types'; | ||
|
||
export class IconConfig { | ||
icon: string; | ||
dstPath?: string; | ||
badges: Array<Badge>; | ||
isAdaptiveIcon?: boolean; | ||
|
||
constructor({ icon, dstPath, badges, isAdaptiveIcon }: Params) { | ||
this.badges = badges; | ||
this.icon = icon; | ||
this.dstPath = dstPath; | ||
this.isAdaptiveIcon = isAdaptiveIcon; | ||
} | ||
} |
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,75 @@ | ||
import { parseBadgeJsonConfigFile } from './parse-badge-json-config-file'; | ||
import { IconConfig } from './icon-config'; | ||
import { addBadge } from '..'; | ||
import { Badge } from '../types'; | ||
import { Command, Option } from 'commander'; | ||
|
||
var version = require('../package.json').version; | ||
|
||
const program = new Command(); | ||
program | ||
.description('command line for app-icon-badge') | ||
.name('app-icon-badge') | ||
.version(version); | ||
|
||
// default command | ||
program | ||
.addCommand( | ||
new Command('add-badge') | ||
.description('add badge to icon using add-badge by default') | ||
.argument('<iconPath>') | ||
.addOption( | ||
new Option('-t, --type <type>', 'badge type') | ||
.choices(['banner', 'ribbon']) | ||
.makeOptionMandatory() | ||
) | ||
.addOption( | ||
new Option('-x, --text <text>', 'badge text').makeOptionMandatory() | ||
) | ||
.addOption( | ||
new Option( | ||
'-p, --position <position>', | ||
'badge position banner:top,bottom ribbon:left,right' | ||
).choices(['top', 'left', 'right', 'bottom']) | ||
) | ||
.addOption( | ||
new Option('-b, --background <background>', 'badge background color') | ||
) | ||
.addOption( | ||
new Option('-c, --color <color>', 'badge color') | ||
.choices(['white', 'black']) | ||
.default('white') | ||
) | ||
.action((iconPath, opts) => { | ||
program.setOptionValue('iconPath', iconPath); | ||
program.setOptionValue('badge', opts); | ||
}), | ||
{ | ||
isDefault: true, | ||
} | ||
) | ||
.showHelpAfterError(); | ||
|
||
program.addCommand( | ||
new Command('use-config') | ||
.description('use json file as a config') | ||
.argument('<configPath>') | ||
.action((configPath) => { | ||
console.log(configPath); | ||
}) | ||
); | ||
|
||
export async function parseArgument() { | ||
if (process.argv.length <= 2) { | ||
program.help(); | ||
return; | ||
} | ||
const opts = program.parse().opts(); | ||
if (opts.iconPath) { | ||
const badges: Badge = opts.badge; | ||
const icon = new IconConfig({ icon: opts.iconPath, badges: [badges] }); | ||
addBadge(icon); | ||
} else if (opts.badges_config) { | ||
parseBadgeJsonConfigFile(opts.badges_config); | ||
} | ||
} |
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,30 @@ | ||
import fs from 'fs'; | ||
import type { Params } from '../types'; | ||
import { validateConfig } from './config-validation'; | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import { addBadge } from '../index'; | ||
|
||
export function parseBadgeJsonConfigFile(filePath: string) { | ||
const resolvedPath = path.resolve(filePath); | ||
if (!fs.existsSync(resolvedPath)) { | ||
console.error( | ||
chalk.red(`'${resolvedPath}'`, 'File does not exist in this path') | ||
); | ||
return false; | ||
} | ||
let badgeConfig: Params[] | null = null; | ||
try { | ||
const data = fs.readFileSync(filePath, 'utf8'); | ||
badgeConfig = JSON.parse(data); | ||
if (badgeConfig && validateConfig(badgeConfig)) { | ||
badgeConfig.forEach((config: Params) => { | ||
addBadge(config); | ||
}); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
return false; | ||
} | ||
return true; | ||
} |
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,24 @@ | ||
export type Banner = { | ||
type: 'banner'; | ||
text: string; | ||
position?: 'top' | 'bottom'; | ||
color?: 'white' | 'black'; | ||
background?: string; | ||
}; | ||
|
||
export type Ribbon = { | ||
type: 'ribbon'; | ||
text: string; | ||
position?: 'left' | 'right'; | ||
color?: 'white' | 'black'; | ||
background?: string; | ||
}; | ||
|
||
export type Badge = Banner | Ribbon; | ||
|
||
export type Params = { | ||
icon: string; | ||
dstPath?: string; | ||
badges: Array<Badge>; | ||
isAdaptiveIcon?: boolean; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.