Skip to content

Commit

Permalink
feat(react): update workspace defaults based on app schematic option
Browse files Browse the repository at this point in the history
The defaults make it easier to generate other apps/libs/components later.
An option `skipWorkspaceJson` can be used to skip updates entirely.
  • Loading branch information
jaysoo committed Aug 15, 2019
1 parent 5b6da28 commit 566676e
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/api-cypress/schematics/cypress-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ng generate cypress-project ...

Type: `string`

A directory where the app is placed
A directory where the project is placed

### linter

Expand Down
8 changes: 8 additions & 0 deletions docs/api-react/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ Type: `boolean`

Skip formatting files

### skipWorkspaceJson

Default: `false`

Type: `boolean`

Skip updating workspace.json with default schematic options based on values provided to this app (e.g. babel, style)

### style

Default: `css`
Expand Down
29 changes: 29 additions & 0 deletions packages/react/src/schematics/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,33 @@ describe('app', () => {
expect(polyfillsSource).toContain('core-js');
});
});

describe('--skipWorkspaceJson', () => {
it('should update workspace with defaults when --skipWorkspaceJson=false', async () => {
const tree = await runSchematic(
'app',
{
name: 'myApp',
babel: true,
style: 'styled-components',
skipWorkspaceJson: false
},
appTree
);

const workspaceJson = readJsonInTree(tree, '/workspace.json');
expect(workspaceJson.schematics['@nrwl/react']).toMatchObject({
application: {
babel: true,
style: 'styled-components'
},
component: {
style: 'styled-components'
},
library: {
style: 'styled-components'
}
});
});
});
});
54 changes: 46 additions & 8 deletions packages/react/src/schematics/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, normalize, Path } from '@angular-devkit/core';
import { join, JsonObject, normalize, Path } from '@angular-devkit/core';
import {
apply,
chain,
Expand All @@ -14,29 +14,28 @@ import {
url
} from '@angular-devkit/schematics';
import {
addGlobal,
addLintFiles,
formatFiles,
generateProjectLint,
insert,
names,
NxJson,
offsetFromRoot,
toFileName,
updateJsonInTree,
generateProjectLint,
addLintFiles,
toPropertyName,
addGlobal
updateWorkspace
} from '@nrwl/workspace';
import {
addDepsToPackageJson,
insertImport,
updateWorkspaceInTree
} from '@nrwl/workspace/src/utils/ast-utils';
import ngAdd from '../ng-add/ng-add';
import * as ts from 'typescript';

import { Schema } from './schema';
import { CSS_IN_JS_DEPENDENCIES } from '../../utils/styled';
import { addRoute, addInitialRoutes } from '../../utils/ast-utils';
import { addInitialRoutes } from '../../utils/ast-utils';
import {
babelCoreVersion,
babelLoaderVersion,
Expand All @@ -49,7 +48,6 @@ import {
reactRouterVersion,
regeneratorVersion
} from '../../utils/versions';
import { addImportToModule } from '../../../../angular/src/utils/ast-utils';
import { assertValidStyle } from '../../utils/assertion';

interface NormalizedSchema extends Schema {
Expand Down Expand Up @@ -93,6 +91,7 @@ export default function(schema: Schema): Rule {
addStyledModuleDependencies(options),
addRouting(options, context),
addBabel(options),
setDefaults(options),
formatFiles(options)
]);
};
Expand Down Expand Up @@ -301,6 +300,45 @@ import 'regenerator-runtime/runtime';
};
}

function setDefaults(options: NormalizedSchema): Rule {
return options.skipWorkspaceJson
? noop()
: updateWorkspace(workspace => {
workspace.extensions.schematics = jsonIdentity(
workspace.extensions.schematics || {}
);
workspace.extensions.schematics['@nrwl/react'] =
workspace.extensions.schematics['@nrwl/react'] || {};
const prev = jsonIdentity(
workspace.extensions.schematics['@nrwl/react']
);

workspace.extensions.schematics = {
...workspace.extensions.schematics,
'@nrwl/react': {
...prev,
application: {
babel: options.babel,
style: options.style,
...jsonIdentity(prev.application)
},
component: {
style: options.style,
...jsonIdentity(prev.component)
},
library: {
style: options.style,
...jsonIdentity(prev.library)
}
}
};
});
}

function jsonIdentity(x: any): JsonObject {
return x as JsonObject;
}

function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const appDirectory = options.directory
? `${toFileName(options.directory)}/${toFileName(options.name)}`
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/schematics/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface Schema {
classComponent?: boolean;
routing?: boolean;
babel?: boolean;
skipWorkspaceJson?: boolean;
}
5 changes: 5 additions & 0 deletions packages/react/src/schematics/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"type": "boolean",
"default": false
},
"skipWorkspaceJson": {
"description": "Skip updating workspace.json with default schematic options based on values provided to this app (e.g. babel, style)",
"type": "boolean",
"default": false
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/schematics/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ describe('lib', () => {
{
name: 'myLib2',
directory: 'myDir',
tags: 'one,two',
simpleModuleName: true
tags: 'one,two'
},
tree
);
Expand Down
7 changes: 2 additions & 5 deletions packages/react/src/schematics/ng-add/ng-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('ng-add', () => {
const result = await runSchematic('ng-add', {}, tree);
const workspaceJson = readJsonInTree(result, 'workspace.json');
expect(workspaceJson.cli.defaultCollection).toEqual('@nrwl/react');
expect(workspaceJson.schematics['@nrwl/react:application'].babel).toBe(
expect(workspaceJson.schematics['@nrwl/react'].application.babel).toBe(
true
);
});
Expand All @@ -48,7 +48,7 @@ describe('ng-add', () => {
const result = await runSchematic('ng-add', {}, tree);
const workspaceJson = readJsonInTree(result, 'workspace.json');
expect(workspaceJson.cli.defaultCollection).toEqual('@nrwl/react');
expect(workspaceJson.schematics['@nrwl/react:application'].babel).toBe(
expect(workspaceJson.schematics['@nrwl/react'].application.babel).toBe(
true
);
});
Expand All @@ -69,9 +69,6 @@ describe('ng-add', () => {
const result = await runSchematic('ng-add', {}, tree);
const workspaceJson = readJsonInTree(result, 'workspace.json');
expect(workspaceJson.cli.defaultCollection).toEqual('@nrwl/angular');
expect(
workspaceJson.schematics['@nrwl/react:application']
).not.toBeDefined();
});
});
});
27 changes: 18 additions & 9 deletions packages/react/src/schematics/ng-add/ng-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,38 @@ function moveDependency(): Rule {

function setDefault(): Rule {
return updateWorkspace(workspace => {
// Set workspace default collection to 'react' if not already set.
workspace.extensions.cli = workspace.extensions.cli || {};

const defaultCollection: string =
workspace.extensions.cli &&
((workspace.extensions.cli as JsonObject).defaultCollection as string);

if (!defaultCollection || defaultCollection === '@nrwl/workspace') {
(workspace.extensions.cli as JsonObject).defaultCollection =
'@nrwl/react';
}

// Also generate apps with babel option by default.
workspace.extensions.schematics = {
...(workspace.extensions.schematics
? (workspace.extensions.schematics as JsonObject)
: {}),
'@nrwl/react:application': {
// Also generate all new react apps with babel.
workspace.extensions.schematics =
jsonIdentity(workspace.extensions.schematics) || {};
const reactSchematics =
jsonIdentity(workspace.extensions.schematics['@nrwl/react']) || {};
workspace.extensions.schematics = {
...workspace.extensions.schematics,
'@nrwl/react': {
application: {
...jsonIdentity(reactSchematics.application),
babel: true
}
};
}
}
};
});
}

function jsonIdentity(x: any): JsonObject {
return x as JsonObject;
}

export default function(schema: Schema) {
return chain([
setDefault(),
Expand Down

0 comments on commit 566676e

Please sign in to comment.