Commit 70b4c31a authored by nanahira's avatar nanahira

first

parent 7331d4c3
Pipeline #6918 passed with stages
in 12 minutes and 15 seconds
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
/koishi.config.*
\ No newline at end of file
webpack.config.js
dist/*
build/*
*.js
/koishi.config.*
\ No newline at end of file
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
/koishi.config.*
\ No newline at end of file
stages:
- build
- combine
- deploy
variables:
GIT_DEPTH: "1"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_TEST_ARM_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
CONTAINER_TEST_X86_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build-x86:
stage: build
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_TEST_X86_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
build-arm:
stage: build
tags:
- docker-arm
script:
- TARGET_IMAGE=$CONTAINER_TEST_ARM_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
combine:
stage: combine
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_TEST_IMAGE
- SOURCE_IMAGE_2=$CONTAINER_TEST_ARM_IMAGE
- SOURCE_IMAGE_1=$CONTAINER_TEST_X86_IMAGE
- docker pull $SOURCE_IMAGE_1
- docker pull $SOURCE_IMAGE_2
- docker manifest create $TARGET_IMAGE --amend $SOURCE_IMAGE_1 --amend
$SOURCE_IMAGE_2
- docker manifest push $TARGET_IMAGE
deploy_latest:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_RELEASE_IMAGE
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- master
deploy_tag:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- tags
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/koishi.config.*
\ No newline at end of file
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:bullseye-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /app/dist ./dist
COPY ./docker ./docker
ENTRYPOINT [ "./docker/entrypoint.sh" ]
CMD [ "npm", "start" ]
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Koishi Bootstrap
Koishi bootstraper
\ No newline at end of file
Koishi bootstrap script, for Docker.
## Docker
The docker image is at `git-registry.mycard.moe/3rdeye/koishi-bootstrap`.
Mount your config file, either `koishi.config.yml` or `koishi.config.js`, to `/app` , and run it.
## Auto plugin install
This Docker image will auto install every plugin mentioned in Koishi config file, whose config includes `$install` variable`.
`$install` variable could be as follows.
* `true` for latest version.
* a specific version name, eg. `^4.0.0-beta.1`.
## Config example
```yaml
port: 8080
logger:
root: logs
allowWrite: false
# 插件设置
plugins:
act:
$install: latest
adapter-onebot:
$install: true # Will always install the latest version
bots:
- protocol: 'ws'
endpoint: 'wss://example.com'
selfId: '1111111111'
token: 'token'
verifer:
$install: '1.0.0-beta.0' # Will install this version. NPM format is also supported.
```
\ No newline at end of file
#!/bin/sh
npm run plugin
pluginInstallCode=$?
if [ "$pluginInstallCode" != 0 ]; then
echo "Plugin install failed: exit $pluginInstallCode"
exit "$pluginInstallCode"
fi
"$@"
\ No newline at end of file
#!/bin/bash
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier
This diff is collapsed.
{
"name": "koishi-bootstrap",
"description": "Koishi bootstraper, for Docker.",
"version": "1.0.0",
"scripts": {
"lint": "eslint --fix .",
"build": "tsc",
"plugin": "node dist/install-plugin.js",
"start": "koishi start"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/koishi-bootstrap.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/koishi-bootstrap/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/koishi-bootstrap",
"devDependencies": {
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"prettier": "^2.4.1",
"typescript": "^4.5.2"
},
"dependencies": {
"@koishijs/cli": "^4.0.0-beta.1",
"koishi": "^4.0.0-beta.1",
"load-json-file": "^6.2.0",
"ws": "^8.2.3",
"yaml": "^1.10.2"
}
}
export interface PluginInstallConfig {
$install: boolean | string;
}
export interface KoishiConfig {
plugins: Record<string, PluginInstallConfig>;
}
export interface PackageConfig {
dependencies: Record<string, string>;
}
import yaml from 'yaml';
import fs from 'fs';
import { KoishiConfig, PackageConfig } from './def/interfaces';
import { Logger } from 'koishi';
import { exec } from 'child_process';
import { promisify } from 'util';
import loadJsonFile from 'load-json-file';
import path from 'path';
const execAsync = promisify(exec);
const logger = new Logger('bootstrap');
async function loadFromYaml(): Promise<KoishiConfig> {
return yaml.parse(await fs.promises.readFile('./koishi.config.yml', 'utf-8'));
}
function loadFromJs(): KoishiConfig {
return require('./koishi.config.js');
}
async function loadConfig(): Promise<KoishiConfig | undefined> {
try {
logger.info(`Reading config from ./koishi.config.yml.`);
return await loadFromYaml();
} catch (e) {
logger.warn(
`Failed reading from YAML: ${(e as any).toString()} , trying JS.`,
);
logger.info(`Reading config from ./koishi.config.js.`);
try {
return loadFromJs();
} catch (e) {
logger.warn(`Failed reading from JS: ${(e as any).toString()} .`);
return;
}
}
}
async function getPackageJsonPackages() {
const { dependencies } = await loadJsonFile<PackageConfig>('./package.json');
return dependencies;
}
async function checkPluginExists(name: string) {
const deps = await getPackageJsonPackages();
const entry = Object.entries(deps).find(
([packageName, version]) =>
packageName === `@koishijs/plugin-${name}` ||
packageName.endsWith(`koishi-plugin-${name}`),
);
if (!entry) {
return;
}
try {
const stat = await fs.promises.stat(path.join('node_modules', entry[0]));
if (!stat?.isDirectory()) {
return;
}
return entry[1];
} catch (e) {
return;
}
}
async function npmInstall(name: string) {
logger.info(`Installing package ${name}.`);
try {
await execAsync(`npm install --loglevel error ${name}`);
const installedVersionEntry = Object.entries(
await getPackageJsonPackages(),
).find(([packageName]) => name.startsWith(packageName));
logger.info(`Package ${name}@${installedVersionEntry![1]} installed.`);
return true;
} catch (e) {
logger.warn(`Package ${name} not found.`);
return false;
}
}
async function tryInstallPackages(names: string[]) {
for (const name of names) {
if (await npmInstall(name)) {
return true;
}
}
return false;
}
async function installPlugin(name: string, version?: string) {
logger.info(`Installing plugin ${name}@${version || 'unknown'}.`);
const existingPluginVersion = await checkPluginExists(name);
if (
existingPluginVersion &&
(existingPluginVersion === version ||
!version ||
version === 'latest' ||
version === 'next')
) {
logger.info(`Plugin ${name}@${existingPluginVersion} exists, skipping.`);
return;
}
const installList: string[] = [];
if (version) {
installList.push(`@koishijs/plugin-${name}@${version}`);
installList.push(`koishi-plugin-${name}@${version}`);
} else {
installList.push(`@koishijs/plugin-${name}@next`);
installList.push(`@koishijs/plugin-${name}@latest`);
installList.push(`koishi-plugin-${name}@next`);
installList.push(`koishi-plugin-${name}@latest`);
}
const result = await tryInstallPackages(installList);
if (!result) {
logger.error(`Plugin ${name}@${version || 'unknown'} install failed.`);
}
}
async function main() {
logger.info(`Bootstrapping`);
const config = await loadConfig();
const plugins = config?.plugins;
if (!plugins) {
logger.warn(`No plugins found, exiting.`);
return;
}
for (const [name, info] of Object.entries(plugins)) {
if (!info.$install) {
continue;
}
const version =
typeof info.$install !== 'boolean' ? info.$install : undefined;
await installPlugin(name, version);
}
logger.info(`Bootstrap finished.`);
}
main();
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true,
"strict": true,
"skipLibCheck": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment