Skip to content

Commit

Permalink
feat(component-store): add ng-add and ng-update schematics (#2598)
Browse files Browse the repository at this point in the history
Closes #2569
  • Loading branch information
timdeschryver authored Jul 2, 2020
1 parent 60cd5cc commit af7b2cc
Show file tree
Hide file tree
Showing 34 changed files with 2,373 additions and 9 deletions.
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@
"commands": [
{
"command": "ng run component-store:build-package"
},
{
"command": "yarn tsc -p modules/component-store/tsconfig.schematics.json"
}
]
}
Expand Down
31 changes: 31 additions & 0 deletions modules/component-store/migrations/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("//tools:defaults.bzl", "pkg_npm", "ts_library")

package(default_visibility = ["//visibility:public"])

ts_library(
name = "migrations",
srcs = glob(
[
"**/*.ts",
],
exclude = [
"**/testing/*.ts",
"**/*.spec.ts",
],
),
module_name = "@ngrx/component-store/migrations",
deps = [
"//modules/component-store/schematics-core",
"@npm//@angular-devkit/schematics",
],
)

pkg_npm(
name = "npm_package",
srcs = [
":migration.json",
],
deps = [
":migrations",
],
)
4 changes: 4 additions & 0 deletions modules/component-store/migrations/migration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {}
}
15 changes: 14 additions & 1 deletion modules/component-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,18 @@
"@angular/core": "^10.0.0",
"rxjs": "^6.5.3"
},
"sideEffects": false
"sideEffects": false,
"ng-update": {
"packageGroup": [
"@ngrx/store",
"@ngrx/effects",
"@ngrx/entity",
"@ngrx/router-store",
"@ngrx/data",
"@ngrx/schematics",
"@ngrx/store-devtools",
"@ngrx/component-store"
],
"migrations": "./migrations/migration.json"
}
}
92 changes: 92 additions & 0 deletions modules/component-store/schematics-core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {
dasherize,
decamelize,
camelize,
classify,
underscore,
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export { isIvyEnabled } from './utility/angular-utils';

export {
findNodes,
getSourceNodes,
getDecoratorMetadata,
getContentOfKeyLiteral,
insertAfterLastOccurrence,
insertImport,
addBootstrapToModule,
addDeclarationToModule,
addExportToModule,
addImportToModule,
addProviderToModule,
replaceImport,
containsProperty,
} from './utility/ast-utils';

export {
Host,
Change,
NoopChange,
InsertChange,
RemoveChange,
ReplaceChange,
createReplaceChange,
createChangeRecorder,
commitChanges,
} from './utility/change';

export { AppConfig, getWorkspace, getWorkspacePath } from './utility/config';

export {
findModule,
findModuleFromOptions,
buildRelativePath,
ModuleOptions,
} from './utility/find-module';

export { findPropertyInAstObject } from './utility/json-utilts';

export {
addReducerToState,
addReducerToStateInterface,
addReducerImportToNgModule,
addReducerToActionReducerMap,
omit,
} from './utility/ngrx-utils';

export { getProjectPath, getProject, isLib } from './utility/project';

export const stringUtils = {
dasherize,
decamelize,
camelize,
classify,
underscore,
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';

export { parseName } from './utility/parse-name';

export { addPackageToPackageJson } from './utility/package';

export { platformVersion } from './utility/libs-version';

export {
visitTSSourceFiles,
visitNgModuleImports,
visitNgModuleExports,
visitComponents,
visitDecorator,
visitNgModules,
visitTemplates,
} from './utility/visitors';
50 changes: 50 additions & 0 deletions modules/component-store/schematics-core/utility/angular-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
JsonParseMode,
dirname,
normalize,
parseJsonAst,
resolve,
} from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from './json-utilts';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/migrations/update-9/utils.ts
export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {
// In version 9, Ivy is turned on by default
// Ivy is opted out only when 'enableIvy' is set to false.

const buffer = tree.read(tsConfigPath);
if (!buffer) {
return true;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);

if (tsCfgAst.kind !== 'object') {
return true;
}

const ngCompilerOptions = findPropertyInAstObject(
tsCfgAst,
'angularCompilerOptions'
);
if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy');

if (enableIvy) {
return !!enableIvy.value;
}
}

const configExtends = findPropertyInAstObject(tsCfgAst, 'extends');
if (configExtends && configExtends.kind === 'string') {
const extendedTsConfigPath = resolve(
dirname(normalize(tsConfigPath)),
normalize(configExtends.value)
);

return isIvyEnabled(tree, extendedTsConfigPath);
}

return true;
}
Loading

0 comments on commit af7b2cc

Please sign in to comment.