Skip to content

Commit

Permalink
chore: examples are submodule (redotvideo#113)
Browse files Browse the repository at this point in the history
* chore: add examples as submodule

* chore: bump examples

* chore: point index to submodule

* chore: simplify index.js and add other examples

* fix: add submodule to github action

* test: see if this works

* test: reenable publish

* fix: publishing

* fix: some examples don't have a package.json
  • Loading branch information
hkonsti authored May 22, 2024
1 parent f14feb1 commit a5f4626
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 226 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
with:
fetch-depth: 0
token: ${{secrets.PUBLISH_TOKEN}}
submodules: true
- uses: actions/setup-node@v3
with:
node-version: 18
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "packages/create/examples"]
path = packages/create/examples
url = https://github.com/redotvideo/examples
1 change: 1 addition & 0 deletions packages/create/examples
Submodule examples added at ca2656
147 changes: 49 additions & 98 deletions packages/create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,21 @@ import {fileURLToPath} from 'node:url';
import path from 'path';
import prompts from 'prompts';

const FILES_TO_MODIFY = {
gitignore: '.gitignore',
'.gitkeep': false,
};

const MANIFEST = JSON.parse(
fs.readFileSync(
path.resolve(fileURLToPath(import.meta.url), '../package.json'),
'utf-8',
),
);

const PLUGINS = {
core: {
package: '@revideo/vite-plugin',
variable: 'motionCanvas',
options: response =>
response.language === 'js' ? `{project: './src/project.js'}` : '',
},
};

(async () => {
const templates = [
'default',
'avatar-with-background',
'google-cloud-run',
'google-cloud-run-parallelized',
'stitching-videos',
'youtube-shorts',
];

async function run() {
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([
// Prompt for project name
{
type: 'text',
name: 'name',
Expand All @@ -64,6 +32,7 @@ const PLUGINS = {
? true
: 'Project name must be a valid npm package name.',
},
// Prompt for project path
{
type: 'text',
name: 'path',
Expand All @@ -90,42 +59,48 @@ const PLUGINS = {
},
format: value => path.resolve(value),
},
// Prompt for which example to scaffold
{
type: 'select',
name: 'starter',
message: 'Choose a starter template',

choices: [
...templates.map(template => ({title: template, value: template})),
],
},
]);

// Abort if the user didn't provide a project name
if (!response.path) {
console.log(kleur.red('× Scaffolding aborted by the user.\n'));
return;
}

const plugins = [PLUGINS.core];
// Clone files
const templateDir = path.resolve(
fileURLToPath(import.meta.url),
'..',
`template-2d-ts`,
`examples/${response.starter}`,
);
copyDirectory(templateDir, response.path);
createConfig(response, plugins);
createConfig(response);

const manifest = JSON.parse(
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8'),
);
manifest.name = response.name;
manifest.dependencies ??= {};
for (const data of plugins) {
if (data.version) {
manifest.dependencies[data.package] = data.version;
}
}
cloneVersions(manifest.dependencies);

if (manifest.devDependencies) {
cloneVersions(manifest.devDependencies);
// Read package.json and modify name
try {
const manifest = JSON.parse(
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8'),
);
manifest.name = response.name;
fs.writeFileSync(
path.join(response.path, 'package.json'),
JSON.stringify(manifest, undefined, 2),
);
} catch (e) {
// Example doesn't have a package.json file
}
fs.writeFileSync(
path.join(response.path, 'package.json'),
JSON.stringify(manifest, undefined, 2),
);

// Tell user that the process is complete
const manager = getPackageManager();
console.log(kleur.green('\n√ Scaffolding complete. You can now run:'));
if (response.path !== process.cwd()) {
Expand All @@ -142,7 +117,7 @@ const PLUGINS = {
console.log(` ${boldManager} start`);
}
console.log();
})();
}

function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
Expand All @@ -153,13 +128,8 @@ function isValidPackageName(projectName) {
function copyDirectory(src, dest) {
fs.mkdirSync(dest, {recursive: true});
for (const file of fs.readdirSync(src)) {
let target = file;
if (file in FILES_TO_MODIFY) {
if (FILES_TO_MODIFY[file] === false) continue;
target = FILES_TO_MODIFY[file];
}
const srcFile = path.resolve(src, file);
const destFile = path.resolve(dest, target);
const destFile = path.resolve(dest, file);
copy(srcFile, destFile);
}
}
Expand All @@ -173,27 +143,17 @@ function copy(src, dest) {
}
}

function createConfig(response, selectedPlugins) {
const imports = [];
const plugins = [];
for (const data of selectedPlugins) {
imports.push(`import ${data.variable} from '${data.package}';\n`);
plugins.push(`${data.variable}(${data.options?.(response) ?? ''}),`);
}

function createConfig(response) {
const configFile = path.resolve(response.path, `vite.config.ts`);

fs.writeFileSync(
configFile,
`import {defineConfig} from 'vite';
${imports.join('')}
export default defineConfig({
plugins: [
${plugins.join('\n ')}
],
});
`,
`import motionCanvas from '@revideo/vite-plugin';
import {defineConfig} from 'vite';
export default defineConfig({
plugins: [motionCanvas()],
});`,
);
}

Expand All @@ -202,13 +162,4 @@ function getPackageManager() {
return ua?.split(' ')[0].split('/')[0] ?? 'npm';
}

function cloneVersions(versions) {
for (const dependency in versions) {
if (
dependency.startsWith('@revideo') &&
MANIFEST.devDependencies[dependency]
) {
versions[dependency] = MANIFEST.devDependencies[dependency];
}
}
}
void run();
2 changes: 1 addition & 1 deletion packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"files": [
"index.js",
"template-*"
"examples"
],
"repository": {
"type": "git",
Expand Down
24 changes: 0 additions & 24 deletions packages/create/template-2d-ts/package.json

This file was deleted.

Empty file.
31 changes: 0 additions & 31 deletions packages/create/template-2d-ts/src/project.meta

This file was deleted.

7 changes: 0 additions & 7 deletions packages/create/template-2d-ts/src/project.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/create/template-2d-ts/src/render.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/create/template-2d-ts/src/revideo.d.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/create/template-2d-ts/src/scenes/example.tsx

This file was deleted.

9 changes: 0 additions & 9 deletions packages/create/template-2d-ts/tsconfig.json

This file was deleted.

0 comments on commit a5f4626

Please sign in to comment.