Skip to content

Feature/cc config.json change #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@ dist
.pnp.*

# JetBrains
.idea/
.idea/

config.json
config.dev.json
5 changes: 0 additions & 5 deletions .run/test.run.xml

This file was deleted.

5 changes: 5 additions & 0 deletions EnvironmentPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.setEnvPath=function () {
const Path = require("path");
global.configPath = Path.resolve(__dirname, global.mode === "run" ? "./config.json" : "./config.dev.json");
global.config = require(configPath);
}
197 changes: 148 additions & 49 deletions UpdateSystem.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,158 @@
const pack=require('./package.json');
let ver;
let mainpack;

switch (process.argv[2]){
case "test":
mainpack=require("../update-test/package.json");
ver="0.0.0"
break;
case "dev":
mainpack=require("../update-test/package.json");
ver=pack.version;
break;
case "run":
mainpack=require("../bot-main/package.json");
ver=pack.version;
break;
default:
console.error("\u001b[33mPlease set command line argument to test, dev or run\u001b[0m");
console.error("Running type set to test");
mainpack=require("../update-test/package.json");
ver="0.0.0";
break;
}
const mainver=mainpack.version;
const pack = require('./package.json');
const Path =require("path");
const db = require("./functions/db.js");
const Enquirer = require("enquirer");
const VersionList = require("./VersionList").versions;
const EnvPath=require("./EnvironmentPath.js");
const {setEnvPath} = require("./EnvironmentPath");

console.log(`${mainver} ---> ${ver}`);

const versions=require('./VersionList.json').versions;
const ind=versions.findIndex(versionData=>versionData.version===ver);
const mainInd=versions.findIndex(versionData=>versionData.version===mainver);
(async() => {
await modeBranch(process.argv[2]);
await db.close();
})();

if(ind>mainInd){
for(let i=mainInd+1;i<=ind;i++){
if(versions[i].update===true){
console.log(`Update v${versions[i-1].version} to v${versions[i].version}`);
const Update=require(`./${versions[i].version}/Update.js`);
Update.update();
}
else{
console.log(`No updates between v${versions[i-1].version} and v${versions[i].version}`);
}
/***
* Branch processing by mode
* @param {string} mode Running type
*/
async function modeBranch(mode) {
switch(mode) {
case "test":
global.targetPath=Path.resolve(__dirname, "../update-test");
global.mode=mode;
await testMord();
break;
case "dev":
global.targetPath=Path.resolve(__dirname, "../update-test");
global.mode=mode;
await productMode();
break
case "run":
global.targetPath=Path.resolve(__dirname, "../bot-main");
global.mode=mode;
await productMode();
break;
case undefined:
await modeChoice();
break;
default:
console.log("Cannot find the mode "+mode);
await modeChoice();
break;
}
}

/***
* Running process of test mode
*/
async function testMord() {
let runType;
await (async() => {
const testRunSet = {
type: "select",
name: "setRunningType",
message: "Please select running type",
choices: ["Update", "Outdate", "Both"]
};
runType = (await Enquirer.prompt(testRunSet)).setRunningType;
})();
global.targetPath=Path.resolve(__dirname, "../update-test");
switch (runType) {
case "Update":
await RunUpdate();
break;
case "Outdate":
await RunOutdate();
break;
case "Both":
await RunUpdate();
await RunOutdate();
}

}

/***
* Running Update.js in ./test/
*/
async function RunUpdate(){
const Update = require("./test/Update.js");
await Update.update();
}
else if(ind<mainInd){
for(let i=mainInd;i<ind+1;i--){
if(versions[i].outdate===true){
console.log(`Outdate v${versions[i].version} to v${versions[i-1].version}`);
const Outdate=require(`./${versions[i].version}/Outdate.js`);
Outdate.outdate;

/***
* Running Outdate.js in ./test/
*/
async function RunOutdate(){
const Outdate = require("./test/Outdate.js");
await Outdate.outdate();
}

/***
* Running process
*/
async function productMode() {
let targetV;
await (async() => {
const targetSelect = {
type: "select",
name: "setTargetVer",
message: "Please select the update (outdate) destination",
choices: VersionList.map(elem => elem.version)
};
targetV = (await Enquirer.prompt(targetSelect)).setTargetVer;
})();
await setEnvPath();
global.targetPath=Path.resolve(__dirname, "../bot-main");

let mainPack = require(Path.resolve(targetPath,"/package.json"));
const mainV = mainPack.version;
console.log(`${mainV} ---> ${targetV}`);

const versions = require('./VersionList.json').versions;
const targetInd = versions.findIndex(versionData => versionData.version === targetV);
const mainInd = versions.findIndex(versionData => versionData.version === mainV);

if(targetInd> mainInd) {
for(let i = mainInd + 1; i <= targetInd; i++) {
if(versions[i].update === true) {
console.log(`Update v${versions[i - 1].version} to v${versions[i].version}`);
const Update = require(`./${versions[i].version}/Update.js`);
await Update.update();
}
else {
console.log(`No updates between v${versions[i - 1].version} and v${versions[i].version}`);
}
}
else{
console.log(`No outdates between v${versions[i].version} and v${versions[i-1].version}`);
}
else if(targetInd < mainInd) {
for(let i = mainInd; i < targetInd + 1; i--) {
if(versions[i].outdate === true) {
console.log(`Outdate v${versions[i].version} to v${versions[i - 1].version}`);
const Outdate = require(`./${versions[i].version}/Outdate.js`);
await Outdate.outdate();
}
else {
console.log(`No outdates between v${versions[i].version} and v${versions[i - 1].version}`);
}
}
}
else if(targetInd === mainInd) {
console.log("No updates needed.")
}
}
if(ind===mainInd){
console.log("No updates needed.")

/***
*Select running mode
*/
async function modeChoice() {
await(async() => {
const modeSelect={
type:"select",
name:"setMode",
message:"Please select running mode",
choices: ["test","dev","run"]
}
await modeBranch((await Enquirer.prompt(modeSelect)).setMode);
})();
}
2 changes: 1 addition & 1 deletion VersionList.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
{"version": "4.0.3","update": false,"outdate": false},
{"version": "4.0.4","update": false,"outdate": false},
{"version": "4.0.5","update": false,"outdate": false},
{"version": "0.0.0","update": true,"outdate": false}
{"version": "4.0.6","update": false,"outdate": false}
]
}
82 changes: 82 additions & 0 deletions functions/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const { MongoClient, ServerApiVersion } = require("mongodb");
const EnvPath=require("../EnvironmentPath");
EnvPath.setEnvPath();
const dbClient = new MongoClient(global.config.db, { serverApi: ServerApiVersion.v1 });

/***
* データベースからデータを取得する
* @param dbName 取得先データベース名
* @param collectionName 取得先コレクション名
* @param filter フィルターを指定
* @returns Promise<[object]>
*/
exports.find = async function (dbName, collectionName, filter) {
const collection = dbClient.db(dbName).collection(collectionName);
return await collection.find(filter).toArray();
}

/***
* データベースを更新する
* @param dbName 更新先データベース名
* @param collectionName 更新先コレクション名
* @param filter 更新対象のフィルターを指定
* @param update update operatorを用いた更新内容の記述
*/
exports.update = async function run(dbName, collectionName, filter, update) {
try {
const database = dbClient.db(dbName);
const collection = database.collection(collectionName);

const result = await collection.updateOne(filter,update)
await console.log(`${dbName}.${collectionName}を更新`,`DB更新実行`);
} catch(err) {
await console.error(`${dbName}.${collectionName}を更新できませんでした`,err,`DB更新失敗`);
}
}


/***
* データベースにレコードを追加する
* @param dbName 追加先データベース名
* @param collectionName 追加先コレクション名
* @param object 追加するレコード(オブジェクト型)
*/
exports.insert = async function run(dbName, collectionName, object) {

try {
const database = dbClient.db(dbName);
const collection = database.collection(collectionName);

const result = await collection.insertOne(object);
await console.log(`${dbName}.${collectionName}にレコード追加`,`DB追加実行`);
} catch(err) {
await console.error(`${dbName}.${collectionName}にレコードを追加できませんでした`,err,`DB追加失敗`);
}
}
/***
* データベースにレコードを削除する
* @param dbName 削除元データベース名
* @param collectionName 削除元コレクション名
* @param filter 削除対象のフィルターを指定
*/
exports.delete = async function run(dbName,collectionName,filter) {

try {
const database = dbClient.db(dbName);
const collection = database.collection(collectionName);

const result = await collection.deleteOne(filter);
await console.log(`${dbName}.${collectionName}からレコード削除`,`DBレコード削除実行`);
} catch(err) {
await console.error(`${dbName}.${collectionName}からレコードを削除できませんでした`,err,`DB削除失敗`);
}
}

/***
* データーベースとの接続を切る
*/
exports.close = async function close(){
await dbClient.close();
console.log("DBとの接続を終了しました。");
}
//引数の詳細については、mongodbの公式ドキュメントを参照すること
Loading