Skip to content

Commit 13356bd

Browse files
Merge pull request #226 from hypermod-io/whitelisted-dependencies
Adds ability to whitelist devDeps of the package to be installed
2 parents 393cac4 + 0f471c5 commit 13356bd

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

.changeset/cold-penguins-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hypermod/types': minor
3+
---
4+
5+
Adds dependencies property to the type definition + adds comments explaining all of the properties

.changeset/pretty-shrimps-hunt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@hypermod/fetcher': minor
3+
'@hypermod/cli': minor
4+
---
5+
6+
Adds experimental dependency property for whitelisting devdeps in co-located modules

packages/cli/src/main.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import semver from 'semver';
33
import chalk from 'chalk';
44
import findUp from 'find-up';
55
import inquirer from 'inquirer';
6+
import fs from 'fs-extra';
67
import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
78
import { installPackage } from '@antfu/install-pkg';
89

@@ -25,9 +26,16 @@ const ExperimentalModuleLoader = () => ({
2526
require: (packageName: string) => require(packageName),
2627
getInfo: (packageName: string) => {
2728
const entryPath = require.resolve(packageName);
29+
const location = entryPath.split(packageName)[0] + packageName;
30+
const packageJsonRaw = fs.readFileSync(
31+
path.join(location, 'package.json'),
32+
'utf8',
33+
);
34+
2835
return {
29-
location: entryPath.split(packageName)[0] + packageName,
30-
entryPath: entryPath,
36+
location,
37+
entryPath,
38+
pkgJson: JSON.parse(packageJsonRaw),
3139
};
3240
},
3341
});

packages/fetcher/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ export async function fetchRemotePackage(
147147
const pkg = packageManager.require(packageName);
148148
const configExport = resolveConfigExport(pkg);
149149

150+
// Install whitelisted deps
151+
// @ts-expect-error legacy module loader doesn't know about these properties
152+
if (info.pkgJson) {
153+
await Promise.all(
154+
configExport.dependencies?.map(dep => {
155+
// @ts-expect-error legacy module loader doesn't know about these properties
156+
const version = info.pkgJson.devDependencies[dep];
157+
return packageManager.install(`${dep}@${version}`);
158+
}) ?? [],
159+
);
160+
}
161+
150162
if (configExport.transforms || configExport.presets) {
151163
return {
152164
filePath: info.location,

packages/types/src/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
11
export interface Config {
2+
/**
3+
* Targets represent the packages that the hypermod package is providing transforms to.
4+
* This is useful for filtering and grouping codemods based on the target package.
5+
*
6+
* For example, a hypermod package that is targetting react and react-dom would have
7+
* the following targets: ['react', 'react-dom'].
8+
*/
29
targets?: string[];
10+
11+
/**
12+
* Github usernames of the maintainers
13+
*/
314
maintainers?: string[];
15+
16+
/**
17+
* Description of the hypermod package, please explain the intetion of the package.
18+
*/
419
description?: string;
20+
21+
/**
22+
* Transforms are the main building blocks of a codemod. When a hypermod package
23+
* is targetting a specific package / focus area, transforms represent the
24+
* migrations between versions of the target package.
25+
*
26+
* Example react v16 -> v17, or react-dom v16 -> v17
27+
*/
528
transforms?: Record<string, string>;
29+
30+
/**
31+
* Presets represent transforms that have no association with a specific
32+
* version of a package / focus area. These should be generic and reusable.
33+
*
34+
* Example: Format imports, remove console logs, etc.
35+
*/
636
presets?: Record<string, string>;
37+
38+
/**
39+
* A list of dependencies to be installed before running the transform.
40+
* These are useful when co-locating codemods with an existing package
41+
* and want to whitelist devDependencies to be installed.
42+
*
43+
* Note: the versions installed are based on the package.json
44+
*
45+
* Example: dependencies: ['@hypermod/utils', 'postcss', 'postcss-less']
46+
*/
47+
dependencies?: string[];
748
}
849

950
export type CodeshiftConfig = Config;

website/docs/configuration.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ Targets list the packages that the codemod package targets.
6565
This is useful for Hypermod packages that have codemods targeting multiple related packages at the same time, such as packages from a monorepo.
6666

6767
For example: `targets: ['@foo/bar', '@foo/baz']`
68+
69+
### `dependencies`
70+
71+
(Experimental feature, this will only work when the `--experimental-loader` flag is specified)
72+
73+
A list of dependencies to be installed before running the transform. These are useful when co-locating codemods with an existing
74+
package and want to specify a whitelist of `devDependencies` to be installed only when run via `@hypermod/cli`.
75+
This avoids codemod-related dependencies from unnecessarily increasing the bundlesize for regular consumers of the package.
76+
77+
Note: the versions installed are based on what's specified in the `package.json`
78+
79+
Example: `dependencies: ['@hypermod/utils', 'postcss', 'postcss-less']`

0 commit comments

Comments
 (0)