Type check safe-chain package

This commit is contained in:
Hans Ott 2025-11-01 13:06:06 +01:00
parent d5dc801c00
commit c88b1a624f
60 changed files with 1179 additions and 33 deletions

View file

@ -3,6 +3,15 @@ import * as os from "os";
import fs from "fs";
import path from "path";
/**
* @typedef AikidoTool
* @property {string} tool
* @property {string} aikidoCommand
*/
/**
* @type {AikidoTool[]}
*/
export const knownAikidoTools = [
{ tool: "npm", aikidoCommand: "aikido-npm" },
{ tool: "npx", aikidoCommand: "aikido-npx" },
@ -30,6 +39,11 @@ export function getPackageManagerList() {
return `${tools.join(", ")}, and ${lastTool} commands`;
}
/**
* @param {string} executableName
*
* @returns {boolean}
*/
export function doesExecutableExistOnSystem(executableName) {
if (os.platform() === "win32") {
const result = spawnSync("where", [executableName], { stdio: "ignore" });
@ -40,6 +54,13 @@ export function doesExecutableExistOnSystem(executableName) {
}
}
/**
* @param {string} filePath
* @param {RegExp} pattern
* @param {string} [eol]
*
* @returns {void}
*/
export function removeLinesMatchingPattern(filePath, pattern, eol) {
if (!fs.existsSync(filePath)) {
return;
@ -54,6 +75,12 @@ export function removeLinesMatchingPattern(filePath, pattern, eol) {
}
const maxLineLength = 100;
/**
* @param {string} line
* @param {RegExp} pattern
* @returns {boolean}
*/
function shouldRemoveLine(line, pattern) {
const isPatternMatch = pattern.test(line);
@ -82,7 +109,14 @@ function shouldRemoveLine(line, pattern) {
return true;
}
export function addLineToFile(filePath, line, eol) {
/**
* @param {string} filePath
* @param {string} line
* @param {string} [eol]
*
* @returns {void}
*/
export function addLineToFile(filePath, line, eol ) {
createFileIfNotExists(filePath);
eol = eol || os.EOL;
@ -92,6 +126,11 @@ export function addLineToFile(filePath, line, eol) {
fs.writeFileSync(filePath, updatedContent, "utf-8");
}
/**
* @param {string} filePath
*
* @returns {void}
*/
function createFileIfNotExists(filePath) {
if (fs.existsSync(filePath)) {
return;

View file

@ -28,6 +28,11 @@ export async function setupCi() {
ui.writeInformation(`Added shims directory to PATH for CI environments.`);
}
/**
* @param {string} shimsDir
*
* @returns {void}
*/
function createUnixShims(shimsDir) {
// Read the template file
const __filename = fileURLToPath(import.meta.url);
@ -64,6 +69,11 @@ function createUnixShims(shimsDir) {
);
}
/**
* @param {string} shimsDir
*
* @returns {void}
*/
function createWindowsShims(shimsDir) {
// Read the template file
const __filename = fileURLToPath(import.meta.url);
@ -97,6 +107,11 @@ function createWindowsShims(shimsDir) {
);
}
/**
* @param {string} shimsDir
*
* @returns {void}
*/
function createShims(shimsDir) {
if (os.platform() === "win32") {
createWindowsShims(shimsDir);
@ -105,6 +120,11 @@ function createShims(shimsDir) {
}
}
/**
* @param {string} shimsDir
*
* @returns {void}
*/
function modifyPathForCi(shimsDir) {
if (process.env.GITHUB_PATH) {
// In GitHub Actions, append the shims directory to GITHUB_PATH

View file

@ -43,7 +43,7 @@ export async function setup() {
ui.emptyLine();
ui.writeInformation(`Please restart your terminal to apply the changes.`);
}
} catch (error) {
} catch (/** @type {any} */ error) {
ui.writeError(
`Failed to set up shell aliases: ${error.message}. Please check your shell configuration.`
);
@ -53,6 +53,7 @@ export async function setup() {
/**
* Calls the setup function for the given shell and reports the result.
* @param {import("./shellDetection.js").Shell} shell
*/
function setupShell(shell) {
let success = false;
@ -60,7 +61,7 @@ function setupShell(shell) {
try {
shell.teardown(knownAikidoTools); // First, tear down to prevent duplicate aliases
success = shell.setup(knownAikidoTools);
} catch (err) {
} catch (/** @type {any} */ err) {
success = false;
error = err;
}

View file

@ -5,6 +5,17 @@ import windowsPowershell from "./supported-shells/windowsPowershell.js";
import fish from "./supported-shells/fish.js";
import { ui } from "../environment/userInteraction.js";
/**
* @typedef Shell
* @property {string} name
* @property {() => boolean} isInstalled
* @property {(tools: import("./helpers.js").AikidoTool[]) => boolean} setup
* @property {(tools: import("./helpers.js").AikidoTool[]) => boolean} teardown
*/
/**
* @returns {Shell[]}
*/
export function detectShells() {
let possibleShells = [zsh, bash, powershell, windowsPowershell, fish];
let availableShells = [];
@ -15,7 +26,7 @@ export function detectShells() {
availableShells.push(shell);
}
}
} catch (error) {
} catch (/** @type {any} */ error) {
ui.writeError(
`We were not able to detect which shells are installed on your system. Please check your shell configuration. Error: ${error.message}`
);

View file

@ -15,6 +15,11 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName);
}
/**
* @param {import("../helpers.js").AikidoTool[]} tools
*
* @returns {boolean}
*/
function teardown(tools) {
const startupFile = getStartupFile();
@ -57,13 +62,18 @@ function getStartupFile() {
}).trim();
return windowsFixPath(path);
} catch (error) {
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
);
}
}
/**
* @param {string} path
*
* @returns {string}
*/
function windowsFixPath(path) {
try {
if (os.platform() !== "win32") {
@ -93,6 +103,11 @@ function hasCygpath() {
}
}
/**
* @param {string} path
*
* @returns {string}
*/
function cygpathw(path) {
try {
var result = spawnSync("cygpath", ["-w", path], {
@ -108,6 +123,9 @@ function cygpathw(path) {
}
}
/**
* @type {import("../shellDetection.js").Shell}
*/
export default {
name: shellName,
isInstalled,

View file

@ -14,6 +14,11 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName);
}
/**
* @param {import("../helpers.js").AikidoTool[]} tools
*
* @returns {boolean}
*/
function teardown(tools) {
const startupFile = getStartupFile();
@ -54,13 +59,16 @@ function getStartupFile() {
encoding: "utf8",
shell: executableName,
}).trim();
} catch (error) {
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
);
}
}
/**
* @type {import("../shellDetection.js").Shell}
*/
export default {
name: shellName,
isInstalled,

View file

@ -13,6 +13,11 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName);
}
/**
* @param {import("../helpers.js").AikidoTool[]} tools
*
* @returns {boolean}
*/
function teardown(tools) {
const startupFile = getStartupFile();
@ -50,13 +55,16 @@ function getStartupFile() {
encoding: "utf8",
shell: executableName,
}).trim();
} catch (error) {
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
);
}
}
/**
* @type {import("../shellDetection.js").Shell}
*/
export default {
name: shellName,
isInstalled,

View file

@ -13,6 +13,11 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName);
}
/**
* @param {import("../helpers.js").AikidoTool[]} tools
*
* @returns {boolean}
*/
function teardown(tools) {
const startupFile = getStartupFile();
@ -50,13 +55,16 @@ function getStartupFile() {
encoding: "utf8",
shell: executableName,
}).trim();
} catch (error) {
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
);
}
}
/**
* @type {import("../shellDetection.js").Shell}
*/
export default {
name: shellName,
isInstalled,

View file

@ -14,6 +14,11 @@ function isInstalled() {
return doesExecutableExistOnSystem(executableName);
}
/**
* @param {import("../helpers.js").AikidoTool[]} tools
*
* @returns {boolean}
*/
function teardown(tools) {
const startupFile = getStartupFile();
@ -54,7 +59,7 @@ function getStartupFile() {
encoding: "utf8",
shell: executableName,
}).trim();
} catch (error) {
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
);

View file

@ -3,6 +3,9 @@ import { ui } from "../environment/userInteraction.js";
import { detectShells } from "./shellDetection.js";
import { knownAikidoTools, getPackageManagerList } from "./helpers.js";
/**
* @returns {Promise<void>}
*/
export async function teardown() {
ui.writeInformation(
chalk.bold("Removing shell aliases.") +
@ -52,7 +55,7 @@ export async function teardown() {
ui.emptyLine();
ui.writeInformation(`Please restart your terminal to apply the changes.`);
}
} catch (error) {
} catch (/** @type {any} */ error) {
ui.writeError(
`Failed to remove shell aliases: ${error.message}. Please check your shell configuration.`
);