@comet/upgrade 1.16.0 → 1.18.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/lib/index.js CHANGED
@@ -50,9 +50,15 @@ function main() {
50
50
  console.error("Missing target version! Usage: npx @comet/upgrade <version>");
51
51
  process.exit(-1);
52
52
  }
53
- const isUpgradeScript = fs_1.default.existsSync(path_1.default.join(__dirname, targetVersionArg.replace(/\.ts$/, ".js")));
53
+ const isUpgradeScript = targetVersionArg.endsWith(".ts");
54
54
  if (isUpgradeScript) {
55
- yield runUpgradeScript(targetVersionArg.replace(/\.ts$/, ".js"));
55
+ if (fs_1.default.existsSync(path_1.default.join(__dirname, targetVersionArg.replace(/\.ts$/, ".js")))) {
56
+ yield runUpgradeScript(targetVersionArg.replace(/\.ts$/, ".js"));
57
+ }
58
+ else {
59
+ console.error(`Can't find upgrade script '${targetVersionArg}'`);
60
+ process.exit(-1);
61
+ }
56
62
  return;
57
63
  }
58
64
  const targetVersion = semver_1.default.coerce(targetVersionArg, { includePrerelease: true });
@@ -0,0 +1,157 @@
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 fs_1 = __importDefault(require("fs"));
39
+ const crypto = __importStar(require("node:crypto"));
40
+ const ts_morph_1 = require("ts-morph");
41
+ function addSitePreviewSecret() {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ updateApiFiles1();
44
+ updateApiFiles2();
45
+ updateApiFiles3();
46
+ updateDotEnvFile();
47
+ updateValuesTplFile();
48
+ });
49
+ }
50
+ exports.default = addSitePreviewSecret;
51
+ function updateApiFiles1() {
52
+ console.log("Add sitePreviewSecret to api/src/config/environment-variables.ts ...");
53
+ const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
54
+ const sourceFile = project.getSourceFile("api/src/config/environment-variables.ts");
55
+ if (!sourceFile) {
56
+ console.error(" Could not file file, make sure to add SITE_PREVIEW_SECRET");
57
+ return;
58
+ }
59
+ const cls = sourceFile.getClass("EnvironmentVariables");
60
+ if (!cls) {
61
+ console.error(" Could not class EnvironmentVariables, make sure to add SITE_PREVIEW_SECRET");
62
+ return;
63
+ }
64
+ cls.addMember("\n@IsString()\n@MinLength(16)\nSITE_PREVIEW_SECRET: string;");
65
+ sourceFile.saveSync();
66
+ console.log(" finished");
67
+ }
68
+ function updateApiFiles2() {
69
+ var _a, _b;
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ console.log("Add sitePreviewSecret to api/src/config/config.ts ...");
72
+ const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
73
+ const sourceFile = project.getSourceFile("api/src/config/config.ts");
74
+ if (!sourceFile) {
75
+ console.error(" Could not file file, make sure to add sitePreviewSecret: envVars.SITE_PREVIEW_SECRET");
76
+ return;
77
+ }
78
+ (_b = (_a = sourceFile
79
+ .getFunction("createConfig")) === null || _a === void 0 ? void 0 : _a.getBody()) === null || _b === void 0 ? void 0 : _b.getDescendantsOfKind(ts_morph_1.SyntaxKind.ReturnStatement)[0].getChildrenOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0].addProperty("sitePreviewSecret: envVars.SITE_PREVIEW_SECRET,");
80
+ sourceFile.saveSync();
81
+ console.log(" finished");
82
+ });
83
+ }
84
+ function updateApiFiles3() {
85
+ var _a, _b;
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ console.log("Add sitePreviewSecret to api/src/app.module.ts ...");
88
+ const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
89
+ const sourceFile = project.getSourceFile("api/src/app.module.ts");
90
+ if (!sourceFile) {
91
+ console.error(" Could not file file, make sure to add sitePreviewSecret to PageTreeModule");
92
+ return;
93
+ }
94
+ (_b = (_a = sourceFile
95
+ .getClassOrThrow("AppModule")
96
+ .getMethodOrThrow("forRoot")
97
+ .getBody()) === null || _a === void 0 ? void 0 : _a.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression).find((call) => call.getText().includes("PageTreeModule.forRoot"))) === null || _b === void 0 ? void 0 : _b.getChildrenOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0].addProperty("sitePreviewSecret: config.sitePreviewSecret,");
98
+ sourceFile.saveSync();
99
+ console.log(" finished");
100
+ });
101
+ }
102
+ function updateDotEnvFile() {
103
+ console.log("Update .env");
104
+ if (!fs_1.default.existsSync(".env")) {
105
+ console.error(" could not find file, please make sure to add SITE_PREVIEW_SECRET manually");
106
+ return;
107
+ }
108
+ fs_1.default.appendFile(".env", `\nSITE_PREVIEW_SECRET=${crypto.randomBytes(8).toString("hex")}\n`, (err) => {
109
+ console.error(` ${err}`);
110
+ });
111
+ console.log(" finished");
112
+ }
113
+ function updateValuesTplFile() {
114
+ const valuesFileName = "deployment/helm/values.tpl.yaml";
115
+ console.log(`Update ${valuesFileName}`);
116
+ if (!fs_1.default.existsSync(valuesFileName)) {
117
+ console.error(" could not find file, please make sure to add SITE_PREVIEW_SECRET to api and remove authproxy-preview.");
118
+ return;
119
+ }
120
+ const file = fs_1.default.readFileSync(valuesFileName, "utf8");
121
+ const lines = file.split(/\n/);
122
+ let currentSection = "";
123
+ let sitePreviewLine = 0;
124
+ let beginAuthproxyPreviewLine = 0;
125
+ let endAuthproxyPreviewLine = 0;
126
+ lines.forEach((line, index) => {
127
+ const match = line.match(/(\s*)(.*):/);
128
+ if (match) {
129
+ if (match[1].length / 2 === 0)
130
+ currentSection = match[2];
131
+ if (currentSection === "api" && match[2] === "secrets") {
132
+ sitePreviewLine = index;
133
+ }
134
+ if (currentSection === "authproxy-preview" && !beginAuthproxyPreviewLine) {
135
+ beginAuthproxyPreviewLine = index;
136
+ }
137
+ if (currentSection !== "authproxy-preview" && beginAuthproxyPreviewLine > 0 && !endAuthproxyPreviewLine) {
138
+ endAuthproxyPreviewLine = index;
139
+ }
140
+ }
141
+ });
142
+ if (sitePreviewLine) {
143
+ lines.splice(sitePreviewLine, 0, ' SITE_PREVIEW_SECRET: "{{ op://$OP_PREFIX-$OP_ENVIRONMENT/site-preview-secret-$APP_ENV/password }}"');
144
+ }
145
+ else {
146
+ console.error(" Could not find api.secrets, please make sure to add SITE_PREVIEW_SECRET manually.");
147
+ }
148
+ if (beginAuthproxyPreviewLine && endAuthproxyPreviewLine) {
149
+ lines.splice(beginAuthproxyPreviewLine, endAuthproxyPreviewLine - beginAuthproxyPreviewLine);
150
+ }
151
+ else {
152
+ console.error(" Could not find authproxy-preview. Please make sure to remove it from deployment manually.");
153
+ }
154
+ const content = lines.join("\n").replace(/oauth2-proxy-preview/g, "site");
155
+ fs_1.default.writeFileSync(valuesFileName, content);
156
+ console.log(" finished.");
157
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comet/upgrade",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "description": "Upgrade scripts for Comet DXP",
5
5
  "homepage": "https://github.com/vivid-planet/comet-upgrade#readme",
6
6
  "bugs": {