Skip to content

Commit

Permalink
Merge pull request #278 from mStirner/dev
Browse files Browse the repository at this point in the history
Finalizing v2
  • Loading branch information
mStirner authored Jan 28, 2023
2 parents 4395f7e + 7ac1cd0 commit 2831eaf
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 108 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"node_modules",
"logs",
"plugins/*",
"dist"
"dist",
"build"
],
"extends": [
"eslint:recommended"
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ plugins/*
tmp

demo-database*
.vscode/archive-browser
.vscode/archive-browser

build
dist
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ WORKDIR /opt/OpenHaus/backend
COPY --from=builder node_modules node_modules
RUN apk --no-cache add openssl

COPY . ./
COPY ./build/ ./
#COPY ./package.json ./

#ENV HTTP_PORT=8080
Expand Down
177 changes: 84 additions & 93 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const path = require("path");
const pkg = require("./package.json");
const cp = require("child_process");
const crypto = require("crypto");
const fs = require("fs");
const os = require("os");

const PATH_DIST = path.resolve(process.cwd(), "dist");
const PATH_BUILD = path.resolve(process.cwd(), "build");

process.env = Object.assign({
NODE_ENV: "production"
}, process.env);

module.exports = function (grunt) {

// Project configuration.
grunt.initConfig({
pkg,
env: {
options: {
//Shared Options Hash
},
prod: {
NODE_ENV: "production",
}
},
uglify: {
/*
// NOTE: if true, this truncate variables&class names
// Are original names neede for production?!
// i dont thinks so, its only usefull in development
options: {
mangle: false
},*/
mangle: {
toplevel: true
}
},
build: {
files: [{
expand: true,
Expand All @@ -37,85 +40,52 @@ module.exports = function (grunt) {
"!scripts/**",
"!tests/**"
],
dest: path.join(process.cwd(), "dist"),
dest: PATH_BUILD,
//cwd: process.cwd()
}]
}
},
run: {
install: {
options: {
cwd: path.join(process.cwd(), "dist")
},
cmd: "npm",
args: [
"install",
"--prod-only"
]
},
clean: {
cmd: "rm",
args: [
"-rf",
"./dist"
]
},
copy: {
exec: "cp ./package*.json ./dist"
},
folder: {
exec: "mkdir ./dist/logs && mkdir ./dist/plugins"
},
"scripts-mock": {
exec: "mkdir ./dist/scripts && echo 'exit 0' > ./dist/scripts/post-install.sh && chmod +x ./dist/scripts/post-install.sh"
},
"scripts-cleanup": {
exec: "rm ./dist/scripts/post-install.sh && rmdir ./dist/scripts"
}
},
compress: {
main: {
options: {
archive: `backend-v${pkg.version}.tgz`
},
files: [{
expand: true,
src: "**/*",
cwd: "dist/"
}]
}
}
});

// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-run");
grunt.loadNpmTasks("grunt-contrib-compress");
grunt.loadNpmTasks("grunt-env");

grunt.registerTask("clean", ["run:clean"]);

grunt.registerTask("build", [
"run:clean",
"env:prod",
"uglify",
"run:folder",
"run:copy",
]);

// install npm dependencies
grunt.registerTask("install", [
"env:prod",
"run:scripts-mock",
"run:install",
"run:scripts-cleanup"
]);

grunt.registerTask("bundle", [
"build",
"install",
"compress"
]);


grunt.registerTask("build", () => {
[
`rm -rf ${path.join(PATH_BUILD, "/*")}`,
`rm -rf ${path.join(PATH_DIST, "/*")}`,
`mkdir -p ${PATH_BUILD}`,
`mkdir ${path.join(PATH_BUILD, "logs")}`,
`mkdir ${path.join(PATH_BUILD, "plugins")}`,
`mkdir ${path.join(PATH_BUILD, "scripts")}`,
`echo "exit 0" > ${path.join(PATH_BUILD, "scripts/post-install.sh")}`,
`chmod +x ${path.join(PATH_BUILD, "scripts/post-install.sh")}`,
`cp ./package*.json ${PATH_BUILD}`,
"grunt uglify",
].forEach((cmd) => {
cp.execSync(cmd, {
env: process.env,
stdio: "inherit"
});
});
});


grunt.registerTask("install", () => {
cp.execSync(`cd ${PATH_BUILD} && npm install --prod-only`, {
env: process.env,
stdio: "inherit"
});
});


grunt.registerTask("compress", () => {
cp.execSync(`cd ${PATH_BUILD} && tar -czvf ${path.join(PATH_DIST, `${pkg.name}-v${pkg.version}.tgz`)} *`, {
env: process.env,
stdio: "inherit"
});
});


grunt.registerTask("build:docker", () => {
cp.execSync(`docker build . -t openhaus/${pkg.name}:latest --build-arg version=${pkg.version}`, {
Expand All @@ -124,15 +94,40 @@ module.exports = function (grunt) {
});
});


grunt.registerTask("checksum", () => {

let m5f = path.join(PATH_DIST, "./checksums.md5");

fs.rmSync(m5f, { force: true });
let files = fs.readdirSync(PATH_DIST);
let fd = fs.openSync(m5f, "w");

files.forEach((name) => {

let file = path.join(PATH_DIST, name);
let content = fs.readFileSync(file);
let hasher = crypto.createHash("md5");
let hash = hasher.update(content).digest("hex");
fs.writeSync(fd, `${hash}\t${name}${os.EOL}`);

});

fs.closeSync(fd);

});


grunt.registerTask("release", () => {
[
"rm -rf ./dist/*",
"npm run build",
"npm run build:docker",
`docker save openhaus/frontend:latest | gzip > ./${pkg.name}-v${pkg.version}-docker.tgz`,
`mkdir -p ${PATH_DIST}`,
"grunt build",
"grunt compress",
`cd dist && NODE_ENV=production npm install --prod-only --ignore-scripts`,
`cd dist && tar -czvf ../${pkg.name}-v${pkg.version}-bundle.tgz *`
"grunt build:docker",
`docker save openhaus/${pkg.name}:latest | gzip > ${path.join(PATH_DIST, `${pkg.name}-v${pkg.version}-docker.tgz`)}`,
"grunt install",
`cd ${PATH_BUILD} && tar -czvf ${path.join(PATH_DIST, `${pkg.name}-v${pkg.version}-bundle.tgz`)} *`,
"grunt checksum"
].forEach((cmd) => {
cp.execSync(cmd, {
env: process.env,
Expand All @@ -141,9 +136,5 @@ module.exports = function (grunt) {
});
});

// Default task(s).
//grunt.registerTask("default", ["uglify"]);
//grunt.registerTask("install", ["install"]);
//grunt.registerTask("compress", ["compress"]);

};
6 changes: 6 additions & 0 deletions components/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class C_PLUGINS extends COMPONENT {
recursive: true
}, (err) => {

// ignore when folder not exists
if (err?.code === "ENOENT") {
this.logger.warn("Plugin folder does not exists, continue anway.", err);
err = null;
}

next(err || null, item, result, _id);

});
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ process.env = Object.assign({
DATABASE_UPDATE_DEBOUNCE_TIMER: "15",
HTTP_PORT: "8080",
HTTP_ADDRESS: "0.0.0.0",
HTTP_SOCKET: "",
HTTP_SOCKET: "/tmp/open-haus.sock",
LOG_PATH: path.resolve(process.cwd(), "logs"),
LOG_LEVEL: "info",
LOG_DATEFORMAT: "yyyy.mm.dd - HH:MM.ss.l",
Expand Down Expand Up @@ -473,9 +473,9 @@ const starter = new Promise((resolve) => {
});

if (bootable.length > started) {
logger.debug(`${started}/${bootable.length} Plugins started (Someones are ignored! Check the logfiles.)`);
logger.warn(`${started}/${bootable.length} Plugins started (Check the previously logs)`);
} else {
logger.debug(`${started}/${bootable.length} Plugins started`);
logger.info(`${started}/${bootable.length} Plugins started`);
}

logger.info("Startup complete");
Expand Down
Loading

0 comments on commit 2831eaf

Please sign in to comment.