|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { logging } from '@angular-devkit/core'; |
| 9 | +import { execSync } from 'child_process'; |
| 10 | +import * as fs from 'fs'; |
| 11 | +import * as os from 'os'; |
| 12 | +import * as path from 'path'; |
| 13 | +import { packages } from '../lib/packages'; |
| 14 | +import build from './build'; |
| 15 | + |
| 16 | + |
| 17 | +function _copy(from: string, to: string) { |
| 18 | + fs.readdirSync(from) |
| 19 | + .forEach(name => { |
| 20 | + const fromPath = path.join(from, name); |
| 21 | + const toPath = path.join(to, name); |
| 22 | + if (fs.statSync(fromPath).isDirectory()) { |
| 23 | + if (!fs.existsSync(toPath)) { |
| 24 | + fs.mkdirSync(toPath); |
| 25 | + } |
| 26 | + _copy(fromPath, toPath); |
| 27 | + } else { |
| 28 | + fs.writeFileSync(toPath, fs.readFileSync(fromPath)); |
| 29 | + } |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +export default function(opts: { force?: boolean }, logger: logging.Logger) { |
| 35 | + // Get the SHA. |
| 36 | + if (execSync(`git status --porcelain`).toString() && !opts.force) { |
| 37 | + logger.error('You cannot run snapshots with local changes.'); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'angular-devkit-publish-')); |
| 42 | + const message = execSync(`git log --format="%h %s" -n1`).toString().trim(); |
| 43 | + |
| 44 | + // Run build. |
| 45 | + logger.info('Building...'); |
| 46 | + build({ snapshot: true }, logger.createChild('build')); |
| 47 | + |
| 48 | + for (const packageName of Object.keys(packages)) { |
| 49 | + const pkg = packages[packageName]; |
| 50 | + |
| 51 | + if (!pkg.snapshot) { |
| 52 | + logger.warn(`Skipping ${pkg.name}.`); |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + logger.info(`Publishing ${pkg.name} to repo ${JSON.stringify(pkg.snapshotRepo)}.`); |
| 57 | + |
| 58 | + const publishLogger = logger.createChild('publish'); |
| 59 | + publishLogger.debug('Temporary directory: ' + root); |
| 60 | + |
| 61 | + const url = `https://github.com/${pkg.snapshotRepo}.git`; |
| 62 | + execSync(`git clone ${JSON.stringify(url)}`, { cwd: root }); |
| 63 | + |
| 64 | + const destPath = path.join(root, path.basename(pkg.snapshotRepo)); |
| 65 | + _copy(pkg.dist, destPath); |
| 66 | + |
| 67 | + execSync(`git config credential.helper "store --file=.git/credentials"`, { cwd: destPath }); |
| 68 | + fs.writeFileSync(path.join(destPath, '.git/credentials'), |
| 69 | + `https://${process.env['GITHUB_ACCESS_TOKEN']}@github.com`); |
| 70 | + |
| 71 | + // Make sure that every snapshots is unique. |
| 72 | + fs.writeFileSync(path.join(destPath, 'uniqueId'), '' + new Date()); |
| 73 | + |
| 74 | + // Commit and push. |
| 75 | + execSync(`git add -A`, { cwd: destPath }); |
| 76 | + execSync(`git commit -am ${JSON.stringify(message)}`, { cwd: destPath }); |
| 77 | + execSync(`git tag ${pkg.snapshotHash}`, { cwd: destPath }); |
| 78 | + execSync(`git push origin`, { cwd: destPath }); |
| 79 | + execSync(`git push --tags origin`, { cwd: destPath }); |
| 80 | + } |
| 81 | +} |
0 commit comments