0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

kintone JS SDK (v0.7.1対応) を使ってコマンドラインでkintoneアプリを修正・反映する方法

Last updated at Posted at 2019-11-06

注意

こちらの記事で紹介している kintone JS SDK ですが、新しくrest-api-clientがリリースされて、JS SDKは非推奨になっています。ご注意ください。
https://github.com/kintone/kintone-js-sdk/

はじめに

kintoneアプリを開発環境から本環境のデプロイをコマンドラインで行う方法について調べてみました。
コードはNode.jsで書いています。SDKには kintone JS SDKを利用します。

本記事の対象

  • 稼働中のアプリの修正を開発環境で行った後に、本環境にデプロイしている。
  • 本環境にログインしてGUIにて修正作業を行いたく無い。

環境

(2019-11-06現在)

  • mac OS 10.14.6
  • npm 6.9.0
  • node v12.3.1
  • kintone JS SDK 0.7.1

サンプルアプリ

スクリーンショット 2019-11-05 18.06.46.png

流れ

所謂デプロイ系APIを使います。

  1. フォームの設定をJSONで取得
  2. エディタで修正
  3. 修正したJSONにてフォームの設定を更新する

フォームの設定の取得

フィールドの一覧を取得する APIを使って、JSONでフィールドの設定情報を取得してみます。
処理にはkintone JS SDKライブラリのgetFormFields()を利用します。

インストールは下記リンク参照
https://kintone.github.io/kintone-js-sdk/latest/getting-started/quickstart-node/

コード

index.js
require('dotenv').config()
const kintone = require('@kintone/kintone-js-sdk');
// 認証
const kintoneAuth = new kintone.Auth();
const auth = {
    username: process.env.USER_NAME, // パスワード認証時のID
    password: process.env.PASS_WORD // パスワード認証時のパスワード
};
kintoneAuth.setPasswordAuth(auth);
// 接続
const conn = {
    domain: process.env.USER_DOMAIN,
    auth: kintoneAuth
};
const kintoneConnection = new kintone.Connection(conn);
const kintoneApp = new kintone.App({connection: kintoneConnection});
const app = process.env.APPID;
// フィールド一覧取得
kintoneApp.getFormFields({app})
.then((response) => {
  console.log(JSON.stringify(response, null, 2));
})
.catch((error) => {
  console.log(error);
});

出力結果を form-fields.json などのファイルにリダイレクトしておきます。

ファイルを修正

消費税率のドロップダウンフィールドの初期値を修正します。
"revision" と "properties" も 読み込み時に必要無いため削除します。

diff --git a/form-fields.json b/form-fields.json
index a4e94f0..7bbc978 100644
--- a/form-fields.json
+++ b/form-fields.json
@@ -1,6 +1,4 @@
 {
-  "revision": "6",
-  "properties": {
     "カテゴリー": {
       "type": "CATEGORY",
       "code": "カテゴリー",
@@ -39,7 +37,7 @@
           "index": "0"
         }
       },
-      "defaultValue": "8%"
+      "defaultValue": "10%"
     },
     "更新者": {
       "type": "MODIFIER",
@@ -71,5 +69,4 @@
       "label": "作成日時",
       "noLabel": false
     }
-  }
 }

フィールドの設定を変更

フィールドの設定を変更するAPI を利用します。
実際の処理にはkintone JS SDKライブラリのupdateFormFields()を利用します。

コード

const fs = require('fs');
require('dotenv').config()
const kintone = require('@kintone/kintone-js-sdk');

// 認証
const kintoneAuth = new kintone.Auth();
const auth = {
    username: process.env.USER_NAME, // パスワード認証時のID
    password: process.env.PASS_WORD // パスワード認証時のパスワード
};
kintoneAuth.setPasswordAuth(auth);

// 接続
const conn = {
    domain: process.env.USER_DOMAIN,
    auth: kintoneAuth
};
const kintoneConnection = new kintone.Connection(conn);
const kintoneApp = new kintone.App({connection: kintoneConnection});
const app = process.env.APPID;
// ファイル読み込み
const f = fs.readFileSync('./form-fields.json', 'utf8');
// console.log(JSON.parse(f));
var fields = JSON.parse(f);
// フィールド更新
kintoneApp.updateFormFields({app, fields})
.then((response) => {
    console.log(response);
})
.catch((error) => {
    console.log(error);
});

デプロイ

デプロイというか前段の修正を反映させます。
アプリの設定を変更→アプリを更新 しても良し。API使っても良し。

APIはアプリの設定の運用環境への反映。SDKはdeployAppSettings()を使います。

コード

実際の処理の時は、パラメーターの内の revision は前段の処理の戻り値をセットする感じになるかと思います。

deploy.js
require('dotenv').config()
const kintone = require('@kintone/kintone-js-sdk');

// 認証
const kintoneAuth = new kintone.Auth();
const auth = {
    username: process.env.USER_NAME, // パスワード認証時のID
    password: process.env.PASS_WORD // パスワード認証時のパスワード
};
kintoneAuth.setPasswordAuth(auth);

// 接続
const conn = {
    domain: process.env.USER_DOMAIN,
    auth: kintoneAuth
};
const kintoneConnection = new kintone.Connection(conn);
const kintoneApp = new kintone.App({connection: kintoneConnection});
const app = process.env.APPID;
const appsettings = [{
    revision: '7',
    app: app
}];
// アプリの設定の運用環境への反映
kintoneApp.deployAppSettings({apps: appsettings, revert: false})
.then((response) => {
    console.log(response);
})
.catch((error) => {
    console.log(error);
});

デプロイ前

アプリを更新が押されて無い状態

スクリーンショット 2019-11-06 5.58.44.png

デプロイ後

アプリを更新が押されている状態

スクリーンショット 2019-11-06 10.02.48.png

ドロップダウンの初期値が10%に変更されています。
スクリーンショット 2019-11-06 10.03.47.png

感じたことなど

kintoneのAPIを試したい時、Node環境とSDKで試すのは楽で良いですね。
ただ、ドキュメントのサンプルコードが分かりにくかった。パラメータの渡すところをもう少し丁寧に書いて欲しかった。

例えば、
https://kintone.github.io/kintone-js-sdk/latest/reference/app/
の下記の箇所

const connection = new kintone.Connection(paramsConnection);
const kintoneApp = new kintone.App({connection});

パラメーターを渡す箇所は

const kintoneApp = new kintone.App({connection: connection});

と書いてある方が、自分としては分かりやすかった。

おかげで色々調べて英語版のドキュメントの読み方の理解は進みました。

もっと便利な方法

ここまで書いてきて何ですが、実際はもっと便利なツールがありますので、通常はこちらを使った方が良いかと思います。


関連リンク

npm 関連

kintone JS SDK 関連

kintone REST API 関連

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?