@comet/upgrade 1.1.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.
package/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) 2022, Vivid Planet Software GmbH
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ - Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ - Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @comet/upgrade
2
+
3
+ Upgrade scripts for Comet DXP
4
+
5
+ ## Usage
6
+
7
+ ```sh
8
+ npx @comet/upgrade <target-version>
9
+ ```
10
+
11
+ For example:
12
+
13
+ ```sh
14
+ npx @comet/upgrade v4
15
+ ```
16
+
17
+ ## Create a new upgrade script
18
+
19
+ 1. Start the development process:
20
+
21
+ ```sh
22
+ yarn start
23
+ ```
24
+
25
+ 2. Add a new script for the version you want to upgrade to, for instance, `src/v4/change-something.ts`.
26
+ The script **must have a function as default export** that performs the changes. See [src/v4/remove-clear-types-script.ts](src/v4/remove-clear-types-script.ts) for an example.
27
+
28
+ 3. Test the upgrade script
29
+
30
+ Navigate to the project you want to upgrade:
31
+
32
+ ```sh
33
+ cd project-you-want-to-upgrade/
34
+ ```
35
+
36
+ Create a new branch to test the script:
37
+
38
+ ```sh
39
+ git checkout -b upgrade-comet-to-4
40
+ ```
41
+
42
+ Execute the local @comet/upgrade binary:
43
+
44
+ ```sh
45
+ ../comet-upgrade/bin/index.js 4 # comet-upgrade directory
46
+
47
+ ```
48
+
49
+ Verify the changes in your project. If something is not as expected, adapt the upgrade script accordingly and then run it again on a clean state:
50
+
51
+ ```sh
52
+ git reset --hard HEAD
53
+
54
+ ../comet-upgrade/bin/index.js 4
55
+ ```
package/bin/index.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require("../lib/index");
package/lib/index.js ADDED
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const child_process_1 = require("child_process");
39
+ const fs_1 = __importDefault(require("fs"));
40
+ const path_1 = __importDefault(require("path"));
41
+ const semver_1 = __importDefault(require("semver"));
42
+ const VERSION_NUMBER = /^v?\d+$/;
43
+ function main() {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ let targetVersionArg = process.argv[2];
46
+ if (targetVersionArg === undefined) {
47
+ console.error("Missing target version! Usage: npx @comet/upgrade <version>");
48
+ process.exit(-1);
49
+ }
50
+ if (!VERSION_NUMBER.test(targetVersionArg)) {
51
+ console.error("Can't parse version number. Example usage: npx @comet/upgrade v4");
52
+ process.exit(-1);
53
+ }
54
+ if (targetVersionArg.startsWith("v")) {
55
+ targetVersionArg = targetVersionArg.substring(1);
56
+ }
57
+ const targetVersion = Number(targetVersionArg);
58
+ const scriptsFolder = path_1.default.join(__dirname, `v${targetVersion}`);
59
+ if (!fs_1.default.existsSync(scriptsFolder)) {
60
+ console.error(`Can't find target version 'v${targetVersionArg}'`);
61
+ listTargetVersions();
62
+ process.exit(-1);
63
+ }
64
+ const currentVersion = getCurrentVersion();
65
+ console.info(`Upgrading from v${currentVersion} to v${targetVersion}`);
66
+ console.info("Updating dependencies");
67
+ yield updateDependencies(targetVersion);
68
+ yield runUpgradeScripts(targetVersion);
69
+ });
70
+ }
71
+ function getCurrentVersion() {
72
+ var _a;
73
+ if (!fs_1.default.existsSync("admin/package.json")) {
74
+ console.error(`File 'admin/package.json' doesn't exist. Make sure to call the script in the root of your project`);
75
+ process.exit(-1);
76
+ }
77
+ const packageJson = JSON.parse(fs_1.default.readFileSync("admin/package.json").toString());
78
+ const versionRange = (_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a["@comet/admin"];
79
+ if (versionRange === undefined) {
80
+ console.error(`Package '@comet/admin' isn't listed as a dependency. Is this a Comet DXP project?`);
81
+ process.exit(-1);
82
+ }
83
+ // ^3.0.0 | ~3.0.0 | 3.0.0-canary -> 3.0.0
84
+ const versionMatches = versionRange.match(/\d\.\d\.\d/);
85
+ if (versionMatches === null) {
86
+ console.error(`Unsupported version range '${versionRange}'. Example range: ^3.0.0`);
87
+ process.exit(-1);
88
+ }
89
+ const version = versionMatches[0];
90
+ return semver_1.default.major(version);
91
+ }
92
+ function updateDependencies(targetVersion) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const microservices = ["api", "admin", "site"];
95
+ const packages = {
96
+ api: ["@comet/blocks-api", "@comet/cms-api"],
97
+ admin: [
98
+ "@comet/admin",
99
+ "@comet/admin-color-picker",
100
+ "@comet/admin-date-time",
101
+ "@comet/admin-icons",
102
+ "@comet/admin-react-select",
103
+ "@comet/admin-rte",
104
+ "@comet/admin-theme",
105
+ "@comet/blocks-admin",
106
+ "@comet/cms-admin",
107
+ ],
108
+ site: ["@comet/cms-site"],
109
+ };
110
+ for (const microservice of microservices) {
111
+ if (!fs_1.default.existsSync(`${microservice}/package.json`)) {
112
+ console.warn(`File '${microservice}/package.json' doesn't exist. Skipping microservice`);
113
+ continue;
114
+ }
115
+ const packageJson = JSON.parse(fs_1.default.readFileSync(`${microservice}/package.json`).toString());
116
+ const dependencies = packages[microservice].filter((packageName) => { var _a; return ((_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a[packageName]) !== undefined; });
117
+ const devDependencies = ["@comet/cli", "@comet/eslint-config"].filter((packageName) => { var _a; return ((_a = packageJson.devDependencies) === null || _a === void 0 ? void 0 : _a[packageName]) !== undefined; });
118
+ if (dependencies.length === 0 && devDependencies.length === 0) {
119
+ console.warn(`Microservice '${microservice}' has no Comet DXP dependencies. Skipping install`);
120
+ continue;
121
+ }
122
+ yield executeCommand("npm", [
123
+ "install",
124
+ "--prefix",
125
+ microservice,
126
+ "--no-audit",
127
+ "--loglevel",
128
+ "error",
129
+ ...dependencies.map((dependency) => `${dependency}@${targetVersion}`),
130
+ ...devDependencies.map((dependency) => `${dependency}@${targetVersion}`),
131
+ ]);
132
+ }
133
+ });
134
+ }
135
+ // Inspired by https://github.com/facebook/create-react-app/blob/main/packages/create-react-app/createReactApp.js#L383
136
+ function executeCommand(command, args = []) {
137
+ return new Promise((resolve, reject) => {
138
+ const child = (0, child_process_1.spawn)(command, args, { stdio: "inherit" });
139
+ child.on("close", (code) => {
140
+ if (code !== 0) {
141
+ reject({
142
+ command: `${command} ${args.join(" ")}`,
143
+ });
144
+ return;
145
+ }
146
+ resolve();
147
+ });
148
+ });
149
+ }
150
+ function runUpgradeScripts(targetVersion) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ var _a;
153
+ const scriptsFolder = path_1.default.join(__dirname, `v${targetVersion}`);
154
+ for (const fileName of fs_1.default.readdirSync(scriptsFolder)) {
155
+ const upgradeScript = yield (_a = path_1.default.join(scriptsFolder, fileName), Promise.resolve().then(() => __importStar(require(_a))));
156
+ try {
157
+ yield upgradeScript.default();
158
+ }
159
+ catch (error) {
160
+ console.error(`Script 'v${targetVersion}/${fileName}' failed to execute. See original error below`);
161
+ console.error(error);
162
+ }
163
+ }
164
+ });
165
+ }
166
+ function listTargetVersions() {
167
+ console.info("Available target versions");
168
+ const targetVersions = fs_1.default.readdirSync(__dirname).filter((entry) => entry.startsWith("v"));
169
+ console.info(targetVersions.map((version) => `- ${version}`).join("\n"));
170
+ }
171
+ main();
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const fs_1 = __importDefault(require("fs"));
16
+ const prettier_1 = __importDefault(require("prettier"));
17
+ /**
18
+ * Removes unnecessary clear:types script before GraphQL code generation that interferes with block code generation.
19
+ */
20
+ function removeClearTypesScript() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ if (!fs_1.default.existsSync("site/package.json")) {
23
+ return;
24
+ }
25
+ const sitePackageJson = JSON.parse(fs_1.default.readFileSync("site/package.json").toString());
26
+ if (sitePackageJson.scripts["clear:types"]) {
27
+ delete sitePackageJson.scripts["clear:types"];
28
+ }
29
+ if (sitePackageJson.scripts["gql:types"]) {
30
+ sitePackageJson.scripts["gql:types"] = sitePackageJson.scripts["gql:types"].replace("npm run clear:types && ", "");
31
+ }
32
+ if (sitePackageJson.scripts["gql:watch"]) {
33
+ sitePackageJson.scripts["gql:watch"] = sitePackageJson.scripts["gql:watch"].replace("npm run clear:types && ", "");
34
+ }
35
+ const prettierConfig = yield prettier_1.default.resolveConfig(process.cwd());
36
+ fs_1.default.writeFileSync("site/package.json", prettier_1.default.format(JSON.stringify(sitePackageJson), Object.assign(Object.assign({}, prettierConfig), { parser: "json" })));
37
+ });
38
+ }
39
+ exports.default = removeClearTypesScript;
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@comet/upgrade",
3
+ "version": "1.1.0",
4
+ "description": "Upgrade scripts for Comet DXP",
5
+ "homepage": "https://github.com/vivid-planet/comet-upgrade#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/vivid-planet/comet-upgrade/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/vivid-planet/comet-upgrade"
12
+ },
13
+ "license": "BSD-2-Clause",
14
+ "author": "Vivid Planet Software GmbH <office@vivid-planet.com> (https://www.vivid-planet.com/)",
15
+ "main": "lib/index.js",
16
+ "bin": "bin/index.js",
17
+ "files": [
18
+ "bin/index.js",
19
+ "lib/**/*.js"
20
+ ],
21
+ "scripts": {
22
+ "build": "yarn clean && tsc",
23
+ "clean": "rimraf lib/",
24
+ "lint": "run-p lint:eslint lint:tsc",
25
+ "lint:eslint": "eslint --max-warnings 0 src/",
26
+ "lint:tsc": "tsc --noEmit",
27
+ "prepare": "husky install",
28
+ "start": "yarn clean && tsc --watch"
29
+ },
30
+ "dependencies": {
31
+ "prettier": "^2.8.1",
32
+ "semver": "^7.3.8"
33
+ },
34
+ "devDependencies": {
35
+ "@comet/eslint-config": "^3.2.1",
36
+ "@types/node": "^14.18.34",
37
+ "@types/prettier": "^2.7.1",
38
+ "@types/semver": "^7.3.13",
39
+ "eslint": "^8.29.0",
40
+ "husky": "^8.0.2",
41
+ "lint-staged": "^13.1.0",
42
+ "rimraf": "^3.0.2",
43
+ "typescript": "^4.9.4",
44
+ "yarn-run-all": "^3.1.1"
45
+ },
46
+ "packageManager": "yarn@3.3.0",
47
+ "engines": {
48
+ "node": "14",
49
+ "yarn": "3"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org"
54
+ }
55
+ }