forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshots.ts
More file actions
152 lines (125 loc) · 5.07 KB
/
snapshots.ts
File metadata and controls
152 lines (125 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable:no-implicit-dependencies
import { logging } from '@angular-devkit/core';
import { execSync, spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { packages } from '../lib/packages';
import build from './build';
import create from './create';
function _copy(from: string, to: string) {
fs.readdirSync(from)
.forEach(name => {
const fromPath = path.join(from, name);
const toPath = path.join(to, name);
if (fs.statSync(fromPath).isDirectory()) {
if (!fs.existsSync(toPath)) {
fs.mkdirSync(toPath);
}
_copy(fromPath, toPath);
} else {
fs.writeFileSync(toPath, fs.readFileSync(fromPath));
}
});
}
function _exec(command: string, args: string[], opts: { cwd?: string }, logger: logging.Logger) {
const { status, error, stdout } = spawnSync(command, args, {
stdio: ['ignore', 'pipe', 'inherit'],
...opts,
});
if (status != 0) {
logger.error(`Command failed: ${command} ${args.map(x => JSON.stringify(x)).join(', ')}`);
throw error;
}
return stdout.toString('utf-8');
}
export interface SnapshotsOptions {
force?: boolean;
githubTokenFile?: string;
githubToken?: string;
}
export default async function(opts: SnapshotsOptions, logger: logging.Logger) {
// Get the SHA.
if (execSync(`git status --porcelain`).toString() && !opts.force) {
logger.error('You cannot run snapshots with local changes.');
process.exit(1);
}
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'angular-cli-publish-'));
const message = execSync(`git log --format="%h %s" -n1`).toString().trim();
const githubToken = (
opts.githubToken
|| (opts.githubTokenFile && fs.readFileSync(opts.githubTokenFile, 'utf-8'))
|| ''
).trim();
if (githubToken) {
logger.info('Setting up global git name.');
_exec('git', ['config', '--global', 'user.email', '[email protected]'], {}, logger);
_exec('git', ['config', '--global', 'user.name', 'Angular Builds'], {}, logger);
_exec('git', ['config', '--global', 'push.default', 'simple'], {}, logger);
}
// Creating a new project and reading the help.
logger.info('Creating temporary project...');
const newProjectTempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'angular-cli-create-'));
const newProjectName = 'help-project';
const newProjectRoot = path.join(newProjectTempRoot, newProjectName);
await create({ _: [newProjectName] }, logger.createChild('create'), newProjectTempRoot);
// Run build.
logger.info('Building...');
await build({ snapshot: true }, logger.createChild('build'));
logger.info('Gathering JSON Help...');
const ngPath = path.join(newProjectRoot, 'node_modules/.bin/ng');
const helpOutputRoot = path.join(packages['@angular/cli'].dist, 'help');
fs.mkdirSync(helpOutputRoot);
const commands = require('../packages/angular/cli/commands.json');
for (const commandName of Object.keys(commands)) {
const options = { cwd: newProjectRoot };
const childLogger = logger.createChild(commandName);
const stdout = _exec(ngPath, [commandName, '--help=json'], options, childLogger);
if (stdout.trim()) {
fs.writeFileSync(path.join(helpOutputRoot, commandName + '.json'), stdout);
}
}
if (!githubToken) {
logger.info('No token given, skipping actual publishing...');
return 0;
}
for (const packageName of Object.keys(packages)) {
const pkg = packages[packageName];
if (!pkg.snapshot) {
logger.warn(`Skipping ${pkg.name}.`);
continue;
}
logger.info(`Publishing ${pkg.name} to repo ${JSON.stringify(pkg.snapshotRepo)}.`);
const publishLogger = logger.createChild('publish');
publishLogger.debug('Temporary directory: ' + root);
const url = `https://${githubToken ? githubToken + '@' : ''}github.com/${pkg.snapshotRepo}.git`;
_exec('git', ['clone', url], { cwd: root }, publishLogger);
const destPath = path.join(root, path.basename(pkg.snapshotRepo));
// Clear snapshot directory before publishing to remove deleted build files.
try {
_exec('git', ['rm', '-rf', './'], {cwd: destPath}, publishLogger);
} catch {
// Ignore errors on delete. :shrug:
}
_copy(pkg.dist, destPath);
if (githubToken) {
_exec('git', ['config', 'commit.gpgSign', 'false'], { cwd: destPath }, publishLogger);
}
// Make sure that every snapshots is unique.
fs.writeFileSync(path.join(destPath, 'uniqueId'), '' + new Date());
// Commit and push.
_exec('git', ['add', '.'], { cwd: destPath }, publishLogger);
_exec('git', ['commit', '-a', '-m', message], { cwd: destPath }, publishLogger);
_exec('git', ['tag', pkg.snapshotHash], { cwd: destPath }, publishLogger);
_exec('git', ['push', 'origin'], { cwd: destPath }, publishLogger);
_exec('git', ['push', '--tags', 'origin'], { cwd: destPath }, publishLogger);
}
return 0;
}