forked from external-repos/squoosh
Merge branch 'cli-invoc' of github.com:GoogleChromeLabs/squoosh into cli-invoc-2
This commit is contained in:
@@ -5,6 +5,7 @@ import 'add-css:./style.css';
|
|||||||
import { cleanSet, cleanMerge } from '../../util/clean-modify';
|
import { cleanSet, cleanMerge } from '../../util/clean-modify';
|
||||||
|
|
||||||
import type { SourceImage, OutputType } from '..';
|
import type { SourceImage, OutputType } from '..';
|
||||||
|
import type SnackBarElement from 'shared/initial-app/custom-els/snack-bar';
|
||||||
import {
|
import {
|
||||||
EncoderOptions,
|
EncoderOptions,
|
||||||
EncoderState,
|
EncoderState,
|
||||||
@@ -19,8 +20,11 @@ import Select from './Select';
|
|||||||
import { Options as QuantOptionsComponent } from 'features/processors/quantize/client';
|
import { Options as QuantOptionsComponent } from 'features/processors/quantize/client';
|
||||||
import { Options as ResizeOptionsComponent } from 'features/processors/resize/client';
|
import { Options as ResizeOptionsComponent } from 'features/processors/resize/client';
|
||||||
|
|
||||||
|
import { generateCliInvocation } from '../../util/cli-invocation-generator';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
index: 0 | 1;
|
index: 0 | 1;
|
||||||
|
showSnack: SnackBarElement['showSnackbar'];
|
||||||
mobileView: boolean;
|
mobileView: boolean;
|
||||||
source?: SourceImage;
|
source?: SourceImage;
|
||||||
encoderState?: EncoderState;
|
encoderState?: EncoderState;
|
||||||
@@ -106,6 +110,22 @@ export default class Options extends Component<Props, State> {
|
|||||||
this.props.onEncoderOptionsChange(this.props.index, newOptions);
|
this.props.onEncoderOptionsChange(this.props.index, newOptions);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private onCreateCLIInvocation = () => {
|
||||||
|
if (!this.props.encoderState) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const cliInvocation = generateCliInvocation(
|
||||||
|
this.props.encoderState,
|
||||||
|
this.props.processorState,
|
||||||
|
);
|
||||||
|
navigator.clipboard.writeText(cliInvocation);
|
||||||
|
} catch (e) {
|
||||||
|
this.props.showSnack(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
render(
|
render(
|
||||||
{ source, encoderState, processorState }: Props,
|
{ source, encoderState, processorState }: Props,
|
||||||
{ supportedEncoderMap }: State,
|
{ supportedEncoderMap }: State,
|
||||||
@@ -125,7 +145,9 @@ export default class Options extends Component<Props, State> {
|
|||||||
<Expander>
|
<Expander>
|
||||||
{!encoderState ? null : (
|
{!encoderState ? null : (
|
||||||
<div>
|
<div>
|
||||||
<h3 class={style.optionsTitle}>Edit</h3>
|
<h3 class={style.optionsTitle}>
|
||||||
|
<button onClick={this.onCreateCLIInvocation}>CLI</button>Edit
|
||||||
|
</h3>
|
||||||
<label class={style.sectionEnabler}>
|
<label class={style.sectionEnabler}>
|
||||||
Resize
|
Resize
|
||||||
<Toggle
|
<Toggle
|
||||||
|
|||||||
@@ -820,7 +820,7 @@ export default class Compress extends Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render(
|
render(
|
||||||
{ onBack }: Props,
|
{ onBack, showSnack }: Props,
|
||||||
{
|
{
|
||||||
loading,
|
loading,
|
||||||
sides,
|
sides,
|
||||||
@@ -839,6 +839,7 @@ export default class Compress extends Component<Props, State> {
|
|||||||
const options = sides.map((side, index) => (
|
const options = sides.map((side, index) => (
|
||||||
<Options
|
<Options
|
||||||
index={index as 0 | 1}
|
index={index as 0 | 1}
|
||||||
|
showSnack={showSnack}
|
||||||
source={source}
|
source={source}
|
||||||
mobileView={mobileView}
|
mobileView={mobileView}
|
||||||
processorState={side.latestSettings.processorState}
|
processorState={side.latestSettings.processorState}
|
||||||
|
|||||||
51
src/client/lazy-app/util/cli-invocation-generator.ts
Normal file
51
src/client/lazy-app/util/cli-invocation-generator.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Google Inc. All Rights Reserved.
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { EncoderState, ProcessorState } from '../feature-meta';
|
||||||
|
|
||||||
|
// Maps our encoder.type values to CLI parameter names
|
||||||
|
const typeMap = new Map<string, string>([
|
||||||
|
['avif', '--avif'],
|
||||||
|
['jxl', '--jxl'],
|
||||||
|
['mozJPEG', '--mozjpeg'],
|
||||||
|
['oxiPNG', '--oxipng'],
|
||||||
|
['webP', '--webp'],
|
||||||
|
['wp2', '--wp2'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Same as JSON.stringify, but with single quotes around the entire value
|
||||||
|
// so that shells don’t do weird stuff.
|
||||||
|
function cliJson<T>(v: T): string {
|
||||||
|
return "'" + JSON.stringify(v) + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateCliInvocation(
|
||||||
|
encoder: EncoderState,
|
||||||
|
processor: ProcessorState,
|
||||||
|
): string {
|
||||||
|
if (!typeMap.has(encoder.type)) {
|
||||||
|
throw Error(`Encoder ${encoder.type} is unsupported in the CLI`);
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
'npx',
|
||||||
|
'@squoosh/cli',
|
||||||
|
...(processor.resize.enabled
|
||||||
|
? ['--resize', cliJson(processor.resize)]
|
||||||
|
: []),
|
||||||
|
...(processor.quantize.enabled
|
||||||
|
? ['--quant', cliJson(processor.quantize)]
|
||||||
|
: []),
|
||||||
|
typeMap.get(encoder.type)!,
|
||||||
|
cliJson(encoder.options),
|
||||||
|
].join(' ');
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user