cleanup(misc): refactor how installs are added during schematics (#2788)
This commit is contained in:
parent
8549b84f6a
commit
efae1694cc
@ -4,9 +4,9 @@ import {
|
|||||||
readJsonInTree,
|
readJsonInTree,
|
||||||
formatFiles,
|
formatFiles,
|
||||||
updateJsonInTree,
|
updateJsonInTree,
|
||||||
checkAndCleanWithSemver
|
checkAndCleanWithSemver,
|
||||||
|
addInstallTask
|
||||||
} from '@nrwl/workspace';
|
} from '@nrwl/workspace';
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
|
||||||
import { gt } from 'semver';
|
import { gt } from 'semver';
|
||||||
|
|
||||||
function updateCLI() {
|
function updateCLI() {
|
||||||
@ -39,9 +39,7 @@ function updateCLI() {
|
|||||||
|
|
||||||
return json;
|
return json;
|
||||||
}),
|
}),
|
||||||
(host, context) => {
|
addInstallTask()
|
||||||
tasks.push(context.addTask(new NodePackageInstallTask()));
|
|
||||||
}
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return { rule, tasks };
|
return { rule, tasks };
|
||||||
|
|||||||
@ -4,9 +4,9 @@ import {
|
|||||||
readJsonInTree,
|
readJsonInTree,
|
||||||
formatFiles,
|
formatFiles,
|
||||||
updateJsonInTree,
|
updateJsonInTree,
|
||||||
checkAndCleanWithSemver
|
checkAndCleanWithSemver,
|
||||||
|
addInstallTask
|
||||||
} from '@nrwl/workspace';
|
} from '@nrwl/workspace';
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
|
||||||
import { gt } from 'semver';
|
import { gt } from 'semver';
|
||||||
|
|
||||||
const updateAngular = addUpdateTask('@angular/core', '8.2.4');
|
const updateAngular = addUpdateTask('@angular/core', '8.2.4');
|
||||||
@ -41,9 +41,7 @@ function updateCLI() {
|
|||||||
|
|
||||||
return json;
|
return json;
|
||||||
}),
|
}),
|
||||||
(host, context) => {
|
addInstallTask()
|
||||||
tasks.push(context.addTask(new NodePackageInstallTask()));
|
|
||||||
}
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return { rule, tasks };
|
return { rule, tasks };
|
||||||
|
|||||||
@ -63,6 +63,7 @@ export { getWorkspace, updateWorkspace } from './src/utils/workspace';
|
|||||||
export { addUpdateTask } from './src/utils/update-task';
|
export { addUpdateTask } from './src/utils/update-task';
|
||||||
export { addLintFiles, generateProjectLint, Linter } from './src/utils/lint';
|
export { addLintFiles, generateProjectLint, Linter } from './src/utils/lint';
|
||||||
|
|
||||||
|
export { addInstallTask } from './src/utils/rules/add-install-task';
|
||||||
export { formatFiles } from './src/utils/rules/format-files';
|
export { formatFiles } from './src/utils/rules/format-files';
|
||||||
export { deleteFile } from './src/utils/rules/deleteFile';
|
export { deleteFile } from './src/utils/rules/deleteFile';
|
||||||
export * from './src/utils/rules/ng-add';
|
export * from './src/utils/rules/ng-add';
|
||||||
|
|||||||
@ -1,12 +1,7 @@
|
|||||||
import { chain, Rule, SchematicContext } from '@angular-devkit/schematics';
|
import { chain, Rule } from '@angular-devkit/schematics';
|
||||||
import { formatFiles } from '@nrwl/workspace/src/utils/rules/format-files';
|
import { formatFiles } from '@nrwl/workspace/src/utils/rules/format-files';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { updatePackagesInPackageJson } from '../../utils/update-packages-in-package-json';
|
import { updatePackagesInPackageJson } from '../../utils/update-packages-in-package-json';
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
|
||||||
|
|
||||||
const addInstall = (_: any, context: SchematicContext) => {
|
|
||||||
context.addTask(new NodePackageInstallTask());
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function(): Rule {
|
export default function(): Rule {
|
||||||
return chain([
|
return chain([
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { formatFiles, updateJsonInTree } from '@nrwl/workspace';
|
import { addInstallTask, formatFiles, updateJsonInTree } from '@nrwl/workspace';
|
||||||
import { chain, SchematicContext } from '@angular-devkit/schematics';
|
import { chain } from '@angular-devkit/schematics';
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
|
||||||
|
|
||||||
const updateCypress = updateJsonInTree('package.json', json => {
|
const updateCypress = updateJsonInTree('package.json', json => {
|
||||||
json.devDependencies = json.devDependencies || {};
|
json.devDependencies = json.devDependencies || {};
|
||||||
@ -11,10 +10,6 @@ const updateCypress = updateJsonInTree('package.json', json => {
|
|||||||
return json;
|
return json;
|
||||||
});
|
});
|
||||||
|
|
||||||
const addInstall = (_: any, context: SchematicContext) => {
|
|
||||||
context.addTask(new NodePackageInstallTask());
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function() {
|
export default function() {
|
||||||
return chain([updateCypress, addInstall, formatFiles()]);
|
return chain([updateCypress, addInstallTask(), formatFiles()]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
import { updateJsonInTree, checkAndCleanWithSemver } from '@nrwl/workspace';
|
import {
|
||||||
import { chain, SchematicContext } from '@angular-devkit/schematics';
|
updateJsonInTree,
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
checkAndCleanWithSemver,
|
||||||
|
addInstallTask
|
||||||
|
} from '@nrwl/workspace';
|
||||||
|
import { chain } from '@angular-devkit/schematics';
|
||||||
import { gt } from 'semver';
|
import { gt } from 'semver';
|
||||||
|
|
||||||
const updateCLI = updateJsonInTree('package.json', json => {
|
const updateCLI = updateJsonInTree('package.json', json => {
|
||||||
@ -27,10 +30,6 @@ const updateCLI = updateJsonInTree('package.json', json => {
|
|||||||
return json;
|
return json;
|
||||||
});
|
});
|
||||||
|
|
||||||
const addInstall = (_: any, context: SchematicContext) => {
|
|
||||||
context.addTask(new NodePackageInstallTask());
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function() {
|
export default function() {
|
||||||
return chain([updateCLI, addInstall]);
|
return chain([updateCLI, addInstallTask()]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import {
|
|||||||
} from '../../utils/versions';
|
} from '../../utils/versions';
|
||||||
import { from } from 'rxjs';
|
import { from } from 'rxjs';
|
||||||
import { mapTo, tap } from 'rxjs/operators';
|
import { mapTo, tap } from 'rxjs/operators';
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
|
||||||
import {
|
import {
|
||||||
offsetFromRoot,
|
offsetFromRoot,
|
||||||
readJsonInTree,
|
readJsonInTree,
|
||||||
@ -28,7 +27,8 @@ import {
|
|||||||
updateJsonInTree,
|
updateJsonInTree,
|
||||||
getWorkspacePath,
|
getWorkspacePath,
|
||||||
renameSyncInTree,
|
renameSyncInTree,
|
||||||
renameDirSyncInTree
|
renameDirSyncInTree,
|
||||||
|
addInstallTask
|
||||||
} from '@nrwl/workspace';
|
} from '@nrwl/workspace';
|
||||||
import { DEFAULT_NRWL_PRETTIER_CONFIG } from '../workspace/workspace';
|
import { DEFAULT_NRWL_PRETTIER_CONFIG } from '../workspace/workspace';
|
||||||
import { JsonArray } from '@angular-devkit/core';
|
import { JsonArray } from '@angular-devkit/core';
|
||||||
@ -554,15 +554,6 @@ function checkCanConvertToWorkspace(options: Schema) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function addInstallTask(options: Schema) {
|
|
||||||
return (host: Tree, context: SchematicContext) => {
|
|
||||||
if (!options.skipInstall) {
|
|
||||||
context.addTask(new NodePackageInstallTask());
|
|
||||||
}
|
|
||||||
return host;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function(schema: Schema): Rule {
|
export default function(schema: Schema): Rule {
|
||||||
const options = {
|
const options = {
|
||||||
...schema,
|
...schema,
|
||||||
|
|||||||
@ -10,12 +10,12 @@ import {
|
|||||||
Tree,
|
Tree,
|
||||||
SchematicContext,
|
SchematicContext,
|
||||||
DirEntry,
|
DirEntry,
|
||||||
noop
|
noop,
|
||||||
|
chain
|
||||||
} from '@angular-devkit/schematics';
|
} from '@angular-devkit/schematics';
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import * as stripJsonComments from 'strip-json-comments';
|
import * as stripJsonComments from 'strip-json-comments';
|
||||||
import { serializeJson } from './fileutils';
|
import { serializeJson } from './fileutils';
|
||||||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
|
||||||
import { getWorkspacePath } from './cli-config-utils';
|
import { getWorkspacePath } from './cli-config-utils';
|
||||||
import {
|
import {
|
||||||
createProjectGraph,
|
createProjectGraph,
|
||||||
@ -24,10 +24,8 @@ import {
|
|||||||
} from '../core/project-graph';
|
} from '../core/project-graph';
|
||||||
import { FileData } from '../core/file-utils';
|
import { FileData } from '../core/file-utils';
|
||||||
import { extname, join, normalize, Path } from '@angular-devkit/core';
|
import { extname, join, normalize, Path } from '@angular-devkit/core';
|
||||||
import {
|
import { NxJson, NxJsonProjectConfig } from '../core/shared-interfaces';
|
||||||
NxJson,
|
import { addInstallTask } from './rules/add-install-task';
|
||||||
NxJsonProjectConfig
|
|
||||||
} from '@nrwl/workspace/src/core/shared-interfaces';
|
|
||||||
|
|
||||||
function nodesByPosition(first: ts.Node, second: ts.Node): number {
|
function nodesByPosition(first: ts.Node, second: ts.Node): number {
|
||||||
return first.getStart() - second.getStart();
|
return first.getStart() - second.getStart();
|
||||||
@ -561,8 +559,6 @@ function requiresAddingOfPackages(packageJsonFile, deps, devDeps): boolean {
|
|||||||
return needsDepsUpdate || needsDevDepsUpdate;
|
return needsDepsUpdate || needsDevDepsUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
let installAdded = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the package.json given the passed deps and/or devDeps. Only updates
|
* Updates the package.json given the passed deps and/or devDeps. Only updates
|
||||||
* if the packages are not yet present
|
* if the packages are not yet present
|
||||||
@ -581,9 +577,8 @@ export function addDepsToPackageJson(
|
|||||||
const currentPackageJson = readJsonInTree(host, 'package.json');
|
const currentPackageJson = readJsonInTree(host, 'package.json');
|
||||||
|
|
||||||
if (requiresAddingOfPackages(currentPackageJson, deps, devDeps)) {
|
if (requiresAddingOfPackages(currentPackageJson, deps, devDeps)) {
|
||||||
return updateJsonInTree(
|
return chain([
|
||||||
'package.json',
|
updateJsonInTree('package.json', (json, context: SchematicContext) => {
|
||||||
(json, context: SchematicContext) => {
|
|
||||||
json.dependencies = {
|
json.dependencies = {
|
||||||
...(json.dependencies || {}),
|
...(json.dependencies || {}),
|
||||||
...deps,
|
...deps,
|
||||||
@ -595,13 +590,12 @@ export function addDepsToPackageJson(
|
|||||||
...(json.devDependencies || {})
|
...(json.devDependencies || {})
|
||||||
};
|
};
|
||||||
|
|
||||||
if (addInstall && !installAdded) {
|
|
||||||
context.addTask(new NodePackageInstallTask());
|
|
||||||
installAdded = true;
|
|
||||||
}
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}),
|
||||||
);
|
addInstallTask({
|
||||||
|
skipInstall: !addInstall
|
||||||
|
})
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
return noop();
|
return noop();
|
||||||
}
|
}
|
||||||
@ -613,21 +607,22 @@ export function updatePackageJsonDependencies(
|
|||||||
devDeps: any,
|
devDeps: any,
|
||||||
addInstall = true
|
addInstall = true
|
||||||
): Rule {
|
): Rule {
|
||||||
return updateJsonInTree('package.json', (json, context: SchematicContext) => {
|
return chain([
|
||||||
json.dependencies = {
|
updateJsonInTree('package.json', (json, context: SchematicContext) => {
|
||||||
...(json.dependencies || {}),
|
json.dependencies = {
|
||||||
...deps
|
...(json.dependencies || {}),
|
||||||
};
|
...deps
|
||||||
json.devDependencies = {
|
};
|
||||||
...(json.devDependencies || {}),
|
json.devDependencies = {
|
||||||
...devDeps
|
...(json.devDependencies || {}),
|
||||||
};
|
...devDeps
|
||||||
if (addInstall && !installAdded) {
|
};
|
||||||
context.addTask(new NodePackageInstallTask());
|
return json;
|
||||||
installAdded = true;
|
}),
|
||||||
}
|
addInstallTask({
|
||||||
return json;
|
skipInstall: !addInstall
|
||||||
});
|
})
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getProjectConfig(host: Tree, name: string): any {
|
export function getProjectConfig(host: Tree, name: string): any {
|
||||||
|
|||||||
14
packages/workspace/src/utils/rules/add-install-task.ts
Normal file
14
packages/workspace/src/utils/rules/add-install-task.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { Rule } from '@angular-devkit/schematics';
|
||||||
|
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
|
||||||
|
|
||||||
|
let installAdded = false;
|
||||||
|
export function addInstallTask(
|
||||||
|
options: { skipInstall: boolean } = { skipInstall: false }
|
||||||
|
): Rule {
|
||||||
|
return (_, context) => {
|
||||||
|
if (!options.skipInstall && !installAdded) {
|
||||||
|
context.addTask(new NodePackageInstallTask());
|
||||||
|
installAdded = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,11 +1,14 @@
|
|||||||
|
import { chain, noop } from '@angular-devkit/schematics';
|
||||||
import { updateJsonInTree } from './ast-utils';
|
import { updateJsonInTree } from './ast-utils';
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { checkAndCleanWithSemver } from './version-utils';
|
import { checkAndCleanWithSemver } from './version-utils';
|
||||||
import { lt } from 'semver';
|
import { lt } from 'semver';
|
||||||
|
import { addInstallTask } from './rules/add-install-task';
|
||||||
|
|
||||||
export function updatePackagesInPackageJson(
|
export function updatePackagesInPackageJson(
|
||||||
migrationFilePath: string,
|
migrationFilePath: string,
|
||||||
versionName: string
|
versionName: string,
|
||||||
|
options: { skipInstall: boolean } = { skipInstall: false }
|
||||||
) {
|
) {
|
||||||
const migrations = JSON.parse(readFileSync(migrationFilePath).toString());
|
const migrations = JSON.parse(readFileSync(migrationFilePath).toString());
|
||||||
const packageJsonUpdates = migrations.packageJsonUpdates[versionName];
|
const packageJsonUpdates = migrations.packageJsonUpdates[versionName];
|
||||||
@ -16,40 +19,47 @@ export function updatePackagesInPackageJson(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatedPackages = packageJsonUpdates.packages;
|
const updatedPackages = packageJsonUpdates.packages;
|
||||||
return updateJsonInTree('package.json', json => {
|
let needsInstall = false;
|
||||||
Object.keys(updatedPackages).forEach(p => {
|
return chain([
|
||||||
/**
|
updateJsonInTree('package.json', json => {
|
||||||
* Check the updated version against semver
|
Object.keys(updatedPackages).forEach(p => {
|
||||||
*/
|
/**
|
||||||
const cleanUpdatedVersion = checkAndCleanWithSemver(
|
* Check the updated version against semver
|
||||||
p,
|
*/
|
||||||
updatedPackages[p].version
|
const cleanUpdatedVersion = checkAndCleanWithSemver(
|
||||||
);
|
|
||||||
|
|
||||||
if (json.devDependencies && json.devDependencies[p]) {
|
|
||||||
const cleanDevVersion = checkAndCleanWithSemver(
|
|
||||||
p,
|
p,
|
||||||
json.devDependencies[p]
|
updatedPackages[p].version
|
||||||
);
|
);
|
||||||
|
|
||||||
if (lt(cleanDevVersion, cleanUpdatedVersion)) {
|
if (json.devDependencies && json.devDependencies[p]) {
|
||||||
json.devDependencies[p] = updatedPackages[p].version;
|
const cleanDevVersion = checkAndCleanWithSemver(
|
||||||
}
|
p,
|
||||||
} else if (json.dependencies && json.dependencies[p]) {
|
json.devDependencies[p]
|
||||||
const cleanVersion = checkAndCleanWithSemver(p, json.dependencies[p]);
|
);
|
||||||
|
|
||||||
if (lt(cleanVersion, cleanUpdatedVersion)) {
|
if (lt(cleanDevVersion, cleanUpdatedVersion)) {
|
||||||
json.dependencies[p] = updatedPackages[p].version;
|
json.devDependencies[p] = updatedPackages[p].version;
|
||||||
}
|
needsInstall = true;
|
||||||
} else if (updatedPackages[p].alwaysAddToPackageJson) {
|
}
|
||||||
const cleanVersion = checkAndCleanWithSemver(p, json.dependencies[p]);
|
} else if (json.dependencies && json.dependencies[p]) {
|
||||||
|
const cleanVersion = checkAndCleanWithSemver(p, json.dependencies[p]);
|
||||||
|
|
||||||
if (lt(cleanVersion, cleanUpdatedVersion)) {
|
if (lt(cleanVersion, cleanUpdatedVersion)) {
|
||||||
if (!json.dependencies) json.dependencies = {};
|
json.dependencies[p] = updatedPackages[p].version;
|
||||||
json.dependencies[p] = updatedPackages[p].version;
|
needsInstall = true;
|
||||||
|
}
|
||||||
|
} else if (updatedPackages[p].alwaysAddToPackageJson) {
|
||||||
|
const cleanVersion = checkAndCleanWithSemver(p, json.dependencies[p]);
|
||||||
|
|
||||||
|
if (lt(cleanVersion, cleanUpdatedVersion)) {
|
||||||
|
if (!json.dependencies) json.dependencies = {};
|
||||||
|
json.dependencies[p] = updatedPackages[p].version;
|
||||||
|
needsInstall = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
return json;
|
||||||
return json;
|
}),
|
||||||
});
|
needsInstall ? addInstallTask(options) : noop()
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user