import { spawnSync } from 'child_process'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { loadDepartmentConfig, writeClaspConfig } from './departmentConfig.mjs'; const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); const department = process.argv[2]; if (!department) { console.error('Usage: npm run push -- '); process.exit(1); } const distDir = path.join(root, 'dist', department); if (!fs.existsSync(path.join(distDir, 'Code.js'))) { console.error(`Nothing to push. Run: npm run build -- ${department}`); process.exit(1); } let scriptIds; try { ({ scriptIds } = loadDepartmentConfig(root, department)); } catch (error) { console.error(error instanceof Error ? error.message : error); process.exit(1); } for (let index = 0; index < scriptIds.length; index += 1) { const scriptId = scriptIds[index]; writeClaspConfig(distDir, scriptId); console.log( `Pushing ${department} → ${scriptId} (${index + 1}/${scriptIds.length})` ); const result = spawnSync('npx', ['clasp', 'push', '--force'], { cwd: distDir, stdio: 'inherit', shell: true, }); if ((result.status ?? 1) !== 0) { console.error(`Push failed for ${scriptId}.`); if (index > 0) { console.error(`Already pushed: ${scriptIds.slice(0, index).join(', ')}`); } if (index < scriptIds.length - 1) { console.error(`Not pushed: ${scriptIds.slice(index + 1).join(', ')}`); } process.exit(result.status ?? 1); } } console.log(`Pushed ${department} to ${scriptIds.length} Apps Script project(s).`);