日: 2022年5月2日

  • CDK プロジェクトに静的検証ツールとコードフォーマッタをインストールする

    コードフォーマッターとしての Prettier (プリティア)をプロジェクトに組み込む方法を紹介。

    また、2021 年に静的検証ツールとしての ESLintがPrettierが merge され、今までは別々にインストールする必要があったが、 Prettier のみインストールすればよくなった。

    “prettier/@typescript-eslint” has been merged into “prettier” in eslint-config-prettier 8.0.0. See: https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md#version-800-2021-02-21

    Prettier

    インストール方法は以下の通り

    npm install -D prettier eslint-config-prettier eslint-plugin-prettier

    npm init @eslint/config

    
    Ok to proceed? (y) y
    ✔ How would you like to use ESLint? · problems
    ✔ What type of modules does your project use? · esm
    ✔ Which framework does your project use? · none
    ✔ Does your project use TypeScript? · No / Yes
    ✔ Where does your code run? · browser
    ✔ What format do you want your config file to be in? · JavaScript
    The config that you've selected requires the following dependencies:
    
    @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
    ✔ Would you like to install them now with npm? · No / Yes
    Installing @typescript-eslint/eslint-plugin@latest, @typescript-eslint/parser@latest
    
    removed 7 packages, and audited 858 packages in 4s
    
    47 packages are looking for funding
      run `npm fund` for details
    
    1 critical severity vulnerability
    
    To address all issues, run:
      npm audit fix
    
    Run `npm audit` for details.
    A config file was generated, but the config file itself may not follow your linting rules.
    Successfully created .eslintrc.js file in /Users/sumito.tsukada/go/src/git.rarejob.com/rarejob-platform/account-web/aws/rjpf-student-account-web-alb-ecsc-ecr-cdeploy
    

    プロジェクト直下に js を配置

    vi .prettierrc.js
    
    module.exports = {
        singleQuote: true, 
        tabWidth: 4, 
        trailingComma: 'es5', 
    };
    

    package.json を一部修正

         "build": "tsc",
         "watch": "tsc -w",
         "test": "jest",
    -    "cdk": "cdk"
    +    "cdk": "cdk",
    +    "lint": "eslint --ext .ts ."
       },
       "devDependencies": {
         "@aws-cdk/assertions": "1.135.0",
    

    警告

     npm run lint
    
    > sample@0.1.0 lint
    > eslint --ext .ts .
    
    
    sample.test.ts
      1:20  warning  'Match' is defined but never used  @typescript-eslint/no-unused-vars
    
    ✖ 1 problem (0 errors, 1 warning)
    
    

  • AWS CDK をはじめる

    cdk init コマンドによりはじめることができる

    cdk init sample-app --language=typescript
    

    出力結果は以下の通り

    % cdk init sample-app --language=typescript
    Applying project template sample-app for typescript
    # Welcome to your CDK TypeScript project!
    
    You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`RjpfStudentAccountWebAlbEcscEcrCdeployStack`)
    which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.
    
    The `cdk.json` file tells the CDK Toolkit how to execute your app.
    
    ## Tutorial  
    See [this useful workshop](https://cdkworkshop.com/20-typescript.html) on working with the AWS CDK for Typescript projects.
    
    
    ## Useful commands
    
     * `npm run build`   compile typescript to js
     * `npm run watch`   watch for changes and compile
     * `npm run test`    perform the jest unit tests
     * `cdk deploy`      deploy this stack to your default AWS account/region
     * `cdk diff`        compare deployed stack with current state
     * `cdk synth`       emits the synthesized CloudFormation template
    
    Executing npm install...
    npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
    npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
    npm WARN deprecated sane@4.1.0: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
    &#x2705; All done!
    

    上記は sample-app としたことで、作成されたファイル群にサンプルコードを入れることができるが、

    cdk init app --language=typescript
    

    にすれば空の CDK アプリケーションを作成することができ、

    cdk init lib --language=typescript
    

    と指定すれば Constructライブラリを生成することができる。こちらのコマンドは TypeScript のみ対応しているようだ。