@intlify/cli
Version:
CLI Tooling for i18n development
270 lines (264 loc) • 7.14 kB
TypeScript
import { DevEnv } from '@intlify/bundle-utils';
export { DevEnv } from '@intlify/bundle-utils';
import { CompilerError, SFCBlock } from '@vue/compiler-sfc';
import { Options } from 'prettier';
/**
* Vue SFC compiler error
*
* @remarks
* This is the error wrapping the error that occurred in Vue SFC compiler
*
* @public
*/
interface SFCParseError extends SyntaxError {
/**
* The error that occurred in Vue SFC compiler
*/
erorrs: CompilerError[];
/**
* The filepath of the source file
*/
filepath: string;
}
/**
* Compile Error Codes
*
* @remarks
* The error codes of {@link compile} function
*
* @public
*/
declare const enum CompileErrorCodes {
/**
* Not supported format
*/
NOT_SUPPORTED_FORMAT = 1,
/**
* Internal compile warning
*/
INTERNAL_COMPILE_WARNING = 2,
/**
* Internal compile error
*/
INTERNAL_COMPILE_ERROR = 3
}
/**
* Compile Options
*
* @remarks
* This optioins is used at {@link compile} function
*
* @public
*/
interface CompileOptions {
/**
* Compile Error handler
*/
onError?: (code: number, source: string, output: string, msg?: string) => void;
/**
* Compile handler
*/
onCompile?: (source: string, output: string) => void;
/**
* Compile mode
*
* @remarks
* The mode of code generation. Default `production` for optimization. If `development`, code generated with meta information from i18n resources.
*/
mode?: DevEnv;
/**
* Whether to generate AST format
*
* @remarks
* default `false`, it will output the function format.
*/
ast?: boolean;
}
/**
* Compile i18n resources
*
* @param source - the i18n resource source path, you can use glob pattern
* @param output - the compiled i18n resource output path
* @param options - {@link CompileOptions}
*
* @remarks
* This functoin is **asyncronous** function. If you want to get about error details, use the handler of {@link CompileOptions} and {@link CompileErrorCodes}
*
* @returns `true` when all i18n resource successfuly compile, not `false`
*
* @public
*/
declare function compile(source: string, output: string, options?: CompileOptions): Promise<boolean>;
/**
* Annotate Warning Codes
*
* @remarks
* The warning codes of {@link annotate} function
*
* @public
*/
declare const enum AnnotateWarningCodes {
/**
* Not supported type
*/
NOT_SUPPORTED_TYPE = 1,
/**
* Lang mismatch block `src` and block content
*/
LANG_MISMATCH_IN_SRC_AND_CONTENT = 2,
/**
* Lang mismatch option and block content
*/
LANG_MISMATCH_IN_OPTION_AND_CONTENT = 3,
/**
* Lang mismatch `lang` and block content
*/
LANG_MISMATCH_IN_ATTR_AND_CONTENT = 4
}
/**
* Annotate options of {@link annotate} function
*
* @public
*/
interface AnnotateOptions {
/**
* The type of the block
*
* @remarks
* Only `i18n` type is supported, if you don't specify it. If any other type is specified, the function will raise the {@link SFCAnnotateError}.
*
* default as `i18n`
*/
type?: string;
/**
* Whether to force annotations
*
* @remarks
* Force annotation of the attribute values of `attrs` option to the block tag. Even if the actual `lang` of the block content in the `lang` attribute is different, it will be enforced if this flag is turned on.
* default as `false`
*/
force?: boolean;
/**
* The Attributes to be annotated on the block tag
*
* @remarks
* default as `{}`
*/
attrs?: Record<string, any>;
/**
* The Vue template compiler version
*
* @remarks
* The version of the Vue template to be parsed by the annotate function.
* If `2` is specified, the `vue-template-compiler` used by Vue 2 is used; if `3` is specified, the `@vue/compiler-sfc` used by Vue 3 is used.
* defalt as `3`
*/
vue?: number;
/**
* The warning handler
*
* @remarks
* Notify warnings generated by the annotate process
*/
onWarn?: (code: number, args: Record<string, any>, block: SFCBlock) => void;
}
/**
* The Annocation error
*
* @public
*/
declare class SFCAnnotateError extends Error {
/**
* The filepath of the target file at annotate processing
*/
filepath: string;
/**
* Constructor
*
* @param message - The error message
* @param filepath - The filepath of the target file at annotate processing
*/
constructor(message: string, filepath: string);
}
/**
* Annoate the Vue SFC block
*
* @param source - The source code of the Vue SFC
* @param filepath - The file path of the Vue SFC
* @param options - The {@link AnnotateOptions | options} of the annotate function
* @returns The annotated source code of the Vue SFC
*
* @public
*/
declare function annotate(source: string, filepath: string, options?: AnnotateOptions): Promise<string>;
/**
* Format options of {@link format} function
*
* @public
*/
interface FormatOptions {
/**
* The Vue template compiler version
*
* @remarks
* The version of the Vue template to be parsed by the annotate function.
* If `2` is specified, the `vue-template-compiler` used by Vue 2 is used; if `3` is specified, the `@vue/compiler-sfc` used by Vue 3 is used.
* defalt as `3`
*/
vue?: number;
/**
* The prettier options
*
* @remarks
* The options for formatting the content of `i18n` custom blocks with prettier
* default as {@link DEFAULT_PRETTIER_OPTIONS}
*/
prettier?: Options;
}
/**
* Fortmat lang fwnot found error
*
* @remarks
* The error that not specified `lang` attribute in `i18n` custom block
*
* @public
*/
declare class FormatLangNotFoundError extends Error {
/**
* The filepath of the target file at formatting processing
*/
filepath: string;
/**
* Constructor
*
* @param message - The error message
* @param filepath - The filepath of the target file at formatting processing
*/
constructor(message: string, filepath: string);
}
/**
* The default prettier options for formatting the content of `i18n` custom blocks
*
* @public
*/
declare const DEFAULT_PRETTIER_OPTIONS: {
printWidth: number;
tabWidth: number;
jsonRecursiveSort: boolean;
plugins: string[];
};
/**
* Format the Vue SFC block
*
* @remarks
* Currently, only i18n custom blocks supporting
*
* @param source - The source code of the Vue SFC
* @param filepath - The file path of the Vue SFC
* @param options - The {@link FormatOptions | options} of the format function
* @returns The formatted source code of the Vue SFC
*
* @public
*/
declare function format(source: string, filepath: string, options?: FormatOptions): Promise<string>;
export { type AnnotateOptions, AnnotateWarningCodes, CompileErrorCodes, type CompileOptions, DEFAULT_PRETTIER_OPTIONS, FormatLangNotFoundError, type FormatOptions, SFCAnnotateError, type SFCParseError, annotate, compile, format };