Skip to content

Commit

Permalink
bin
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 30, 2024
1 parent 19ec6fa commit 5e134a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
66 changes: 55 additions & 11 deletions src/create-symlinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { mkdirpSync as mkdirp } from 'mkdirp';
import * as path from 'path';
import { sync as rimraf } from 'rimraf';
import { LogLevel } from './log';
import { execSync } from 'child_process';

interface PackageInfo {
name: string;
folderName: string;
path: string;
json: any;
workspace: string;
dependencies: string[];
devDependencies: string[];
Expand All @@ -33,6 +35,50 @@ function fileOrFolderOrLinkExists(path) {
return true;
}

function linkBinCommands(
packageInfo: PackageInfo,
packagePath: string,
packageName: string,
log: (str: string, level: LogLevel) => void
) {

// START .bin
if (packageInfo.json.bin) {
Object.keys(packageInfo.json.bin).forEach(binCommand => {
const targetBinPath = path.join(packageInfo.path, 'dist', packageInfo.json.bin[binCommand]);
const symlinkBinPath = path.join(packagePath, 'node_modules', '.bin', binCommand);
execSync(`chmod +x ${targetBinPath}`);

if (fileOrFolderOrLinkExists(symlinkBinPath)) {
rimraf(symlinkBinPath); // Remove existing symlink if it exists
}
mkdirp(path.dirname(symlinkBinPath));
fs.symlinkSync(targetBinPath, symlinkBinPath, 'file'); // Create a symbolic link
log(`[${chalk.magenta(packageName)}]: command ${chalk.green(binCommand)} link ${chalk.blue(symlinkBinPath)}`, 'info');
log(`[${chalk.magenta(packageName)}]: command ${chalk.green(binCommand)} target ${chalk.green(targetBinPath)}`, 'info');
});
}

}

function linkModule(
packageInfo: PackageInfo,
depInfo: PackageInfo,
log: (str: string, level: LogLevel) => void
) {

const depDistPath = path.join(depInfo.path, 'dist');
const symlinkTarget = path.join(packageInfo.path, 'node_modules', depInfo.name);
log(`[${chalk.blue(packageInfo.name)}]: Linking dependency ${chalk.green(depInfo.name)}`, 'info');
if (fileOrFolderOrLinkExists(symlinkTarget)) {
rimraf(symlinkTarget);
}
mkdirp(path.dirname(symlinkTarget));
fs.symlinkSync(depDistPath, symlinkTarget, 'junction');


}

export function processPackages(
packageInfos: PackageInfo[],
rootDir: string,
Expand All @@ -48,16 +94,8 @@ export function processPackages(
packageInfo.dependencies.forEach(dep => {
const depInfo = packageInfos.find(info => info.name === dep);
if (depInfo) {
const depDistPath = path.join(depInfo.path, 'dist');
const symlinkTarget = path.join(packageInfo.path, 'node_modules', depInfo.name);

log(`[${chalk.blue(packageName)}]: Linking dependency ${chalk.green(dep)}`, 'info');
if (fileOrFolderOrLinkExists(symlinkTarget)) {
log(`[${chalk.blue(packageName)}]: ${chalk.grey(`Removing existing node_modules directory: ${symlinkTarget}`)}`, 'info');
rimraf(symlinkTarget);
}
mkdirp(path.dirname(symlinkTarget));
fs.symlinkSync(depDistPath, symlinkTarget, 'junction');
linkBinCommands(depInfo, packageInfo.path, packageInfo.name, log);
linkModule(packageInfo, depInfo, log);
}
});
log(`[${chalk.blue(packageName)}]: ${chalk.green(`Finished processing ${packageName}`)}`, 'info');
Expand All @@ -67,19 +105,25 @@ export function processPackages(
log(chalk.blue(`Processing root node_modules at ${rootNodeModulesPath}`), 'debug');

packageInfos.forEach(packageInfo => {

// START .bin
linkBinCommands(packageInfo, rootDir, '<root>', log);
// END .bin

// START MODULE
const packageName = chalk.green(packageInfo.name);
const symlinkPath = path.join(rootNodeModulesPath, packageInfo.name);

// Check and remove the existing symlink if it exists
if (fileOrFolderOrLinkExists(symlinkPath)) {
log(chalk.red(`Removing existing symlink for ${packageName} at root`), 'info');
rimraf(symlinkPath);
}

const distPath = path.join(packageInfo.path, 'dist');
log(chalk.yellow(`Creating symlink in root for ${packageName}`), 'info');
mkdirp(path.dirname(symlinkPath));
fs.symlinkSync(distPath, symlinkPath, 'junction');
// END MODULE
});

log(chalk.blue('All packages processed successfully including root node_modules.'), 'debug');
Expand Down
2 changes: 2 additions & 0 deletions src/read-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface PackageInfo {
name: string;
folderName: string;
path: string;
json: any;
workspace: string;
dependencies: string[];
devDependencies: string[];
Expand Down Expand Up @@ -45,6 +46,7 @@ export const readLernaProject = (
// Push the package info to the packages array
packages.push({
name: packageName,
json: packageJson,
folderName: folderName,
path: fullPath,
workspace: workspaceName,
Expand Down

0 comments on commit 5e134a7

Please sign in to comment.