Skip to content

Commit

Permalink
feat(arguments-builder): 支持 Egern 转换
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Nov 3, 2024
1 parent 4fcf043 commit 45d3861
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-radios-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@iringo/arguments-builder": minor
---

支持 Egern 转换
1 change: 1 addition & 0 deletions packages/arguments-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"test": "jest"
},
"dependencies": {
"@iringo/surge2egern": "workspace:^",
"@iringo/utils": "workspace:^",
"commander": "^12.1.0",
"date-fns": "^4.1.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/arguments-builder/src/bin/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const outputSchema = z.object({
.object({
template: z.string().optional(),
path: z.string().optional(),
transformEgern: z
.object({
enable: z.boolean().optional().default(true),
path: z.string().optional(),
})
.optional(),
})
.optional(),
loon: z
Expand Down
23 changes: 15 additions & 8 deletions packages/arguments-builder/src/bin/surge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import { Surge2Egern } from '@iringo/surge2egern';
import { logger } from '@iringo/utils';
import type { ArgumentsBuilder } from '../core';
import type { Output } from './config';
Expand All @@ -21,15 +22,21 @@ export const buildSurgeArguments = async (
try {
const template = handlebars.compile(fs.readFileSync(templatePath, 'utf-8'));
const { argumentsText, argumentsDescription, scriptParams } = builder.buildSurgeArguments();
await safeWriteFile(
outputPath,
template({
arguments: argumentsText,
argumentsDesc: argumentsDescription.replace(/\n/g, '\\n'),
scriptParams,
}),
);
const result = template({
arguments: argumentsText,
argumentsDesc: argumentsDescription.replace(/\n/g, '\\n'),
scriptParams,
});
await safeWriteFile(outputPath, result);
logger.success(`Successfully generated Surge module to ${outputPath}`);

if (output?.transformEgern?.enable) {
const surge2Egern = new Surge2Egern();
const egernModule = await surge2Egern.transformModule(result);
const egernOutputPath = output?.transformEgern?.path ?? path.resolve('egern.yaml');
await safeWriteFile(egernOutputPath, egernModule);
logger.success(`Successfully generated Egern module to ${egernOutputPath}`);
}
} catch (error) {
logger.error(`Failed to generate Surge module to ${outputPath}`, error);
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45d3861

Please sign in to comment.