-
Notifications
You must be signed in to change notification settings - Fork 24
/
analysisTool.ts
469 lines (431 loc) · 20.8 KB
/
analysisTool.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/**
* ngMigration Assistant scans an AngularJS application and recommends
* a particular migration path to take to Angular. It looks for good practices
* and anti-patterns to help determine which stage in migration the
* application is and which migration path is right for you.
*/
/**
* @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
*/
import * as fs from 'fs';
const nodesloc = require('node-sloc');
const gitignore = require('parse-gitignore');
const glob = require('glob');
export class AnalysisTool {
private CODE_LIMIT_MULTIPLIER: number = 1.25;
private VALUE_NOT_FOUND: number = -1;
analysisDetails = {
rewriteThreshold: 880, // 880 lines is considered 1 month's work of coding
angularElement: false,
scope: false,
rootScope: false,
compile: false,
uiRouter: false,
angularjsRouter: false,
angularRouter: false,
hasUnitTest: false,
usingAngular: false,
usingAngularJS: false,
totalFilesOrFolderCount: 0,
relevantFilesOrFolderCount: 0,
jsFileCount: 0,
tsFileCount: 0,
controllersCount: 0,
componentCount: 0,
linesOfCode: 0,
mapOfFilesToConvert: new Map()
};
/**
* Calls countLinesOfCode and waits for it to finish executing before calling
* the contained methods: runAnalysis(), runAntiPatternReport() which must
* finish executing before calling runRecommendation().
* @param rootpath user input original directory path
*/
constructor(rootpath: string) {
this.analysisDetails.mapOfFilesToConvert = new Map<String, Array<String>>();
setTimeout(() => { }, 1000);
this.countLinesOfCode(rootpath, this.buildPathIgnoringGlobs(rootpath))
.then(sourceLines => {
this.analysisDetails.linesOfCode = sourceLines;
this.runAnalysis(rootpath);
return this.runAntiPatternReport();
})
.catch(err => {
console.error("Error 1: ", err);
})
.then(report => {
console.log(this.runAppStatistics());
console.log(this.runRecommendation(report));
console.log("rewriteThreshold: " + Math.round(this.analysisDetails.rewriteThreshold) + ", sloc: " + this.analysisDetails.linesOfCode + "\n");
})
.catch(err => {
console.error("Error 2: ", err);
});
}
/**
* Builds a new filesystem by removing files matching the ignore globs using glob.
* Returns as an array of files and folders.
* @param rootPath original directory path
*/
buildPathIgnoringGlobs(rootpath: string) {
let ignoreGlobs = this.getGlobsFromGitignore(rootpath);
let filesWithoutIgnores = glob.sync("**", { ignore: ignoreGlobs, cwd: rootpath });
this.analysisDetails.totalFilesOrFolderCount = filesWithoutIgnores.length;
return filesWithoutIgnores;
}
/**
* Parses .gitignore file into an array of ignoreGlobs and appends default ignoreGlobs
* to the array. Filters out patterns starting with ! from the ignoreGlobs array
* because ! means to never ignore. Returns the globs to ignore.
* @param rootpath original directory path
*/
getGlobsFromGitignore(rootpath: string): string[] {
let allIgnoreGlobs: string[] = [];
let defaultIgnoreGlobs = [
'node_modules', 'node_modules/**', '**/node_modules', '**/node_modules/**',
'.git', '.git/**', '**/.git', '**/.git/**',
'tsconfig.json', 'tsconfig.json/**', '**/tsconfig.json', '**/tsconfig.json/**',
'e2e', 'e2e/**', '**/e2e', '**/e2e/**',
'jquery.js', 'jquery.js/**', '**/jquery.js', '**/jquery.js/**',
'angular.js', 'angular.js/**', '**/angular.js', '**/angular.js/**'
];
allIgnoreGlobs = [...gitignore(rootpath + "/.gitignore"), ...defaultIgnoreGlobs].filter((pattern) => {
return !pattern.startsWith("!");
});
return allIgnoreGlobs;
}
// /**
// * DO NOT DELETE - possible for future implementation!
// * Another way of parsing the .gitignore and building a new filesystem. Scans only
// * git repos and asks git for a list of files to check from the .gitignore file
// * within the given directory. Uses grep to ignore default ignore globs.
// */
// const exec = require('child_process').exec;
// buildPathIgnoringGlobs(path: string) {
// exec("cd \"" + path + "\" && git ls-tree -r master --name-only | egrep -v \".*(node_modules|e2e|.git|tsconfig.json).*\"", function (error: string, data: string) {
// let a = data.split("\n");
// a.pop();
// console.log(a);
// });
// }
/**
* Counts the source lines of code (sloc) using node-sloc to traverse through
* the passed in filesystem. Uses filtered filesystem as not count sloc
* in node_modules and other ignored files. Returns a promise that resolves to sloc.
* @param rootPath original directory path
* @param filteredFilePaths filtered filesystem buildPathIgnoringGlobs() returns
*/
async countLinesOfCode(rootPath: string, filteredFilePaths: string[]): Promise<any> {
return new Promise((resolve, reject) => {
const promisesINeedResolvedForMeToBeDone = [];
let lines: number = 0;
for (let file of filteredFilePaths) {
const options = {
path: rootPath + "/" + file,
extensions: ['js', 'ts', 'html'],
ignorePaths: ['node_modules'],
ignoreDefaut: false,
}
promisesINeedResolvedForMeToBeDone.push(
nodesloc(options)
.then((results: any) => {
lines += results.sloc.sloc || 0;
})
.catch((err: any) => {
console.log("Is there")
console.error(err);
reject(err);
})
);
}
Promise.all(promisesINeedResolvedForMeToBeDone).then(() => resolve(lines));
});
}
/**
* Traverses through filtered filesystem returned by buildPathIgnoringGlobs()
* and calls testFile() to run the individual tests.
* @param rootPath original directory path
*/
private runAnalysis(rootpath: string) {
const list = fs.readdirSync(rootpath);
let currentPath: string = "";
for (let fileOrFolder of this.buildPathIgnoringGlobs(rootpath)) {
currentPath = rootpath + "/" + fileOrFolder;
if (fs.lstatSync(currentPath).isFile()) {
this.testFile(currentPath);
}
}
}
/**
* Calls tests on each file in the filtered filesystem to looks for anti-patterns,
* Angular version, and additional practices.
* @param currentPath
*/
testFile(currentPath: string) {
let tests = [
(filename: string, data: string) => this.checkFileForScope(filename, data),
(filename: string, data: string) => this.checkFileForRootScope(filename, data),
(filename: string, data: string) => this.checkFileForCompile(filename, data),
(filename: string, data: string) => this.checkFileForAngularElement(filename, data),
(filename: string, data: string) => this.checkFileForRouter(filename, data),
(filename: string, data: string) => this.checkFileForUnitTests(filename, data),
(filename: string, data: string) => this.checkAngularVersion(filename, data),
(filename: string, data: string) => this.checkFileForScriptingLanguage(filename, data),
(filename: string, data: string) => this.checkFileForComponent(filename, data),
];
if (currentPath.substr(-3) === '.js' || this.fileHasTsExtension(currentPath) || currentPath.substr(-5) === '.html' || currentPath.substr(-5) === '.json') {
this.analysisDetails.relevantFilesOrFolderCount++;
for (let test of tests) {
try {
test(currentPath, fs.readFileSync(currentPath, "utf8"));
}
catch (error) {
console.error(`Error: Could not read ${currentPath}`);
}
}
}
}
fileHasTsExtension(filename: string): boolean {
return filename.endsWith('.ts') && !filename.endsWith('.d.ts');
}
checkFileForScope(filename: string, fileData: string) {
if(filename.substr(-7, 4) != 'spec' && !filename.includes('test')) {
if(fileData.match(/\$scope/) && fileData.match(/\.controller/)) {
this.analysisDetails.scope = true;
this.pushValueOnKey(this.analysisDetails.mapOfFilesToConvert, filename, "$scope");
}
}
}
checkFileForRootScope(filename: string, fileData: string) {
if (filename.substr(-7, 4) != 'spec' && !filename.includes('test')) {
if (fileData.match(/\$rootScope/)) {
this.analysisDetails.rootScope = true;
this.pushValueOnKey(this.analysisDetails.mapOfFilesToConvert, filename, " $rootScope");
}
}
}
checkFileForCompile(filename: string, fileData: string) {
if (filename.substr(-7, 4) != 'spec' && !filename.includes('test')) {
if (fileData.match(/compile\(/)) {
this.analysisDetails.compile = true;
this.pushValueOnKey(this.analysisDetails.mapOfFilesToConvert, filename, " $compile");
}
}
}
checkFileForAngularElement(filename: string, fileData: string) {
if (fileData.match(/NgElementConstructor/)) {
this.analysisDetails.angularElement = true;
}
}
checkFileForRouter(filename: string, fileData: string) {
if (fileData.match(/['"]ui\.router['"]/)) {
this.analysisDetails.uiRouter = true;
} else if (fileData.match(/['"]ngRoute['"]/)) {
this.analysisDetails.angularjsRouter = true;
} else if (fileData.match(/['"]\@angular\/router['"]/)) {
this.analysisDetails.angularRouter = true;
}
}
checkFileForUnitTests(filename: string, fileData: string) {
if (filename.substr(-7, 4) === 'spec' || filename.includes('test')) {
this.analysisDetails.hasUnitTest = true;
}
}
checkAngularVersion(filename: string, fileData: string) {
if (filename.substr(-12) === 'package.json' || filename.substr(-10) === 'bower.json') {
if (fileData.match(/\"\@angular\/core\"\:/) || fileData.match(/\"angular2\"\:/)) {
this.analysisDetails.usingAngular = true;
}
if (fileData.match(/\"angular\"\:/)) {
this.analysisDetails.usingAngularJS = true;
}
} else if (fileData.match(/https\:\/\/ajax\.googleapis\.com\/ajax\/libs\/angularjs/)) {
this.analysisDetails.usingAngularJS = true;
}
}
checkFileForScriptingLanguage(filename: string, fileData: string) {
if (filename.substr(-3) === '.js') {
this.analysisDetails.jsFileCount++;
this.pushValueOnKey(this.analysisDetails.mapOfFilesToConvert, filename, " JavaScript");
} else if (this.fileHasTsExtension(filename)) {
this.analysisDetails.tsFileCount++;
}
}
checkFileForComponent(filename: string, fileData: string) {
const controllerMatches = fileData.match(/\.controller\(/g);
const componentMatches = fileData.match(/component\(/g);
const decoratedComponentMatches = fileData.match(/@Component\(/g);
if (controllerMatches) {
this.analysisDetails.controllersCount += controllerMatches.length;
this.pushValueOnKey(this.analysisDetails.mapOfFilesToConvert, filename, " controller");
}
if (componentMatches) {
this.analysisDetails.componentCount += componentMatches.length;
}
// AngularJS decorated component matches
if (decoratedComponentMatches && !fileData.includes('@angular/core')) {
this.analysisDetails.componentCount += decoratedComponentMatches.length;
}
}
pushValueOnKey(map: Map<any, any>, key: string, value: string) {
if (map.has(key)) {
let values = map.get(key);
values.push(value);
map.set(key, values);
} else {
let newValuesArray: string[] = [value];
map.set(key, newValuesArray);
}
}
/**
* Creates the anti-pattern report and calculates the rewriteThreshold runRecommendation() uses.
* Each time an anti-pattern is found, general instructions and files needing corrections
* are appended to the preparation report. Returns a promise that resolves to the preparation report.
*/
private runAntiPatternReport(): Promise<any> {
let preparationReport = "";
if (this.analysisDetails.scope) {
preparationReport += "\n * App controller contains $scope, please refactor to remove $scope dependancy.";
this.analysisDetails.rewriteThreshold *= this.CODE_LIMIT_MULTIPLIER;
}
if (this.analysisDetails.rootScope) {
preparationReport += "\n * App contains $rootScope, please refactor rootScope into services.";
this.analysisDetails.rewriteThreshold *= this.CODE_LIMIT_MULTIPLIER;
}
if (this.analysisDetails.compile) {
preparationReport += "\n * App contains $compile, please rewrite compile to eliminate dynamic feature of templates.";
this.analysisDetails.rewriteThreshold *= this.CODE_LIMIT_MULTIPLIER;
}
if (!this.analysisDetails.hasUnitTest) {
preparationReport += "\n * App does not contain unit tests, please include unit tests.";
this.analysisDetails.rewriteThreshold *= this.CODE_LIMIT_MULTIPLIER;
}
if (this.analysisDetails.jsFileCount > 0) {
preparationReport += "\n * App contains " + this.analysisDetails.jsFileCount + " JavaScript files that need to be converted to TypeScript."
+ "\n To learn more, visit https://angular.io/guide/upgrade#migrating-to-typescript";
this.analysisDetails.rewriteThreshold *= this.CODE_LIMIT_MULTIPLIER;
}
if (this.analysisDetails.controllersCount > 0) {
preparationReport += "\n * App contains " + this.analysisDetails.controllersCount
+ " controllers that need to be converted to AngularJS components.\n To learn more, visit https://docs.angularjs.org/guide/component";
this.analysisDetails.rewriteThreshold *= this.CODE_LIMIT_MULTIPLIER;
}
if (this.analysisDetails.mapOfFilesToConvert.size > 0) {
preparationReport += "\x1b[1m\n\nFiles that contain AngularJS patterns and need to be modified:\x1b[0m";
let index = 1;
for (let key of this.analysisDetails.mapOfFilesToConvert.keys()) {
preparationReport += "\n" + index++ + ". " + key + " --> Patterns found: " + this.analysisDetails.mapOfFilesToConvert.get(key);
}
}
return Promise.resolve(preparationReport);
}
public runAppStatistics(): string {
let report = "\x1b[1m\nApp Statistics\x1b[0m";
if (this.analysisDetails.controllersCount > 0
|| this.analysisDetails.componentCount > 0
|| this.analysisDetails.jsFileCount > 0
|| this.analysisDetails.tsFileCount > 0) {
report += "\n * Complexity: " + this.analysisDetails.controllersCount + " controllers, "
+ this.analysisDetails.componentCount + " AngularJS components, " +
+ this.analysisDetails.jsFileCount + " JavaScript files, and "
+ this.analysisDetails.tsFileCount + " Typescript files.";
}
report += "\n * App size: " + this.analysisDetails.linesOfCode + " lines of code"
+ "\n * File Count: " + this.analysisDetails.totalFilesOrFolderCount + " total files/folders, "
+ this.analysisDetails.relevantFilesOrFolderCount + " relevant files/folders"
+ "\n * AngularJS Patterns: ";
if (this.analysisDetails.rootScope) {
report += " $rootScope, ";
}
if (this.analysisDetails.scope) {
report += " $scope, ";
}
if (this.analysisDetails.compile) {
report += " $compile, ";
}
if (!this.analysisDetails.hasUnitTest) {
report += " no unit tests, ";
}
if (this.analysisDetails.jsFileCount > 0) {
report += " JavaScript, ";
}
if (this.analysisDetails.controllersCount > 0) {
report += " .controller, ";
}
if (report.endsWith(", ")) {
report = report.slice(0, -2);
} else if (report.endsWith(" * AngularJS Patterns: ")) {
report += "N/A";
}
return report;
}
/**
* Recommendation algorithm that checks type of application (AngularJS, Angular, or
* hybrid), checks if sloc is under the rewriteThreshold, and checks if passes the
* ngUpgrade requirements. Returns a recommendation with a preparation report only
* when needed.
* @param preparationReport preparation instructions for upgrading
*/
public runRecommendation(preparationReport: string): string {
let recommendation = "\x1b[1m\nYour Recommendation\n\x1b[0m";
if (this.typeOfApplication() == "angular") {
recommendation += "\x1b[34mThis is already an Angular application. You do not need to migrate.\n\x1b[0m";
return recommendation;
}
//Rewriting from scratch
if (this.analysisDetails.rewriteThreshold >= this.analysisDetails.linesOfCode) {
if (this.typeOfApplication() == "hybrid") {
recommendation += "\x1b[34mEven though you have already begun making a hybrid application with"
+ " both AngularJS and Angular, the simplest solution is to rewrite your application from scratch.\n\x1b[0m";;
} else {
recommendation += "\x1b[34mThe simplest solution is to rewrite your application from scratch.\n\x1b[0m";
}
if (!this.analysisDetails.hasUnitTest) {
recommendation += "Please include unit tests in your new Angular application.\n";
}
} else {
if (this.passesNgUpgradeRequirements()) {
recommendation += "\x1b[34mYou have passed the necessary requirements and can use ngUpgrade as your migration path.\n\x1b[0m";
if (this.analysisDetails.angularElement == true) {
recommendation += "Continue using Angular Elements for components.";
} else if (this.analysisDetails.uiRouter == true) {
recommendation += "Use the hybrid ui-router in addition.";
} else if (this.analysisDetails.angularjsRouter) {
recommendation += "Use the hyrbid AngularJS and Angular router in addition.";
}
} else {
if (this.typeOfApplication() == "hybrid") {
recommendation += "\x1b[34mEven though you have already begun making a hybrid application with"
+ " both AngularJS and Angular, your app does not pass the necessary requirements to use ngUpgrade.\n";
+ "Please follow these preparation steps in the files identified before migrating with ngUpgrade.\x1b[0m";
} else {
recommendation += "\x1b[34mPlease follow these preparation steps in the files identified before migrating with ngUpgrade.\x1b[0m";
}
recommendation += preparationReport + "\n";
}
}
return recommendation + "\x1b[34m\nHead to \x1b[1mngMigration-Forum to understand this migration approach.\n\x1b[1mhttps://github.com/angular/ngMigration-Forum/wiki\n\x1b[0m";
}
typeOfApplication(): string {
if (this.analysisDetails.usingAngular && this.analysisDetails.usingAngularJS) {
return "hybrid";
} else if (this.analysisDetails.usingAngular && !this.analysisDetails.usingAngularJS) {
return "angular"
}
return "angularjs";
}
passesNgUpgradeRequirements(): boolean {
if (this.analysisDetails.rootScope == false &&
this.analysisDetails.compile == false &&
this.analysisDetails.hasUnitTest == true &&
this.analysisDetails.jsFileCount == 0 &&
this.analysisDetails.controllersCount == 0) {
return true;
}
return false;
}
}