From 73260384248ad559ba3427dd86f09ede0bca7224 Mon Sep 17 00:00:00 2001 From: garronej Date: Thu, 24 Oct 2024 23:21:12 +0000 Subject: [PATCH] Fix linking script on windows --- scripts/build/vendorFrontendDependencies.ts | 8 +- scripts/link-in-app.ts | 16 ++- scripts/link-in-starter.ts | 111 +++++++++++++------- scripts/tools/removeNodeModules.ts | 27 +++++ 4 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 scripts/tools/removeNodeModules.ts diff --git a/scripts/build/vendorFrontendDependencies.ts b/scripts/build/vendorFrontendDependencies.ts index b26c4b3a..d3ec18cc 100644 --- a/scripts/build/vendorFrontendDependencies.ts +++ b/scripts/build/vendorFrontendDependencies.ts @@ -1,11 +1,5 @@ import * as fs from "fs"; -import { - join as pathJoin, - relative as pathRelative, - basename as pathBasename, - dirname as pathDirname, - sep as pathSep -} from "path"; +import { join as pathJoin, basename as pathBasename, dirname as pathDirname } from "path"; import { assert } from "tsafe/assert"; import { run } from "../shared/run"; import { cacheDirPath as cacheDirPath_base } from "../shared/cacheDirPath"; diff --git a/scripts/link-in-app.ts b/scripts/link-in-app.ts index da7661fc..6fad1afb 100644 --- a/scripts/link-in-app.ts +++ b/scripts/link-in-app.ts @@ -55,7 +55,6 @@ const commonThirdPartyDeps = [ Buffer.from(modifiedPackageJsonContent, "utf8") ); } - const yarnGlobalDirPath = pathJoin(rootDirPath, ".yarn_home"); fs.rmSync(yarnGlobalDirPath, { recursive: true, force: true }); @@ -64,6 +63,21 @@ fs.mkdirSync(yarnGlobalDirPath); const execYarnLink = (params: { targetModuleName?: string; cwd: string }) => { const { targetModuleName, cwd } = params; + if (targetModuleName === undefined) { + const packageJsonFilePath = pathJoin(cwd, "package.json"); + + const packageJson = JSON.parse( + fs.readFileSync(packageJsonFilePath).toString("utf8") + ); + + delete packageJson["packageManager"]; + + fs.writeFileSync( + packageJsonFilePath, + Buffer.from(JSON.stringify(packageJson, null, 2)) + ); + } + const cmd = [ "yarn", "link", diff --git a/scripts/link-in-starter.ts b/scripts/link-in-starter.ts index 4b41e14d..aded7f42 100644 --- a/scripts/link-in-starter.ts +++ b/scripts/link-in-starter.ts @@ -1,49 +1,88 @@ import * as fs from "fs"; -import { join } from "path"; -import { startRebuildOnSrcChange } from "./shared/startRebuildOnSrcChange"; -import { crawl } from "../src/bin/tools/crawl"; +import { join as pathJoin, sep as pathSep } from "path"; import { run } from "./shared/run"; +import cliSelect from "cli-select"; +import { getThisCodebaseRootDirPath } from "../src/bin/tools/getThisCodebaseRootDirPath"; +import chalk from "chalk"; +import { removeNodeModules } from "./tools/removeNodeModules"; +import { startRebuildOnSrcChange } from "./shared/startRebuildOnSrcChange"; -{ - const dirPath = "node_modules"; +(async () => { + const parentDirPath = pathJoin(getThisCodebaseRootDirPath(), ".."); - try { - fs.rmSync(dirPath, { recursive: true, force: true }); - } catch { - // NOTE: This is a workaround for windows - // we can't remove locked executables. + const { starterName } = await (async () => { + const starterNames = fs + .readdirSync(parentDirPath) + .filter( + basename => + basename.includes("starter") && + basename.includes("angular") && + fs.statSync(pathJoin(parentDirPath, basename)).isDirectory() + ); - crawl({ - dirPath, - returnedPathsType: "absolute" - }).forEach(filePath => { - try { - fs.rmSync(filePath, { force: true }); - } catch (error) { - if (filePath.endsWith(".exe")) { - return; - } - throw error; + if (starterNames.length === 0) { + console.log( + chalk.red( + `No starter found. Keycloakify Angular starter found in ${parentDirPath}` + ) + ); + process.exit(-1); + } + + const starterName = await (async () => { + if (starterNames.length === 1) { + return starterNames[0]; } - }); - } -} -fs.rmSync("dist", { recursive: true, force: true }); -fs.rmSync(".yarn_home", { recursive: true, force: true }); + console.log(chalk.cyan(`\nSelect a starter to link in:`)); -run("yarn install"); -run("yarn build"); + const { value } = await cliSelect({ + values: starterNames.map(starterName => `..${pathSep}${starterName}`) + }).catch(() => { + process.exit(-1); + }); -const starterName = "keycloakify-starter"; + return value.split(pathSep)[1]; + })(); -fs.rmSync(join("..", starterName, "node_modules"), { - recursive: true, - force: true -}); + return { starterName }; + })(); -run("yarn install", { cwd: join("..", starterName) }); + const startTime = Date.now(); -run(`npx tsx ${join("scripts", "link-in-app.ts")} ${starterName}`); + console.log(chalk.cyan(`\n\nLinking in ..${pathSep}${starterName}...`)); -startRebuildOnSrcChange(); + removeNodeModules({ + nodeModulesDirPath: pathJoin(getThisCodebaseRootDirPath(), "node_modules") + }); + + fs.rmSync(pathJoin(getThisCodebaseRootDirPath(), "dist"), { + recursive: true, + force: true + }); + fs.rmSync(pathJoin(getThisCodebaseRootDirPath(), ".yarn_home"), { + recursive: true, + force: true + }); + + run("yarn install"); + run("yarn build"); + + const starterDirPath = pathJoin(parentDirPath, starterName); + + removeNodeModules({ + nodeModulesDirPath: pathJoin(starterDirPath, "node_modules") + }); + + run("yarn install", { cwd: starterDirPath }); + + run(`npx tsx ${pathJoin("scripts", "link-in-app.ts")} ${starterName}`); + + const durationSeconds = Math.round((Date.now() - startTime) / 1000); + + await new Promise(resolve => setTimeout(resolve, 1000)); + + startRebuildOnSrcChange(); + + console.log(chalk.green(`\n\nLinked in ${starterName} in ${durationSeconds}s`)); +})(); diff --git a/scripts/tools/removeNodeModules.ts b/scripts/tools/removeNodeModules.ts new file mode 100644 index 00000000..1dc2125c --- /dev/null +++ b/scripts/tools/removeNodeModules.ts @@ -0,0 +1,27 @@ +import * as fs from "fs"; +import { crawl } from "../../src/bin/tools/crawl"; + +export function removeNodeModules(params: { nodeModulesDirPath: string }) { + const { nodeModulesDirPath } = params; + + try { + fs.rmSync(nodeModulesDirPath, { recursive: true, force: true }); + } catch { + // NOTE: This is a workaround for windows + // we can't remove locked executables. + + crawl({ + dirPath: nodeModulesDirPath, + returnedPathsType: "absolute" + }).forEach(filePath => { + try { + fs.rmSync(filePath, { force: true }); + } catch (error) { + if (filePath.endsWith(".exe")) { + return; + } + throw error; + } + }); + } +}