From 4afe9b3c4e785fbddf118762f2efbfa270088c5a Mon Sep 17 00:00:00 2001 From: "dennis.ludwig" Date: Thu, 10 Jun 2021 14:27:44 +0200 Subject: [PATCH] Resolve paths and add check for yarn/npx --- config/paths.js | 19 +++++++++++++++++++ package.json | 2 +- scripts/buildRadar.js | 32 ++++++++++++++++++++++---------- scripts/generateJson.js | 2 -- 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 config/paths.js diff --git a/config/paths.js b/config/paths.js new file mode 100644 index 0000000..e200379 --- /dev/null +++ b/config/paths.js @@ -0,0 +1,19 @@ +"use strict"; + +const path = require("path"); +const fs = require("fs"); + +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath = "") => + path.resolve(appDirectory, relativePath); +const templateDirectory = fs.realpathSync(__dirname); +const resolveTemplate = (relativePath = "") => + path.resolve(templateDirectory, "..", relativePath); + +module.exports = { + template: resolveTemplate(), + templateBuild: resolveTemplate("build"), + appRdJson: resolveApp("build/rd.json"), + appBuild: resolveApp("build"), + appYarnLock: resolveApp("yarn.lock"), +}; diff --git a/package.json b/package.json index 1119044..86cb667 100644 --- a/package.json +++ b/package.json @@ -14,13 +14,13 @@ "buildRadar": "scripts/buildRadar.js" }, "scripts": { + "prepare": "husky install && yarn build:tasks && yarn build", "start": "react-scripts start", "build": "react-scripts build", "build:tasks": "tsc --project tsconfig.tasks.json", "test": "react-scripts test", "ts:check": "tsc --noEmit", "lint": "yarn ts:check", - "prepare": "husky install", "eject": "react-scripts eject" }, "dependencies": { diff --git a/scripts/buildRadar.js b/scripts/buildRadar.js index 030d89b..7404c0d 100644 --- a/scripts/buildRadar.js +++ b/scripts/buildRadar.js @@ -14,6 +14,7 @@ process.on("unhandledRejection", (err) => { }); const fs = require("fs-extra"); +const paths = require("../config/paths"); const runCommand = (command, args) => { const cp = require("child_process"); @@ -37,17 +38,28 @@ const runCommand = (command, args) => { }); }; -// TODO: Check if rd.json was created -// TODO: Check how to empty folders without interfere with generateJson job -// TODO: add dist folder for precompiled builder +const buildTemplate = () => { + const packageManager = fs.existsSync(paths.appYarnLock) ? "yarn" : "npx"; -process.chdir("node_modules/aoe_technology_radar"); -fs.emptyDirSync("build"); -runCommand("yarn build") - .then(() => { - fs.copySync("build", "../../build"); - }) - .catch((error) => { + fs.emptyDirSync(paths.templateBuild); + process.chdir(paths.template); + return runCommand(`${packageManager} build`).catch((error) => { console.error(error); process.exit(1); }); +}; + +// TODO: Use other output folder than bin for compiled tasks, because bin is misleading with node bin folder +// TODO: add dist folder for precompiled builder + +if (fs.existsSync(paths.appRdJson)) { + buildTemplate().then(() => { + fs.copySync(paths.templateBuild, paths.appBuild); + console.log(`${paths.appBuild} was created and can be deployed.`); + }); +} else { + console.error( + `${paths.appRdJson} does not exist. You have to generate it first.` + ); + process.exit(1); +} diff --git a/scripts/generateJson.js b/scripts/generateJson.js index e7890eb..28825dc 100644 --- a/scripts/generateJson.js +++ b/scripts/generateJson.js @@ -13,8 +13,6 @@ process.on("unhandledRejection", (err) => { throw err; }); -// TODO: execute yarn build:tasks to create bin folder with compiled radarJsonGenerator - require("../bin/tasks/radarjson") .radarJsonGenerator() .then(() => {