@twick/cli 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 motion-canvas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/editor.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.launchEditor = launchEditor;
7
+ const vite_plugin_1 = __importDefault(require("@twick/vite-plugin"));
8
+ const vite_1 = require("vite");
9
+ async function launchEditor(projectPath, port) {
10
+ const server = await (0, vite_1.createServer)({
11
+ configFile: false,
12
+ plugins: [
13
+ (0, vite_plugin_1.default)({
14
+ project: projectPath,
15
+ buildForEditor: false,
16
+ }),
17
+ ],
18
+ build: {
19
+ minify: false,
20
+ rollupOptions: {
21
+ output: {
22
+ entryFileNames: '[name].js',
23
+ },
24
+ },
25
+ },
26
+ server: {
27
+ port: parseInt(port),
28
+ },
29
+ });
30
+ return server.listen();
31
+ }
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const telemetry_1 = require("@twick/telemetry");
5
+ const commander_1 = require("commander");
6
+ const editor_1 = require("./editor");
7
+ const index_1 = require("./server/index");
8
+ const program = new commander_1.Command();
9
+ const VERSION = '0.10.4';
10
+ program
11
+ .name('twick')
12
+ .description('CLI to interact with the twick service')
13
+ .version(VERSION);
14
+ program
15
+ .command('serve')
16
+ .description('Exposes a render endpoint to render videos from a project file. Automatically rebuilds the project when the project file changes. Use for local development.')
17
+ .option('--projectFile <path>', 'Path to the project file', './src/project.ts')
18
+ .option('--port <number>', 'Port on which to start the server', '4000')
19
+ .action(async (options) => {
20
+ (0, telemetry_1.sendEvent)(telemetry_1.EventName.CLICommand);
21
+ const { projectFile, port } = options;
22
+ process.env.PROJECT_FILE = projectFile;
23
+ process.env.REVIDEO_PORT = port;
24
+ (0, index_1.createServer)().listen(port, () => {
25
+ console.log(`Server listening on port ${port}`);
26
+ console.log();
27
+ });
28
+ });
29
+ program
30
+ .command('editor')
31
+ .description('Start the twick editor')
32
+ .option('--projectFile <path>', 'Path to the project file', './src/project.ts')
33
+ .option('--port <number>', 'Port on which to start the server', '9000')
34
+ .action(async (options) => {
35
+ const editor = await (0, editor_1.launchEditor)(options.projectFile, options.port);
36
+ console.log(`Editor running on port ${editor.config.server.port}`);
37
+ });
38
+ program.parse(process.argv);
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.download = download;
7
+ const fs_1 = require("fs");
8
+ const path_1 = __importDefault(require("path"));
9
+ async function download(req, res) {
10
+ const { projectName } = req.params;
11
+ const resultFilePath = path_1.default.join(process.cwd(), `output/${projectName}`);
12
+ try {
13
+ await fs_1.promises.access(resultFilePath);
14
+ res.download(resultFilePath, `${projectName}`, async (err) => {
15
+ if (err) {
16
+ console.error(err);
17
+ res.status(500).send('Error downloading the file.');
18
+ }
19
+ });
20
+ }
21
+ catch (error) {
22
+ console.error(`File not found ${resultFilePath}:`, error);
23
+ res.status(404).send('File not found.');
24
+ }
25
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createServer = createServer;
7
+ const cors_1 = __importDefault(require("cors"));
8
+ const express_1 = __importDefault(require("express"));
9
+ const download_1 = require("./download");
10
+ const render_1 = require("./render");
11
+ function createServer() {
12
+ const app = (0, express_1.default)();
13
+ app.use(express_1.default.json({ limit: '50mb' }));
14
+ app.use((0, cors_1.default)());
15
+ app.post('/render', render_1.render);
16
+ app.get('/download/:projectName', download_1.download);
17
+ return app;
18
+ }
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.render = render;
7
+ const renderer_1 = require("@twick/renderer");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const uuid_1 = require("uuid");
11
+ const utils_1 = require("../utils");
12
+ async function render(req, res) {
13
+ const { callbackUrl } = req.body;
14
+ if (callbackUrl) {
15
+ await renderWithCallback(req, res);
16
+ }
17
+ else {
18
+ await renderWithoutCallback(req, res);
19
+ }
20
+ }
21
+ async function renderWithCallback(req, res) {
22
+ // TODO: validate request body
23
+ const { variables, callbackUrl, settings } = req.body;
24
+ const tempProjectName = (0, uuid_1.v4)();
25
+ const outputFileName = `${tempProjectName}.mp4`;
26
+ res.json({ tempProjectName });
27
+ try {
28
+ await (0, renderer_1.renderVideo)({
29
+ projectFile: process.env.PROJECT_FILE || '',
30
+ variables,
31
+ settings: {
32
+ ...settings,
33
+ outFile: outputFileName,
34
+ },
35
+ });
36
+ const resultFilePath = path_1.default.join(process.cwd(), `output/${outputFileName}`);
37
+ const downloadLink = `${req.protocol}://${req.get('host')}/download/${outputFileName}`;
38
+ const response = await axios_1.default.post(callbackUrl, {
39
+ tempProjectName,
40
+ status: 'success',
41
+ downloadLink,
42
+ }, {
43
+ headers: {
44
+ // eslint-disable-next-line
45
+ 'Content-Type': 'application/json',
46
+ },
47
+ });
48
+ if (response.status !== 200) {
49
+ throw new Error('Callback URL responded with an error');
50
+ }
51
+ (0, utils_1.scheduleCleanup)(resultFilePath);
52
+ }
53
+ catch (error) {
54
+ console.error(error);
55
+ await axios_1.default.post(callbackUrl, {
56
+ tempProjectName,
57
+ status: 'error',
58
+ error: error.message,
59
+ }, {
60
+ headers: {
61
+ // eslint-disable-next-line
62
+ 'Content-Type': 'application/json',
63
+ },
64
+ });
65
+ }
66
+ }
67
+ async function renderWithoutCallback(req, res) {
68
+ // TODO: validate request body
69
+ const { variables, streamProgress, settings } = req.body;
70
+ const tempProjectName = `${(0, uuid_1.v4)()}.mp4`;
71
+ const resultFilePath = path_1.default.join(process.cwd(), `output/${tempProjectName}`);
72
+ if (streamProgress) {
73
+ res.writeHead(200, {
74
+ // eslint-disable-next-line
75
+ 'Content-Type': 'text/event-stream',
76
+ // eslint-disable-next-line
77
+ 'Cache-Control': 'no-cache',
78
+ // eslint-disable-next-line
79
+ Connection: 'keep-alive',
80
+ });
81
+ const sendProgress = (worker, progress) => {
82
+ res.write(`event: progress\n`);
83
+ res.write(`data: ${JSON.stringify({ worker, progress })}\n\n`);
84
+ };
85
+ try {
86
+ await (0, renderer_1.renderVideo)({
87
+ projectFile: process.env.PROJECT_FILE || '',
88
+ variables,
89
+ settings: {
90
+ ...settings,
91
+ outFile: tempProjectName,
92
+ progressCallback: sendProgress,
93
+ },
94
+ });
95
+ const downloadLink = `${req.protocol}://${req.get('host')}/download/${tempProjectName}`;
96
+ res.write(`event: completed\n`);
97
+ res.write(`data: ${JSON.stringify({ status: 'success', downloadLink })}\n\n`);
98
+ res.end();
99
+ (0, utils_1.scheduleCleanup)(resultFilePath);
100
+ }
101
+ catch (error) {
102
+ console.error(error);
103
+ res.write(`event: error\n`);
104
+ res.write(`data: ${JSON.stringify({ status: 'error', message: error.message })}\n\n`);
105
+ res.end();
106
+ }
107
+ }
108
+ else {
109
+ try {
110
+ await (0, renderer_1.renderVideo)({
111
+ projectFile: process.env.PROJECT_FILE || '',
112
+ variables,
113
+ settings: {
114
+ ...settings,
115
+ outFile: tempProjectName,
116
+ },
117
+ });
118
+ const downloadLink = `${req.protocol}://${req.get('host')}/download/${tempProjectName}`;
119
+ res.json({
120
+ status: 'success',
121
+ downloadLink: downloadLink,
122
+ });
123
+ (0, utils_1.scheduleCleanup)(resultFilePath);
124
+ }
125
+ catch (error) {
126
+ console.error(error);
127
+ res.status(500).json({
128
+ status: 'error',
129
+ message: error.message,
130
+ });
131
+ }
132
+ }
133
+ }
package/dist/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scheduleCleanup = scheduleCleanup;
4
+ const fs_1 = require("fs");
5
+ function scheduleCleanup(filePath) {
6
+ const clean = async () => {
7
+ try {
8
+ await fs_1.promises.unlink(filePath);
9
+ console.log(`Successfully deleted file: ${filePath}`);
10
+ }
11
+ catch (error) {
12
+ console.error(`Error deleting file ${filePath}: ${error}`);
13
+ }
14
+ };
15
+ // Wait 10 minutes before removing file.
16
+ setTimeout(clean, 10 * 60 * 1000);
17
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@twick/cli",
3
+ "version": "0.11.0",
4
+ "description": "A CLI for twick",
5
+ "main": "dist/index.js",
6
+ "author": "twick",
7
+ "homepage": "https://re.video/",
8
+ "bugs": "https://github.com/ncounterspecialist/twick-base/issues",
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "start": "node dist/index.js",
13
+ "dev": "ts-node-dev --respawn --transpile-only src/index.ts"
14
+ },
15
+ "bin": {
16
+ "twick": "dist/index.js"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "types"
21
+ ],
22
+ "devDependencies": {
23
+ "@types/cors": "^2.8.17",
24
+ "@types/express": "^4.17.21",
25
+ "typescript": "^5.4.3"
26
+ },
27
+ "dependencies": {
28
+ "@twick/renderer": "^0.11.0",
29
+ "@twick/telemetry": "^0.11.0",
30
+ "@twick/vite-plugin": "^0.11.0",
31
+ "chokidar": "^3.5.3",
32
+ "commander": "^12.0.0",
33
+ "cors": "^2.8.5",
34
+ "express": "^4.19.2",
35
+ "uuid": "^9.0.1"
36
+ },
37
+ "gitHead": "59f38f4e7d3a9a30943bbad830dc0201eaa57ce7"
38
+ }