|
| 1 | +#!/usr/bin/env node |
| 2 | +import * as clack from '@clack/prompts'; |
| 3 | +import fs from 'fs'; |
| 4 | +import { execSync } from 'child_process'; |
| 5 | +import path from 'path'; |
| 6 | +import { fileURLToPath } from 'url'; |
| 7 | +import waitOn from 'wait-on'; |
| 8 | + |
| 9 | +// @ts-ignore |
| 10 | +import packageJson from '../package.json'; |
| 11 | + |
| 12 | +// @ts-ignore |
| 13 | +const __filename = fileURLToPath(import.meta.url); |
| 14 | +const __dirname = path.dirname(__filename); |
| 15 | +const packageRoot = path.join(__dirname, '..'); |
| 16 | + |
| 17 | +async function main() { |
| 18 | + clack.intro('Welcome Restack Get started'!); |
| 19 | + |
| 20 | + const currentDir = process.cwd(); |
| 21 | + let targetDir: string; |
| 22 | + |
| 23 | + // Copy files |
| 24 | + const projectName = (await clack.text({ |
| 25 | + message: 'Enter the project folder name:', |
| 26 | + defaultValue: 'restack-get-started', |
| 27 | + initialValue: 'restack-get-started', |
| 28 | + validate(value) { |
| 29 | + if (value.length === 0) return `⚠️ Project folder name is required`; |
| 30 | + } |
| 31 | + })) as string; |
| 32 | + |
| 33 | + if (projectName) { |
| 34 | + targetDir = path.join(currentDir, projectName); |
| 35 | + const filesToCopy = ['src', 'scheduleWorkflow.ts', 'tsconfig.json', '.gitignore']; |
| 36 | + filesToCopy.forEach(file => { |
| 37 | + fs.cpSync(path.join(packageRoot, file), path.join(targetDir, file), { recursive: true }); |
| 38 | + }); |
| 39 | + |
| 40 | + // Copy package.json separately and modify it |
| 41 | + delete packageJson.bin; |
| 42 | + delete packageJson.files; |
| 43 | + fs.writeFileSync(path.join(targetDir, 'package.json'), JSON.stringify(packageJson, null, 2)); |
| 44 | + } |
| 45 | + |
| 46 | + const installDependencies = (await clack.confirm({ |
| 47 | + message: 'Install dependencies?', |
| 48 | + initialValue: true, |
| 49 | + })) as boolean; |
| 50 | + |
| 51 | + if (installDependencies) { |
| 52 | + console.log('Installing dependencies...'); |
| 53 | + execSync('npm install', { stdio: 'inherit', cwd: targetDir }); |
| 54 | + } |
| 55 | + |
| 56 | + const startRestack = (await clack.confirm({ |
| 57 | + message: 'Start Restack Engine?', |
| 58 | + initialValue: true, |
| 59 | + })) as boolean; |
| 60 | + |
| 61 | + if (startRestack) { |
| 62 | + console.log('Starting Restack Engine...'); |
| 63 | + execSync('docker rm -f studio', { stdio: 'inherit', cwd: targetDir }); |
| 64 | + execSync('docker run -d --pull always --name studio -p 5233:5233 -p 6233:6233 -p 7233:7233 ghcr.io/restackio/engine:main', { stdio: 'inherit', cwd: currentDir }); |
| 65 | + console.log(`Restack Engine Studio started on http://localhost:5233`); |
| 66 | + } |
| 67 | + |
| 68 | + const openRestack = (await clack.confirm({ |
| 69 | + message: 'Open Restack Engine Studio?', |
| 70 | + initialValue: true, |
| 71 | + })) as boolean; |
| 72 | + |
| 73 | + if (openRestack) { |
| 74 | + const s = clack.spinner(); |
| 75 | + s.start('Waiting for Restack Engine Studio to start'); |
| 76 | + await waitOn({ resources: ['http://localhost:5233'] }); |
| 77 | + s.stop(); |
| 78 | + execSync('open http://localhost:5233', { stdio: 'inherit', cwd: targetDir }); |
| 79 | + } |
| 80 | + |
| 81 | + const blue = '\x1b[34m'; |
| 82 | + const noColor = '\x1b[0m'; |
| 83 | + |
| 84 | + clack.outro(` |
| 85 | +Project created successfully! |
| 86 | +
|
| 87 | +We suggest that you begin with following commands: |
| 88 | +
|
| 89 | +To navigate to the project, run: ${blue}cd ${projectName}${noColor} |
| 90 | +
|
| 91 | +To start the service, run: ${blue}npm run service${noColor} |
| 92 | +
|
| 93 | +To schedule a workflow, run: ${blue}npm run schedule${noColor} |
| 94 | +`); |
| 95 | +} |
| 96 | + |
| 97 | +main(); |
0 commit comments