Skip to content

Commit

Permalink
fixup! fix(localize): install @angular/localize in `devDependencies…
Browse files Browse the repository at this point in the history
…` by default
  • Loading branch information
petebacondarwin committed Sep 2, 2020
1 parent 7bae350 commit 4ad5b3b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
5 changes: 4 additions & 1 deletion packages/localize/package.json
Expand Up @@ -16,6 +16,9 @@
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
},
"ng-add": {
"save": "devDependencies"
},
"sideEffects": [
"**/init/index.js",
"**/init.js",
Expand All @@ -36,4 +39,4 @@
"publishConfig": {
"registry": "https://wombat-dressing-room.appspot.com"
}
}
}
41 changes: 20 additions & 21 deletions packages/localize/schematics/ng-add/index.ts
Expand Up @@ -9,7 +9,7 @@
*/

import {virtualFs, workspaces} from '@angular-devkit/core';
import {chain, Rule, SchematicsException, Tree} from '@angular-devkit/schematics';
import {chain, Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
import {addPackageJsonDependency, NodeDependencyType, removePackageJsonDependency} from '@schematics/angular/utility/dependencies';
import {getWorkspace} from '@schematics/angular/utility/workspace';
Expand Down Expand Up @@ -97,22 +97,24 @@ function prependToTargetFiles(
};
}

function moveToDependencyType(type: NodeDependencyType): Rule {
return (host, context) => {
debugger;
if (host.exists('package.json')) {
// Remove the previous dependency and add in a new one under the desired type.
removePackageJsonDependency(host, '@angular/localize');
addPackageJsonDependency(
host, {name: '@angular/localize', type, version: `^0.0.0-PLACEHOLDER`});

// Add a task to run the package manager. This is necessary because we updated
// "package.json" and we want lock files to reflect this.
context.addTask(new NodePackageInstallTask());
};
};
function moveToDependencies(host: Tree, context: SchematicContext) {
if (host.exists('package.json')) {
// Remove the previous dependency and add in a new one under the desired type.
removePackageJsonDependency(host, '@angular/localize');
addPackageJsonDependency(host, {
name: '@angular/localize',
type: NodeDependencyType.Default,
version: `~0.0.0-PLACEHOLDER`
});

// Add a task to run the package manager. This is necessary because we updated
// "package.json" and we want lock files to reflect this.
context.addTask(new NodePackageInstallTask());
}
}

function noop() {}

export default function(options: Schema): Rule {
return async (host: Tree) => {
if (!options.name) {
Expand All @@ -132,15 +134,12 @@ export default function(options: Schema): Rule {
${localizePolyfill}
`;

// If `$localize` will not be used at runtime then we can install `@angular/localize` as a
// devDependency.
const dependencyType =
options.useAtRuntime ? NodeDependencyType.Default : NodeDependencyType.Dev;

return chain([
prependToTargetFiles(project, Builders.Browser, 'polyfills', localizeStr),
prependToTargetFiles(project, Builders.Server, 'main', localizeStr),
moveToDependencyType(dependencyType),
// If `$localize` will be used at runtime then must install `@angular/localize`
// into `dependencies`, rather than the default of `devDependencies`.
options.useAtRuntime ? moveToDependencies : noop
]);
};
}
14 changes: 8 additions & 6 deletions packages/localize/schematics/ng-add/index_spec.ts
Expand Up @@ -33,11 +33,13 @@ export { renderModule, renderModuleFactory } from '@angular/platform-server';`;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
host.create('package.json', `{
"dependencies": {
"@angular/localize": "old-version"
host.create('package.json', JSON.stringify({
'devDependencies': {
// The default (according to `ng-add` in its package.json) is for `@angular/localize` to be
// saved to `devDependencies`.
'@angular/localize': '~0.0.0-PLACEHOLDER',
}
}`);
}));
host.create('src/polyfills.ts', polyfillsContent);
host.create('src/another-polyfills.ts', polyfillsContent);
host.create('src/unrelated-polyfills.ts', polyfillsContent);
Expand Down Expand Up @@ -176,7 +178,7 @@ export { renderModule, renderModuleFactory } from '@angular/platform-server';`;
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const packageJsonText = host.readContent('/package.json');
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize'])
.toBe('^0.0.0-PLACEHOLDER');
.toBe('~0.0.0-PLACEHOLDER');
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize']).toBeUndefined();
});

Expand All @@ -186,7 +188,7 @@ export { renderModule, renderModuleFactory } from '@angular/platform-server';`;
.toPromise();
const packageJsonText = host.readContent('/package.json');
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize'])
.toBe('^0.0.0-PLACEHOLDER');
.toBe('~0.0.0-PLACEHOLDER');
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize']).toBeUndefined();
});
});
4 changes: 2 additions & 2 deletions packages/localize/schematics/ng-add/schema.json
Expand Up @@ -13,8 +13,8 @@
},
"useAtRuntime": {
"type": "boolean",
"description": "If true then the dependency is included in the `dependencies` section of packge.json, rather than `devDependencies`.",
"x-prompt": "Will this project use $localize at runtime?"
"description": "If set then `@angular/localize` is included in the `dependencies` section of `package.json`, rather than `devDependencies`, which is the default.",
"default": false
}
},
"required": []
Expand Down

0 comments on commit 4ad5b3b

Please sign in to comment.