Fix linking script on windows

This commit is contained in:
garronej 2024-10-24 23:21:12 +00:00
parent 12e632d221
commit 7326038424
4 changed files with 118 additions and 44 deletions

View File

@ -1,11 +1,5 @@
import * as fs from "fs"; import * as fs from "fs";
import { import { join as pathJoin, basename as pathBasename, dirname as pathDirname } from "path";
join as pathJoin,
relative as pathRelative,
basename as pathBasename,
dirname as pathDirname,
sep as pathSep
} from "path";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import { run } from "../shared/run"; import { run } from "../shared/run";
import { cacheDirPath as cacheDirPath_base } from "../shared/cacheDirPath"; import { cacheDirPath as cacheDirPath_base } from "../shared/cacheDirPath";

View File

@ -55,7 +55,6 @@ const commonThirdPartyDeps = [
Buffer.from(modifiedPackageJsonContent, "utf8") Buffer.from(modifiedPackageJsonContent, "utf8")
); );
} }
const yarnGlobalDirPath = pathJoin(rootDirPath, ".yarn_home"); const yarnGlobalDirPath = pathJoin(rootDirPath, ".yarn_home");
fs.rmSync(yarnGlobalDirPath, { recursive: true, force: true }); fs.rmSync(yarnGlobalDirPath, { recursive: true, force: true });
@ -64,6 +63,21 @@ fs.mkdirSync(yarnGlobalDirPath);
const execYarnLink = (params: { targetModuleName?: string; cwd: string }) => { const execYarnLink = (params: { targetModuleName?: string; cwd: string }) => {
const { targetModuleName, cwd } = params; 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 = [ const cmd = [
"yarn", "yarn",
"link", "link",

View File

@ -1,49 +1,88 @@
import * as fs from "fs"; import * as fs from "fs";
import { join } from "path"; import { join as pathJoin, sep as pathSep } from "path";
import { startRebuildOnSrcChange } from "./shared/startRebuildOnSrcChange";
import { crawl } from "../src/bin/tools/crawl";
import { run } from "./shared/run"; 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";
{ (async () => {
const dirPath = "node_modules"; const parentDirPath = pathJoin(getThisCodebaseRootDirPath(), "..");
try { const { starterName } = await (async () => {
fs.rmSync(dirPath, { recursive: true, force: true }); const starterNames = fs
} catch { .readdirSync(parentDirPath)
// NOTE: This is a workaround for windows .filter(
// we can't remove locked executables. basename =>
basename.includes("starter") &&
basename.includes("angular") &&
fs.statSync(pathJoin(parentDirPath, basename)).isDirectory()
);
crawl({ if (starterNames.length === 0) {
dirPath, console.log(
returnedPathsType: "absolute" chalk.red(
}).forEach(filePath => { `No starter found. Keycloakify Angular starter found in ${parentDirPath}`
try { )
fs.rmSync(filePath, { force: true }); );
} catch (error) { process.exit(-1);
if (filePath.endsWith(".exe")) {
return;
} }
throw error;
const starterName = await (async () => {
if (starterNames.length === 1) {
return starterNames[0];
} }
console.log(chalk.cyan(`\nSelect a starter to link in:`));
const { value } = await cliSelect<string>({
values: starterNames.map(starterName => `..${pathSep}${starterName}`)
}).catch(() => {
process.exit(-1);
}); });
}
}
fs.rmSync("dist", { recursive: true, force: true }); return value.split(pathSep)[1];
fs.rmSync(".yarn_home", { recursive: true, force: true }); })();
run("yarn install"); return { starterName };
run("yarn build"); })();
const starterName = "keycloakify-starter"; const startTime = Date.now();
fs.rmSync(join("..", starterName, "node_modules"), { console.log(chalk.cyan(`\n\nLinking in ..${pathSep}${starterName}...`));
removeNodeModules({
nodeModulesDirPath: pathJoin(getThisCodebaseRootDirPath(), "node_modules")
});
fs.rmSync(pathJoin(getThisCodebaseRootDirPath(), "dist"), {
recursive: true,
force: true
});
fs.rmSync(pathJoin(getThisCodebaseRootDirPath(), ".yarn_home"), {
recursive: true, recursive: true,
force: true force: true
}); });
run("yarn install", { cwd: join("..", starterName) }); run("yarn install");
run("yarn build");
run(`npx tsx ${join("scripts", "link-in-app.ts")} ${starterName}`); 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(); startRebuildOnSrcChange();
console.log(chalk.green(`\n\nLinked in ${starterName} in ${durationSeconds}s`));
})();

View File

@ -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;
}
});
}
}