Skip to content

Commit 9d4f31b

Browse files
beemanjorgeucano
authored andcommitted
Fix ng-add to allow to be ran multiple times (#35)
1 parent 32e4baf commit 9d4f31b

1 file changed

Lines changed: 38 additions & 27 deletions

File tree

schematics/scully/src/ng-add/index.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,50 @@ export default function(options: Schema): Rule {
2525

2626
// add new polyfills
2727
// @ts-ignore
28-
let polyfills = (host.read('./src/polyfills.ts')).toString();
29-
polyfills = polyfills + `\n/***************************************************************************************************
30-
\n* SCULLY IMPORTS
31-
\n*/
32-
\n// tslint:disable-next-line: align \nimport 'zone.js/dist/task-tracking';`;
33-
host.overwrite('./src/polyfills.ts', polyfills);
28+
let polyfills = host.read('./src/polyfills.ts').toString();
29+
if (polyfills.includes('SCULLY IMPORTS')) {
30+
context.logger.info('⚠️️ Skipping polyfills.ts');
31+
} else {
32+
polyfills =
33+
polyfills +
34+
`\n/***************************************************************************************************
35+
\n* SCULLY IMPORTS
36+
\n*/
37+
\n// tslint:disable-next-line: align \nimport 'zone.js/dist/task-tracking';`;
38+
host.overwrite('./src/polyfills.ts', polyfills);
39+
}
3440

3541
try {
36-
// inject iddleService
37-
const appComponent = (host.read('./src/app/app.component.ts')).toString();
38-
const iddleImport = 'import {IdleMonitorService} from \'@scullyio/ng-lib\';';
39-
// add
40-
const idImport = `${iddleImport} \n ${appComponent}`;
41-
const iddle = 'private idle: IdleMonitorService';
42-
let output = '';
43-
// check if exist
44-
if (idImport.search(/constructor/).toString() === '-1') {
45-
// add if no exist the constructor
46-
const add = ` \n constructor (${iddle}) { } \n`;
47-
const position = idImport.search(/export class AppComponent {/g) + 'export class AppComponent {'.length;
48-
output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
42+
// inject idleService
43+
const appComponent = host.read('./src/app/app.component.ts').toString();
44+
if (appComponent.includes('IdleMonitorService')) {
45+
context.logger.info('⚠️️ Skipping ./src/app/app.component.ts');
4946
} else {
50-
const coma = haveMoreInjects(idImport);
51-
const add = `${iddle}${coma}`;
52-
if (idImport.search(/constructor \(/).toString() === '-1') {
53-
const position = idImport.search(/constructor\(/g) + 'constructor('.length;
47+
const idleImport = "import {IdleMonitorService} from '@scullyio/ng-lib';";
48+
// add
49+
const idImport = `${idleImport} \n ${appComponent}`;
50+
const idle = 'private idle: IdleMonitorService';
51+
let output = '';
52+
// check if exist
53+
if (idImport.search(/constructor/).toString() === '-1') {
54+
// add if no exist the constructor
55+
const add = ` \n constructor (${idle}) { } \n`;
56+
const position =
57+
idImport.search(/export class AppComponent {/g) + 'export class AppComponent {'.length;
5458
output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
5559
} else {
56-
const position = idImport.search(/constructor \(/g) + 'constructor ('.length;
57-
output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
60+
const coma = haveMoreInjects(idImport);
61+
const add = `${idle}${coma}`;
62+
if (idImport.search(/constructor \(/).toString() === '-1') {
63+
const position = idImport.search(/constructor\(/g) + 'constructor('.length;
64+
output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
65+
} else {
66+
const position = idImport.search(/constructor \(/g) + 'constructor ('.length;
67+
output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
68+
}
5869
}
70+
host.overwrite('./src/app/app.component.ts', output);
5971
}
60-
host.overwrite('./src/app/app.component.ts', output);
6172

6273
function haveMoreInjects(fullComponent: string) {
6374
const match = '\(([^()]*(private|public)[^()]*)\)';
@@ -70,7 +81,7 @@ export default function(options: Schema): Rule {
7081

7182

7283
} catch (e) {
73-
console.log('error in iddle service');
84+
console.log('error in idle service');
7485
}
7586

7687

0 commit comments

Comments
 (0)