如何创建一个Azure函数来读取由Typescript生成的dist文件夹?

yshpjwxd  于 2023-03-09  发布在  TypeScript
关注(0)|答案(1)|浏览(99)

我有一个azure函数,在JS中工作得很好,但我已经将代码迁移到TS。当我运行部署时,我确实看到了TS生成的dist文件夹,然而,如果我转到Azure门户,我仍然看到我的旧JS函数,但没有dist文件夹的迹象。我已经通过Kudu确认dist文件夹存在,它包含新代码。
如何让Azure函数知道从dist文件夹读取?
这是我当前的文件夹结构:

- Project
  - azure_func
    - main_func_app
      - funcs
        -func_js
           index.js
           my_ref_vals.json
           function.json
        -func_ts
           index.ts
           my_ref_vals.json
           function.json
        -dist
         -func_ts
            index.js
            index.js.map
       package.json
       host.json
       tsconfig.json

tsconfig.json的内容

{
    "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "strict": true
    },
    "include": ["./**/*.ts"],
    "exclude": ["dist", "node_modules"]
}

包的内容. json

{
"version": "0.0.0",
"description": "",
"scripts": {
  "build": "tsc",
  "watch": "tsc -w",
  "prestart": "npm run build",
  "start": "func host start --typescript",
  "test": "echo \"No tests yet...\"",
  "lint": "eslint .",
  "format": "prettier --write ."
},
"devDependencies":{
  "@types/jest": "^28.1.6",
  "@types/node": "16.x",
  "@typescript-eslint/eslint-plugin": "^5.31.0",
  "@typescript-eslint/parser": "^5.31.0",
  "eslint": "^8.20.0",
  "eslint-config-prettier": "^8.5.0",
}
}

部署文件:

- stage: Build
  displayName: Build stage for Azure Function
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
      - task: NodeTool@0
        inputs:
          versionSpec: '14.x'
          displayName: 'Install Node.js'

      - task: AzureCLI@2
        inputs:
          azureSubscription: 'mysub-test-group-SPN'
          scriptType: 'bash'
          scriptLocation: 'inlineScript'
          inlineScript: |
            npm install
            npm run build

      - task: ArchiveFiles@2
        displayName: 'Create artifact'
        inputs: 
           rootFolderOrFile: 'Project/azure_func/main_func_app/funcs'
           includeRootFolder: false
           archiveType: zip
           archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
           replaceExistingArchive: true

      - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        displayName: 'Publish Azure Functions Artifact'
        artifact: functions

      - task: AzureFunctionApp@1
        displayName: 'Deploy Azure Functions'
        inputs:
          azureSubscription: 'mysub-test-group-SPN'
          appType: 'functionApp'
          appName: 'azure_funcs'
          package: '$(Pipeline.Workspace)/functions/$(Build.BuildId).zip'
          deploymentMethod: 'auto'

如果有不清楚的地方请发表意见。

umuewwlo

umuewwlo1#

当然,首先要检查你的文件是否被正确部署/复制。我不确定你是说你没有设法部署你的文件夹,还是你的代码没有设法定位你的文件夹/文件。
我注意到根文件夹可能不是以同样的方式定位的,无论你是调试你的应用还是部署它。部署的应用维护文件夹结构,但我的本地azf没有(可能是我这边的配置)。
如果它是一个路径问题..在C#中我不得不读一个文件太在我的AZF。我做了这样的东西:

string templateFileName = "myfile.txt";
string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

// First part of the ternary identify local file when you debug it
// If not, it will into my AZF project under this path ~/Functions/ExportFunction/myfile.txt
string templatePath = File.Exists(Path.Combine(currentDirectory, templateFileName))
                        ? Path.Combine(currentDirectory, templateFileName)
                        : Path.Combine(currentDirectory, "Functions", "ExportFunction", templateFileName);

您应该能够在JS中找到等价的
不确定是否有帮助

相关问题