Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new plugin that makes the current local package accessible in another location. #26

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@ The rush subspace is a new feature that intends to move common projects into 1 w

Are you struggling with the complex dependencies between packages? This plugin helps you visualize the dependency relationships between packages.

[More](./rush-plugins/rush-dep-graph-plugin/README.md)
[More](./rush-plugins/rush-dep-graph-plugin/README.md)

# rush-link-project-plugin

[![npm](https://img.shields.io/npm/dt/rush-link-project-plugin.svg?style=flat-square)](https://www.npmjs.com/package/rush-link-project-plugin)
[![npm](https://img.shields.io/npm/dw/rush-link-project-plugin.svg?style=flat-square)](https://www.npmjs.com/package/rush-link-project-plugin)

Tired of manually managing local packages in your Rush project? This plugin links your local package to the project, making it easily accessible in other locations.

[More](./rush-plugins/rush-link-project-plugin/README.md)
10 changes: 10 additions & 0 deletions common/changes/rush-link-project-plugin/main_2025-01-15-08-38.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "rush-link-project-plugin",
"comment": "A new plugin to create a symbolic link for a package.",
"type": "minor"
}
],
"packageName": "rush-link-project-plugin"
}
47 changes: 47 additions & 0 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions rush-plugins/rush-link-project-plugin/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This is a workaround for https://github.com/eslint/eslint/issues/3458
require('@rushstack/eslint-config/patch/modern-module-resolution');

module.exports = {
extends: [
'@rushstack/eslint-config/profile/node-trusted-tool',
'@rushstack/eslint-config/mixins/friendly-locals'
],
parserOptions: { tsconfigRootDir: __dirname },
ignorePatterns: ['node_modules/', 'lib/', 'jest.config.js'],
rules: {
'@typescript-eslint/no-explicit-any': 'off'
}
};
1 change: 1 addition & 0 deletions rush-plugins/rush-link-project-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
35 changes: 35 additions & 0 deletions rush-plugins/rush-link-project-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# THIS IS A STANDARD TEMPLATE FOR .npmignore FILES IN THIS REPO.

# Ignore all files by default, to avoid accidentally publishing unintended files.
*

# Use negative patterns to bring back the specific things we want to publish.
!/bin/**
!/lib/**
!/lib-*/**
!/dist/**

!CHANGELOG.md
!CHANGELOG.json
!heft-plugin.json
!rush-plugin-manifest.json
!ThirdPartyNotice.txt

# Ignore certain patterns that should not get published.
/dist/*.stats.*
/lib/**/test/
/lib-*/**/test/
*.test.js

# NOTE: These don't need to be specified, because NPM includes them automatically.
#
# package.json
# README.md
# LICENSE

# ---------------------------------------------------------------------------
# DO NOT MODIFY ABOVE THIS LINE! Add any project-specific overrides below.
# ---------------------------------------------------------------------------

!/includes/**
!command-line.json
21 changes: 21 additions & 0 deletions rush-plugins/rush-link-project-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) TikTok Pte. Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions rush-plugins/rush-link-project-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# rush-link-project-plugin

A plugin that makes the current local package accessible in another location.

# Prerequisite

Rush.js >= 5.83.2


# Quick Start

1. Enabling this rush plugin

Please follow the [official doc](https://rushjs.io/pages/maintainer/using_rush_plugins/) to enable this plugin in your repo.

2. Running `link-project` command

```
usage: rush link-project [-h] [--path PATH]

Optional arguments:
-h, --help Show this help message and exit.
--path PATH
```
22 changes: 22 additions & 0 deletions rush-plugins/rush-link-project-plugin/command-line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/command-line.schema.json",
"commands": [
{
"name": "link-project",
"commandKind": "global",
"summary": "A plugin that makes the current local package accessible in another location.",
"description": "A plugin that makes the current local package accessible in another location.",
"shellCommand": "ts-node common/autoinstallers/link-package/src/index.ts",
"autoinstallerName": "link-project"
}
],
"parameters": [
{
"parameterKind": "string",
"argumentName": "PATH",
"description": "The path to the package you want to link. This can be an absolute or relative path.",
"longName": "--path",
"associatedCommands": ["link-project"]
}
]
}
7 changes: 7 additions & 0 deletions rush-plugins/rush-link-project-plugin/config/rig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// The "rig.json" file directs tools to look for their config files in an external package.
// Documentation for this system: https://www.npmjs.com/package/@rushstack/rig-package
"$schema": "https://developer.microsoft.com/json-schemas/rig-package/rig.schema.json",

"rigPackageName": "@rushstack/heft-node-rig"
}
5 changes: 5 additions & 0 deletions rush-plugins/rush-link-project-plugin/lint-staged.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-env es6 */
const config = {
'*': "echo 'good job'"
};
module.exports = config;
42 changes: 42 additions & 0 deletions rush-plugins/rush-link-project-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "rush-link-project-plugin",
"version": "0.1.0",
"description": "A plugin that makes the current local package accessible in another location.",
"keywords": [
"rush",
"plugin",
"command",
"link"
],
"homepage": "https://github.com/tiktok/rush-plugins#readme",
"repository": {
"type": "git",
"url": "https://github.com/tiktok/rush-plugins",
"directory": "rush-plugins/rush-link-project-plugin"
},
"license": "MIT",
"main": "lib/index.js",
"scripts": {
"build": "heft build --clean",
"build:watch": "heft build --watch",
"prepublishOnly": "npm run build",
"test": "heft test"
},
"dependencies": {
"@rushstack/node-core-library": "3.62.0",
"@rushstack/rush-sdk": "^5.139.0",
"@rushstack/terminal": "~0.14.2",
"fs-extra": "~10.1.0",
"minimist": "^1.2.8"
},
"devDependencies": {
"@rushstack/eslint-config": "3.3.4",
"@rushstack/heft": "0.61.0",
"@rushstack/heft-node-rig": "2.2.25",
"@types/fs-extra": "~9.0.13",
"@types/heft-jest": "1.0.1",
"@types/minimist": "1.2.2",
"@types/node": "18.17.15",
"typescript": "~5.0.4"
}
}
10 changes: 10 additions & 0 deletions rush-plugins/rush-link-project-plugin/rush-plugin-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush-plugin-manifest.schema.json",
"plugins": [
{
"pluginName": "link-project",
"description": "A plugin that makes the current local package accessible in another location.",
"commandLineJsonFilePath": "command-line.json"
}
]
}
5 changes: 5 additions & 0 deletions rush-plugins/rush-link-project-plugin/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EnvironmentVariableNames } from '@rushstack/rush-sdk';

export const RUSH_EVOKE_FOLDER: string = process.env[EnvironmentVariableNames.RUSH_INVOKED_FOLDER] as string;

export const PACKAGE_JSON: 'package.json' = 'package.json';
51 changes: 51 additions & 0 deletions rush-plugins/rush-link-project-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import minimist from 'minimist';
import path from 'path';
import fs from 'fs';
import { JsonFile, INodePackageJson } from '@rushstack/node-core-library';
import { Colorize } from '@rushstack/terminal';

import { PACKAGE_JSON, RUSH_EVOKE_FOLDER } from './constants';
import { lookUpPackageJson, logger } from './utils';

(async () => {
const argv: minimist.ParsedArgs = minimist(process.argv.slice(2));
const targetPath: string = path.resolve(RUSH_EVOKE_FOLDER, argv.path);
const targetJsonPath: string = path.resolve(targetPath, PACKAGE_JSON);

if (!fs.existsSync(targetJsonPath)) {
throw new Error(`Cannot find ${PACKAGE_JSON} in the path ${argv.path}`);
}

const originJsonPath: string | undefined = lookUpPackageJson(RUSH_EVOKE_FOLDER);

if (!originJsonPath) {
throw new Error(`Cannot find ${PACKAGE_JSON} in the path ${RUSH_EVOKE_FOLDER}`);
}

const packageJson: INodePackageJson = JsonFile.load(targetJsonPath);

let packageName: string = packageJson.name;
let nodeModulesPath: string = path.resolve(originJsonPath, 'node_modules');
if (packageName.includes('/')) {
const [scope, packageBaseName] = packageName.split('/');
nodeModulesPath = path.resolve(nodeModulesPath, scope);
packageName = packageBaseName;
}

if (!fs.existsSync(nodeModulesPath)) {
fs.mkdirSync(nodeModulesPath, { recursive: true });
}

const linkPath: string = path.resolve(nodeModulesPath, packageName);

if (fs.existsSync(linkPath)) {
logger.writeLine(
Colorize.yellow(`Soft link already exists for '${packageName}' in 'node_modules'. Skipping creation.`)
);
} else {
fs.symlinkSync(targetPath, linkPath);
logger.writeLine(
Colorize.green(`Successfully created a symbolic link for '${packageName}' in 'node_modules'.`)
);
}
})().catch((e) => logger.writeErrorLine(e?.message ?? e));
18 changes: 18 additions & 0 deletions rush-plugins/rush-link-project-plugin/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'fs';
import path from 'path';
import { Terminal, ConsoleTerminalProvider } from '@rushstack/terminal';
import { PACKAGE_JSON } from './constants';

export const logProvider: ConsoleTerminalProvider = new ConsoleTerminalProvider();
export const logger: Terminal = new Terminal(logProvider);

export const lookUpPackageJson = (currentDir: string): string | undefined => {
while (currentDir !== '.') {
const packageJsonPath: string = path.resolve(currentDir, PACKAGE_JSON);
if (fs.existsSync(packageJsonPath)) {
return currentDir;
}
currentDir = path.resolve(currentDir, '..');
}
return undefined;
};
Loading
Loading