🎥 Make videos programmatically with React
update to show all the templates
Shankhadeep Dey858616f6427c
diff --git a/package-lock.json b/package-lock.jsonindex b7ebd4bff7..0f66f3a8af 100644--- a/package-lock.json+++ b/package-lock.json@@ -11,6 +11,7 @@"@react-three/fiber": "^6.1.2","react": "^17.0.1","react-dom": "^17.0.1",+ "strip-ansi": "^3.0.1","three": "^0.128.0"},"devDependencies": {@@ -1884,7 +1885,6 @@"version": "2.1.1","resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz","integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=",- "dev": true,"engines": {"node": ">=0.10.0"}@@ -12418,7 +12418,6 @@"version": "3.0.1","resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz","integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",- "dev": true,"dependencies": {"ansi-regex": "^2.0.0"},@@ -15283,8 +15282,7 @@"ansi-regex": {"version": "2.1.1","resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",- "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=",- "dev": true+ "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8="},"ansi-styles": {"version": "3.2.1",@@ -23740,7 +23738,6 @@"version": "3.0.1","resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz","integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",- "dev": true,"requires": {"ansi-regex": "^2.0.0"}diff --git a/package.json b/package.jsonindex 6523b622d6..0be6b1a225 100644--- a/package.json+++ b/package.json@@ -29,6 +29,7 @@"@react-three/fiber": "^6.1.2","react": "^17.0.1","react-dom": "^17.0.1",+ "strip-ansi": "^3.0.1","three": "^0.128.0"}}diff --git a/packages/create-video/src/init-v2.ts b/packages/create-video/src/init-v2.tsindex fa6aa1e74c..c1a723370d 100644--- a/packages/create-video/src/init-v2.ts+++ b/packages/create-video/src/init-v2.ts@@ -2,9 +2,10 @@ import chalk from 'chalk';import execa from 'execa';import fs from 'fs-extra';import path from 'path';-import prompts from 'prompts';+import stripAnsi from 'strip-ansi';import * as CreateDirectory from './create-directory';import {Log} from './log';+import prompts, {selectAsync} from './prompts';const FEATURED_TEMPLATES = [{@@ -19,6 +20,12 @@ const FEATURED_TEMPLATES = [},];+function padEnd(str: string, width: number): string {+ // Pulled from commander for overriding+ const len = Math.max(0, width - stripAnsi(str).length);+ return str + Array(len + 1).join(' ');+}+function assertValidName(folderName: string) {const validation = CreateDirectory.validateName(folderName);if (typeof validation === 'string') {@@ -135,21 +142,60 @@ const resolveProjectRootAsync = async () => {return projectRoot;};+const isNodeVersionGreater = () => {+ return process.versions.node >= '16.0.0';+};+export const init = async () => {// let projectName = process.argv[2];// console.log(projectName, 'new');const projectRoot = await resolveProjectRootAsync();+ const greaterNodeVersion = isNodeVersionGreater();++ const descriptionColumn =+ Math.max(+ ...FEATURED_TEMPLATES.map((t) =>+ typeof t === 'object' ? t.shortName.length : 0+ )+ ) + 2;++ const template = await selectAsync(+ {+ message: 'Choose a template:',+ optionsPerPage: 20,+ choices: FEATURED_TEMPLATES.map((template) => {+ if (typeof template === 'string') {+ return prompts.separator(template);+ } else {+ return {+ value: template.name,+ title:+ chalk.bold(padEnd(template.shortName, descriptionColumn)) ++ template.description.trim(),+ short: template.name,+ };+ }+ }),+ },+ {}+ );+await execa('git', ['clone',- `https://github.com/remotion-dev/${FEATURED_TEMPLATES[0].name}`,+ `https://github.com/remotion-dev/${template}`,projectRoot,]);- await execa('rm', ['-r', path.join(projectRoot, '.git')]);++ if (greaterNodeVersion) {+ fs.rmSync(path.join(projectRoot, '.git'), {recursive: true});+ } else {+ fs.rmdirSync(path.join(projectRoot, '.git'), {recursive: true});+ }const isGitInitialized = await initGitRepoAsync(projectRoot, {silent: true,commit: true,});- console.log(projectRoot, isGitInitialized, 'nice');+ Log.info('Template downloaded and initialized');};diff --git a/packages/create-video/src/prompts.ts b/packages/create-video/src/prompts.tsnew file mode 100644index 0000000000..7138b65735--- /dev/null+++ b/packages/create-video/src/prompts.ts@@ -0,0 +1,117 @@+import prompts, {Options, PromptObject} from 'prompts';++export type Question<V extends string = string> = PromptObject<V> & {+ optionsPerPage?: number;+};+export type NamelessQuestion = Omit<Question<'value'>, 'name' | 'type'>;+type PromptOptions = {nonInteractiveHelp?: string} & Options;++export default function prompt(+ questions: Question | Question[],+ {nonInteractiveHelp, ...options}: PromptOptions = {}+) {+ questions = Array.isArray(questions) ? questions : [questions];+ return prompts(questions, {+ onCancel() {+ throw new Error();+ },+ ...options,+ });+}++prompt.separator = (title: string) => ({+ title,+ disabled: true,+ value: undefined,+});++export async function selectAsync(+ questions: NamelessQuestion,+ options?: PromptOptions+): Promise<any> {+ const {value} = await prompt(+ {+ limit: 11,+ ...questions,+ // @ts-ignore: onRender not in the types+ onRender(this: {+ cursor: number;+ firstRender: boolean;+ choices: (Omit<prompts.Choice, 'disable'> & {disabled?: boolean})[];+ value: string;+ render: () => void;+ moveCursor: (n: number) => void;+ fire: () => void;+ up: () => void;+ down: () => void;+ bell: () => void;+ next: () => void;+ }) {+ if (this.firstRender) {+ // Ensure the initial state isn't on a disabled item.+ while (this.choices[this.cursor].disabled) {+ this.cursor++;+ if (this.cursor > this.choices.length - 1) break;+ }+ this.fire();+ // Without this, the value will be `0` instead of a string.+ this.value = (this.choices[this.cursor] || {}).value;++ // Support up arrow and `k` key -- no looping+ this.up = () => {+ let next = this.cursor;+ while (true) {+ if (next <= 0) break;+ next--;+ if (!this.choices[next].disabled) break;+ }+ if (!this.choices[next].disabled && next !== this.cursor) {+ this.moveCursor(next);+ this.render();+ } else {+ this.bell();+ }+ };++ // Support down arrow and `j` key -- no looping+ this.down = () => {+ let next = this.cursor;+ while (true) {+ if (next >= this.choices.length - 1) break;+ next++;+ if (!this.choices[next].disabled) break;+ }+ if (!this.choices[next].disabled && next !== this.cursor) {+ this.moveCursor(next);+ this.render();+ } else {+ this.bell();+ }+ };++ // Support tab -- looping+ this.next = () => {+ let next = this.cursor;+ let i = 0;+ while (i < this.choices.length) {+ i++;+ next = (next + 1) % this.choices.length;+ if (!this.choices[next].disabled) break;+ }+ if (!this.choices[next].disabled) {+ this.moveCursor(next);+ this.render();+ } else {+ // unexpected+ this.bell();+ }+ };+ }+ },+ name: 'value',+ type: 'select',+ },+ options+ );+ return value ?? null;+}