Skip to content

Commit

Permalink
feat: 实现配置管理类
Browse files Browse the repository at this point in the history
  • Loading branch information
LokiSharp committed Oct 14, 2023
1 parent e7c6a50 commit 30a2ac1
Show file tree
Hide file tree
Showing 17 changed files with 741 additions and 12 deletions.
27 changes: 27 additions & 0 deletions common/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"plugins": [
"prettier"
],
"ignorePatterns": [
"dist",
"coverage",
"vite.config.ts"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/explicit-member-accessibility": "warn",
"no-unused-vars": "warn",
"prettier/prettier": "error"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": [
"./tsconfig.json"
]
}
}
24 changes: 24 additions & 0 deletions common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions common/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"roots": ["<rootDir>/tests"],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"testMatch": [
"**/__tests__/**/*.+(ts)",
"**/?(*.)+(spec|test).+(ts)"
],
"transform": {
"^.+\\.(ts)$": "ts-jest"
}
}
45 changes: 45 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@neo-screeps/common",
"private": true,
"version": "0.0.0",
"type": "module",
"files": [
"dist"
],
"main": "./dist/common.cjs",
"module": "./dist/common.js",
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/common.js",
"require": "./dist/common.cjs"
},
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx",
"test": "jest",
"coverage": "jest --coverage"
},
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.199",
"@types/node": "^20.8.3",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.7.0",
"lodash": "^4.17.21",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vite-plugin-dts": "^3.6.0"
},
"_moduleAliases": {
"@": "src"
},
"dependencies": {
"q": "^1.5.1"
}
}
18 changes: 18 additions & 0 deletions common/src/ConfigManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { constants } from "@/configs/constants";
import { storage } from "@/configs/storage";
import { dbCollections } from "@/configs/dbCollections";
import { engine } from "@/configs/engine";

export class ConfigManager {
public config = new Config();
}

class Config {
public common = {
constants: constants,
storage: storage,
dbCollections: dbCollections,
system: {},
};
public engine = engine;
}
3 changes: 3 additions & 0 deletions common/src/configs/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const constants = {
OK: 0,
};
1 change: 1 addition & 0 deletions common/src/configs/dbCollections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const dbCollections = [];
4 changes: 4 additions & 0 deletions common/src/configs/engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const engine = {
reportMemoryUsageInterval: 0,
enableInspector: false,
};
5 changes: 5 additions & 0 deletions common/src/configs/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const storage = {
db: {},
queue: {},
env: {},
};
5 changes: 5 additions & 0 deletions common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ConfigManager } from "@/ConfigManager";

export const common = {
configManager: new ConfigManager(),
};
3 changes: 3 additions & 0 deletions common/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function helloWorld(): void {
console.log("Hello, World!");
}
7 changes: 7 additions & 0 deletions common/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { helloWorld } from "@/main";

test("Hello, World!", () => {
const logSpy = jest.spyOn(console, "log");
helloWorld();
expect(logSpy).toHaveBeenCalledWith("Hello, World!");
});
40 changes: 40 additions & 0 deletions common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"alwaysStrict": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"@/*": [
"./src/*"
]
}
},
"include": [
"src/**/*.ts",
"tests/**/*.ts"
],
"references": [
{
"path": "./tsconfig.node.json"
}
]
}
10 changes: 10 additions & 0 deletions common/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
25 changes: 25 additions & 0 deletions common/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineConfig } from 'vite'
import { resolve } from "node:path";
import * as path from "path";
import dts from "vite-plugin-dts";

export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, "src/index.ts"),
formats: ["es", "cjs"],
},
sourcemap: true,
},
resolve: {
alias: {
"@": path.join(__dirname, "src"),
},
},
optimizeDeps: { disabled: true },
plugins: [
dts({
rollupTypes: true
})
]
})
10 changes: 3 additions & 7 deletions driver/src/runtime/UserVM.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import IsolatedVM from "isolated-vm";
import * as v8 from "v8";
import fs from "fs";
import { Config, Metric, NodeJS, StaticTerrainData, VM } from "@/runtime/types";
import { Metric, NodeJS, StaticTerrainData, VM } from "@/runtime/types";
import RuntimeGlobal = NodeJS.RuntimeGlobal;
import { common } from "@neo-screeps/common";

export class UserVM {
public vms: { [userId: string]: VM } = {};
public config: Config = {
engine: {
reportMemoryUsageInterval: 0,
enableInspector: false,
},
};
public config = common.configManager.config;
public snapshot?: NonNullable<unknown>;
public async create(
userId: string,
Expand Down
Loading

0 comments on commit 30a2ac1

Please sign in to comment.