generated from zyyv/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add commands * feat(config): resolve config * chore: update * chore: update * chore: test
- Loading branch information
Showing
13 changed files
with
249 additions
and
74 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
dist | ||
*.log | ||
untiny.config.ts | ||
mock |
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.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import consola from 'consola' | ||
import { startCli } from './cli-start' | ||
import { startCli } from './ui' | ||
|
||
startCli().catch(consola.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { dirname, resolve } from 'node:path' | ||
import fs from 'node:fs' | ||
import { createConfigLoader, loadConfig } from 'unconfig' | ||
import { sourcePackageJsonFields } from 'unconfig/presets' | ||
import type { Config } from './types' | ||
|
||
const _sources = [ | ||
{ | ||
files: 'untiny.config', | ||
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''], | ||
}, | ||
sourcePackageJsonFields({ | ||
fields: 'untiny', | ||
}), | ||
] | ||
|
||
const _defaults = { | ||
apiKey: '', | ||
} | ||
|
||
export async function getConfig(cwd = process.cwd()) { | ||
const { config } = await loadConfig<Config>({ | ||
sources: _sources, | ||
cwd, | ||
defaults: _defaults, | ||
merge: true, | ||
}) | ||
|
||
return config | ||
} | ||
|
||
export async function resolveConfig<U extends Config>( | ||
cwd = process.cwd(), | ||
configOrPath: string | U = cwd, | ||
defaults: Config = _defaults, | ||
) { | ||
let inlineConfig = {} as U | ||
if (typeof configOrPath !== 'string') { | ||
inlineConfig = configOrPath | ||
if (inlineConfig.configFile != null && inlineConfig.configFile === false) { | ||
return { | ||
config: inlineConfig as U, | ||
sources: [], | ||
} | ||
} | ||
else { | ||
configOrPath = inlineConfig.configFile || process.cwd() | ||
} | ||
} | ||
|
||
const resolved = resolve(configOrPath) | ||
|
||
let isFile = false | ||
if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) { | ||
isFile = true | ||
cwd = dirname(resolved) | ||
} | ||
|
||
const loader = createConfigLoader<Config>({ | ||
sources: isFile | ||
? [ | ||
{ | ||
files: resolved, | ||
extensions: [], | ||
}, | ||
] | ||
: _sources, | ||
cwd, | ||
defaults: _defaults, | ||
merge: true, | ||
}) | ||
|
||
const result = await loader.load() | ||
result.config = Object.assign(defaults, result.config || inlineConfig) | ||
|
||
return result | ||
} |
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,93 @@ | ||
import { resolve } from 'node:path' | ||
import cac from 'cac' | ||
import type { Command } from 'cac' | ||
import pkg from '../../package.json' | ||
import type { CliOption } from '../types' | ||
import { resolveConfig } from '../config' | ||
import { createUntiny } from '..' | ||
import { promptUI } from './prompt' | ||
|
||
export async function startCli(cwd = process.cwd()) { | ||
const cli = cac('untiny') | ||
|
||
const passCommonOptions = (command: Command) => { | ||
return command | ||
.option('-c, --config [file]', 'Config file') | ||
.option('-k, --key <key>', 'Your access key') | ||
.option('-o, --out <out>', 'Output file', { default: './' }) | ||
.option('-d, --debug', 'Open debug mode') | ||
} | ||
|
||
passCommonOptions(cli.command('ci <path>', 'compress a image')) | ||
.example('untiny ci ./test.png') | ||
.action(async (path, options) => { | ||
compress({ | ||
cwd, | ||
path, | ||
type: 'image', | ||
...options, | ||
}) | ||
}) | ||
|
||
passCommonOptions(cli.command('cis <...paths>', 'compress array of images')) | ||
.example('untiny cis ./test.png ./test2.png') | ||
.action(async (path, options) => { | ||
compress({ | ||
cwd, | ||
path, | ||
type: 'images', | ||
...options, | ||
}) | ||
}) | ||
|
||
passCommonOptions(cli.command('cd <dir>', 'compress a directory')) | ||
.example('untiny cd ./assets/images') | ||
.action(async (path, options) => { | ||
compress({ | ||
cwd, | ||
path, | ||
type: 'directory', | ||
...options, | ||
}) | ||
}) | ||
|
||
cli | ||
.command('ui', 'Untiny Prompt UI') | ||
.action(() => { | ||
promptUI(cwd) | ||
}) | ||
|
||
cli.help() | ||
cli.version(pkg.version) | ||
cli.parse() | ||
} | ||
|
||
async function compress(options: CliOption) { | ||
options.key = options.key || (await resolveConfig(options.cwd, options.config)).config.apiKey | ||
const untinyIns = await createUntiny(options.key) | ||
|
||
const handler = (_originPath: string, originImgName: string) => resolve(...[ | ||
options.out!.startsWith('/') ? '' : options.cwd, | ||
options.out!, | ||
originImgName, | ||
].filter(Boolean)) | ||
|
||
if (options.type === 'image') { | ||
await untinyIns.compressImage(options.path as string, { | ||
handler, | ||
debug: options.debug, | ||
}) | ||
} | ||
else if (options.type === 'images') { | ||
await untinyIns.compressImages(options.path as string[], { | ||
handler, | ||
debug: options.debug, | ||
}) | ||
} | ||
else if (options.type === 'directory') { | ||
await untinyIns.compressDir(options.path as string, { | ||
handler, | ||
debug: options.debug, | ||
}) | ||
} | ||
} |
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 @@ | ||
export * from './cmd' |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default { | ||
apiKey: 'RWDMNgkQJGldpgBVhn5MJ2944cHxN2CK', | ||
apiKey: 'qweqweqwe', | ||
} |
Oops, something went wrong.