@eclipse-glsp/cli 1.1.0-next.4846de1.120

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/LICENSE +642 -0
  2. package/README.md +89 -0
  3. package/bin/glsp +2 -0
  4. package/lib/app.d.ts +3 -0
  5. package/lib/app.d.ts.map +1 -0
  6. package/lib/app.js +32 -0
  7. package/lib/app.js.map +1 -0
  8. package/lib/commands/check-header.d.ts +24 -0
  9. package/lib/commands/check-header.d.ts.map +1 -0
  10. package/lib/commands/check-header.js +234 -0
  11. package/lib/commands/check-header.js.map +1 -0
  12. package/lib/commands/coverage-report.d.ts +29 -0
  13. package/lib/commands/coverage-report.d.ts.map +1 -0
  14. package/lib/commands/coverage-report.js +100 -0
  15. package/lib/commands/coverage-report.js.map +1 -0
  16. package/lib/commands/release/common.d.ts +69 -0
  17. package/lib/commands/release/common.d.ts.map +1 -0
  18. package/lib/commands/release/common.js +273 -0
  19. package/lib/commands/release/common.js.map +1 -0
  20. package/lib/commands/release/release-client.d.ts +3 -0
  21. package/lib/commands/release/release-client.d.ts.map +1 -0
  22. package/lib/commands/release/release-client.js +53 -0
  23. package/lib/commands/release/release-client.js.map +1 -0
  24. package/lib/commands/release/release-eclipse-integration.d.ts +18 -0
  25. package/lib/commands/release/release-eclipse-integration.d.ts.map +1 -0
  26. package/lib/commands/release/release-eclipse-integration.js +90 -0
  27. package/lib/commands/release/release-eclipse-integration.js.map +1 -0
  28. package/lib/commands/release/release-java-server.d.ts +3 -0
  29. package/lib/commands/release/release-java-server.d.ts.map +1 -0
  30. package/lib/commands/release/release-java-server.js +68 -0
  31. package/lib/commands/release/release-java-server.js.map +1 -0
  32. package/lib/commands/release/release-server-node.d.ts +18 -0
  33. package/lib/commands/release/release-server-node.d.ts.map +1 -0
  34. package/lib/commands/release/release-server-node.js +51 -0
  35. package/lib/commands/release/release-server-node.js.map +1 -0
  36. package/lib/commands/release/release-theia-integration.d.ts +18 -0
  37. package/lib/commands/release/release-theia-integration.d.ts.map +1 -0
  38. package/lib/commands/release/release-theia-integration.js +62 -0
  39. package/lib/commands/release/release-theia-integration.js.map +1 -0
  40. package/lib/commands/release/release-vscode-integration.d.ts +18 -0
  41. package/lib/commands/release/release-vscode-integration.d.ts.map +1 -0
  42. package/lib/commands/release/release-vscode-integration.js +51 -0
  43. package/lib/commands/release/release-vscode-integration.js.map +1 -0
  44. package/lib/commands/release/release.d.ts +14 -0
  45. package/lib/commands/release/release.d.ts.map +1 -0
  46. package/lib/commands/release/release.js +165 -0
  47. package/lib/commands/release/release.js.map +1 -0
  48. package/lib/util/command-util.d.ts +26 -0
  49. package/lib/util/command-util.d.ts.map +1 -0
  50. package/lib/util/command-util.js +53 -0
  51. package/lib/util/command-util.js.map +1 -0
  52. package/lib/util/git-util.d.ts +60 -0
  53. package/lib/util/git-util.d.ts.map +1 -0
  54. package/lib/util/git-util.js +171 -0
  55. package/lib/util/git-util.js.map +1 -0
  56. package/lib/util/logger.d.ts +23 -0
  57. package/lib/util/logger.d.ts.map +1 -0
  58. package/lib/util/logger.js +50 -0
  59. package/lib/util/logger.js.map +1 -0
  60. package/lib/util/validation-util.d.ts +6 -0
  61. package/lib/util/validation-util.d.ts.map +1 -0
  62. package/lib/util/validation-util.js +64 -0
  63. package/lib/util/validation-util.js.map +1 -0
  64. package/package.json +64 -0
  65. package/src/app.ts +30 -0
  66. package/src/commands/check-header.ts +315 -0
  67. package/src/commands/coverage-report.ts +113 -0
  68. package/src/commands/release/common.ts +312 -0
  69. package/src/commands/release/release-client.ts +64 -0
  70. package/src/commands/release/release-eclipse-integration.ts +113 -0
  71. package/src/commands/release/release-java-server.ts +73 -0
  72. package/src/commands/release/release-server-node.ts +61 -0
  73. package/src/commands/release/release-theia-integration.ts +77 -0
  74. package/src/commands/release/release-vscode-integration.ts +65 -0
  75. package/src/commands/release/release.ts +215 -0
  76. package/src/util/command-util.ts +54 -0
  77. package/src/util/git-util.ts +172 -0
  78. package/src/util/logger.ts +57 -0
  79. package/src/util/validation-util.ts +63 -0
@@ -0,0 +1,77 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ import * as sh from 'shelljs';
18
+ import { LOGGER } from '../../util/logger';
19
+ import {
20
+ asMvnVersion,
21
+ checkoutAndCd,
22
+ commitAndTag,
23
+ isExistingMavenVersion,
24
+ lernaSetVersion,
25
+ publish,
26
+ ReleaseOptions,
27
+ updateLernaForDryRun,
28
+ updateServerConfig,
29
+ updateVersion,
30
+ yarnInstall
31
+ } from './common';
32
+
33
+ let REPO_ROOT: string;
34
+
35
+ export async function releaseTheiaIntegration(options: ReleaseOptions): Promise<void> {
36
+ LOGGER.info('Prepare glsp-theia-integration release');
37
+ LOGGER.debug('Release options: ', options);
38
+ REPO_ROOT = checkoutAndCd(options);
39
+ updateExternalGLSPDependencies(options.version);
40
+ await updateDownloadServerScript(options.version);
41
+ generateChangeLog();
42
+ lernaSetVersion(REPO_ROOT, options.version);
43
+ build();
44
+ if (options.npmDryRun) {
45
+ updateLernaForDryRun();
46
+ }
47
+ commitAndTag(options.version, REPO_ROOT);
48
+ publish(REPO_ROOT, options);
49
+ }
50
+
51
+ function updateExternalGLSPDependencies(version: string): void {
52
+ LOGGER.info('Update external GLSP dependencies (Client and workflow example)');
53
+ sh.cd(REPO_ROOT);
54
+ updateVersion({ name: '@eclipse-glsp/client', version }, { name: '@eclipse-glsp-examples/workflow-glsp', version });
55
+ }
56
+
57
+ async function updateDownloadServerScript(version: string): Promise<void> {
58
+ LOGGER.info('Update example server download config');
59
+ const mvnVersion = asMvnVersion(version);
60
+ if (!isExistingMavenVersion('org.eclipse.glsp', 'org.eclipse.glsp.server', mvnVersion)) {
61
+ LOGGER.warn(`No Java GLSP server with version ${mvnVersion} found on maven central!. Please release a new Java GLSP Server version
62
+ before publishing this release!`);
63
+ }
64
+
65
+ sh.cd(`${REPO_ROOT}/examples/workflow-theia/src/node`);
66
+ updateServerConfig('server-config.json', mvnVersion, false);
67
+ }
68
+
69
+ function build(): void {
70
+ LOGGER.info('Install & Build with yarn');
71
+ yarnInstall(REPO_ROOT);
72
+ LOGGER.debug('Build successful');
73
+ }
74
+
75
+ function generateChangeLog(): void {
76
+ // do nothing for now
77
+ }
@@ -0,0 +1,65 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ import * as sh from 'shelljs';
18
+ import { LOGGER } from '../../util/logger';
19
+ import {
20
+ checkoutAndCd,
21
+ commitAndTag,
22
+ lernaSetVersion,
23
+ publish,
24
+ ReleaseOptions,
25
+ updateLernaForDryRun,
26
+ updateVersion,
27
+ yarnInstall
28
+ } from './common';
29
+
30
+ let REPO_ROOT: string;
31
+
32
+ export async function releaseVscodeIntegration(options: ReleaseOptions): Promise<void> {
33
+ LOGGER.info('Prepare glsp-vscode-integration release');
34
+ LOGGER.debug('Release options: ', options);
35
+ REPO_ROOT = checkoutAndCd(options);
36
+ updateExternalGLSPDependencies(options.version);
37
+ generateChangeLog();
38
+ lernaSetVersion(REPO_ROOT, options.version);
39
+ build();
40
+ if (options.npmDryRun) {
41
+ updateLernaForDryRun();
42
+ }
43
+ commitAndTag(options.version, REPO_ROOT);
44
+ publish(REPO_ROOT, options);
45
+ }
46
+
47
+ function updateExternalGLSPDependencies(version: string): void {
48
+ LOGGER.info('Update external GLSP dependencies (Protocol)');
49
+ sh.cd(REPO_ROOT);
50
+ updateVersion(
51
+ { name: '@eclipse-glsp/protocol', version },
52
+ { name: '@eclipse-glsp/client', version },
53
+ { name: '@eclipse-glsp-examples/workflow-glsp', version }
54
+ );
55
+ }
56
+
57
+ function build(): void {
58
+ LOGGER.info('Install & Build with yarn');
59
+ yarnInstall(REPO_ROOT);
60
+ LOGGER.debug('Build successful');
61
+ }
62
+
63
+ function generateChangeLog(): void {
64
+ // do nothing for now
65
+ }
@@ -0,0 +1,215 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+ import { ChildProcess } from 'child_process';
17
+ import { Argument } from 'commander';
18
+ import { exit } from 'process';
19
+ import { createInterface } from 'readline';
20
+ import * as readline from 'readline-sync';
21
+ import * as semver from 'semver';
22
+ import * as sh from 'shelljs';
23
+ import { baseCommand, configureShell, fatalExec, getShellConfig } from '../../util/command-util';
24
+ import { configureLogger, LOGGER } from '../../util/logger';
25
+ import { validateDirectory, validateVersion } from '../../util/validation-util';
26
+ import {
27
+ asMvnVersion,
28
+ checkIfMavenVersionExists,
29
+ checkIfNpmVersionExists,
30
+ Component,
31
+ ReleaseOptions,
32
+ ReleaseType,
33
+ VERDACCIO_REGISTRY
34
+ } from './common';
35
+ import { releaseClient } from './release-client';
36
+ import { releaseEclipseIntegration } from './release-eclipse-integration';
37
+ import { releaseJavaServer } from './release-java-server';
38
+ import { releaseServerNode } from './release-server-node';
39
+ import { releaseTheiaIntegration } from './release-theia-integration';
40
+ import { releaseVscodeIntegration } from './release-vscode-integration';
41
+
42
+ interface ReleaseCmdOptions {
43
+ checkoutDir: string;
44
+ branch: string;
45
+ force: boolean;
46
+ publish: boolean;
47
+ npmDryRun: boolean;
48
+ draft: boolean;
49
+ verbose: boolean;
50
+ }
51
+
52
+ export const ReleaseCommand = baseCommand()
53
+ .name('release')
54
+ .description('Prepare & publish a new release for a glsp component')
55
+ .addArgument(new Argument('<component>', 'The glsp component to be released').choices(Component.CLI_CHOICES).argParser(Component.parse))
56
+ .addArgument(new Argument('<releaseType>', 'The release type').choices(ReleaseType.CLI_CHOICES))
57
+ .argument('[customVersion]', 'Custom version number. Will be ignored if the release type is not "custom"', validateVersion)
58
+ .option('-f, --force', 'Enable force mode', false)
59
+ .option('-d, --checkoutDir <checkoutDir>', 'The git checkout directory', validateDirectory, process.cwd())
60
+ .option('-b, --branch <branch>', 'The git branch to checkout', 'master')
61
+ .option('-v, --verbose', 'Enable verbose (debug) log output', false)
62
+ .option('--no-publish', 'Only prepare release but do not publish to github', true)
63
+ .option('--draft', 'Publish github releases as drafts', false)
64
+ .option(
65
+ '--npm-dryRun',
66
+ 'Execute a npm dry-run for inspection. Publishes to the local npm registry and does not publish to github',
67
+ false
68
+ )
69
+ .action(release);
70
+
71
+ let verdaccioChildProcess: ChildProcess | undefined = undefined;
72
+
73
+ export async function release(
74
+ component: Component,
75
+ releaseType: ReleaseType,
76
+ customVersion: string | undefined,
77
+ cliOptions: ReleaseCmdOptions
78
+ ): Promise<void> {
79
+ try {
80
+ LOGGER.debug('Cli options:', cliOptions);
81
+ configureShell({ silent: !cliOptions.verbose });
82
+ configureLogger(cliOptions.verbose);
83
+ checkGHCli();
84
+ const version = deriveVersion(releaseType, customVersion);
85
+ const options: ReleaseOptions = { ...cliOptions, component, releaseType, version };
86
+ if (cliOptions.npmDryRun && options.component.releaseRepo === 'npm') {
87
+ await launchVerdaccio().catch(error => LOGGER.error('Error occurred during verdaccio launch', error));
88
+ }
89
+ switch (component.type) {
90
+ case 'server-java':
91
+ checkIfMavenVersionExists('org.eclipse.glsp', 'org.eclipse.glsp.server', asMvnVersion(version));
92
+ return releaseJavaServer(options);
93
+ case 'server-node':
94
+ await checkIfNpmVersionExists('@eclipse-glsp/server-node', version);
95
+ return releaseServerNode(options);
96
+ case 'client':
97
+ await checkIfNpmVersionExists('@eclipse-glsp/client', version);
98
+ return releaseClient(options);
99
+ case 'theia-integration':
100
+ await checkIfNpmVersionExists('@eclipse-glsp/theia-integration', version);
101
+ return releaseTheiaIntegration(options);
102
+ case 'vscode-integration':
103
+ await checkIfNpmVersionExists('@eclipse-glsp/vscode-integration', version);
104
+ return releaseVscodeIntegration(options);
105
+ case 'eclipse-integration':
106
+ await checkIfNpmVersionExists('@eclipse-glsp/ide', version);
107
+ return releaseEclipseIntegration(options);
108
+ }
109
+ } catch (err) {
110
+ console.error('An error occurred during command execution:', err);
111
+ exit(1);
112
+ } finally {
113
+ if (verdaccioChildProcess) {
114
+ verdaccioChildProcess.kill();
115
+ }
116
+ }
117
+ }
118
+
119
+ function checkGHCli(): void {
120
+ LOGGER.debug('Verify that Github CLI is configured correctly');
121
+ if (!isGithubCLIAuthenticated()) {
122
+ throw new Error("Github CLI is not configured properly. No user is logged in for host 'github.com'");
123
+ }
124
+ }
125
+
126
+ function isGithubCLIAuthenticated(): boolean {
127
+ LOGGER.debug('Verify that Github CLI is installed');
128
+ fatalExec('which gh', 'Github CLI is not installed!');
129
+
130
+ const status = sh.exec('gh auth status', getShellConfig());
131
+ if (status.code !== 0) {
132
+ if (status.stderr.includes('You are not logged into any GitHub hosts')) {
133
+ return false;
134
+ }
135
+ throw new Error(status.stderr);
136
+ }
137
+ if (!status.stderr.trim().includes('Logged in to github.com')) {
138
+ LOGGER.debug("No user is logged in for host 'github.com'");
139
+ return false;
140
+ }
141
+ LOGGER.debug('Github CLI is authenticated and ready to use');
142
+ return true;
143
+ }
144
+
145
+ function launchVerdaccio(): Promise<void> {
146
+ LOGGER.debug('Verify that verdaccio is installed and start if necessary');
147
+ fatalExec('which verdaccio', 'Verdaccio is not installed!');
148
+ // Check if verdaccio is alreaddy running
149
+ const result = sh.exec(`curl -i ${VERDACCIO_REGISTRY}`, getShellConfig());
150
+ if (result.code !== 0) {
151
+ LOGGER.info('Starting local verdaccio registry');
152
+ verdaccioChildProcess = sh.exec('verdaccio', { async: true, silent: true });
153
+ return new Promise(resolve => {
154
+ createInterface(verdaccioChildProcess!.stdout!).on('line', line => {
155
+ if (line.includes(`http address - ${VERDACCIO_REGISTRY}`)) {
156
+ resolve();
157
+ }
158
+ });
159
+ });
160
+ }
161
+ return Promise.resolve();
162
+ }
163
+
164
+ function deriveVersion(releaseType: ReleaseType, customVersion?: string): string {
165
+ LOGGER.debug(`Derive version from release type: ${release}`);
166
+ switch (releaseType) {
167
+ case 'custom':
168
+ return getCustomVersion(customVersion);
169
+ case 'rc':
170
+ return getRCVersion();
171
+ case 'patch':
172
+ case 'major':
173
+ case 'minor':
174
+ return semverInc(releaseType);
175
+ }
176
+ }
177
+
178
+ const REFERENCE_NPM_PACKAGE = '@eclipse-glsp/ide';
179
+
180
+ function getRCVersion(): string {
181
+ LOGGER.debug('Retrieve and new RC version');
182
+ const newBaseVersion = semverInc('minor');
183
+ const currentRcVersion = sh.exec(`npm view ${REFERENCE_NPM_PACKAGE} dist-tags.rc`, getShellConfig()).stdout.trim();
184
+ const currentRcBaseVersion = currentRcVersion.split('-')[0];
185
+ if (currentRcBaseVersion !== newBaseVersion) {
186
+ return `${newBaseVersion}-RC01`;
187
+ }
188
+ let rcNumber = Number.parseInt(currentRcVersion.replace(`${currentRcBaseVersion}-RC`, ''), 10);
189
+ rcNumber++;
190
+
191
+ return `${newBaseVersion}-RC` + `${rcNumber}`.padStart(2, '0');
192
+ }
193
+
194
+ function getCustomVersion(customVersion?: string): string {
195
+ LOGGER.debug('Retrieve and validate custom version');
196
+ LOGGER.debug(customVersion ? `Custom version has been passed as argument: ${customVersion}` : 'Prompt custom version from user');
197
+ const version = customVersion ?? readline.question('> Please enter the new version');
198
+ return validateVersion(version);
199
+ }
200
+
201
+ function getCurrentVersion(): string {
202
+ LOGGER.debug('Retrieve base version by querying the latest tag of the reference npm package');
203
+ const version = sh.exec(`npm view ${REFERENCE_NPM_PACKAGE} dist-tags.latest`, getShellConfig()).stdout.trim();
204
+ return validateVersion(version);
205
+ }
206
+
207
+ function semverInc(releaseType: semver.ReleaseType, identifier?: string): string {
208
+ const currentVersion = getCurrentVersion();
209
+ LOGGER.debug(`Execute: semver.inc("${currentVersion}", ${releaseType}, ${identifier})`);
210
+ const newVersion = semver.inc(currentVersion, releaseType, identifier);
211
+ if (!newVersion) {
212
+ throw new Error(`Could not increment version: ${currentVersion} `);
213
+ }
214
+ return newVersion;
215
+ }
@@ -0,0 +1,54 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+ import { Command } from 'commander';
17
+ import * as sh from 'shelljs';
18
+
19
+ // Commander.js utils
20
+ export function baseCommand(cmd = new Command()): Command {
21
+ return cmd //
22
+ .showSuggestionAfterError(true)
23
+ .showHelpAfterError(true)
24
+ .allowUnknownOption(false);
25
+ }
26
+
27
+ export type ShellConfig = sh.ExecOptions & { async: false };
28
+ export const SH_CONFIG: ShellConfig = {
29
+ async: false,
30
+ fatal: true,
31
+ silent: false
32
+ };
33
+
34
+ export function getShellConfig(options: Partial<Omit<ShellConfig, 'async'>> = {}): ShellConfig {
35
+ return {
36
+ ...SH_CONFIG,
37
+ ...options
38
+ };
39
+ }
40
+
41
+ export function configureShell(config: Partial<sh.ShellConfig>): void {
42
+ sh.config.reset();
43
+ getShellConfig({});
44
+ SH_CONFIG.silent = config.silent;
45
+ SH_CONFIG.fatal = config.fatal;
46
+ }
47
+
48
+ export function fatalExec(command: string, fatalErrorMessage: string, options: Partial<Omit<ShellConfig, 'async'>> = {}): sh.ShellString {
49
+ const result = sh.exec(command, getShellConfig(options));
50
+ if (result.code !== 0) {
51
+ throw new Error(fatalErrorMessage);
52
+ }
53
+ return result;
54
+ }
@@ -0,0 +1,172 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ import { resolve } from 'path';
18
+ import * as sh from 'shelljs';
19
+ import { getShellConfig } from './command-util';
20
+
21
+ export function isGitRepository(path?: string): boolean {
22
+ cdIfPresent(path);
23
+ const isGitRepo =
24
+ sh
25
+ .exec('git rev-parse --is-inside-work-tree', getShellConfig({ silent: true }))
26
+ .stdout.trim()
27
+ .toLocaleLowerCase() === 'true';
28
+ return isGitRepo;
29
+ }
30
+
31
+ export function getGitRoot(path?: string): string {
32
+ cdIfPresent(path);
33
+ const fileString = sh.exec('git rev-parse --show-toplevel', getShellConfig()).stdout.trim();
34
+ return resolve(fileString);
35
+ }
36
+
37
+ export function hasGitChanges(path?: string): boolean {
38
+ return getUncommittedChanges(path).length > 0;
39
+ }
40
+
41
+ /**
42
+ * Returns the files that have uncommitted changes (staged, not staged and untracked) of a git repository.
43
+ * Filepaths are absolute.
44
+ */
45
+ export function getUncommittedChanges(path?: string): string[] {
46
+ cdIfPresent(path);
47
+ return sh
48
+ .exec('git status --porcelain', getShellConfig())
49
+ .stdout.trim()
50
+ .split('\n')
51
+ .map(fileInfo =>
52
+ // Extract relative file path from the info string and convert to absolute path
53
+ resolve(path ?? process.cwd(), fileInfo.trim().split(' ').pop() ?? '')
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Returns the files tha have been changed with the last commit (also includes currently staged but uncommitted changes)
59
+ * Filepaths are absolute.
60
+ */
61
+ export function getChangesOfLastCommit(path?: string): string[] {
62
+ cdIfPresent(path);
63
+ return sh
64
+ .exec('git diff --name-only HEAD^', getShellConfig())
65
+ .stdout.trim()
66
+ .split('\n')
67
+ .map(file => resolve(path ?? process.cwd(), file));
68
+ }
69
+
70
+ /**
71
+ * Returns the last modification date of a file (or the last commit) in a git repo.
72
+ * @param filePath The file. If undefined the modification date of the last commit will be returned
73
+ * @param repoRoot The path to the repo root. If undefined the current working directory is used.
74
+ * @returns The date or undefined if the file is outside of the git repo.
75
+ */
76
+ export function getLastModificationDate(filePath?: string, repoRoot?: string): Date | undefined {
77
+ cdIfPresent(repoRoot);
78
+ const result = sh.exec(`git log -1 --pretty="format:%ci" ${filePath ?? ''}`, getShellConfig());
79
+ if (result.code !== 0) {
80
+ return undefined;
81
+ }
82
+ return new Date(result.stdout.trim());
83
+ }
84
+ /**
85
+ * Returns the last modification date of a file in a git repo.
86
+ * @param filePath The file
87
+ * @param repoRoot The path to the repo root. If undefined the current working directory is used.
88
+ * @returns The date or undefined if the file is outside of the git repo.
89
+ */
90
+ export function getFirstModificationDate(filePath: string, repoRoot?: string): Date | undefined {
91
+ cdIfPresent(repoRoot);
92
+ const result = sh.exec(`git log --pretty="format:%ci" --follow ${filePath}`, getShellConfig());
93
+ if (result.code !== 0) {
94
+ return undefined;
95
+ }
96
+ const datesString = result.stdout.trim();
97
+ if (datesString.length === 0) {
98
+ return new Date();
99
+ }
100
+
101
+ const date = datesString.split('\n').pop();
102
+ return date ? new Date(date) : undefined;
103
+ }
104
+
105
+ export function getFilesOfCommit(commitHash: string, repoRoot?: string): string[] {
106
+ cdIfPresent(repoRoot);
107
+ const result = sh.exec(`git show --pretty="" --name-only ${commitHash}`, getShellConfig());
108
+ if (result.code !== 0) {
109
+ return [];
110
+ }
111
+
112
+ return result.stdout.trim().split('\n');
113
+ }
114
+
115
+ /**
116
+ * Returns the commit hash of the initial commit of the given repository
117
+ * @param repoRoot The path to the repo root. If undefined the current working directory is used.
118
+ * @returns The commit hash or undefined if something went wrong.
119
+ */
120
+ export function getInitialCommit(repoRoot?: string): string | undefined {
121
+ cdIfPresent(repoRoot);
122
+ const result = sh.exec('git log --pretty=oneline --reverse', getShellConfig());
123
+ if (result.code !== 0) {
124
+ return undefined;
125
+ }
126
+ const commits = result.stdout.trim();
127
+ if (commits.length === 0) {
128
+ return undefined;
129
+ }
130
+ return commits.substring(0, commits.indexOf(' '));
131
+ }
132
+
133
+ /**
134
+ * Returns the commit hash of the first commit for a given file (across renames).
135
+ * @param repoRoot The path to the repo root. If undefined the current working directory is used.
136
+ * @returns The commit hash or undefined if something went wrong.
137
+ */
138
+ export function getFirstCommit(filePath: string, repoRoot?: string): string | undefined {
139
+ cdIfPresent(repoRoot);
140
+ const result = sh.exec(`git log --follow --pretty=format:"%H" ${filePath}`, getShellConfig());
141
+ if (result.code !== 0) {
142
+ return undefined;
143
+ }
144
+ return result.stdout.trim().split('\n').pop();
145
+ }
146
+
147
+ export function getLatestGithubRelease(path?: string): string {
148
+ cdIfPresent(path);
149
+ const release = sh.exec('gh release list --exclude-drafts -L 1', getShellConfig()).stdout.trim().split('\t');
150
+ return release[release.length - 2];
151
+ }
152
+
153
+ export function getLatestTag(path?: string): string {
154
+ cdIfPresent(path);
155
+ return sh.exec('git describe --abbrev=0 --tags', getShellConfig()).stdout.trim();
156
+ }
157
+
158
+ export function hasBranch(branch: string, path?: string): boolean {
159
+ cdIfPresent(path);
160
+ return sh.exec(`git branch --list ${branch}`, getShellConfig()).stdout.trim().length !== 0;
161
+ }
162
+
163
+ export function getRemoteUrl(path?: string): string {
164
+ cdIfPresent(path);
165
+ return sh.exec('git config --get remote.origin.url', getShellConfig()).stdout.trim();
166
+ }
167
+
168
+ function cdIfPresent(path?: string): void {
169
+ if (path) {
170
+ sh.cd(path);
171
+ }
172
+ }
@@ -0,0 +1,57 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ export interface Logger extends Pick<Console, LogLevel> {
18
+ newLine(): void;
19
+ }
20
+
21
+ export type LogLevel = 'info' | 'debug' | 'error' | 'warn';
22
+
23
+ const levels: Record<LogLevel, { threshold: number; color: string }> = {
24
+ error: { threshold: 0, color: '\x1b[31m' }, // red
25
+ warn: { threshold: 1, color: '\x1b[33m' }, // yellow
26
+ info: { threshold: 2, color: '\x1b[0m' }, // default terminal color
27
+ debug: { threshold: 3, color: '\x1b[32m' } // green
28
+ };
29
+
30
+ let levelThreshold: number = levels.info.threshold;
31
+
32
+ export const LOGGER: Logger = {
33
+ info: (...args) => log('info', ...args),
34
+ error: (...args) => log('error', ...args),
35
+ warn: (...args) => log('warn', ...args),
36
+ debug: (...args) => log('debug', ...args),
37
+ newLine: () => console.log('')
38
+ } as const;
39
+
40
+ function log(level: LogLevel, ...args: any[]): void {
41
+ const levelData = levels[level];
42
+ if (levelThreshold < levelData.threshold) {
43
+ return;
44
+ }
45
+ console[level](levelData.color, ...args, '\x1b[0m');
46
+ }
47
+
48
+ export function configureLogger(level: LogLevel): void;
49
+ export function configureLogger(verbose: boolean): void;
50
+ export function configureLogger(levelOrVerbose: LogLevel | boolean): void {
51
+ if (typeof levelOrVerbose === 'boolean') {
52
+ const level: LogLevel = levelOrVerbose ? 'debug' : 'info';
53
+ levelThreshold = levels[level].threshold;
54
+ } else {
55
+ levelThreshold = levels[levelOrVerbose].threshold;
56
+ }
57
+ }