@comet/upgrade 1.50.0 → 1.51.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
@@ -12,12 +12,17 @@ const microservices = ["api", "admin", "site"];
12
12
  function microserviceExists(microservice) {
13
13
  return fs_1.default.existsSync(`${microservice}/package.json`);
14
14
  }
15
+ const isRunningViaNpx = Boolean(process.env.npm_execpath?.includes("npx"));
16
+ const isLocalDevelopment = !isRunningViaNpx;
15
17
  async function main() {
16
18
  let targetVersionArg = process.argv[2];
17
19
  if (targetVersionArg === undefined) {
18
20
  console.error("Missing target version! Usage: npx @comet/upgrade <version>");
19
21
  process.exit(-1);
20
22
  }
23
+ if (isLocalDevelopment) {
24
+ console.warn("Not running via npx -> assuming local development. Scripts will run twice to ensure idempotency.");
25
+ }
21
26
  const isUpgradeScript = targetVersionArg.endsWith(".ts");
22
27
  if (isUpgradeScript) {
23
28
  if (fs_1.default.existsSync(path_1.default.join(__dirname, targetVersionArg.replace(/\.ts$/, ".js")))) {
@@ -147,6 +152,10 @@ async function runUpgradeScripts(scripts) {
147
152
  async function runUpgradeScript(script) {
148
153
  try {
149
154
  await script.script();
155
+ if (isLocalDevelopment) {
156
+ // run upgrade scripts twice locally to ensure that the scripts are idempotent
157
+ await script.script();
158
+ }
150
159
  }
151
160
  catch (error) {
152
161
  console.error(`Script '${script.name}' failed to execute. See original error below`);
@@ -32,8 +32,8 @@ const ts_morph_1 = require("ts-morph");
32
32
  const execute_command_util_1 = require("../util/execute-command.util");
33
33
  async function addSitePreviewSecret() {
34
34
  updateApiFiles1();
35
- updateApiFiles2();
36
- updateApiFiles3();
35
+ await updateApiFiles2();
36
+ await updateApiFiles3();
37
37
  updateDotEnvFile();
38
38
  updateValuesTplFile();
39
39
  updateChart();
@@ -41,7 +41,6 @@ async function addSitePreviewSecret() {
41
41
  }
42
42
  exports.default = addSitePreviewSecret;
43
43
  function updateApiFiles1() {
44
- console.log("Add sitePreviewSecret to api/src/config/environment-variables.ts ...");
45
44
  const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
46
45
  const sourceFile = project.getSourceFile("api/src/config/environment-variables.ts");
47
46
  if (!sourceFile) {
@@ -53,18 +52,30 @@ function updateApiFiles1() {
53
52
  console.error(" Could not class EnvironmentVariables, make sure to add SITE_PREVIEW_SECRET");
54
53
  return;
55
54
  }
55
+ if (cls.getMember("SITE_PREVIEW_SECRET")) {
56
+ return;
57
+ }
58
+ console.log("Add sitePreviewSecret to api/src/config/environment-variables.ts ...");
56
59
  cls.addMember("\n@IsString()\n@MinLength(16)\nSITE_PREVIEW_SECRET: string;");
57
60
  sourceFile.saveSync();
58
61
  console.log(" finished");
59
62
  }
60
63
  async function updateApiFiles2() {
61
- console.log("Add sitePreviewSecret to api/src/config/config.ts ...");
62
64
  const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
63
65
  const sourceFile = project.getSourceFile("api/src/config/config.ts");
64
66
  if (!sourceFile) {
65
67
  console.error(" Could not file file, make sure to add sitePreviewSecret: envVars.SITE_PREVIEW_SECRET");
66
68
  return;
67
69
  }
70
+ if (sourceFile
71
+ .getFunction("createConfig")
72
+ ?.getBody()
73
+ ?.getDescendantsOfKind(ts_morph_1.SyntaxKind.ReturnStatement)[0]
74
+ .getChildrenOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0]
75
+ .getProperty("sitePreviewSecret")) {
76
+ return;
77
+ }
78
+ console.log("Add sitePreviewSecret to api/src/config/config.ts ...");
68
79
  sourceFile
69
80
  .getFunction("createConfig")
70
81
  ?.getBody()
@@ -75,13 +86,23 @@ async function updateApiFiles2() {
75
86
  console.log(" finished");
76
87
  }
77
88
  async function updateApiFiles3() {
78
- console.log("Add sitePreviewSecret to api/src/app.module.ts ...");
79
89
  const project = new ts_morph_1.Project({ tsConfigFilePath: "./api/tsconfig.json" });
80
90
  const sourceFile = project.getSourceFile("api/src/app.module.ts");
81
91
  if (!sourceFile) {
82
92
  console.error(" Could not file file, make sure to add sitePreviewSecret to PageTreeModule");
83
93
  return;
84
94
  }
95
+ if (sourceFile
96
+ .getClassOrThrow("AppModule")
97
+ .getMethodOrThrow("forRoot")
98
+ .getBody()
99
+ ?.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression)
100
+ .find((call) => call.getText().includes("PageTreeModule.forRoot"))
101
+ ?.getChildrenOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0]
102
+ .getProperty("sitePreviewSecret")) {
103
+ return;
104
+ }
105
+ console.log("Add sitePreviewSecret to api/src/app.module.ts ...");
85
106
  sourceFile
86
107
  .getClassOrThrow("AppModule")
87
108
  .getMethodOrThrow("forRoot")
@@ -94,14 +115,15 @@ async function updateApiFiles3() {
94
115
  console.log(" finished");
95
116
  }
96
117
  function updateDotEnvFile() {
97
- console.log("Update .env");
98
118
  if (!fs_1.default.existsSync(".env")) {
99
119
  console.error(" could not find file, please make sure to add SITE_PREVIEW_SECRET manually");
100
120
  return;
101
121
  }
102
- fs_1.default.appendFile(".env", `\nSITE_PREVIEW_SECRET=${crypto.randomBytes(8).toString("hex")}\n`, (err) => {
103
- console.error(` ${err}`);
104
- });
122
+ if (fs_1.default.readFileSync(".env").includes("SITE_PREVIEW_SECRET")) {
123
+ return;
124
+ }
125
+ console.log("Update .env");
126
+ fs_1.default.appendFileSync(".env", `\nSITE_PREVIEW_SECRET=${crypto.randomBytes(8).toString("hex")}\n`);
105
127
  console.log(" finished");
106
128
  }
107
129
  function updateValuesTplFile() {
@@ -117,11 +139,15 @@ function updateValuesTplFile() {
117
139
  let sitePreviewLine = 0;
118
140
  let beginAuthproxyPreviewLine = 0;
119
141
  let endAuthproxyPreviewLine = 0;
142
+ let alreadyContainsSitePreviewSecret = false;
120
143
  lines.forEach((line, index) => {
121
144
  const match = line.match(/(\s*)(.*):/);
122
145
  if (match) {
123
146
  if (match[1].length / 2 === 0)
124
147
  currentSection = match[2];
148
+ if (currentSection === "api" && line.includes("SITE_PREVIEW_SECRET:")) {
149
+ alreadyContainsSitePreviewSecret = true;
150
+ }
125
151
  if (currentSection === "api" && match[2] === "secrets") {
126
152
  sitePreviewLine = index;
127
153
  }
@@ -133,11 +159,13 @@ function updateValuesTplFile() {
133
159
  }
134
160
  }
135
161
  });
136
- if (sitePreviewLine) {
137
- lines.splice(sitePreviewLine, 0, ' SITE_PREVIEW_SECRET: "{{ op://$OP_PREFIX-$OP_ENVIRONMENT/site-preview-secret-$APP_ENV/password }}"');
138
- }
139
- else {
140
- console.error(" Could not find api.secrets, please make sure to add SITE_PREVIEW_SECRET manually.");
162
+ if (!alreadyContainsSitePreviewSecret) {
163
+ if (sitePreviewLine) {
164
+ lines.splice(sitePreviewLine, 0, ' SITE_PREVIEW_SECRET: "{{ op://$OP_PREFIX-$OP_ENVIRONMENT/site-preview-secret-$APP_ENV/password }}"');
165
+ }
166
+ else {
167
+ console.error(" Could not find api.secrets, please make sure to add SITE_PREVIEW_SECRET manually.");
168
+ }
141
169
  }
142
170
  if (beginAuthproxyPreviewLine && endAuthproxyPreviewLine) {
143
171
  lines.splice(beginAuthproxyPreviewLine, endAuthproxyPreviewLine - beginAuthproxyPreviewLine);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comet/upgrade",
3
- "version": "1.50.0",
3
+ "version": "1.51.0",
4
4
  "description": "Upgrade scripts for Comet DXP",
5
5
  "homepage": "https://github.com/vivid-planet/comet-upgrade#readme",
6
6
  "bugs": {