Skip to content

Visualize Your Ideas With Code

feat(create): add exporter selection (#673)

Jacob82fd47d93ffa
diff --git a/packages/create/index.js b/packages/create/index.js
index 4e396871..375150dd 100755
--- a/packages/create/index.js
+++ b/packages/create/index.js
@@ -19,8 +19,45 @@ const MANIFEST = JSON.parse(
),
);
+const PLUGINS = {
+ core: {
+ package: '@motion-canvas/vite-plugin',
+ variable: 'motionCanvas',
+ options: response =>
+ response.language === 'js' ? `{project: './src/project.js'}` : '',
+ },
+ ffmpeg: {
+ package: '@motion-canvas/ffmpeg',
+ variable: 'ffmpeg',
+ version: '^1.1.0',
+ },
+};
+
(async () => {
- prompts.override(minimist(process.argv.slice(2)));
+ const options = minimist(process.argv.slice(2));
+ if (options.plugins !== undefined) {
+ if (typeof options.plugins === 'string') {
+ options.plugins = options.plugins.split(',');
+ }
+
+ if (!Array.isArray(options.plugins)) {
+ options.plugins = [options.plugins];
+ }
+
+ const plugins = ['core'];
+ for (const plugin of options.plugins) {
+ if (plugin === 'core') continue;
+ if (!(plugin in PLUGINS)) {
+ console.log(kleur.yellow(`! Unknown plugin "${plugin}".\n`));
+ continue;
+ }
+ plugins.push(plugin);
+ }
+
+ options.plugins = plugins;
+ }
+
+ prompts.override(options);
const response = await prompts([
{
type: 'text',
@@ -73,9 +110,42 @@ const MANIFEST = JSON.parse(
},
],
},
+ {
+ type: 'multiselect',
+ name: 'plugins',
+ message: 'How would you like to render your animation?',
+ choices: [
+ {
+ title: 'Image sequence',
+ value: 'core',
+ disabled: true,
+ selected: true,
+ },
+ {
+ title: 'Video (FFmpeg)',
+ value: 'ffmpeg',
+ selected: true,
+ },
+ ],
+ warn: 'This option is always included.',
+ onRender() {
+ this.value[0].selected = true;
+ },
+ validate: value => {
+ if (!Array.isArray(value)) {
+ return '"plugins" option must be an array.';
+ }
+
+ if (!value.includes('core')) {
+ return 'Image sequence is required.';
+ }
+
+ return true;
+ },
+ },
]);
- if (!response.language) {
+ if (!response.plugins) {
console.log(kleur.red('× Scaffolding aborted by the user.\n'));
return;
}
@@ -86,14 +156,21 @@ const MANIFEST = JSON.parse(
`template-2d-${response.language}`,
);
copyDirectory(templateDir, response.path);
+ createConfig(response);
const manifest = JSON.parse(
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8'),
);
manifest.name = response.name;
- if (manifest.dependencies) {
- cloneVersions(manifest.dependencies);
+ manifest.dependencies ??= {};
+ for (const plugin of response.plugins) {
+ const data = PLUGINS[plugin];
+ if (data.version) {
+ manifest.dependencies[data.package] = data.version;
+ }
}
+ cloneVersions(manifest.dependencies);
+
if (manifest.devDependencies) {
cloneVersions(manifest.devDependencies);
}
@@ -149,6 +226,33 @@ function copy(src, dest) {
}
}
+function createConfig(response) {
+ const imports = [];
+ const plugins = [];
+ for (const plugin of response.plugins) {
+ const data = PLUGINS[plugin];
+ imports.push(`import ${data.variable} from '${data.package}';\n`);
+ plugins.push(`${data.variable}(${data.options?.(response) ?? ''}),`);
+ }
+
+ const configFile = path.resolve(
+ response.path,
+ `vite.config.${response.language}`,
+ );
+
+ fs.writeFileSync(
+ configFile,
+ `import {defineConfig} from 'vite';
+${imports.join('')}
+export default defineConfig({
+ plugins: [
+ ${plugins.join('\n ')}
+ ],
+});
+`,
+ );
+}
+
function getPackageManager() {
const ua = process.env.npm_config_user_agent;
return ua?.split(' ')[0].split('/')[0] ?? 'npm';
diff --git a/packages/create/template-2d-js/vite.config.js b/packages/create/template-2d-js/vite.config.js
deleted file mode 100644
index d3c6905c..00000000
--- a/packages/create/template-2d-js/vite.config.js
+++ /dev/null
@@ -1,10 +0,0 @@
-import {defineConfig} from 'vite';
-import motionCanvas from '@motion-canvas/vite-plugin';
-
-export default defineConfig({
- plugins: [
- motionCanvas({
- project: './src/project.js',
- }),
- ],
-});
diff --git a/packages/create/template-2d-ts/vite.config.ts b/packages/create/template-2d-ts/vite.config.ts
deleted file mode 100644
index bf320881..00000000
--- a/packages/create/template-2d-ts/vite.config.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import {defineConfig} from 'vite';
-import motionCanvas from '@motion-canvas/vite-plugin';
-
-export default defineConfig({
- plugins: [motionCanvas()],
-});