Skip to content

Commit ebb8678

Browse files
Million cli (#578)
Co-authored-by: Aiden Bai <[email protected]>
1 parent 59db68a commit ebb8678

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"million": "./dist/index.js"
2727
},
2828
"dependencies": {
29+
"@antfu/ni": "^0.21.8",
2930
"@clack/prompts": "^0.7.0",
3031
"chalk": "^5.3.0",
3132
"diff": "^5.1.0",

packages/cli/src/utils/constants.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ import type { BuildTool, PackageManager } from '../types';
44
* Package managers
55
*/
66

7-
const yarn: PackageManager = {
7+
8+
export const yarn: PackageManager = {
89
name: 'yarn',
910
label: 'Yarn',
1011
lockFile: 'yarn.lock',
1112
installCommand: 'yarn add',
1213
};
13-
const pnpm: PackageManager = {
14+
15+
export const pnpm: PackageManager = {
1416
name: 'pnpm',
1517
label: 'pnpm',
1618
lockFile: 'pnpm-lock.yaml',
1719
installCommand: 'pnpm install',
1820
};
19-
const npm: PackageManager = {
21+
22+
export const npm: PackageManager = {
2023
name: 'npm',
2124
label: 'npm',
2225
lockFile: 'package-lock.json',
2326
installCommand: 'npm install',
2427
};
25-
const bun: PackageManager = {
28+
29+
export const bun: PackageManager = {
2630
name: 'bun',
2731
label: 'bun',
2832
lockFile: 'bun.lockb',

packages/cli/src/utils/modify-config.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import chalk from 'chalk';
4-
import { abort, getNextRouter, highlightCodeDifferences } from './utils';
4+
import * as clack from '@clack/prompts';
5+
import {
6+
abort,
7+
abortIfCancelled,
8+
getNextRouter,
9+
highlightCodeDifferences,
10+
} from './utils';
511
import type { BuildTool } from '../types';
612

713
export async function modifyConfigFile(detectedBuildTool: BuildTool) {
@@ -30,6 +36,7 @@ export async function modifyConfigFile(detectedBuildTool: BuildTool) {
3036
*/
3137
if (detectedModuleType === 'cjs') {
3238
// 1.
39+
3340
const importStatement = `const million = require('million/compiler');\n`;
3441
configFileContent = importStatement + configFileContent;
3542

@@ -60,6 +67,7 @@ export async function modifyConfigFile(detectedBuildTool: BuildTool) {
6067
} else if (detectedModuleType === 'esm') {
6168
// 1.
6269
const importStatement = `import million from 'million/compiler';\n`;
70+
6371
configFileContent = importStatement + configFileContent;
6472

6573
// 2.
@@ -92,12 +100,31 @@ export async function modifyConfigFile(detectedBuildTool: BuildTool) {
92100
* Refer user to read installation docs
93101
* */
94102

95-
abort(
103+
return abort(
96104
`${chalk.yellow(
97105
`Could not detect module type for ${detectedBuildTool.configFilePath}.`,
98-
)}\nPlease refer docs:${chalk.cyan(
106+
)}\nPlease refer docs to setup manually:${chalk.cyan(
99107
'https://million.dev/docs/install#use-the-compiler',
100-
)} to setup manually.`,
108+
)} `,
109+
);
110+
}
111+
112+
const confirmation = await abortIfCancelled(
113+
clack.confirm({
114+
message: 'Do these changes to your config file cause errors?',
115+
initialValue: false,
116+
}),
117+
);
118+
if (confirmation) {
119+
await fs.promises.writeFile(filePath, oldCode, {
120+
encoding: 'utf-8',
121+
flag: 'w',
122+
});
123+
124+
return abort(
125+
`Reverted the changes. \nPlease refer docs for manual setup: ${chalk.cyan(
126+
`https://million.dev/docs/install#use-the-compiler`,
127+
)}`,
101128
);
102129
}
103130
} catch (e) {

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import { exec } from 'child_process';
2-
import * as fs from 'fs';
32
import * as path from 'path';
43
import { setTimeout as sleep } from 'node:timers/promises';
4+
import { detect } from '@antfu/ni';
55
import * as clack from '@clack/prompts';
66
import chalk from 'chalk';
77
import { abort, abortIfCancelled } from './utils';
8-
import { packageManagers } from './constants';
8+
import { npm, pnpm, yarn, bun, packageManagers } from './constants';
99
import type { PackageManager } from '../types';
1010

1111
/**
1212
* Detect package manager by checking if the lock file exists.
1313
*/
14-
export function detectPackageManger(): PackageManager | null {
15-
for (const packageManager of packageManagers) {
16-
if (fs.existsSync(path.join(process.cwd(), packageManager.lockFile))) {
17-
return packageManager;
18-
}
14+
export async function detectPackageManger(): Promise<PackageManager | null> {
15+
const packageManager = await detect({
16+
programmatic: true,
17+
cwd: path.join(process.cwd()),
18+
});
19+
20+
switch (packageManager) {
21+
case 'yarn':
22+
return yarn;
23+
case 'pnpm':
24+
return pnpm;
25+
case 'npm':
26+
return npm;
27+
case 'bun':
28+
return bun;
29+
default:
30+
return null;
1931
}
20-
return null;
2132
}
2233

2334
/**
@@ -38,7 +49,7 @@ async function getPackageManager(): Promise<PackageManager> {
3849
const s = clack.spinner();
3950
s.start('Detecting package manager.');
4051

41-
const detectedPackageManager = detectPackageManger();
52+
const detectedPackageManager = await detectPackageManger();
4253
await sleep(1000);
4354
s.stop(
4455
`${chalk.bold(

pnpm-lock.yaml

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)