Skip to content

Commit

Permalink
feat: 更新模版渲染
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Oct 20, 2024
1 parent 9727480 commit ae8a232
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 73 deletions.
32 changes: 20 additions & 12 deletions packages/modkit/plugins/loon/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,20 @@ export class LoonTemplate extends Template {
super(params);
}

get Metadata() {
const result: Record<string, string | undefined> = {};
result.name = this.metadata.name;
result.desc = this.metadata.description;
result.system = this.metadata.system?.join();
result.version = this.metadata.version;
Object.entries(this.metadata.extra || {}).forEach(([key, value]) => {
result[key] = Array.isArray(value) ? value.join(',') : value;
});
return this.renderKeyValuePairs(result, { prefix: '#!' });
}

get General() {
return this.renderKeyValuePairs(this.content.loonGeneral).trim();
return this.renderKeyValuePairs(this.content.loonGeneral);
}

get Rule() {
Expand Down Expand Up @@ -129,20 +141,16 @@ export class LoonTemplate extends Template {
logger.warn('[Loon] Unsupported script type: dns');
break;
}
const parameters: string[] = [];
parameters.push(`script-path=${this.utils.getScriptPath(scriptKey)}`);
parameters.push(`tag=${name || `Script${index}`}`);
const parameters: Record<string, any> = {};
parameters['script-path'] = this.utils.getScriptPath(scriptKey);
parameters.tag = name || `Script${index}`;
objectEntries(rest).forEach(([key, value]) => {
parameters.push(`${toKebabCase(key)}=${value}`);
parameters[toKebabCase(key)] = value;
});
if (argument || injectArgument) {
if (argument) {
parameters.push(`argument=${argument}`);
} else {
parameters.push(`argument=[${this.source.arguments?.map((item) => `{${item.key}}`).join(',')}]`);
}
if (injectArgument || argument) {
parameters.argument = argument || `[${this.source.arguments?.map((item) => `{${item.key}}`).join(',')}]`;
}
line += parameters.join(', ');
line += this.renderKeyValuePairs(parameters, { join: ', ', separator: '=' });
return line;
})
.join('\n')
Expand Down
87 changes: 30 additions & 57 deletions packages/modkit/plugins/surge/src/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Template, type TemplateParametersParams } from '@iringo/modkit-shared';
import { Template, type TemplateParametersParams, objectEntries, toKebabCase } from '@iringo/modkit-shared';
import type { SurgePluginOptions } from './index';

export class SurgeTemplate extends Template {
Expand All @@ -9,6 +9,25 @@ export class SurgeTemplate extends Template {
super(params);
}

get Metadata() {
const { argumentsText, argumentsDescription } = this.#handleArguments();
const result: Record<string, string | undefined> = {};
result.name = this.metadata.name;
result.desc = this.metadata.description;
result.requirement = this.metadata.system?.map((item) => `SYSTEM = ${item}`).join(' || ');
result.version = this.metadata.version;
if (argumentsText) {
result.arguments = argumentsText;
}
if (argumentsDescription) {
result['arguments-desc'] = argumentsDescription;
}
Object.entries(this.metadata.extra || {}).forEach(([key, value]) => {
result[key] = Array.isArray(value) ? value.join(',') : value;
});
return this.renderKeyValuePairs(result, { prefix: '#!' });
}

get General() {
return this.renderKeyValuePairs(this.content.surgeGeneral).trim();
}
Expand Down Expand Up @@ -42,69 +61,23 @@ export class SurgeTemplate extends Template {
.trim();
}

get Metadata() {
const { argumentsText, argumentsDescription } = this.#handleArguments();
const result: Record<string, string | undefined> = {};
result.name = this.metadata.name;
result.desc = this.metadata.description;
result.requirement = this.metadata.system?.map((item) => `SYSTEM = ${item}`).join(' || ');
result.version = this.metadata.version;
if (argumentsText) {
result.arguments = argumentsText;
}
if (argumentsDescription) {
result['arguments-desc'] = argumentsDescription;
}
Object.entries(this.metadata.extra || {}).forEach(([key, value]) => {
result[key] = Array.isArray(value) ? value.join(',') : value;
});
return Object.entries(result)
.map(([key, value]) => (!!key && !!value ? `#!${key} = ${value}` : ''))
.filter(Boolean)
.join('\n')
.trim();
}

get Script() {
const { scriptParams } = this.#handleArguments();
return (this.content.script || [])
.map((script, index) => {
const parameters = [];
parameters.push(`type=${script.type}`);
if (script.pattern) {
parameters.push(`pattern=${script.pattern}`);
}
if (script.type === 'cron' && script.cronexp) {
parameters.push(`cronexp="${script.cronexp}"`);
}
if (script.engine) {
parameters.push(`requires-body=${JSON.stringify(!!script.requiresBody)}`);
}
if (script.binaryBodyMode) {
parameters.push(`binary-body-mode=${JSON.stringify(!!script.binaryBodyMode)}`);
}
if (script.scriptKey) {
parameters.push(`script-path=${this.utils.getScriptPath(script.scriptKey)}`);
}
if (script.engine) {
parameters.push(`engine=${script.engine}`);
}
if (script.maxSize) {
parameters.push(`max-size=${script.maxSize}`);
}
if (script.timeout) {
parameters.push(`timeout=${script.timeout}`);
}
if (script.scriptUpdateInterval) {
parameters.push(`script-update-interval=${script.scriptUpdateInterval}`);
}
if (script.debug || process.env.NODE_ENV === 'development') {
parameters.push('debug=true');
const parameters: Record<string, any> = {};
const { name, scriptKey, debug, ...rest } = script;
parameters['script-path'] = this.utils.getScriptPath(scriptKey);
objectEntries(rest).forEach(([key, value]) => {
parameters[toKebabCase(key)] = value;
});
if (debug || process.env.NODE_ENV === 'development') {
parameters.debug = true;
}
if (script.injectArgument || script.argument) {
parameters.push(`argument=${script.argument || scriptParams}`);
parameters.argument = script.argument || scriptParams;
}
return `${script.name || `script${index}`} = ${parameters.join(', ')}`;
return `${name || `script${index}`} = ${this.renderKeyValuePairs(parameters, { join: ', ', separator: '=' })}`;
})
.join('\n')
.trim();
Expand Down
10 changes: 6 additions & 4 deletions packages/modkit/shared/src/plugin/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export class Template {
};
}

renderKeyValuePairs(ojb?: Record<string, string>) {
renderKeyValuePairs(ojb?: Record<string, string | undefined>, { separator = ' = ', join = '\n', prefix = '' } = {}) {
return Object.entries(ojb || {})
.map(([key, value]) => `${key} = ${value}`)
.join('\n');
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${prefix}${key}${separator}${value}`)
.join(join)
.trim();
}

renderLines(lines?: string[]) {
return (lines || []).join('\n');
return (lines || []).join('\n').trim();
}
}

0 comments on commit ae8a232

Please sign in to comment.