1- import fsExtra from "fs-extra" ;
21import { DirectoryPath } from "../../types/file/directoryPath.js" ;
32import { ActionResult } from "../action-result.js" ;
43import { ApiTransformPrompts } from "../../prompts/api/transform.js" ;
5- import { DestinationFormats } from "../../types/api/transform.js" ;
6- import { getFileNameFromPath } from "../../utils/utils.js" ;
7- import { FileService } from "../../infrastructure/file-service.js" ;
8- import { validateFileInputParams } from "../../infrastructure/api-utils.js" ;
94import { withDirPath } from "../../infrastructure/tmp-extensions.js" ;
10- import { TransformationService } from "../../infrastructure/services/transform-service.js" ;
11- import { FilePath } from "../../types/file/filePath.js" ;
12- import { FileName } from "../../types/file/fileName.js" ;
13- import { Result } from "../../types/common/result.js" ;
14- import { ApiValidationSummary } from "@apimatic/sdk" ;
5+ import { TransformationService } from "../../infrastructure/services/transformation-service.js" ;
6+ import { ExportFormats } from "@apimatic/sdk" ;
157import { ApiValidatePrompts } from "../../prompts/api/validate.js" ;
168import { CommandMetadata } from "../../types/common/command-metadata.js" ;
17- import { getValidFormat } from "../../controllers/api/transform.js" ;
9+ import { TransformContext } from "../../types/transform-context.js" ;
10+ import { ResourceInput } from "../../types/file/resource-input.js" ;
11+ import { ResourceContext } from "../../types/resource-context.js" ;
1812
19- export interface TransformationResultData {
20- stream : NodeJS . ReadableStream ;
21- apiValidationSummary : ApiValidationSummary ;
22- }
2313
2414export class TransformAction {
2515 private readonly prompts : ApiTransformPrompts = new ApiTransformPrompts ( ) ;
2616 private readonly validatePrompts : ApiValidatePrompts = new ApiValidatePrompts ( ) ;
2717 private readonly transformationService : TransformationService = new TransformationService ( ) ;
28- private readonly fileService : FileService = new FileService ( ) ;
2918 private readonly configDir : DirectoryPath ;
3019 private readonly commandMetadata : CommandMetadata ;
3120 private readonly authKey : string | null ;
@@ -37,80 +26,41 @@ export class TransformAction {
3726 }
3827
3928 public readonly execute = async (
40- format : string ,
29+ resourcePath : ResourceInput ,
30+ format : ExportFormats ,
4131 destination : DirectoryPath ,
42- force : boolean ,
43- file ?: FilePath ,
44- url ?: string
32+ force : boolean
4533 ) : Promise < ActionResult > => {
46- const validationResult = await validateFileInputParams ( file , url ) ;
47-
48- if ( ! validationResult . isSuccess ( ) ) {
49- // TODO: Render the message here: validationResult.error!
50- return ActionResult . failed ( ) ;
51- }
52-
53- this . prompts . displayApiTransformationMessage ( ) ;
54-
55- const parsedFormat = getValidFormat ( format ) ;
56-
57- const destinationFileExt : string = DestinationFormats [ format as keyof typeof DestinationFormats ] ;
58- const destinationFilePrefix = file ? getFileNameFromPath ( file . toString ( ) ) : getFileNameFromPath ( url || "" ) ;
59-
60- const destinationFileName = `${ destinationFilePrefix } _${ format } .${ destinationFileExt } ` ;
61- const destinationFilePath = new FilePath ( destination , new FileName ( destinationFileName ) ) ;
62-
63- if ( ( await fsExtra . pathExists ( destinationFilePath . toString ( ) ) ) && ! force ) {
64- // TODO: Render the error message here
65- // return ActionResult.error(
66- // `Can't download transformed file to path ${destinationFilePath.toString()}, because it already exists`
67- // );
68-
69- return ActionResult . failed ( ) ;
70- }
71-
72- if ( ! ( await fsExtra . pathExists ( destination . toString ( ) ) ) ) {
73- await fsExtra . ensureDir ( destination . toString ( ) ) ;
74- }
75-
7634 return await withDirPath ( async ( tempDirectory ) => {
77- let result : Result < TransformationResultData , string > ;
35+ const resourceContext = new ResourceContext ( tempDirectory ) ;
36+ const specFileDirResult = await resourceContext . resolveTo ( resourcePath ) ;
37+ if ( specFileDirResult . isErr ( ) ) {
38+ this . prompts . networkError ( specFileDirResult . error ) ;
39+ return ActionResult . failed ( ) ;
40+ }
41+ const transformContext = new TransformContext ( specFileDirResult . value , format , destination ) ;
42+ if ( ! force && ( await transformContext . exists ( ) ) && ! ( await this . prompts . overwriteApi ( destination ) ) ) {
43+ this . prompts . transformedApiAlreadyExists ( ) ;
44+ return ActionResult . cancelled ( ) ;
45+ }
7846
79- if ( file ) {
80- result = await this . transformationService . transformViaFile ( {
81- file,
82- format : parsedFormat ,
83- configDir : this . configDir ,
84- commandMetadata : this . commandMetadata ,
85- authKey : this . authKey
86- } ) ;
87- } else {
88- result = await this . transformationService . transformViaUrl ( {
89- url : url ! ,
90- format : parsedFormat ,
47+ const result = await this . prompts . transformApi (
48+ this . transformationService . transformViaFile ( {
49+ file : specFileDirResult . value ,
50+ format : format ,
9151 configDir : this . configDir ,
9252 commandMetadata : this . commandMetadata ,
9353 authKey : this . authKey
94- } ) ;
95- }
96-
97- const tempTransformedFilePath = new FilePath ( tempDirectory , new FileName ( `transformed_${ destinationFileName } ` ) ) ;
98- await this . fileService . writeFile ( tempTransformedFilePath , result . value ?. stream as NodeJS . ReadableStream ) ;
54+ } )
55+ ) ;
9956
100- if ( ! result . isSuccess ( ) ) {
101- this . validatePrompts . displayValidationMessages (
102- result . value ?. apiValidationSummary || { warnings : [ ] , errors : [ ] , messages : [ ] }
103- ) ;
104- this . prompts . displayApiTransformationFailureMessage ( ) ;
105-
106- // TODO: Render the error message here
107- //return ActionResult.error(result.error || "An unknown error occurred");
57+ if ( result . isErr ( ) ) {
58+ this . prompts . logTransformationError ( result . error ) ;
10859 return ActionResult . failed ( ) ;
10960 }
11061
111- this . prompts . displayApiTransformationSuccessMessage ( ) ;
112- this . validatePrompts . displayValidationMessages ( result . value ! . apiValidationSummary ) ;
113-
62+ await transformContext . save ( result . value . stream ) ;
63+ this . validatePrompts . displayValidationMessages ( result . value . apiValidationSummary ) ;
11464 return ActionResult . success ( ) ;
11565 } ) ;
11666 } ;
0 commit comments