Skip to content

Commit c34da19

Browse files
Merge pull request #195 from hypermod-io/temp-tsc-bundler-plus-install-pkg
Move bundling to TSC + experimental package loader
2 parents 5a41594 + bdeb5c9 commit c34da19

File tree

8 files changed

+1127
-1082
lines changed

8 files changed

+1127
-1082
lines changed

.changeset/light-pumas-give.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@codeshift/cli': minor
3+
'@hypermod/cli': minor
4+
---
5+
6+
Implements alternate dependency downloader which is available via the --experimental-loader flag

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"start:dev": "ts-node src/index.ts"
1818
},
1919
"dependencies": {
20+
"@antfu/install-pkg": "^0.1.1",
2021
"@hypermod/core": "^0.2.0",
2122
"@hypermod/fetcher": "^0.5.0",
2223
"@hypermod/initializer": "^0.5.2",

packages/cli/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
import { readFileSync } from 'fs';
13
import chalk from 'chalk';
24
import { Command, Option, CommanderError } from 'commander';
35

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

10-
// import packageJson from '../package.json';
12+
const packageJson = readFileSync(
13+
path.join(__dirname, '..', 'package.json'),
14+
'utf-8',
15+
);
1116

1217
const program = new Command();
1318

1419
program
1520
.enablePositionalOptions()
16-
.version('packageJson.version', '-v, --version')
21+
.version(JSON.parse(packageJson).version, '-v, --version')
1722
.name('hypermod')
1823
.argument('[path...]')
1924
.usage('[global options] <file-paths>...')
@@ -61,6 +66,10 @@ program
6166
'--registryToken <value>',
6267
'Define an authentication token to use as credentials for the registry',
6368
)
69+
.option(
70+
'--experimental-loader',
71+
'Enables the experimental package downloader',
72+
)
6473
.addOption(
6574
new Option(
6675
'--verbose <parser>',

packages/cli/src/main.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import chalk from 'chalk';
55
import findUp from 'find-up';
66
import inquirer from 'inquirer';
77
import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
8+
import { installPackage } from '@antfu/install-pkg';
89

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

19+
const ExperimentalModuleLoader = () => ({
20+
install: async (packageName: string) => await installPackage(packageName),
21+
require: (packageName: string) => require(packageName),
22+
getInfo: (packageName: string) => ({
23+
location: require.resolve(packageName),
24+
}),
25+
});
26+
1827
export default async function main(
1928
paths: string[],
2029
flags: Partial<core.Flags>,
@@ -41,7 +50,9 @@ export default async function main(
4150
};
4251
}
4352

44-
const packageManager = new PluginManager(pluginManagerConfig);
53+
const packageManager = flags.experimentalLoader
54+
? ExperimentalModuleLoader()
55+
: new PluginManager(pluginManagerConfig);
4556

4657
let transforms: string[] = [];
4758

@@ -193,6 +204,7 @@ export default async function main(
193204

194205
const { community, remote } = await fetchPackages(
195206
pkgName,
207+
// @ts-expect-error Experimental loader
196208
packageManager,
197209
);
198210

packages/cli/src/utils/fetch-package.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ export async function fetchPackages(
4747
`${chalk.green(`Attempting to download npm package:`)} ${packageName}`,
4848
);
4949
remotePackage = await fetchRemotePackage(packageName, packageManager);
50-
spinner.succeed(`${chalk.green('Found hypermod package:')} ${packageName}`);
50+
spinner.succeed(
51+
`${chalk.green('Found remote Hypermod package:')} ${packageName}`,
52+
);
5153
} catch (error) {
5254
spinner.warn(
53-
`${chalk.yellow('Unable to locate hypermod package:')} ${packageName}`,
55+
`${chalk.yellow(
56+
'Unable to locate remote Hypermod package:',
57+
)} ${packageName}`,
5458
);
5559
}
5660

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

6367
if (!hypermodPackage && !remotePackage) {
6468
throw new Error(
65-
`Unable to locate package from Hypermod community or NPM.
69+
`Unable to locate package from Hypermod Community or NPM.
6670
Make sure the package name "${packageName}" is correct and try again.`,
6771
);
6872
}

packages/core/src/runner.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ function getAllFiles(paths: string[], filter: (name: string) => boolean) {
153153
});
154154
}
155155

156-
export function run(entrypointPath: string, paths: string[], options: Flags) {
156+
export function run(
157+
entrypointPath: string,
158+
paths: string[],
159+
options: Omit<Flags, 'experimentalLoader'>,
160+
) {
157161
const cpus = options.cpus
158162
? Math.min(availableCpus, options.cpus)
159163
: availableCpus;

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ export interface Flags {
6161
silent: boolean;
6262
stdin: boolean;
6363
verbose?: 0 | 1 | 2;
64+
experimentalLoader: boolean;
6465
}

yarn.lock

Lines changed: 1083 additions & 1075 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)