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

Experimental package loader #195

Merged
merged 6 commits into from
Sep 12, 2023
Merged
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
6 changes: 6 additions & 0 deletions .changeset/light-pumas-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@codeshift/cli': minor
'@hypermod/cli': minor
---

Implements alternate dependency downloader which is available via the --experimental-loader flag
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"start:dev": "ts-node src/index.ts"
},
"dependencies": {
"@antfu/install-pkg": "^0.1.1",
"@hypermod/core": "^0.2.0",
"@hypermod/fetcher": "^0.5.0",
"@hypermod/initializer": "^0.5.2",
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';
import { readFileSync } from 'fs';
import chalk from 'chalk';
import { Command, Option, CommanderError } from 'commander';

Expand All @@ -7,13 +9,16 @@ import init from './init';
import validate from './validate';
import { InvalidUserInputError, InvalidConfigError } from './errors';

// import packageJson from '../package.json';
const packageJson = readFileSync(
path.join(__dirname, '..', 'package.json'),
'utf-8',
);

const program = new Command();

program
.enablePositionalOptions()
.version('packageJson.version', '-v, --version')
.version(JSON.parse(packageJson).version, '-v, --version')
.name('hypermod')
.argument('[path...]')
.usage('[global options] <file-paths>...')
Expand Down Expand Up @@ -61,6 +66,10 @@ program
'--registryToken <value>',
'Define an authentication token to use as credentials for the registry',
)
.option(
'--experimental-loader',
'Enables the experimental package downloader',
)
.addOption(
new Option(
'--verbose <parser>',
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from 'chalk';
import findUp from 'find-up';
import inquirer from 'inquirer';
import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
import { installPackage } from '@antfu/install-pkg';

import * as core from '@hypermod/core';
import { CodeshiftConfig } from '@hypermod/types';
Expand All @@ -15,6 +16,14 @@ import { fetchPackages } from './utils/fetch-package';
import { mergeConfigs } from './utils/merge-configs';
import { getConfigPrompt, getMultiConfigPrompt } from './prompt';

const ExperimentalModuleLoader = () => ({
install: async (packageName: string) => await installPackage(packageName),
require: (packageName: string) => require(packageName),
getInfo: (packageName: string) => ({
location: require.resolve(packageName),
}),
});

export default async function main(
paths: string[],
flags: Partial<core.Flags>,
Expand All @@ -41,7 +50,9 @@ export default async function main(
};
}

const packageManager = new PluginManager(pluginManagerConfig);
const packageManager = flags.experimentalLoader
? ExperimentalModuleLoader()
: new PluginManager(pluginManagerConfig);

let transforms: string[] = [];

Expand Down Expand Up @@ -193,6 +204,7 @@ export default async function main(

const { community, remote } = await fetchPackages(
pkgName,
// @ts-expect-error Experimental loader
packageManager,
);

Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/utils/fetch-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ export async function fetchPackages(
`${chalk.green(`Attempting to download npm package:`)} ${packageName}`,
);
remotePackage = await fetchRemotePackage(packageName, packageManager);
spinner.succeed(`${chalk.green('Found hypermod package:')} ${packageName}`);
spinner.succeed(
`${chalk.green('Found remote Hypermod package:')} ${packageName}`,
);
} catch (error) {
spinner.warn(
`${chalk.yellow('Unable to locate hypermod package:')} ${packageName}`,
`${chalk.yellow(
'Unable to locate remote Hypermod package:',
)} ${packageName}`,
);
}

Expand All @@ -62,7 +66,7 @@ export async function fetchPackages(

if (!hypermodPackage && !remotePackage) {
throw new Error(
`Unable to locate package from Hypermod community or NPM.
`Unable to locate package from Hypermod Community or NPM.
Make sure the package name "${packageName}" is correct and try again.`,
);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ function getAllFiles(paths: string[], filter: (name: string) => boolean) {
});
}

export function run(entrypointPath: string, paths: string[], options: Flags) {
export function run(
entrypointPath: string,
paths: string[],
options: Omit<Flags, 'experimentalLoader'>,
) {
const cpus = options.cpus
? Math.min(availableCpus, options.cpus)
: availableCpus;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ export interface Flags {
silent: boolean;
stdin: boolean;
verbose?: 0 | 1 | 2;
experimentalLoader: boolean;
}
2,158 changes: 1,083 additions & 1,075 deletions yarn.lock

Large diffs are not rendered by default.

Loading