import * as prettier from "prettier" ; import * as fsextra from "fs-extra" ; import * as path from "path" ; import { CodeModel } from "@autorest/codemodel" ; import { Project , IndentationText } from "ts-morph" ; import { AutorestExtensionHost } from "@autorest/extension-base" ; import { transformCodeModel } from "./transforms/transforms" ; import { transformSamples } from './transforms/samplesTransforms' ; import { generateClient } from "./generators/clientFileGenerator" ; import { generateModels } from "./generators/modelsGenerator" ; import { generateMappers } from "./generators/mappersGenerator" ; import { generateIndexFile } from "./generators/indexGenerator" ; import { generatePackageJson } from "./generators/static/packageFileGenerator" ; import { generateApiExtractorConfig } from "./generators/static/apiExtractorConfig" ; import { generateLicenseFile } from "./generators/static/licenseFileGenerator" ; import { generateReadmeFile } from "./generators/static/readmeFileGenerator" ; import { generateSampleTestFile } from "./generators/test/sampleTestGenerator" ; import { generateTsConfig } from "./generators/static/tsConfigFileGenerator" ; import { generateRollupConfig } from "./generators/static/rollupConfigFileGenerator" ; import { generateOperations } from "./generators/operationGenerator" ; import { generateOperationsInterfaces } from "./generators/operationInterfaceGenerator" ; import { generateSampleEnv } from './generators/samples/sampleEnvGenerator' ; import { generateSamples } from './generators/samples/sampleGenerator' ; import { generateParameters } from "./generators/parametersGenerator" ; import { generateLroFiles } from "./generators/LROGenerator" ; import { generateTracingFile } from "./generators/tracingFileGenerator" ; import { getAutorestOptions } from "./autorestSession" ; import { conflictResolver } from "./conflictResolver" ; const prettierTypeScriptOptions = { parser : "typescript" , arrowParens : "always" , bracketSpacing : true , endOfLine : "lf" , printWidth : 80 , semi : true , singleQuote : false , tabWidth : 2 } ; const prettierJSONOptions = { parser : "json" , tabWidth : 2 , semi : false , singleQuote : false } ; export async function generateTypeScriptLibrary ( codeModel , host ) { const project = new Project ( { useInMemoryFileSystem : true , manipulationSettings : { indentationText : IndentationText . TwoSpaces } } ) ; const { packageDetails , licenseHeader : shouldGenerateLicense , generateTest , generateSample , outputPath , srcPath } = getAutorestOptions ( ) ; const clientDetails = await transformCodeModel ( codeModel ) ; conflictResolver ( clientDetails ) ; if ( generateSample ) { clientDetails . samples = await transformSamples ( codeModel , clientDetails ) ; } generatePackageJson ( project , clientDetails ) ; generateLicenseFile ( project ) ; generateReadmeFile ( clientDetails , project ) ; if ( generateTest ) { generateSampleTestFile ( project ) ; } generateTsConfig ( project ) ; generateRollupConfig ( project ) ; generateApiExtractorConfig ( project ) ; generateClient ( clientDetails , project ) ; generateModels ( clientDetails , project ) ; generateMappers ( clientDetails , project ) ; generateOperations ( clientDetails , project ) ; generateOperationsInterfaces ( clientDetails , project ) ; if ( generateSample && clientDetails ?. samples ?. length && clientDetails ?. samples ?. length > 0 ) { generateSamples ( clientDetails , project ) ; generateSampleEnv ( project ) ; } generateParameters ( clientDetails , project ) ; generateIndexFile ( project , clientDetails ) ; await generateLroFiles ( clientDetails , project ) ; generateTracingFile ( project ) ; const licenseHeader = `
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT License.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 * Changes may cause incorrect behavior and will be lost if the code is regenerated.
 */
` ; project . saveSync ( ) ; const fs = project . getFileSystem ( ) ; const pathToClear = outputPath ? path . join ( outputPath , srcPath ) : srcPath ; fsextra . emptyDirSync ( `${ pathToClear }` ) ; for ( const file of project . getSourceFiles ( ) ) { const filePath = file . getFilePath ( ) ; const isJson = /\.json$/gi . test ( filePath ) ; const isSourceCode = /\.(ts|js)$/gi . test ( filePath ) ; let fileContents = fs . readFileSync ( filePath ) ; if ( shouldGenerateLicense && isSourceCode ) { fileContents = `${ licenseHeader . trimLeft ( ) }\n${ fileContents }` ; } if ( isJson || isSourceCode ) { fileContents = prettier . format ( fileContents , isJson ? prettierJSONOptions : prettierTypeScriptOptions ) ; } host . writeFile ( { filename : filePath . substr ( 1 ) , content : fileContents } ) ; } }