Escher-js / SkimmedMilk

0 stars 0 forks source link

install diff2html to electron (obsolete) #27

Closed psmuler closed 1 year ago

psmuler commented 1 year ago

diff2hmtlについてもいきなり実装せず、まずはインストール、インポートができているか確認するためにバージョン番号を表示してみましょう

psmuler commented 1 year ago

diff2htmlのバージョン番号を表示するには、まずdiff2htmlをインストールし、次にメインファイル(例:main.js)にインポートしてバージョン番号を表示するコードを追加します。

  1. diff2htmlをインストール: プロジェクトディレクトリで以下のコマンドを実行して、diff2htmlをインストールします。
npm install diff2html --save
  1. diff2htmlをインポートしてバージョン番号を表示: メインファイル(例:main.js)にdiff2htmlをインポートし、バージョン番号を表示するコードを追加します。
const { Diff2Html } = require('diff2html');
console.log(`Diff2Html version: ${Diff2Html.version}`);

アプリを実行すると、コンソールにDiff2Htmlのバージョン番号が表示されます。これで、diff2htmlのインストールとインポートが正常に行われていることが確認できます。

psmuler commented 1 year ago

インストールできていないようです。あなたが想定しているdiff2htmlのバージョンを教えてください

psmuler commented 1 year ago

申し訳ありませんが、私の知識は2021年9月までのものであり、その後の情報は分かりません。2021年9月時点での最新バージョンはdiff2html 3.4.0です。ただし、現在はもっと新しいバージョンが利用可能である可能性があります。

最新バージョンの情報を取得するには、以下の手順を実行してください。

  1. npm公式ウェブサイトでの検索: npmの公式ウェブサイト(https://www.npmjs.com/)で`diff2html`を検索し、表示されるページで最新バージョンを確認できます

  2. コマンドラインでの確認: コマンドラインでnpm showコマンドを実行することで、最新バージョンを確認できます。

npm show diff2html version

このコマンドは、最新のdiff2htmlバージョンを表示します。最新バージョンをインストールするには、以下のコマンドを実行してください。

npm install diff2html@latest --save

これで最新バージョンのdiff2htmlがインストールされます。その後、正常にインポートされるかどうかを確認するために、先程の手順でバージョン番号を表示するコードを追加してください。

psmuler commented 1 year ago

chatGPTさんのお手伝いが欲しいので、version3.4.0で実装しましょう。https://diff2html.xyz/なども参考にしてください

psmuler commented 1 year ago

diff2html 3.4.0を使用して、ElectronアプリでGitのdiffをHTML形式で表示する方法を説明します。diff2html 3.4.0をインストールして、main.jsindex.htmlファイルを変更します。

  1. diff2html 3.4.0をインストール: プロジェクトディレクトリで以下のコマンドを実行して、diff2html 3.4.0をインストールします。
npm install diff2html@3.4.0 --save
  1. main.jsファイルの変更: main.jsファイルに、diff2htmlをインポートして、GitのdiffをHTML形式に変換するコードを追加します。ウェブページに表示するために、変換されたHTMLをindex.htmlファイルに渡します。
const { app, BrowserWindow } = require('electron');
const { Diff2Html } = require('diff2html');
const fs = require('fs');
const path = require('path');

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    },
  });

  win.loadFile('index.html');

  fs.readFile(path.join(__dirname, 'example.diff'), 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return;
    }

    const diffHtml = Diff2Html.getPrettyHtml(data, {
      inputFormat: 'diff',
      outputFormat: 'line-by-line',
      showFiles: true,
      matching: 'lines',
      synchronisedScroll: true,
    });

    win.webContents.send('diffHtml', diffHtml);
  });
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
  1. index.htmlファイルの変更: index.htmlファイルに、表示するGitのdiffを受け取るためのコードを追加します。ElectronのipcRendererを使って、main.jsファイルからのメッセージをリッスンします。
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Diff2Html Electron App</title>
    <script>
      const { ipcRenderer } = require('electron');

      ipcRenderer.on('diffHtml', (event, diffHtml) => {
        document.getElementById('diff-container').innerHTML = diffHtml;
      });
    </script>
  </head>
  <body>
    <div id="diff-container"></div>
  </body>
</html>
  1. アプリの実行: 以下のコマンドを実行して、Electronアプリを起動します。
npm start

これで、ElectronアプリでGitのdiffをHTML形式で表示する方法

psmuler commented 1 year ago

error...

結局この点は公式Documentを読んだ方が早かった。 問題となっていたのは今回の場合require文の不適切さ(後で見返すと、他のブランチでchatGPTから提案はあった。) 誤: const { Diff2Html } = require('diff2html'); 正: const Diff2Html = require('diff2html');

外部ライブラリは使用の切り替わりが早いので、chatGPTも一つの仕様に収斂するわけではなくなってしまう。 versionを指定する方法もうまくいかないことが多いので、そこをもう少し掘り込んでもいいかもしれない。