@carbon/upgrade 10.14.0 → 10.17.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,172 +0,0 @@
1
- /**
2
- * Copyright IBM Corp. 2019, 2019
3
- *
4
- * This source code is licensed under the Apache-2.0 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- 'use strict';
9
-
10
- const glob = require('fast-glob');
11
- const fs = require('fs-extra');
12
- const merge = require('lodash.merge');
13
- const path = require('path');
14
- const semver = require('semver');
15
- const { UpgradeError } = require('../error');
16
- const { hash } = require('../hash');
17
-
18
- class Workspace {
19
- /**
20
- * @param {string} directory
21
- * @returns {object}
22
- */
23
- static async load(directory) {
24
- const tree = await loadWorkspace(directory);
25
- const visited = new Map();
26
- const workspace = visit(tree);
27
-
28
- function visit(node) {
29
- let workspace = Workspace.create(node);
30
-
31
- if (visited.has(workspace.id)) {
32
- return visited.get(workspace.id);
33
- }
34
-
35
- visited.set(workspace.id, workspace);
36
-
37
- if (node.type === 'worktree') {
38
- node.children.forEach((node) => {
39
- const child = visit(node);
40
- workspace.addChildWorkspace(child);
41
- });
42
- }
43
-
44
- return workspace;
45
- }
46
-
47
- for (const node of visited.values()) {
48
- for (const dependency of node.dependencies.values()) {
49
- if (!visited.has(dependency.name)) {
50
- continue;
51
- }
52
- const workspace = visited.get(dependency.name);
53
- if (semver.satisfies(workspace.version, dependency.version)) {
54
- dependency.workspace = workspace;
55
- }
56
- }
57
- }
58
-
59
- return workspace;
60
- }
61
-
62
- static create(workspace) {
63
- return new Workspace(workspace);
64
- }
65
-
66
- constructor({ directory, name, version, dependencies }) {
67
- this.directory = directory;
68
- this.name = name;
69
- this.version = version;
70
- this.dependencies = dependencies;
71
- this.workspaces = new Set();
72
- this.id = hash(this.directory);
73
- }
74
-
75
- addChildWorkspace(workspace) {
76
- this.workspaces.add(workspace);
77
- }
78
-
79
- *getWorkspaces() {
80
- yield this;
81
- for (const workspace of this.workspaces) {
82
- yield* workspace.getWorkspaces();
83
- }
84
- }
85
-
86
- getPackageJson() {
87
- const packageJsonPath = path.join(this.directory, 'package.json');
88
- return fs.readJson(packageJsonPath);
89
- }
90
-
91
- async updatePackageJson(packageJson) {
92
- const packageJsonPath = path.join(this.directory, 'package.json');
93
- const current = await this.getPackageJson();
94
- await fs.writeJson(packageJsonPath, merge(current, packageJson), {
95
- spaces: 2,
96
- });
97
- }
98
- }
99
-
100
- async function loadWorkspace(directory) {
101
- const packageJsonPath = path.join(directory, 'package.json');
102
-
103
- if (!fs.existsSync(packageJsonPath)) {
104
- throw new UpgradeError(`Unable to find package.json at ${packageJsonPath}`);
105
- }
106
-
107
- const packageJson = await fs.readJson(packageJsonPath);
108
- const types = ['dependencies', 'devDependencies', 'peerDependencies'];
109
- const dependencies = [];
110
-
111
- for (const type of types) {
112
- if (!packageJson[type]) {
113
- continue;
114
- }
115
-
116
- for (const [name, version] of Object.entries(packageJson[type])) {
117
- dependencies.push({
118
- type,
119
- name,
120
- version,
121
- });
122
- }
123
- }
124
-
125
- if (packageJson.workspaces) {
126
- if (
127
- !Array.isArray(packageJson.workspaces) &&
128
- !Array.isArray(packageJson.workspaces.packages)
129
- ) {
130
- throw new UpgradeError(
131
- `Invalid workspace configuration found at ${packageJsonPath}`
132
- );
133
- }
134
-
135
- const patterns = Array.isArray(packageJson.workspaces)
136
- ? packageJson.workspaces
137
- : packageJson.workspaces.packages;
138
- const children = await glob(
139
- patterns.map((pattern) => `${pattern}/package.json`),
140
- {
141
- cwd: directory,
142
- }
143
- ).then((matches) => {
144
- return Promise.all(
145
- matches.map((match) => {
146
- return loadWorkspace(path.dirname(path.join(directory, match)));
147
- })
148
- );
149
- });
150
-
151
- return {
152
- type: 'worktree',
153
- directory,
154
- dependencies,
155
- children,
156
- name: packageJson.name,
157
- version: packageJson.version,
158
- };
159
- }
160
-
161
- return {
162
- type: 'workspace',
163
- directory,
164
- dependencies,
165
- name: packageJson.name,
166
- version: packageJson.version,
167
- };
168
- }
169
-
170
- module.exports = {
171
- Workspace,
172
- };
package/src/runner.js DELETED
@@ -1,76 +0,0 @@
1
- /**
2
- * Copyright IBM Corp. 2019, 2019
3
- *
4
- * This source code is licensed under the Apache-2.0 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- 'use strict';
9
-
10
- const chalk = require('chalk');
11
- const clone = require('lodash.cloneDeep');
12
- const { diff } = require('jest-diff');
13
-
14
- const logger = {
15
- info(message) {
16
- console.log(`${chalk.yellow.inverse(' INFO ')} ${message}`);
17
- },
18
- };
19
-
20
- const Runner = {
21
- /**
22
- * Run the given migrations for each workspace
23
- * @param {Array<WorkspaceMigration} migrationsByWorkspace
24
- * @returns void
25
- */
26
- async run(migrationsByWorkspace, options) {
27
- for (const { workspace, migrationOptions } of migrationsByWorkspace) {
28
- logger.info(
29
- `Running migrations for ${workspace.name} in ${workspace.directory}`
30
- );
31
-
32
- const updated = [];
33
- for (const { dependency, migration } of migrationOptions) {
34
- logger.info(
35
- `Running migration for ${migration.packageName} from ${dependency.version} to ${migration.to}`
36
- );
37
-
38
- updated.push({
39
- ...dependency,
40
- version: migration.to,
41
- });
42
- }
43
-
44
- // Update package.json
45
- const originalPackageJson = await workspace.getPackageJson();
46
- const updatedPackageJson = updatePackageJson(
47
- originalPackageJson,
48
- updated
49
- );
50
-
51
- if (!options.write) {
52
- console.log(
53
- diff(originalPackageJson, updatedPackageJson, {
54
- aAnnotation: 'Original',
55
- bAnnotation: 'Modified',
56
- contextLines: 3,
57
- })
58
- );
59
- }
60
- }
61
- },
62
- };
63
-
64
- function updatePackageJson(packageJson, dependencies) {
65
- const result = clone(packageJson);
66
-
67
- for (const { name, version, type } of dependencies) {
68
- result[type][name] = version;
69
- }
70
-
71
- return result;
72
- }
73
-
74
- module.exports = {
75
- Runner,
76
- };