🎥 Make videos programmatically with React
simplify degit
Jonny Burger2adfb7669ae5
diff --git a/packages/create-video/src/degit/index.ts b/packages/create-video/src/degit/index.tsnew file mode 100644index 0000000000..42e8287da9--- /dev/null+++ b/packages/create-video/src/degit/index.ts@@ -0,0 +1,94 @@+import fs from 'fs';+import path from 'path';+import tar from 'tar';+import {Log} from '../log';++import https from 'https';+import {homedir, tmpdir} from 'os';++const homeOrTemp = homedir() || tmpdir();++export function mkdirp(dir: string) {+ const parent = path.dirname(dir);+ if (parent === dir) return;++ mkdirp(parent);++ try {+ fs.mkdirSync(dir);+ } catch (err) {+ if ((err as {code: string}).code !== 'EEXIST') throw err;+ }+}++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);+ });+}++const base = path.join(homeOrTemp, '.degit');++export const degit = async (+ repoOrg: string,+ repoName: string,+ src: string,+ dest: string+) => {+ const dir = path.join(base, repoOrg, repoName);++ await cloneWithTar(src, dir, dest);+};++async function cloneWithTar(src: string, dir: string, dest: string) {+ const file = `${dir}/HEAD.tar.gz`;+ const url = `${src}/archive/HEAD.tar.gz`;++ try {+ try {+ fs.statSync(file);+ } catch (err) {+ mkdirp(path.dirname(file));++ await fetch(url, file);+ }+ } catch (err) {+ Log.error('Could not download ' + url);+ Log.error(err);+ process.exit(1);+ }++ mkdirp(dest);+ await untar(file, dest);+}++function untar(file: string, dest: string) {+ return tar.extract(+ {+ file,+ strip: 1,+ C: dest,+ },+ []+ );+}diff --git a/packages/create-video/src/init.ts b/packages/create-video/src/init.tsindex aafe3a2912..6f977b9889 100644--- a/packages/create-video/src/init.ts+++ b/packages/create-video/src/init.ts@@ -1,8 +1,5 @@import chalk from 'chalk';-import degit from 'degit';import execa from 'execa';-import fs from 'fs';-import path from 'path';import {stripAnsi} from './strip-ansi';import {Log} from './log';import {openInEditorFlow} from './open-in-editor-flow';@@ -12,51 +9,59 @@ import {selectPackageManager,} from './pkg-managers';import prompts, {selectAsync} from './prompts';-import {homedir, tmpdir} from 'os';import {resolveProjectRoot} from './resolve-project-root';import {patchReadmeMd} from './patch-readme';import {patchPackageJson} from './patch-package-json';+import {degit} from './degit';type TEMPLATES = {shortName: string;- name: string;description: string;+ org: string;+ repoName: string;};const FEATURED_TEMPLATES: TEMPLATES[] = [{shortName: 'Hello World',- name: 'remotion-dev/template-helloworld',+ org: 'remotion-dev',+ repoName: 'template-helloworld',description: 'The default starter template (recommended)',},{shortName: 'Blank',- name: 'remotion-dev/template-empty',description: 'Nothing except an empty canvas',+ org: 'remotion-dev',+ repoName: 'template-empty',},{shortName: 'Hello World (Javascript)',- name: 'remotion-dev/template-helloworld-javascript',+ org: 'remotion-dev',+ repoName: 'template-helloworld-javascript',description: 'The default starter template in plain JS',},{shortName: 'React Three Fiber',- name: 'remotion-dev/template-three',+ org: 'remotion-dev',+ repoName: 'template-three',description: 'Remotion + React Three Fiber Starter Template',},{shortName: 'Still images',- name: 'remotion-dev/template-still',+ org: 'remotion-dev',+ repoName: 'template-still',description: 'Dynamic PNG/JPEG template with built-in server',},{shortName: 'Text To Speech',- name: 'FelippeChemello/Remotion-TTS-Example',+ org: 'FelippeChemello',+ repoName: 'Remotion-TTS-Example',description: 'Turns text into speech and makes a video',},{shortName: 'Audiogram',- name: 'marcusstenbeck/remotion-template-audiogram',+ org: 'marcusstenbeck',+ repoName: 'remotion-template-audiogram',description: 'Text and waveform visualization for podcasts',},];@@ -120,7 +125,7 @@ export const init = async () => {}return {- value: template.name,+ value: template,title:chalk.bold(padEnd(template.shortName, descriptionColumn)) +template.description.trim(),@@ -128,30 +133,17 @@ export const init = async () => {}),},{}- )) as string;+ )) as TEMPLATES;const pkgManager = selectPackageManager();try {- const homeOrTmp = homedir() || tmpdir();-- // Remove degit cache because of https://github.com/remotion-dev/remotion/issues/852- // https://github.com/Rich-Harris/degit/issues/313- const degitFolder = path.join(- homeOrTmp,- '.degit',- 'github',- ...selectedTemplate.split('/')+ await degit(+ selectedTemplate.org,+ selectedTemplate.repoName,+ `https://github.com/${selectedTemplate}`,+ projectRoot);-- if (fs.existsSync(degitFolder)) {- await (fs.promises.rm ?? fs.promises.rmdir)(degitFolder, {- recursive: true,- });- }-- const emitter = degit(`https://github.com/${selectedTemplate}`);- await emitter.clone(projectRoot);patchReadmeMd(projectRoot, pkgManager);patchPackageJson(projectRoot, folderName);} catch (e) {