feat: creates support for swaps on the storefront (#355)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
const path = require("path");
|
||||
const { spawn } = require("child_process");
|
||||
const path = require("path")
|
||||
const { spawn } = require("child_process")
|
||||
|
||||
const { setPort } = require("./use-api");
|
||||
const { setPort } = require("./use-api")
|
||||
|
||||
module.exports = ({ cwd, verbose }) => {
|
||||
const serverPath = path.join(__dirname, "test-server.js");
|
||||
const serverPath = path.join(__dirname, "test-server.js")
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const medusaProcess = spawn("node", [path.resolve(serverPath)], {
|
||||
@@ -18,15 +18,15 @@ module.exports = ({ cwd, verbose }) => {
|
||||
stdio: verbose
|
||||
? ["inherit", "inherit", "inherit", "ipc"]
|
||||
: ["ignore", "ignore", "ignore", "ipc"],
|
||||
});
|
||||
})
|
||||
|
||||
medusaProcess.on("uncaughtException", (err) => {
|
||||
medusaProcess.kill();
|
||||
});
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
medusaProcess.on("message", (port) => {
|
||||
setPort(port);
|
||||
resolve(medusaProcess);
|
||||
});
|
||||
});
|
||||
};
|
||||
setPort(port)
|
||||
resolve(medusaProcess)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
const path = require("path");
|
||||
const express = require("express");
|
||||
const getPort = require("get-port");
|
||||
const importFrom = require("import-from");
|
||||
const path = require("path")
|
||||
const express = require("express")
|
||||
const getPort = require("get-port")
|
||||
const importFrom = require("import-from")
|
||||
|
||||
const initialize = async () => {
|
||||
const app = express();
|
||||
const app = express()
|
||||
|
||||
const loaders = importFrom(process.cwd(), "@medusajs/medusa/dist/loaders")
|
||||
.default;
|
||||
.default
|
||||
|
||||
const { dbConnection } = await loaders({
|
||||
directory: path.resolve(process.cwd()),
|
||||
expressApp: app,
|
||||
});
|
||||
})
|
||||
|
||||
const PORT = await getPort();
|
||||
const PORT = await getPort()
|
||||
|
||||
return {
|
||||
db: dbConnection,
|
||||
app,
|
||||
port: PORT,
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const setup = async () => {
|
||||
const { app, port } = await initialize();
|
||||
const { app, port } = await initialize()
|
||||
|
||||
app.listen(port, (err) => {
|
||||
process.send(port);
|
||||
});
|
||||
};
|
||||
process.send(port)
|
||||
})
|
||||
}
|
||||
|
||||
setup();
|
||||
setup()
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
const axios = require("axios").default;
|
||||
const axios = require("axios").default
|
||||
|
||||
const ServerTestUtil = {
|
||||
port_: null,
|
||||
client_: null,
|
||||
|
||||
setPort: function (port) {
|
||||
this.client_ = axios.create({ baseURL: `http://localhost:${port}` });
|
||||
this.client_ = axios.create({ baseURL: `http://localhost:${port}` })
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const instance = ServerTestUtil;
|
||||
const instance = ServerTestUtil
|
||||
|
||||
module.exports = {
|
||||
setPort: function (port) {
|
||||
instance.setPort(port);
|
||||
instance.setPort(port)
|
||||
},
|
||||
useApi: function () {
|
||||
return instance.client_;
|
||||
return instance.client_
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: path.join(__dirname, "../.env") });
|
||||
const path = require("path")
|
||||
require("dotenv").config({ path: path.join(__dirname, "../.env") })
|
||||
|
||||
const { dropDatabase, createDatabase } = require("pg-god");
|
||||
const { createConnection } = require("typeorm");
|
||||
const { dropDatabase, createDatabase } = require("pg-god")
|
||||
const { createConnection } = require("typeorm")
|
||||
|
||||
const DB_USERNAME = process.env.DB_USERNAME || "postgres";
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD || "";
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration`;
|
||||
const DB_USERNAME = process.env.DB_USERNAME || "postgres"
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD || ""
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration`
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
};
|
||||
}
|
||||
|
||||
const keepTables = [
|
||||
"staged_job",
|
||||
@@ -20,57 +20,57 @@ const keepTables = [
|
||||
"payment_provider",
|
||||
"country",
|
||||
"currency",
|
||||
];
|
||||
]
|
||||
|
||||
let connectionType = "postgresql";
|
||||
let connectionType = "postgresql"
|
||||
|
||||
const DbTestUtil = {
|
||||
db_: null,
|
||||
|
||||
setDb: function (connection) {
|
||||
this.db_ = connection;
|
||||
this.db_ = connection
|
||||
},
|
||||
|
||||
clear: async function () {
|
||||
this.db_.synchronize(true);
|
||||
this.db_.synchronize(true)
|
||||
},
|
||||
|
||||
teardown: async function () {
|
||||
const entities = this.db_.entityMetadatas;
|
||||
const manager = this.db_.manager;
|
||||
const entities = this.db_.entityMetadatas
|
||||
const manager = this.db_.manager
|
||||
|
||||
if (connectionType === "sqlite") {
|
||||
await manager.query(`PRAGMA foreign_keys = OFF`);
|
||||
await manager.query(`PRAGMA foreign_keys = OFF`)
|
||||
} else {
|
||||
await manager.query(`SET session_replication_role = 'replica';`);
|
||||
await manager.query(`SET session_replication_role = 'replica';`)
|
||||
}
|
||||
|
||||
for (const entity of entities) {
|
||||
if (keepTables.includes(entity.tableName)) {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
await manager.query(`DELETE FROM "${entity.tableName}";`);
|
||||
await manager.query(`DELETE FROM "${entity.tableName}";`)
|
||||
}
|
||||
if (connectionType === "sqlite") {
|
||||
await manager.query(`PRAGMA foreign_keys = ON`);
|
||||
await manager.query(`PRAGMA foreign_keys = ON`)
|
||||
} else {
|
||||
await manager.query(`SET session_replication_role = 'origin';`);
|
||||
await manager.query(`SET session_replication_role = 'origin';`)
|
||||
}
|
||||
},
|
||||
|
||||
shutdown: async function () {
|
||||
await this.db_.close();
|
||||
const databaseName = "medusa-integration";
|
||||
return await dropDatabase({ databaseName }, pgGodCredentials);
|
||||
await this.db_.close()
|
||||
const databaseName = "medusa-integration"
|
||||
return await dropDatabase({ databaseName }, pgGodCredentials)
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const instance = DbTestUtil;
|
||||
const instance = DbTestUtil
|
||||
|
||||
module.exports = {
|
||||
initDb: async function ({ cwd }) {
|
||||
const configPath = path.resolve(path.join(cwd, `medusa-config.js`));
|
||||
const configPath = path.resolve(path.join(cwd, `medusa-config.js`))
|
||||
|
||||
const modelsLoader = require(path.join(
|
||||
cwd,
|
||||
@@ -80,21 +80,21 @@ module.exports = {
|
||||
`dist`,
|
||||
`loaders`,
|
||||
`models`
|
||||
)).default;
|
||||
const entities = modelsLoader({}, { register: false });
|
||||
)).default
|
||||
const entities = modelsLoader({}, { register: false })
|
||||
|
||||
const { projectConfig } = require(configPath);
|
||||
const { projectConfig } = require(configPath)
|
||||
if (projectConfig.database_type === "sqlite") {
|
||||
connectionType = "sqlite";
|
||||
connectionType = "sqlite"
|
||||
const dbConnection = await createConnection({
|
||||
type: "sqlite",
|
||||
database: projectConfig.database_database,
|
||||
synchronize: true,
|
||||
entities,
|
||||
});
|
||||
})
|
||||
|
||||
instance.setDb(dbConnection);
|
||||
return dbConnection;
|
||||
instance.setDb(dbConnection)
|
||||
return dbConnection
|
||||
} else {
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
@@ -105,31 +105,31 @@ module.exports = {
|
||||
`dist`,
|
||||
`migrations`
|
||||
)
|
||||
);
|
||||
)
|
||||
|
||||
const databaseName = "medusa-integration";
|
||||
await createDatabase({ databaseName }, pgGodCredentials);
|
||||
const databaseName = "medusa-integration"
|
||||
await createDatabase({ databaseName }, pgGodCredentials)
|
||||
|
||||
const connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
migrations: [`${migrationDir}/*.js`],
|
||||
});
|
||||
})
|
||||
|
||||
await connection.runMigrations();
|
||||
await connection.close();
|
||||
await connection.runMigrations()
|
||||
await connection.close()
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities,
|
||||
});
|
||||
})
|
||||
|
||||
instance.setDb(dbConnection);
|
||||
return dbConnection;
|
||||
instance.setDb(dbConnection)
|
||||
return dbConnection
|
||||
}
|
||||
},
|
||||
useDb: function () {
|
||||
return instance;
|
||||
return instance
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,39 +3,39 @@ const ServerTestUtil = {
|
||||
app_: null,
|
||||
|
||||
setApp: function (app) {
|
||||
this.app_ = app;
|
||||
this.app_ = app
|
||||
},
|
||||
|
||||
start: async function () {
|
||||
this.server_ = await new Promise((resolve, reject) => {
|
||||
const s = this.app_.listen(PORT, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
reject(err)
|
||||
}
|
||||
});
|
||||
resolve(s);
|
||||
});
|
||||
})
|
||||
resolve(s)
|
||||
})
|
||||
},
|
||||
|
||||
kill: function () {
|
||||
return new Promise((resolve, _) => {
|
||||
if (this.server_) {
|
||||
this.server_.close(() => resolve());
|
||||
this.server_.close(() => resolve())
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
resolve()
|
||||
})
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const instance = ServerTestUtil;
|
||||
const instance = ServerTestUtil
|
||||
|
||||
module.exports = {
|
||||
setApp: function (app) {
|
||||
instance.setApp(app);
|
||||
return instance;
|
||||
instance.setApp(app)
|
||||
return instance
|
||||
},
|
||||
|
||||
useServer: function () {
|
||||
return instance;
|
||||
return instance
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user