|
| 1 | +import {cp, mkdtemp, readFile, rm, writeFile} from 'fs/promises'; |
| 2 | +import {Deployment} from './deployments'; |
| 3 | +import {join} from 'path'; |
| 4 | + |
| 5 | +import {tmpdir} from 'os'; |
| 6 | +import {spawnSync} from 'child_process'; |
| 7 | +import {getCredentialFilePath} from './credential'; |
| 8 | + |
| 9 | +export async function deployToFirebase( |
| 10 | + deployment: Deployment, |
| 11 | + configPath: string, |
| 12 | + distDirPath: string, |
| 13 | +) { |
| 14 | + if (deployment.destination == undefined) { |
| 15 | + console.log(`No deployment necessary for docs created from: ${deployment.branch}`); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + console.log('Preparing for deployment to firebase...'); |
| 20 | + |
| 21 | + const tmpDeployDir = await mkdtemp(join(tmpdir(), 'deploy-directory')); |
| 22 | + const deployConfigPath = join(tmpDeployDir, 'firebase.json'); |
| 23 | + |
| 24 | + const config = JSON.parse(await readFile(configPath, {encoding: 'utf-8'})) as { |
| 25 | + hosting: {public: string}; |
| 26 | + }; |
| 27 | + config['hosting']['public'] = './dist'; |
| 28 | + |
| 29 | + await writeFile(deployConfigPath, JSON.stringify(config, null, 2)); |
| 30 | + |
| 31 | + await cp(distDirPath, join(tmpDeployDir, 'dist'), {recursive: true}); |
| 32 | + spawnSync(`chmod 777 -R ${tmpDeployDir}`, {encoding: 'utf-8', shell: true}); |
| 33 | + |
| 34 | + firebase( |
| 35 | + `target:clear --config ${deployConfigPath} --project angular-dev-site hosting angular-docs`, |
| 36 | + tmpDeployDir, |
| 37 | + ); |
| 38 | + firebase( |
| 39 | + `target:apply --config ${deployConfigPath} --project angular-dev-site hosting angular-docs ${deployment.destination}`, |
| 40 | + tmpDeployDir, |
| 41 | + ); |
| 42 | + firebase( |
| 43 | + `deploy --config ${deployConfigPath} --project angular-dev-site --only hosting --non-interactive`, |
| 44 | + tmpDeployDir, |
| 45 | + ); |
| 46 | + firebase( |
| 47 | + `target:clear --config ${deployConfigPath} --project angular-dev-site hosting angular-docs`, |
| 48 | + tmpDeployDir, |
| 49 | + ); |
| 50 | + |
| 51 | + await rm(tmpDeployDir, {recursive: true}); |
| 52 | +} |
| 53 | + |
| 54 | +export async function setupRedirect(deployment: Deployment) { |
| 55 | + if (deployment.redirect === undefined) { |
| 56 | + console.log(`No redirect necessary for docs created from: ${deployment.branch}`); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + console.log('Preparing to set up redirect on firebase...'); |
| 61 | + |
| 62 | + const redirectConfig = JSON.stringify( |
| 63 | + { |
| 64 | + hosting: { |
| 65 | + target: 'angular-docs', |
| 66 | + redirects: [ |
| 67 | + { |
| 68 | + type: 302, |
| 69 | + regex: '^(.*)$', |
| 70 | + destination: `${deployment.redirect.to}:1`, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + }, |
| 75 | + null, |
| 76 | + 2, |
| 77 | + ); |
| 78 | + |
| 79 | + const tmpRedirectDir = await mkdtemp(join(tmpdir(), 'redirect-directory')); |
| 80 | + const redirectConfigPath = join(tmpRedirectDir, 'firebase.json'); |
| 81 | + |
| 82 | + await writeFile(redirectConfigPath, redirectConfig); |
| 83 | + spawnSync(`chmod 777 -R ${tmpRedirectDir}`, {encoding: 'utf-8', shell: true}); |
| 84 | + |
| 85 | + firebase( |
| 86 | + `target:clear --config ${redirectConfigPath} --project angular-dev-site hosting angular-docs`, |
| 87 | + tmpRedirectDir, |
| 88 | + ); |
| 89 | + firebase( |
| 90 | + `target:apply --config ${redirectConfigPath} --project angular-dev-site hosting angular-docs ${deployment.redirect.from}`, |
| 91 | + tmpRedirectDir, |
| 92 | + ); |
| 93 | + firebase( |
| 94 | + `deploy --config ${redirectConfigPath} --project angular-dev-site --only hosting --non-interactive`, |
| 95 | + tmpRedirectDir, |
| 96 | + ); |
| 97 | + firebase( |
| 98 | + `target:clear --config ${redirectConfigPath} --project angular-dev-site hosting angular-docs`, |
| 99 | + tmpRedirectDir, |
| 100 | + ); |
| 101 | + |
| 102 | + await rm(tmpRedirectDir, {recursive: true}); |
| 103 | +} |
| 104 | + |
| 105 | +function firebase(cmd: string, cwd?: string) { |
| 106 | + spawnSync('npx', `-y firebase-tools@13.15.1 ${cmd}`.split(' '), { |
| 107 | + cwd, |
| 108 | + encoding: 'utf-8', |
| 109 | + shell: true, |
| 110 | + stdio: 'inherit', |
| 111 | + env: { |
| 112 | + ...process.env, |
| 113 | + GOOGLE_APPLICATION_CREDENTIALS: getCredentialFilePath(), |
| 114 | + }, |
| 115 | + }); |
| 116 | +} |
0 commit comments