create-medusa-app 1.2.1 → 1.2.2-next-20230929123725

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.2.2-next-20230929123725
4
+
5
+ ### Patch Changes
6
+
7
+ - [#5189](https://github.com/medusajs/medusa/pull/5189) [`18a05dee8`](https://github.com/medusajs/medusa/commit/18a05dee86f55366c92a2669eacbccf526373ff5) Thanks [@shahednasser](https://github.com/shahednasser)! - fix(create-medusa-app): fix inconsistency in checking errors in migrations
8
+
3
9
  ## 1.2.1
4
10
 
5
11
  ### Patch Changes
@@ -65,7 +65,7 @@ export default async ({ repoUrl = "", seed, boilerplate, skipDb, dbUrl, browser,
65
65
  const projectPath = getProjectPath(projectName, directoryPath);
66
66
  const adminEmail = !skipDb && migrations ? await askForAdminEmail(seed, boilerplate) : "";
67
67
  const installNextjs = withNextjsStarter || (await askForNextjsStarter());
68
- const { client, dbConnectionString } = !skipDb
68
+ let { client, dbConnectionString } = !skipDb
69
69
  ? await getDbClientAndCredentials({
70
70
  dbName,
71
71
  dbUrl,
@@ -107,7 +107,7 @@ export default async ({ repoUrl = "", seed, boilerplate, skipDb, dbUrl, browser,
107
107
  ...factBoxOptions,
108
108
  title: "Creating database...",
109
109
  });
110
- await runCreateDb({ client, dbName, spinner });
110
+ client = await runCreateDb({ client, dbName, spinner });
111
111
  factBoxOptions.interval = displayFactBox({
112
112
  ...factBoxOptions,
113
113
  message: `Database ${dbName} created`,
@@ -131,6 +131,7 @@ export default async ({ repoUrl = "", seed, boilerplate, skipDb, dbUrl, browser,
131
131
  migrations,
132
132
  onboardingType: installNextjs ? "nextjs" : "default",
133
133
  nextjsDirectory,
134
+ client,
134
135
  });
135
136
  }
136
137
  catch (e) {
@@ -8,12 +8,20 @@ export default async function createDb({ client, db }) {
8
8
  await client.query(`CREATE DATABASE "${db}"`);
9
9
  }
10
10
  export async function runCreateDb({ client, dbName, spinner, }) {
11
- // create postgres database
11
+ let newClient = client;
12
12
  try {
13
+ // create postgres database
13
14
  await createDb({
14
15
  client,
15
16
  db: dbName,
16
17
  });
18
+ // create a new connection with database selected
19
+ await client.end();
20
+ newClient = await postgresClient({
21
+ user: client.user,
22
+ password: client.password,
23
+ database: dbName,
24
+ });
17
25
  }
18
26
  catch (e) {
19
27
  spinner.stop();
@@ -22,6 +30,7 @@ export async function runCreateDb({ client, dbName, spinner, }) {
22
30
  type: "error",
23
31
  });
24
32
  }
33
+ return newClient;
25
34
  }
26
35
  async function getForDbName(dbName) {
27
36
  let client;
@@ -5,7 +5,7 @@ import promiseExec from "./promise-exec.js";
5
5
  import { EOL } from "os";
6
6
  import { displayFactBox } from "./facts.js";
7
7
  import { clearProject } from "./clear-project.js";
8
- export default async ({ directory, dbConnectionString, admin, seed, boilerplate, spinner, processManager, abortController, skipDb, migrations, onboardingType = "default", nextjsDirectory = "", }) => {
8
+ export default async ({ directory, dbConnectionString, admin, seed, boilerplate, spinner, processManager, abortController, skipDb, migrations, onboardingType = "default", nextjsDirectory = "", client, }) => {
9
9
  // initialize execution options
10
10
  const execOptions = {
11
11
  cwd: directory,
@@ -97,9 +97,23 @@ export default async ({ directory, dbConnectionString, admin, seed, boilerplate,
97
97
  await processManager.runProcess({
98
98
  process: async () => {
99
99
  const proc = await promiseExec("npx @medusajs/medusa-cli@latest migrations run", npxOptions);
100
- // ensure that migrations actually ran in case of an uncaught error
101
- if (!proc.stdout.includes("Migrations completed")) {
102
- throw new Error(`An error occurred while running migrations: ${proc.stderr || proc.stdout}`);
100
+ if (client) {
101
+ // check the migrations table is in the database
102
+ // to ensure that migrations ran
103
+ let errorOccurred = false;
104
+ try {
105
+ const migrations = await client.query(`SELECT * FROM "migrations"`);
106
+ errorOccurred = migrations.rowCount == 0;
107
+ }
108
+ catch (e) {
109
+ // avoid error thrown if the migrations table
110
+ // doesn't exist
111
+ errorOccurred = true;
112
+ }
113
+ // ensure that migrations actually ran in case of an uncaught error
114
+ if (errorOccurred) {
115
+ throw new Error(`An error occurred while running migrations: ${proc.stderr || proc.stdout}`);
116
+ }
103
117
  }
104
118
  },
105
119
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-medusa-app",
3
- "version": "1.2.1",
3
+ "version": "1.2.2-next-20230929123725",
4
4
  "description": "Create a Medusa project using a single command.",
5
5
  "type": "module",
6
6
  "exports": "./dist/index.js",
@@ -103,7 +103,7 @@ export default async ({
103
103
  !skipDb && migrations ? await askForAdminEmail(seed, boilerplate) : ""
104
104
  const installNextjs = withNextjsStarter || (await askForNextjsStarter())
105
105
 
106
- const { client, dbConnectionString } = !skipDb
106
+ let { client, dbConnectionString } = !skipDb
107
107
  ? await getDbClientAndCredentials({
108
108
  dbName,
109
109
  dbUrl,
@@ -153,7 +153,7 @@ export default async ({
153
153
  ...factBoxOptions,
154
154
  title: "Creating database...",
155
155
  })
156
- await runCreateDb({ client, dbName, spinner })
156
+ client = await runCreateDb({ client, dbName, spinner })
157
157
 
158
158
  factBoxOptions.interval = displayFactBox({
159
159
  ...factBoxOptions,
@@ -179,6 +179,7 @@ export default async ({
179
179
  migrations,
180
180
  onboardingType: installNextjs ? "nextjs" : "default",
181
181
  nextjsDirectory,
182
+ client,
182
183
  })
183
184
  } catch (e: any) {
184
185
  if (isAbortError(e)) {
@@ -24,13 +24,23 @@ export async function runCreateDb({
24
24
  client: pg.Client
25
25
  dbName: string
26
26
  spinner: Ora
27
- }) {
28
- // create postgres database
27
+ }): Promise<pg.Client> {
28
+ let newClient = client
29
+
29
30
  try {
31
+ // create postgres database
30
32
  await createDb({
31
33
  client,
32
34
  db: dbName,
33
35
  })
36
+
37
+ // create a new connection with database selected
38
+ await client.end()
39
+ newClient = await postgresClient({
40
+ user: client.user,
41
+ password: client.password,
42
+ database: dbName,
43
+ })
34
44
  } catch (e) {
35
45
  spinner.stop()
36
46
  logMessage({
@@ -38,6 +48,8 @@ export async function runCreateDb({
38
48
  type: "error",
39
49
  })
40
50
  }
51
+
52
+ return newClient
41
53
  }
42
54
 
43
55
  async function getForDbName(dbName: string): Promise<{
@@ -5,6 +5,7 @@ type PostgresConnection = {
5
5
  user?: string
6
6
  password?: string
7
7
  connectionString?: string
8
+ database?: string
8
9
  }
9
10
 
10
11
  export default async (connect: PostgresConnection) => {
@@ -7,6 +7,7 @@ import { EOL } from "os"
7
7
  import { displayFactBox, FactBoxOptions } from "./facts.js"
8
8
  import ProcessManager from "./process-manager.js"
9
9
  import { clearProject } from "./clear-project.js"
10
+ import type { Client } from "pg"
10
11
 
11
12
  type PrepareOptions = {
12
13
  directory: string
@@ -23,6 +24,7 @@ type PrepareOptions = {
23
24
  migrations?: boolean
24
25
  onboardingType?: "default" | "nextjs"
25
26
  nextjsDirectory?: string
27
+ client: Client | null
26
28
  }
27
29
 
28
30
  export default async ({
@@ -38,6 +40,7 @@ export default async ({
38
40
  migrations,
39
41
  onboardingType = "default",
40
42
  nextjsDirectory = "",
43
+ client,
41
44
  }: PrepareOptions) => {
42
45
  // initialize execution options
43
46
  const execOptions = {
@@ -145,13 +148,27 @@ export default async ({
145
148
  npxOptions
146
149
  )
147
150
 
148
- // ensure that migrations actually ran in case of an uncaught error
149
- if (!proc.stdout.includes("Migrations completed")) {
150
- throw new Error(
151
- `An error occurred while running migrations: ${
152
- proc.stderr || proc.stdout
153
- }`
154
- )
151
+ if (client) {
152
+ // check the migrations table is in the database
153
+ // to ensure that migrations ran
154
+ let errorOccurred = false
155
+ try {
156
+ const migrations = await client.query(`SELECT * FROM "migrations"`)
157
+ errorOccurred = migrations.rowCount == 0
158
+ } catch (e) {
159
+ // avoid error thrown if the migrations table
160
+ // doesn't exist
161
+ errorOccurred = true
162
+ }
163
+
164
+ // ensure that migrations actually ran in case of an uncaught error
165
+ if (errorOccurred) {
166
+ throw new Error(
167
+ `An error occurred while running migrations: ${
168
+ proc.stderr || proc.stdout
169
+ }`
170
+ )
171
+ }
155
172
  }
156
173
  },
157
174
  })
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "ESNext",
4
- "module": "ESNext",
4
+ "module": "Node16",
5
5
  "moduleResolution": "node16",
6
6
  "outDir": "./dist",
7
7
  "esModuleInterop": true,