curl_transcode
Version:
Transcode cURL query to various programming languages
178 lines (162 loc) • 5.37 kB
text/typescript
import * as path from "path";
import * as fs from "fs";
import * as curlconverter from "curlconverter";
import * as process from "process";
import "dotenv/config";
import { Config } from "./config.ts";
import { TranscodeError } from "./error.ts";
export function makeExampleCode(
sourceDirectory: string,
targetDirectory: string
) {
let config = Config.fromDefaultFile();
let sourceFileNames = readSourceDirectory(sourceDirectory);
for (let sourceFileName of sourceFileNames) {
if (!isSourceFile(sourceFileName)) {
continue;
}
let sourceFilePath = path.join(sourceDirectory, sourceFileName);
makeExampleCodeForFile(
sourceFilePath,
sourceDirectory,
targetDirectory,
config
);
}
}
function readSourceDirectory(sourceDirectory: string): Array<string> {
try {
return fs.readdirSync(sourceDirectory);
} catch (error: any) {
let { errno } = error;
if (error instanceof Error && errno === -2) {
throw new TranscodeError(`cannot read directory: ${sourceDirectory}`);
}
throw error;
}
}
function makeExampleCodeForFile(
sourceFilePath: string,
sourceDirectory: string,
targetDirectory: string,
config: Config
) {
makeExampleCodeCurl(sourceFilePath, targetDirectory, config);
makeExampleCodeNode(sourceFilePath, targetDirectory, config);
makeExampleCodeRust(sourceFilePath, targetDirectory, config);
makeExampleCodePython(sourceFilePath, targetDirectory, config);
makeExampleCodeDotnet(sourceFilePath, targetDirectory, config);
makeExampleCodeJava(sourceFilePath, targetDirectory, config);
makeExampleCodeGo(sourceFilePath, targetDirectory, config);
let relativePath = path.relative(sourceDirectory, sourceFilePath);
console.log(`transcode: ${relativePath}`);
}
function makeExampleCodeCurl(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".sh");
writeTargetFile(targetPath, sourceCode);
}
function makeExampleCodeNode(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetCode = curlconverter.toNodeFetch(sourceCode);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".js");
writeTargetFile(targetPath, targetCode);
}
function makeExampleCodeRust(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetCode = curlconverter.toRust(sourceCode);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".rs");
writeTargetFile(targetPath, targetCode);
}
function makeExampleCodePython(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetCode = curlconverter.toPython(sourceCode);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".py");
writeTargetFile(targetPath, targetCode);
}
function makeExampleCodeDotnet(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetCode = curlconverter.toCSharp(sourceCode);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".cs");
writeTargetFile(targetPath, targetCode);
}
function makeExampleCodeJava(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetCode = curlconverter.toJava(sourceCode);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".java");
writeTargetFile(targetPath, targetCode);
}
function makeExampleCodeGo(
sourceFilePath: string,
targetDirectory: string,
config: Config
) {
let sourceCode = readFileCurl(sourceFilePath, config);
let targetCode = curlconverter.toGo(sourceCode);
let targetPath = targetFilePath(sourceFilePath, targetDirectory, ".go");
writeTargetFile(targetPath, targetCode);
}
function readFileCurl(filePath: string, config: Config): string {
let raw = fs.readFileSync(filePath, "utf-8");
for (let environmentVariableName of config.replace) {
let regex = new RegExp("\\$" + environmentVariableName, "g");
raw = raw.replace(regex, getEnvironmentVariable(environmentVariableName));
}
return raw;
}
function getEnvironmentVariable(name: string): string {
let value = process.env[name];
if (typeof value !== "string" || value.length <= 0) {
throw new Error("expect environment variable: " + name);
}
return value;
}
function targetFilePath(
sourceFilePath: string,
targetDirectory: string,
targetExtension: string
) {
let sourceExtension = ".curl.sh";
let targetFilename =
path.basename(sourceFilePath, sourceExtension) + targetExtension;
return path.join(targetDirectory, targetFilename);
}
function isSourceFile(fileName: string) {
let basename = path.basename(fileName);
return /.curl.sh$/.test(basename);
}
function writeTargetFile(filePath: string, data: string) {
try {
fs.writeFileSync(filePath, data);
} catch (error: any) {
let { errno } = error;
if (error instanceof Error && errno === -2) {
throw new TranscodeError(`cannot write to file: ${filePath}`);
}
throw error;
}
}