1. 程式人生 > >使用Theia——構建你自己的IDE

使用Theia——構建你自己的IDE

上一篇:Theia架構

構建你自己的IDE

  本指南將教你如何構建你自己的Theia應用。

必要條件

  你需要安裝node 10版本(譯者:事實上最新的node穩定版即可):

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
nvm install 10

  以及yarn:

npm install -g yarn

  還需要確保已安裝python 2.x,可通過python --version來檢查。

安裝

  首先請建立一個空目錄,然後切換到這個目錄下:

mkdir my-app
cd my-app

  在這個目錄下建立package.json:

{
  "private": true,
  "dependencies": {
    "typescript": "latest",
    "@theia/typescript": "next",
    "@theia/navigator": "next",
    "@theia/terminal": "next",
    "@theia/outline-view": "next",
    "@theia/preferences": "next",
    "@theia/messages": "next",
    "@theia/git": "next",
    "@theia/file-search": "next",
    "@theia/markers": "next",
    "@theia/preview": "next",
    "@theia/callhierarchy": "next",
    "@theia/merge-conflicts": "next",
    "@theia/search-in-workspace": "next",
    "@theia/json": "next",
    "@theia/textmate-grammars": "next",
    "@theia/mini-browser": "next"
  },
  "devDependencies": {
    "@theia/cli": "next"
  }
}

  簡而言之,Theia應用程式和擴充套件包都是Node.js包。每一個包都包含一個package.json檔案,裡面列出了包的一些元資料,如name、version、執行時和構建時的依賴關係等。

  我們來看看這個包的內容:
  • name和version被省略了,因為我們不打算將它作為一個依賴項來使用。同時它被標記為private,因為不打算將它釋出為一個獨立的Node.js包。
  • 我們在dependencies中列出了所有執行時依賴的擴充套件包,如@theia/navigator。
    • 有些擴充套件包需要額外的工具來進行安裝,例如,@theia/python需要Python Language Server來安裝。此時你需要參考相應的文件。
    • 可以在這裡檢視所有已釋出的擴充套件包。
  • 我們將@theis/cli列為構建時的依賴項,它提供了構建和執行應用程式的指令碼。

構建

  首先,安裝所有的依賴項。

yarn

  然後,使用Theia CLI來構建應用程式。

yarn theia build

  yarn在我們應用程式的上下文中查詢由@theia/cli提供的theia可執行檔案,然後使用theia執行build命令。這可能需要一些時間,因為預設情況下應用程式會在production模式下進行構建,即它會進行模糊處理和最小化處理。

執行

  構建完成之後,我們就可以啟動應用程式:

yarn theia start

  你可以在命令的第一個引數中指定一個workspace路徑,--hostname和--port選項用來指定部署的主機名和埠號。例如下面的命令在指定的位置和埠號上開啟/workspace:

yarn theia start /my-workspace --hostname 0.0.0.0 --port 8080

  在終端中,你應該看到Theia應用程式已經啟動並監聽:

   開啟瀏覽器並輸入上面顯示的地址,你就可以開啟應用程式了。

故障排除

通過代理構建本地依賴項

  如果你通過代理執行yarn命令,在構建本地依賴項時有可能會遇到一些問題(如onigurma),例如下面的這個錯誤:

[4/4] Building fresh packages...
[1/9]  XXXXX
[2/9]  XXXXX
[3/9]  XXXXX
[4/9]  XXXXX
error /theiaide/node_modules/XXXXX: Command failed.
Exit code: 1
Command: node-gyp rebuild
Arguments:
Directory: /theiaide/node_modules/XXXXX
Output:
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp http GET https://nodejs.org/download/release/v8.15.0/node-v8.15.0-headers.tar.gz
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: read ECONNRESET
gyp ERR! stack at TLSWrap.onread (net.js:622:25)
gyp ERR! System Linux 3.10.0-862.11.6.el7.x86_64
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /theiaide/node_modules/XXXXX
gyp ERR! node -v v8.15.0

  這是因為node-gyp在system/NPM的代理設定中不工作。如果遇到這種情況,可以通過錯誤堆疊中提供的連結下載node-headers檔案(如上面例子中的https://nodejs.org/download/release/v8.15.0/node-v8.15.0-headers.tar.gz),然後使用下面的命令進行構建:

npm_config_tarball=/path/to/node-v8.15.0-headers.tar.gz yarn install

 

原文地址:https://theia-ide.org/docs/composing_applications/