Skip to content

Commit

Permalink
Sander/chores (#261)
Browse files Browse the repository at this point in the history
* small fixes

* fix(fix killserver on install bug): some small touchups
  • Loading branch information
SanderElias authored Jan 31, 2020
1 parent 1bd3fac commit 784c694
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 115 deletions.
121 changes: 12 additions & 109 deletions schematics/scully/src/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import {chain, Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-config';
import {Schema} from './schema';
import {scullyVersion, scullyComponentVersion} from './version-names';
import {NodePackageInstallTask, RunSchematicTask} from '@angular-devkit/schematics/tasks';
import {getSourceFile, getSrc} from '../utils/utils';
import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
import {addImportToModule, insertImport} from '@schematics/angular/utility/ast-utils';
import {InsertChange} from '@schematics/angular/utility/change';
import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';

import {getSourceFile, getSrc} from '../utils/utils';
import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-config';
import {Schema} from './schema';
import {scullyComponentVersion, scullyVersion} from './version-names';
export default (options: Schema): Rule => {
return chain([
addDependencies(),
// importHttpClientModule(options.project),
// addHttpClientModule(options.project),
checkAngularVersion(),
importScullyModule(options.project),
addScullyModule(options.project),
addPolyfill(options.project),
// injectIdleService(options.project),
runBlogSchematic(options),
runScullySchematic(options),
addDependencies(),
]);
};
const addDependencies = () => (tree: Tree, context: SchematicContext) => {
addPackageToPackageJson(tree, '@scullyio/scully', `${scullyVersion}`);
const checkAngularVersion = () => (tree: Tree, context: SchematicContext) => {
const ngCoreVersionTag = getPackageVersionFromPackageJson(tree, '@angular/core');
if (+ngCoreVersionTag.search(/(\^7|~7|\^6|~6|\^5|~5|\^4|~4)/g) === 0) {
console.log('==============================================================');
Expand All @@ -33,6 +29,10 @@ const addDependencies = () => (tree: Tree, context: SchematicContext) => {
console.log('==============================================================');
process.exit(0);
}
};
const addDependencies = () => (tree: Tree, context: SchematicContext) => {
addPackageToPackageJson(tree, '@scullyio/scully', `${scullyVersion}`);
const ngCoreVersionTag = getPackageVersionFromPackageJson(tree, '@angular/core');
if (+ngCoreVersionTag.search(/(\^8|~8)/g) === 0) {
context.logger.info('Install ng-lib for Angular v8');
addPackageToPackageJson(tree, '@scullyio/ng-lib-v8', `${scullyComponentVersion}`);
Expand All @@ -42,53 +42,10 @@ const addDependencies = () => (tree: Tree, context: SchematicContext) => {
}
context.logger.info('✅️ Added dependency');
};
/*
const importHttpClientModule = (project: string) => (tree: Tree, context: SchematicContext) => {
try {
const mainFilePath = `./${getSrc(tree, project)}/app/app.module.ts`;
const recorder = tree.beginUpdate(mainFilePath);
const mainFileSource = getSourceFile(tree, mainFilePath);
const importChange = insertImport(
mainFileSource,
mainFilePath,
'HttpClientModule',
'@angular/common/http'
) as InsertChange;
if (importChange.toAdd) {
recorder.insertLeft(importChange.pos, importChange.toAdd);
}
tree.commitUpdate(recorder);
return tree;
} catch (e) {
context.logger.error('error into import httpclient', e);
}
};
const addHttpClientModule = (project: string) => (tree: Tree, context: SchematicContext) => {
const mainFilePath = `./${getSrc(tree, project)}/app/app.module.ts`;
const text = tree.read(mainFilePath);
if (text === null) {
throw new SchematicsException(`File ${mainFilePath} does not exist.`);
}
const sourceText = text.toString();
const source = ts.createSourceFile(mainFilePath, sourceText, ts.ScriptTarget.Latest, true);
const changes = addImportToModule(source, mainFilePath, 'HttpClientModule', '@angular/common/http');
const recorder = tree.beginUpdate(mainFilePath);
for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
tree.commitUpdate(recorder);
return tree;
};
*/
const importScullyModule = (project: string) => (tree: Tree, context: SchematicContext) => {
try {
const mainFilePath = `./${getSrc(tree, project)}/app/app.module.ts`;
const recorder = tree.beginUpdate(mainFilePath);

const mainFileSource = getSourceFile(tree, mainFilePath);
const importChange = insertImport(
mainFileSource,
Expand All @@ -105,7 +62,6 @@ const importScullyModule = (project: string) => (tree: Tree, context: SchematicC
context.logger.error('error into import SculyLib', e);
}
};

const addScullyModule = (project: string) => (tree: Tree, context: SchematicContext) => {
const mainFilePath = `./${getSrc(tree, project)}/app/app.module.ts`;
const text = tree.read(mainFilePath);
Expand All @@ -124,7 +80,6 @@ const addScullyModule = (project: string) => (tree: Tree, context: SchematicCont
tree.commitUpdate(recorder);
return tree;
};

const addPolyfill = (project: string) => (tree: Tree, context: SchematicContext) => {
let polyfills = tree.read(`${getSrc(tree, project)}/polyfills.ts`).toString();
if (polyfills.includes('SCULLY IMPORTS')) {
Expand All @@ -140,57 +95,6 @@ import 'zone.js/dist/task-tracking';`;
tree.overwrite(`${getSrc(tree, project)}/polyfills.ts`, polyfills);
}
};

// const injectIdleService = (project: string) => (tree: Tree, context: SchematicContext) => {
// try {
// const appComponentPath = `${getSrc(tree, project)}/app/app.component.ts`;
// const appComponent = tree.read(appComponentPath).toString();
// if (appComponent.includes('IdleMonitorService')) {
// context.logger.info(`⚠️️ Skipping ${appComponentPath}`);
// } else {
// const ngCoreVersionTag = getPackageVersionFromPackageJson(tree, '@angular/core');
// let v8 = '';
// if (+ngCoreVersionTag.search(/(\^8|~8)/g) === 0) {
// v8 = '-v8';
// }
// const idleImport = `import {IdleMonitorService} from '@scullyio/ng-lib${v8}';`;
// // add
// const idImport = `${idleImport}\n${appComponent}`;
// const idle = 'private idle: IdleMonitorService';
// let output = '';
// // check if exist
// if (idImport.search(/constructor/) === -1) {
// // add if no exist the constructor
// const add = ` \n constructor (${idle}) { } \n`;
// const position =
// idImport.search(/export class AppComponent {/g) + 'export class AppComponent {'.length;
// output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
// } else {
// const coma = haveMoreInjects(idImport);
// const add = `${idle}${coma}`;
// if (idImport.search(/constructor \(/) === -1) {
// const position = idImport.search(/constructor\(/g) + 'constructor('.length;
// output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
// } else {
// const position = idImport.search(/constructor \(/g) + 'constructor ('.length;
// output = [idImport.slice(0, position), add, idImport.slice(position)].join('');
// }
// }
// tree.overwrite(appComponentPath, output);
// }

// function haveMoreInjects(fullComponent: string) {
// const match = '(([^()]*(private|public)[^()]*))';
// if (fullComponent.search(match) !== -1) {
// return ',';
// }
// return '';
// }
// } catch (e) {
// context.logger.error('error in idle service');
// }
// };

const runBlogSchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const nextRules: Rule[] = [];
if (options.blog === true) {
Expand All @@ -200,7 +104,6 @@ const runBlogSchematic = (options: Schema) => (tree: Tree, context: SchematicCon
}
return chain(nextRules);
};

const runScullySchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const nextRules: Rule[] = [];
nextRules.push((host: Tree, ctx: SchematicContext) => {
Expand Down
3 changes: 0 additions & 3 deletions scully/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
"scully": "./scully.js"
},
"main": "/index.js",
"scripts": {
"postinstall": "npx scully killServer"
},
"dependencies": {
"asciidoctor.js": "^1.5.9",
"chalk": "2.4.2",
Expand Down
10 changes: 7 additions & 3 deletions scully/utils/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ export const startScully = (url?: string) => {
const duration = durations.Duration;
// tslint:disable-next-line:variable-name
const seconds = duration / 1000;
const routesProSecond = Math.ceil((numberOfRoutes / seconds) * 100) / 100;
const singleTime = duration / numberOfRoutes;
const routesProSecond = Math.ceil((1000 / singleTime) * 100) / 100;
log(`
Generating took ${yellow(Math.floor(seconds * 100) / 100)} seconds for ${yellow(numberOfRoutes)} pages:
That is ${yellow(routesProSecond)} pages per second,
or ${yellow(Math.ceil(singleTime))} milliseconds for each page.
Finding routes in the angular app took ${logSeconds(durations.Traverse)}
${
durations.Traverse
? `
Finding routes in the angular app took ${logSeconds(durations.Traverse)}`
: ''
}
Pulling in route-data took ${logSeconds(durations.Discovery)}
Rendering the pages took ${logSeconds(durations.Render)}
Expand Down

0 comments on commit 784c694

Please sign in to comment.