|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as os from 'node:os'; |
| 4 | +import * as path from 'node:path'; |
| 5 | + |
| 6 | +import { ensureDir } from 'fs-extra'; |
| 7 | +import { ListrTaskWrapper } from 'listr2'; |
| 8 | + |
| 9 | +import { MONDAY_GITHUB_REPO, MONDAY_GITHUB_REPO_BRANCH, MONDAY_GITHUB_REPO_URL } from 'consts/scaffold'; |
| 10 | +import { cloneFolderFromGitRepo } from 'services/git-service'; |
| 11 | +import { ScaffoldTaskContext } from 'types/commands/scaffold'; |
| 12 | +import logger from 'utils/logger'; |
| 13 | + |
| 14 | +const DEBUG_TAG = 'scaffold_service'; |
| 15 | +const isWindows = () => process.platform === 'win32'; |
| 16 | +const npmCmd = isWindows() ? 'npm.cmd' : 'npm'; |
| 17 | + |
| 18 | +export const downloadTemplateTask = async ( |
| 19 | + ctx: ScaffoldTaskContext, |
| 20 | + task: ListrTaskWrapper<ScaffoldTaskContext, any>, |
| 21 | +) => { |
| 22 | + const output = (data: string) => { |
| 23 | + task.output = data; |
| 24 | + }; |
| 25 | + |
| 26 | + task.title = 'Downloading template from GitHub'; |
| 27 | + const gitRepoUrl = `https://github.com/${MONDAY_GITHUB_REPO}`; |
| 28 | + const folderPath = `apps/${ctx.project.name}`; |
| 29 | + |
| 30 | + await cloneFolderFromGitRepo(gitRepoUrl, folderPath, MONDAY_GITHUB_REPO_BRANCH, ctx.projectPath, output); |
| 31 | + task.title = 'Template downloaded successfully'; |
| 32 | +}; |
| 33 | + |
| 34 | +export const editEnvFileTask = async (ctx: ScaffoldTaskContext, task: ListrTaskWrapper<ScaffoldTaskContext, any>) => { |
| 35 | + task.title = 'Configuring environment variables'; |
| 36 | + const filePath = path.join(ctx.projectPath, '.env'); |
| 37 | + |
| 38 | + if (!fs.existsSync(filePath)) { |
| 39 | + task.skip('.env file not found, skipping configuration'); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (!ctx.signingSecret) { |
| 44 | + task.skip('No signing secret provided, skipping .env configuration'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + let envLines = fs.readFileSync(filePath, 'utf8').replaceAll('\r\n', '\n').split('\n'); |
| 50 | + |
| 51 | + // Update MONDAY_SIGNING_SECRET if provided |
| 52 | + envLines = envLines.map(line => |
| 53 | + line.startsWith('MONDAY_SIGNING_SECRET=') ? `MONDAY_SIGNING_SECRET=${ctx.signingSecret}` : line, |
| 54 | + ); |
| 55 | + |
| 56 | + fs.writeFileSync(filePath, envLines.join(os.EOL), 'utf8'); |
| 57 | + task.title = 'Environment variables configured'; |
| 58 | + } catch (error) { |
| 59 | + logger.debug(error, DEBUG_TAG); |
| 60 | + task.skip('Failed to configure environment variables'); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +export const openSetupFileTask = async (ctx: ScaffoldTaskContext, task: ListrTaskWrapper<ScaffoldTaskContext, any>) => { |
| 65 | + if (!ctx.project.openSetupMd) { |
| 66 | + task.skip('No setup documentation for this template'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + task.title = 'Opening setup documentation'; |
| 71 | + const setupUrl = `${MONDAY_GITHUB_REPO_URL}/blob/${MONDAY_GITHUB_REPO_BRANCH}/apps/${ctx.project.name}/SETUP.md`; |
| 72 | + |
| 73 | + try { |
| 74 | + // Map platform identifiers to their corresponding open commands |
| 75 | + const platformCommands: Record<string, string> = { |
| 76 | + darwin: 'open', |
| 77 | + win32: 'start', |
| 78 | + linux: 'xdg-open', |
| 79 | + }; |
| 80 | + |
| 81 | + const command = platformCommands[process.platform] ?? platformCommands.linux; |
| 82 | + spawn(command, [setupUrl], { detached: true, stdio: 'ignore' }).unref(); |
| 83 | + task.title = `Setup documentation opened in browser`; |
| 84 | + } catch (error) { |
| 85 | + logger.debug(error, DEBUG_TAG); |
| 86 | + task.skip(`Setup URL: ${setupUrl}`); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +export const installDependenciesTask = async ( |
| 91 | + ctx: ScaffoldTaskContext, |
| 92 | + task: ListrTaskWrapper<ScaffoldTaskContext, any>, |
| 93 | +) => { |
| 94 | + task.title = 'Installing npm packages'; |
| 95 | + |
| 96 | + return new Promise<void>((resolve, reject) => { |
| 97 | + const installProcess = spawn(npmCmd, ['install'], { |
| 98 | + cwd: ctx.projectPath, |
| 99 | + shell: true, |
| 100 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 101 | + }); |
| 102 | + |
| 103 | + let errorOutput = ''; |
| 104 | + |
| 105 | + installProcess.stderr?.on('data', (data: Buffer) => { |
| 106 | + errorOutput += data.toString(); |
| 107 | + }); |
| 108 | + |
| 109 | + installProcess.on('exit', code => { |
| 110 | + if (code === 0) { |
| 111 | + task.title = 'Dependencies installed successfully'; |
| 112 | + resolve(); |
| 113 | + } else { |
| 114 | + logger.debug(`npm install failed with code ${code}: ${errorOutput}`, DEBUG_TAG); |
| 115 | + reject(new Error(`Failed to install dependencies (exit code ${code})`)); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + installProcess.on('error', error => { |
| 120 | + logger.debug(error, DEBUG_TAG); |
| 121 | + reject(new Error(`Failed to run npm install: ${error.message}`)); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}; |
| 125 | + |
| 126 | +export const runProjectTask = async (ctx: ScaffoldTaskContext, task: ListrTaskWrapper<ScaffoldTaskContext, any>) => { |
| 127 | + task.title = 'Starting the project'; |
| 128 | + |
| 129 | + return new Promise<void>((resolve, reject) => { |
| 130 | + const startProcess = spawn(npmCmd, ['run', ctx.startCommand], { |
| 131 | + cwd: ctx.projectPath, |
| 132 | + shell: true, |
| 133 | + stdio: 'inherit', |
| 134 | + }); |
| 135 | + |
| 136 | + // Handle process cleanup on exit |
| 137 | + const cleanup = () => { |
| 138 | + if (!startProcess.killed) { |
| 139 | + startProcess.kill('SIGTERM'); |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + process.on('SIGINT', cleanup); |
| 144 | + process.on('SIGTERM', cleanup); |
| 145 | + process.on('exit', cleanup); |
| 146 | + |
| 147 | + startProcess.on('exit', code => { |
| 148 | + if (code === 0) { |
| 149 | + task.title = 'Project started successfully'; |
| 150 | + resolve(); |
| 151 | + } else if (code !== null) { |
| 152 | + reject(new Error(`Project exited with code ${code}`)); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + startProcess.on('error', error => { |
| 157 | + logger.debug(error, DEBUG_TAG); |
| 158 | + reject(new Error(`Failed to start project: ${error.message}`)); |
| 159 | + }); |
| 160 | + |
| 161 | + // Resolve after a short delay to let the process start |
| 162 | + setTimeout(() => { |
| 163 | + task.title = `Project is running (npm run ${ctx.startCommand})`; |
| 164 | + resolve(); |
| 165 | + }, 2000); |
| 166 | + }); |
| 167 | +}; |
| 168 | + |
| 169 | +export const validateDestination = async (destination: string): Promise<void> => { |
| 170 | + try { |
| 171 | + await ensureDir(destination); |
| 172 | + } catch { |
| 173 | + throw new Error(`Invalid destination directory: ${destination}`); |
| 174 | + } |
| 175 | +}; |
0 commit comments