Skip to content

🎥 Make videos programmatically with React

add more validation

Shankhadeep Deyb005ee071ad1
diff --git a/packages/create-video/package.json b/packages/create-video/package.json
index 0473155362..7784eb9fd9 100644
--- a/packages/create-video/package.json
+++ b/packages/create-video/package.json
@@ -19,12 +19,14 @@
"dependencies": {
"chalk": "^4.1.0",
"execa": "^5.0.0",
+ "fs-extra": "^9.1.0",
"npm-package-arg": "^8.1.2",
"prompts": "^2.4.1",
"xns": "^2.0.7"
},
"devDependencies": {
"@jonny/eslint-config": "^2.1.233",
+ "@types/fs-extra": "^9.0.11",
"@types/node": "^15.0.1",
"@types/npm-package-arg": "^6.1.0",
"@types/prompts": "^2.0.12",
diff --git a/packages/create-video/src/create-directory.ts b/packages/create-video/src/create-directory.ts
index ea9b6ae7bd..c52bfbc366 100644
--- a/packages/create-video/src/create-directory.ts
+++ b/packages/create-video/src/create-directory.ts
@@ -1,3 +1,27 @@
+import fs from 'fs-extra';
+import * as path from 'path';
+import {Log} from './log';
+
+const TOLERABLE_FILES = [
+ // System
+ '.DS_Store',
+ 'Thumbs.db',
+ // Git
+ '.git',
+ '.gitattributes',
+ '.gitignore',
+ // Project
+ '.npmignore',
+ '.travis.yml',
+ 'LICENSE',
+ 'docs',
+ '.idea',
+ // Package manager
+ 'npm-debug.log',
+ 'yarn-debug.log',
+ 'yarn-error.log',
+];
+
export function validateName(name?: string): string | true {
if (typeof name !== 'string' || name === '') {
return 'The project name can not be empty.';
@@ -7,3 +31,49 @@ export function validateName(name?: string): string | true {
}
return true;
}
+
+export function getConflictsForDirectory(
+ projectRoot: string,
+ tolerableFiles: string[] = TOLERABLE_FILES
+): string[] {
+ return fs
+ .readdirSync(projectRoot)
+ .filter(
+ (file: string) => !(/\.iml$/.test(file) || tolerableFiles.includes(file))
+ );
+}
+
+export async function assertFolderEmptyAsync({
+ projectRoot,
+ folderName = path.dirname(projectRoot),
+ overwrite,
+}: {
+ projectRoot: string;
+ folderName?: string;
+ overwrite: boolean;
+}): Promise<boolean> {
+ const conflicts = getConflictsForDirectory(projectRoot);
+ if (conflicts.length) {
+ Log.info(
+ `The directory ${Log.chalk.green(
+ folderName
+ )} has files that might be overwritten:`
+ );
+ Log.newLine();
+ for (const file of conflicts) {
+ Log.info(` ${file}`);
+ }
+
+ if (overwrite) {
+ Log.newLine();
+ Log.info(`Removing existing files from ${Log.chalk.green(folderName)}`);
+ await Promise.all(
+ conflicts.map((conflict) => fs.remove(path.join(projectRoot, conflict)))
+ );
+ return true;
+ }
+
+ return false;
+ }
+ return true;
+}
diff --git a/packages/create-video/src/init-v2.ts b/packages/create-video/src/init-v2.ts
index 11cd59f731..ee3d04f092 100644
--- a/packages/create-video/src/init-v2.ts
+++ b/packages/create-video/src/init-v2.ts
@@ -1,7 +1,39 @@
import chalk from 'chalk';
+import fs from 'fs-extra';
import path from 'path';
import prompts from 'prompts';
-import {validateName} from './create-directory';
+import * as CreateDirectory from './create-directory';
+import {Log} from './log';
+
+function assertValidName(folderName: string) {
+ const validation = CreateDirectory.validateName(folderName);
+ if (typeof validation === 'string') {
+ throw new Error(
+ `Cannot create an app named ${chalk.red(
+ `"${folderName}"`
+ )}. ${validation}`
+ );
+ }
+}
+
+async function assertFolderEmptyAsync(
+ projectRoot: string,
+ folderName?: string
+) {
+ if (
+ !(await CreateDirectory.assertFolderEmptyAsync({
+ projectRoot,
+ folderName,
+ overwrite: false,
+ }))
+ ) {
+ const message = 'Try using a new directory name, or moving these files.';
+ Log.newLine();
+ Log.info(message);
+ Log.newLine();
+ throw new Error(message);
+ }
+}
export const init = async () => {
let projectName = process.argv[2];
@@ -15,7 +47,9 @@ export const init = async () => {
message: 'What would you like to name your app?',
initial: 'my-app',
validate: (name) => {
- const validation = validateName(path.basename(path.resolve(name)));
+ const validation = CreateDirectory.validateName(
+ path.basename(path.resolve(name))
+ );
if (typeof validation === 'string') {
return 'Invalid project name: ' + validation;
}
@@ -28,18 +62,18 @@ export const init = async () => {
}
} catch (error) {
// Handle the aborted message in a custom way.
- console.log('nice');
if (error.code !== 'ABORTED') {
throw error;
}
}
- const validation = validateName(projectName);
- if (typeof validation === 'string') {
- throw new Error(
- `Cannot create an app named ${chalk.red(
- `"${projectName}"`
- )}. ${validation}`
- );
- }
+
+ const projectRoot = path.resolve(projectName);
+ const folderName = path.basename(projectRoot);
+
+ assertValidName(folderName);
+
+ await fs.ensureDir(projectRoot);
+
+ await assertFolderEmptyAsync(projectRoot, folderName);
}
};
diff --git a/packages/create-video/src/log.ts b/packages/create-video/src/log.ts
new file mode 100644
index 0000000000..d8f0f325f5
--- /dev/null
+++ b/packages/create-video/src/log.ts
@@ -0,0 +1,22 @@
+/* eslint-disable no-console */
+
+import chalk from 'chalk';
+
+export const Log = {
+ chalk: chalk,
+ verbose: (...args: Parameters<typeof console.log>) => {
+ return console.log(chalk.blueBright(...args));
+ },
+ info: (...args: Parameters<typeof console.log>) => {
+ return console.log(...args);
+ },
+ warn: (...args: Parameters<typeof console.log>) => {
+ return console.warn(chalk.yellow(...args));
+ },
+ error: (...args: Parameters<typeof console.log>) => {
+ return console.error(chalk.red(...args));
+ },
+ newLine: () => {
+ return console.log();
+ },
+};