Skip to content

Commit

Permalink
Schematics/multiproject (#227)
Browse files Browse the repository at this point in the history
* schematics(project): add option project for support multi-projects apps

* schematics(projects): move to configure the project by default

* schematics(projects): change creation of scully.config.js

* schematics(projects): fix name of projectName

* schematics(projects): change options for support project into npm run scully

* Schematics(test): update test for support project

* schematics(project): fix missing project function

* feat(schematics): add project option for support multiple project

* improvement(scully): rewrite the scully.config.js everytime with the project name

* feat(scully): check project option exist in angular.json
  • Loading branch information
jorgeucano authored and SanderElias committed Jan 24, 2020
1 parent 965ef93 commit 641ca23
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 81 deletions.
Binary file added schematics/scully/scullyio-init-0.0.17.tgz
Binary file not shown.
1 change: 1 addition & 0 deletions schematics/scully/src/add-blog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function(options: Schema): Rule {
slug: 'slug',
sourceDir: 'blog',
route: 'blog',
project: options.project,
};

if (options.routingScope) {
Expand Down
5 changes: 5 additions & 0 deletions schematics/scully/src/add-blog/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"type": "string",
"description": "The scope for the new routing module.",
"default": "Child"
},
"project": {
"type": "string",
"description": "add the project",
"default": "defaultProject"
}
},
"required": []
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/add-blog/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface Schema {
* The scope for the new routing module.
*/
routingScope?: 'Child' | 'Root';
project?: string;
}
9 changes: 5 additions & 4 deletions schematics/scully/src/add-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {Rule, Tree, url, applyTemplates, move, chain, SchematicContext} from '@angular-devkit/schematics';
import {strings, normalize} from '@angular-devkit/core';
import {Schema} from './schema';
import {applyWithOverwrite, getRoot} from '../utils/utils';
import {applyWithOverwrite, getRoot, getScullyConfig} from '../utils/utils';

export default (options: Schema): Rule => {
return chain([addPlugin(options), registerPlugin(options)]);
};

const addPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const sourceRoot = getRoot(tree);
const sourceRoot = getRoot(tree, options.project);
const pathName = strings.dasherize(`${sourceRoot}/scullyPlugins/${options.name}.js`);
return applyWithOverwrite(url('../files/add-plugin'), [
applyTemplates({
Expand All @@ -22,7 +22,8 @@ const addPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) =
};

const registerPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) => {
let scullyConfig = tree.read(`${getRoot(tree)}/scully.config.js`).toString();
const scullyConfigFile = getScullyConfig(tree, options.project);
let scullyConfig = tree.read(`${getRoot(tree, options.project)}/${scullyConfigFile}`).toString();
scullyConfig = `require('./scullyPlugins/extra-plugin.js');\n${scullyConfig}`;
tree.overwrite(`${getRoot(tree)}/scully.config.js`, scullyConfig);
tree.overwrite(`${getRoot(tree, options.project)}/${scullyConfigFile}`, scullyConfig);
};
5 changes: 5 additions & 0 deletions schematics/scully/src/add-plugin/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"type": "string",
"description": "add the name for the plugin",
"x-prompt": "What name do you want to use for the plugin?"
},
"project": {
"type": "string",
"description": "add the project",
"default": "defaultProject"
}
},
"required": ["name"]
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/add-plugin/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface Schema {
* add the name for the plugin
*/
name: string;
project: string;
}
21 changes: 9 additions & 12 deletions schematics/scully/src/create-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ import {
getSrc,
getFileContents,
toAscii,
getScullyConfig,
} from '../utils/utils';
import {RunSchematicTask} from '@angular-devkit/schematics/tasks';

const SCULLY_CONF_FILE = '/scully.config.js';
const ANGULAR_CONF_FILE = './angular.json';

export default (options: Schema): Rule => {
options.name = toAscii(options.name) || 'blog';
options.slug = toAscii(options.slug) || 'id';
options.route = toAscii(options.route) || options.name;
options.sourceDir = options.sourceDir || options.name;
return chain([
addPost(options),
updateScullyConfig(options),
addModule(options),
]);
return chain([addPost(options), updateScullyConfig(options), addModule(options)]);
};

const addPost = (options: Schema) => (tree: Tree, context: SchematicContext) => {
Expand All @@ -45,9 +41,10 @@ const addPost = (options: Schema) => (tree: Tree, context: SchematicContext) =>
};

const updateScullyConfig = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const scullyJs = getFileContents(tree, SCULLY_CONF_FILE);
const scullyConfigFile = getScullyConfig(tree, options.project);
const scullyJs = getFileContents(tree, scullyConfigFile);
if (!scullyJs) {
context.logger.error(`No scully configuration file found ${SCULLY_CONF_FILE}`);
context.logger.error(`No scully configuration file found ${scullyConfigFile}`);
}
const newScullyJs = addRouteToScullyConfig(scullyJs, {
name: options.name,
Expand All @@ -57,16 +54,16 @@ const updateScullyConfig = (options: Schema) => (tree: Tree, context: SchematicC
route: options.route,
});

tree.overwrite(SCULLY_CONF_FILE, newScullyJs);
context.logger.info(`✅️ Update ${SCULLY_CONF_FILE}`);
tree.overwrite(scullyConfigFile, newScullyJs);
context.logger.info(`✅️ Update ${scullyConfigFile}`);
};

const addModule = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const sourceDir = getSrc(tree);
const sourceDir = getSrc(tree, options.project);
const pathName = strings.dasherize(`${sourceDir}/app/${options.name}`);
let prefix = 'app';
if (tree.exists(ANGULAR_CONF_FILE)) {
prefix = getPrefix(getFileContents(tree, ANGULAR_CONF_FILE));
prefix = getPrefix(tree, getFileContents(tree, ANGULAR_CONF_FILE), options.project);
addRouteToModule(tree, options);
}

Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/create-markdown/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('create-markdown', () => {
const defaultOptions: Schema = {
name: '',
slug: '',
project: 'defaultProject',
};
let appTree: UnitTestTree;

Expand Down
5 changes: 5 additions & 0 deletions schematics/scully/src/create-markdown/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"type": "string",
"description": "define the route where your post will be available",
"x-prompt": "Under which route do you want your files to be requested?"
},
"project": {
"type": "string",
"description": "add the project",
"default": "defaultProject"
}
},
"required": ["name", "slug"]
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/create-markdown/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface Schema {
* define the route where your post will be available
*/
route?: string;
project: string;
}
26 changes: 13 additions & 13 deletions schematics/scully/src/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeSc
export default (options: Schema): Rule => {
return chain([
addDependencies(),
importHttpClientModule(),
addHttpClientModule(),
addPolyfill(),
injectIdleService(),
importHttpClientModule(options.project),
addHttpClientModule(options.project),
addPolyfill(options.project),
injectIdleService(options.project),
runBlogSchematic(options),
runScullySchematic(options),
]);
Expand All @@ -32,9 +32,9 @@ const addDependencies = () => (tree: Tree, context: SchematicContext) => {
context.logger.info('✅️ Added dependency');
};

const importHttpClientModule = () => (tree: Tree, context: SchematicContext) => {
const importHttpClientModule = (project: string) => (tree: Tree, context: SchematicContext) => {
try {
const mainFilePath = `./${getSrc(tree)}/app/app.module.ts`;
const mainFilePath = `./${getSrc(tree, project)}/app/app.module.ts`;
const recorder = tree.beginUpdate(mainFilePath);

const mainFileSource = getSourceFile(tree, mainFilePath);
Expand All @@ -54,8 +54,8 @@ const importHttpClientModule = () => (tree: Tree, context: SchematicContext) =>
}
};

const addHttpClientModule = () => (tree: Tree, context: SchematicContext) => {
const mainFilePath = `./${getSrc(tree)}/app/app.module.ts`;
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.`);
Expand All @@ -73,8 +73,8 @@ const addHttpClientModule = () => (tree: Tree, context: SchematicContext) => {
return tree;
};

const addPolyfill = () => (tree: Tree, context: SchematicContext) => {
let polyfills = tree.read(`${getSrc(tree)}/polyfills.ts`).toString();
const addPolyfill = (project: string) => (tree: Tree, context: SchematicContext) => {
let polyfills = tree.read(`${getSrc(tree, project)}/polyfills.ts`).toString();
if (polyfills.includes('SCULLY IMPORTS')) {
context.logger.info('⚠️ Skipping polyfills.ts');
} else {
Expand All @@ -85,13 +85,13 @@ const addPolyfill = () => (tree: Tree, context: SchematicContext) => {
*/
// tslint:disable-next-line: align
import 'zone.js/dist/task-tracking';`;
tree.overwrite(`${getSrc(tree)}/polyfills.ts`, polyfills);
tree.overwrite(`${getSrc(tree, project)}/polyfills.ts`, polyfills);
}
};

const injectIdleService = () => (tree: Tree, context: SchematicContext) => {
const injectIdleService = (project: string) => (tree: Tree, context: SchematicContext) => {
try {
const appComponentPath = `${getSrc(tree)}/app/app.component.ts`;
const appComponentPath = `${getSrc(tree, project)}/app/app.component.ts`;
const appComponent = tree.read(appComponentPath).toString();
if (appComponent.includes('IdleMonitorService')) {
context.logger.info(`⚠️️ Skipping ${appComponentPath}`);
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/ng-add/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('ng-add schematic', () => {
const project = 'foo';
const defaultOptions: Schema = {
blog: true,
project: 'defaultProject',
};
let appTree: UnitTestTree;

Expand Down
5 changes: 5 additions & 0 deletions schematics/scully/src/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"type": "boolean",
"description": "add full blog",
"default": false
},
"project": {
"type": "string",
"description": "Use a non default project from angular.json",
"default": "defaultProject"
}
},
"required": []
Expand Down
1 change: 1 addition & 0 deletions schematics/scully/src/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Schema {
blog: boolean;
project: string;
}
31 changes: 19 additions & 12 deletions schematics/scully/src/scully/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {Rule, SchematicContext, Tree, SchematicsException, chain} from '@angular-devkit/schematics';
import {getSrc, getPackageJson, overwritePackageJson} from '../utils/utils';

const SCULLY_CONFIG_FILE = './scully.config.js';
import {getSrc, getPackageJson, overwritePackageJson, getProject, checkProjectExist} from '../utils/utils';
import {Schema} from '../ng-add/schema';

export default (options: any): Rule => {
return chain([verifyAngularWorkspace(), modifyPackageJson(), createScullyConfig()]);
return chain([verifyAngularWorkspace(), modifyPackageJson(options), createScullyConfig(options)]);
};

const verifyAngularWorkspace = () => (tree: Tree, context: SchematicContext) => {
Expand All @@ -14,26 +13,34 @@ const verifyAngularWorkspace = () => (tree: Tree, context: SchematicContext) =>
}
};

const modifyPackageJson = () => (tree: Tree, context: SchematicContext) => {
const modifyPackageJson = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const jsonContent = getPackageJson(tree);
jsonContent.scripts.scully = 'scully';
jsonContent.scripts['scully:serve'] = 'scully serve';
const projectName = options.project === 'defaultProject' ? '' : getProject(tree, options.project);
jsonContent.scripts.scully = projectName === '' ? 'scully' : `scully --projectName=${projectName}`;
jsonContent.scripts['scully:serve'] =
projectName === '' ? 'scully serve' : `scully serve --projectName=${projectName}`;
overwritePackageJson(tree, jsonContent);
context.logger.info('✅️ Update package.json');
};

const createScullyConfig = () => (tree: Tree, context: SchematicContext) => {
if (!tree.exists(SCULLY_CONFIG_FILE)) {
const srcFolder = getSrc(tree);
const createScullyConfig = (options: Schema) => (tree: Tree, context: SchematicContext) => {
const scullyConfigFile = `scully.${getProject(tree, options.project)}.config.js`;
if (!checkProjectExist(tree, getProject(tree, options.project))) {
throw new SchematicsException(`There is no ${options.project} project in angular.json`);
}
if (!tree.exists(scullyConfigFile)) {
const srcFolder = getSrc(tree, options.project);
const projectName = getProject(tree, options.project);
tree.create(
SCULLY_CONFIG_FILE,
scullyConfigFile,
`exports.config = {
projectRoot: "./${srcFolder}/app",
projectName: "${projectName}",
outDir: './dist/static',
routes: {
}
};`
);
context.logger.info(`✅️ Created scully configuration file in ${SCULLY_CONFIG_FILE}`);
context.logger.info(`✅️ Created scully configuration file in ${scullyConfigFile}`);
}
};
Loading

0 comments on commit 641ca23

Please sign in to comment.