-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngupdate.service.ts
75 lines (62 loc) · 3.07 KB
/
ngupdate.service.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
import * as exec from '@actions/exec';
import * as core from '@actions/core';
import { ExecOptions } from '@actions/exec/lib/interfaces';
import { Helpers } from './helpers';
export class PackageToUpdate {
constructor(public name: string, public oldVersion: string, public newVersion: string) { }
}
export interface NgUpdateResult {
packages: PackageToUpdate[];
ngUpdateOutput: string;
ngUpdateErrorOutput?: string;
}
export class NgUpdateService {
public static readonly NO_UPDATE_FOUND = 'We analyzed your package.json and everything seems to be in order. Good work!';
public static readonly UPDATE_FOUND = 'We analyzed your package.json, there are some packages to update:';
constructor(
private projectPath: string,
private nodeModulesPath: string
) { }
public async runUpdate(): Promise<NgUpdateResult> {
let ngUpdateOutput = '';
let ngUpdateErrorOutput = '';
const ngUpdateOptions: ExecOptions = {
listeners: {
stdout: (data: Buffer) => ngUpdateOutput += data.toString(),
stderr: (data: Buffer) => ngUpdateErrorOutput = data.toString(),
},
cwd: this.projectPath,
};
const npmRegistry = core.getInput('npm-registry');
const ngUpdateArgs = npmRegistry ? [`registry=${npmRegistry}`] : [];
core.debug(`🤖 Ensuring NPM modules are installed under '${this.nodeModulesPath}'...`);
await Helpers.ensureNodeModules(this.nodeModulesPath, process.env.FORCE_INSTALL_NODE_MODULES === 'true');
core.debug(`🤖 Running initial 'ng update${ngUpdateArgs}'...`);
const ngExec = Helpers.getLocalNgExecPath(this.nodeModulesPath);
await exec.exec(`"${ngExec}"`, ['update', ...ngUpdateArgs], ngUpdateOptions);
if (ngUpdateOutput.indexOf(NgUpdateService.NO_UPDATE_FOUND) > 0) {
core.info('🤖 Congratulations 👏, you are already using the latest version of Angular!');
return { packages: [], ngUpdateOutput, ngUpdateErrorOutput };
} else if (ngUpdateOutput.indexOf(NgUpdateService.UPDATE_FOUND) > 0) {
const ngUpdateRegEx = /\s+([@\-/a-zA-Z0-9]+)\s+(\d+\.\d+\.\d+)\s+->\s+(\d+\.\d+\.\d+)\s+ng update/gm;
const pkgsToUpdate: PackageToUpdate[] = [];
for (let match: RegExpExecArray | null; (match = ngUpdateRegEx.exec(ngUpdateOutput));) {
pkgsToUpdate.push(new PackageToUpdate(match[1], match[2], match[3]));
}
if (pkgsToUpdate.length) {
core.info(`🤖 Updating outdated ng dependencies: ${pkgsToUpdate.map(p => `'${p.name}'`)}`);
const ngUpdatePkgsArgs = [...ngUpdateArgs, '--allow-dirty', ...(pkgsToUpdate.map(p => `${p.name}@${p.newVersion}`))];
const ngUpdatePkgsOptions: ExecOptions = {
cwd: this.projectPath,
};
await exec.exec(`"${ngExec}"`, ['update', ...ngUpdatePkgsArgs], ngUpdatePkgsOptions);
}
return { packages: pkgsToUpdate, ngUpdateOutput, ngUpdateErrorOutput };
}
if (ngUpdateErrorOutput.length) {
core.warning('🤖 It looks like the "ng update" command failed.');
core.warning(ngUpdateErrorOutput);
}
return { packages: [], ngUpdateOutput, ngUpdateErrorOutput };
}
}