首頁 > web前端 > js教程 > 主體

使用 degit 在 CLI 工具中下載範本。

王林
發布: 2024-08-17 06:51:32
原創
594 人瀏覽過

我在Remotion原始碼中發現了一個名為「degit」的檔案。
Remotion 可協助您以程式設計方式製作影片。

在本文中,我們將了解以下概念:

  1. 什麼是數字?
  2. 受 Remotion 的 degit 檔案啟發,建立一個簡單的 degit 函數

什麼是數字?

我確實記得在開源自述文件之一中提到過“degit”,但我不記得它是哪個存儲庫,所以我用谷歌搜索了 degit 的含義並找到了這個 degit npm 包。

簡單來說,您可以使用 degit 只需下載最新的提交即可快速複製 Github 儲存庫
而不是整個 git 歷史記錄。

訪問 degit 的官方 npm 包以了解有關此包的更多資訊。

您也可以使用此 degit 套件從 Gitlab 或 Bitbucket 下載儲存庫,因此它不僅限於 Github 儲存庫。

# download from GitLab
degit gitlab:user/repo

# download from BitBucket
degit bitbucket:user/repo

degit user/repo
# these commands are equivalent
degit github:user/repo
登入後複製

這是 Javascript 的範例用法:

const degit = require('degit');

const emitter = degit('user/repo', {
    cache: true,
    force: true,
    verbose: true,
});

emitter.on('info', info => {
    console.log(info.message);
});

emitter.clone('path/to/dest').then(() => {
    console.log('done');
});
登入後複製

受 Remotion 的 degit 檔案啟發,建立一個簡單的 degit 函數

要了解如何建立簡單的 degit 函數,讓我們分解 Remotion 的 degit.ts 檔案中的程式碼。該檔案實作了 degit npm 套件的基本版本:取得 GitHub 儲存庫的最新狀態,而不下載完整的歷史記錄。

1.使用進口

import https from 'https';
import fs from 'node:fs';
import {tmpdir} from 'node:os';
import path from 'node:path';
import tar from 'tar';
import {mkdirp} from './mkdirp';
登入後複製
  • https:用於發出網路請求以取得儲存庫。
  • fs:與檔案系統交互,例如寫入下載的檔案。
  • tmpdir:提供系統的暫存目錄路徑。
  • path:處理和轉換檔案路徑。
  • tar:提取 tarball(壓縮檔案)的內容。
  • mkdirp:遞歸建立目錄的輔助函數,在單獨的檔案中提供。

2:取得儲存庫

export function fetch(url: string, dest: string) {
    return new Promise<void>((resolve, reject) => {
        https.get(url, (response) => {
            const code = response.statusCode as number;
            if (code >= 400) {
                reject(
                    new Error(
                        `Network request to ${url} failed with code ${code} (${response.statusMessage})`,
                    ),
                );
            } else if (code >= 300) {
                fetch(response.headers.location as string, dest)
                    .then(resolve)
                    .catch(reject);
            } else {
                response
                    .pipe(fs.createWriteStream(dest))
                    .on('finish', () => resolve())
                    .on('error', reject);
            }
        }).on('error', reject);
    });
}
登入後複製
  • URL處理:此函數檢查請求是否成功(狀態代碼低於300)。 如果是重定向(程式碼在 300 到 399 之間),它將遵循新的 URL。 如果是錯誤(代碼 400+),則拒絕承諾。
  • 檔案儲存:使用 fs.createWriteStream 下載儲存庫並將其儲存到目標路徑。

3:提取儲存庫

下載儲存庫後,需要擷取壓縮包的內容:

function untar(file: string, dest: string) {
    return tar.extract(
        {
            file,
            strip: 1,
            C: dest,
        },
        [],
    );
}
登入後複製
  • Tar 提取:此函數將 .tar.gz 檔案的內容提取到指定的目標目錄。

4:把它們放在一起

主要的 degit 函數將所有內容連結在一起,處理目錄建立、取得和提取儲存庫:

export const degit = async ({
    repoOrg,
    repoName,
    dest,
}: {
    repoOrg: string;
    repoName: string;
    dest: string;
}) => {
    const base = path.join(tmpdir(), '.degit');
    const dir = path.join(base, repoOrg, repoName);
    const file = `${dir}/HEAD.tar.gz`;
    const url = `https://github.com/${repoOrg}/${repoName}/archive/HEAD.tar.gz`;

    mkdirp(path.dirname(file));
    await fetch(url, file);

    mkdirp(dest);
    await untar(file, dest);
    fs.unlinkSync(file);
};
登入後複製

mkdirp 用於建立
遞歸地目錄。

結論:

我發現當您執行安裝指令時,remotion 使用 degit 下載範本:

npx create-video@latest
登入後複製

此指令要求您選擇一個模板,這是 degit 開始下載的地方
所選模板的最新提交

您可以從 create-video 套件中檢查此程式碼作為證明。

Use degit to download a template in your CLI tool.

取得受開源最佳實踐啟發的免費課程。

關於我:

網址:https://ramunarasinga.com/

Linkedin:https://www.linkedin.com/in/ramu-narasinga-189361128/

Github:https://github.com/Ramu-Narasinga

電子郵件:ramu.narasinga@gmail.com

了解開源中使用的最佳實踐。

參考:

  1. https://github.com/Rich-Harris/degit
  2. https://github.com/remotion-dev/remotion/blob/main/packages/create-video/src/degit.ts
  3. https://github.com/remotion-dev/remotion/blob/c535e676badd055187d1ea8007f9ac76ab0ad315/packages/create-video/src/init.ts#L10ad315/packages/create-video/src/init.ts#L109
  4. https://github.com/remotion-dev/remotion/blob/main/packages/create-video/src/mkdirp.ts

以上是使用 degit 在 CLI 工具中下載範本。的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!