我在remotion源代码中发现了一个名为“degit”的文件。
remotion 可帮助您以编程方式制作视频。
在本文中,我们将了解以下概念:
什么是degit?受 remotion 的 degit 文件启发,构建一个简单的 degit 函数
什么是数字?
我记得在开源的自述文件中看到过“degit”,但我不记得它是哪个存储库,所以我在谷歌上搜索了 degit 的含义并找到了这个 degit npm 包。
简单来说,您可以使用 degit 只需下载最新的提交即可快速复制 github 存储库
而不是整个 git 历史记录。
访问 degit 官方 npm 包以了解有关此包的更多信息。
您也可以使用此 degit 包从 gitlab 或 bitbucket 下载存储库,因此它不仅限于 github 存储库。
# download from gitlabdegit gitlab:user/repo# download from bitbucketdegit bitbucket:user/repodegit user/repo# these commands are equivalentdegit 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((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:提取存储库
下载存储库后,需要提取tarball的内容:
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包中查看此代码作为证明。

获得受开源最佳实践启发的免费课程。
关于我:
网站:https://ramunarasinga.com/
linkedin:https://www.linkedin.com/in/ramu-narasinga-189361128/
github:https://github.com/ramu-narasinga
邮箱:ramu.narasinga@gmail.com
学习开源中使用的最佳实践。
参考:
https://github.com/rich-harris/degithttps://github.com/remotion-dev/remotion/blob/main/packages/create-video/src/degit.tshttps://github.com/remotion-dev/remotion/blob/c535e676badd055187d1ea8007f9ac76ab0ad315/packages/create-video/src/init.ts#l109https://github.com/remotion-dev/remotion/blob/main/packages/create-video/src/mkdirp.ts
以上就是使用 degit 在 CLI 工具中下载模板的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1490547.html
微信扫一扫
支付宝扫一扫