forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.ts
91 lines (79 loc) · 2.74 KB
/
executor.ts
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
/**
* @license
* Copyright Google LLC 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.dev/license
*/
import { tags } from '@angular-devkit/core';
import { SpawnOptions, spawn } from 'node:child_process';
import * as path from 'node:path';
import { SchematicContext, TaskExecutor } from '../../src';
import {
RepositoryInitializerTaskFactoryOptions,
RepositoryInitializerTaskOptions,
} from './options';
export default function (
factoryOptions: RepositoryInitializerTaskFactoryOptions = {},
): TaskExecutor<RepositoryInitializerTaskOptions> {
const rootDirectory = factoryOptions.rootDirectory || process.cwd();
return async (options: RepositoryInitializerTaskOptions = {}, context: SchematicContext) => {
const authorName = options.authorName;
const authorEmail = options.authorEmail;
const execute = (args: string[], ignoreErrorStream?: boolean) => {
const outputStream = 'ignore';
const errorStream = ignoreErrorStream ? 'ignore' : process.stderr;
const spawnOptions: SpawnOptions = {
stdio: [process.stdin, outputStream, errorStream],
shell: true,
cwd: path.join(rootDirectory, options.workingDirectory || ''),
env: {
...process.env,
...(authorName ? { GIT_AUTHOR_NAME: authorName, GIT_COMMITTER_NAME: authorName } : {}),
...(authorEmail
? { GIT_AUTHOR_EMAIL: authorEmail, GIT_COMMITTER_EMAIL: authorEmail }
: {}),
},
};
return new Promise<void>((resolve, reject) => {
spawn('git', args, spawnOptions).on('close', (code: number) => {
if (code === 0) {
resolve();
} else {
reject(code);
}
});
});
};
const hasCommand = await execute(['--version']).then(
() => true,
() => false,
);
if (!hasCommand) {
return;
}
const insideRepo = await execute(['rev-parse', '--is-inside-work-tree'], true).then(
() => true,
() => false,
);
if (insideRepo) {
context.logger.info(tags.oneLine`
Directory is already under version control.
Skipping initialization of git.
`);
return;
}
// if git is not found or an error was thrown during the `git`
// init process just swallow any errors here
// NOTE: This will be removed once task error handling is implemented
try {
await execute(['init']);
await execute(['add', '.']);
if (options.commit) {
const message = options.message || 'initial commit';
await execute(['commit', `-m "${message}"`]);
}
context.logger.info('Successfully initialized git.');
} catch {}
};
}