-
-
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.
- Loading branch information
1 parent
f95af87
commit ba4edca
Showing
38 changed files
with
133,455 additions
and
2,670 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
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,5 +1,84 @@ | ||
#!/usr/bin/env node | ||
|
||
const cli = {} | ||
import _ from 'lodash' | ||
import { Input, Value } from './type' | ||
import * as task from './task' | ||
|
||
export default cli | ||
call(process.argv) | ||
|
||
/** | ||
* Parse the input into an action and state. | ||
*/ | ||
|
||
function call(argv: Array<string>) { | ||
const input = read(argv) | ||
|
||
switch (input.action) { | ||
// case 'compress': | ||
// return build.compress(input) | ||
case 'convert': | ||
return task.convert(input) | ||
// case 'replace': | ||
// return build.replace(input) | ||
// case 'create': | ||
// return build.create(input) | ||
// case 'remove': | ||
// return build.remove(input) | ||
// case 'rename': | ||
// return build.rename(input) | ||
// case 'resize': | ||
// return build.resize(input) | ||
// case 'update': | ||
// return build.update(input) | ||
// case 'upload': | ||
// return build.upload(input) | ||
// case 'build': | ||
// return build.build(input) | ||
// case 'slice': | ||
// return build.slice(input) | ||
// case 'split': | ||
// return build.split(input) | ||
// case 'read': | ||
// return build.read(input) | ||
// case 'add': | ||
// return build.add(input) | ||
default: | ||
throw new Error(`Action ${input.action} undefined`) | ||
} | ||
} | ||
|
||
/** | ||
* Generically fetch CLI args. | ||
*/ | ||
|
||
function read(argv: Array<string>) { | ||
let i = 2 | ||
const input: Input = { | ||
action: argv[i++], | ||
object: [], | ||
detail: {}, | ||
} | ||
|
||
while (i < argv.length) { | ||
const item = argv[i++] | ||
if (item.match(/^-+/)) { | ||
const name = item | ||
const cased = name.match(/^--/) ? _.camelCase(name) : name | ||
let value: Value = argv[i++] | ||
if (!value) { | ||
value = true | ||
} else if (value.match(/^-+/)) { | ||
i-- | ||
value = true | ||
} | ||
input.detail[cased] = input.detail[cased] || [] | ||
input.detail[cased].push(value) | ||
} else { | ||
input.object.push(item) | ||
} | ||
} | ||
|
||
return input | ||
} | ||
|
||
console.log(process.argv) |
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,53 @@ | ||
import { Input, Link, Value } from './type' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import call, { convertImageWithImageMagick } from '../node' | ||
|
||
export const CONVERT_KEY: Record<string, Link> = { | ||
'-i': { name: 'inputPath' }, | ||
'-o': { name: 'outputPath', need: false }, | ||
'-I': { name: 'inputExtension', need: false }, | ||
'-O': { name: 'outputExtension', need: false }, | ||
'-x': { name: 'architecture', need: false }, | ||
'--arch': { name: 'architecture', need: false }, | ||
'-s': { name: 'syntax', need: false }, | ||
'-f': { name: 'prettify', need: false }, | ||
'-h': { name: 'help', need: false, like: 'boolean' }, | ||
} | ||
|
||
export async function convert(source: Input) { | ||
const input = makeInput(source, CONVERT_KEY) | ||
if (input.help) { | ||
console.log(` | ||
call: | ||
task convert -i <input-path> -o <output-path> | ||
`) | ||
} else { | ||
const call = (await import('../node')).default | ||
await call('convert', input) | ||
} | ||
} | ||
|
||
function makeInput(source: Input, map: Record<string, Link>) { | ||
const out: Record<string, Value | Array<Value>> = {} | ||
for (const name in source.detail) { | ||
const form = map[name] | ||
const mapName = form?.name ?? name | ||
const detail = source.detail[name].map(v => { | ||
switch (form?.like) { | ||
case 'string': | ||
return v | ||
case 'integer': | ||
return parseInt(v as string, 10) | ||
case 'decimal': | ||
return parseFloat(v as string) | ||
} | ||
return v | ||
}) | ||
const val = form?.list ? detail : detail[0] | ||
out[mapName] = val | ||
} | ||
return out | ||
} |
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,14 @@ | ||
export type Input = { | ||
action: string | ||
object: Array<string> | ||
detail: Record<string, Array<Value>> | ||
} | ||
|
||
export type Value = string | boolean | number | ||
|
||
export type Link = { | ||
name: string | ||
list?: boolean | ||
need?: boolean | ||
like?: string | ||
} |
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.