Skip to content

Commit

Permalink
Million cli (#578)
Browse files Browse the repository at this point in the history
Co-authored-by: Aiden Bai <[email protected]>
  • Loading branch information
kunal00000 and aidenybai authored Sep 14, 2023
1 parent 59db68a commit ebb8678
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"million": "./dist/index.js"
},
"dependencies": {
"@antfu/ni": "^0.21.8",
"@clack/prompts": "^0.7.0",
"chalk": "^5.3.0",
"diff": "^5.1.0",
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ import type { BuildTool, PackageManager } from '../types';
* Package managers
*/

const yarn: PackageManager = {

export const yarn: PackageManager = {
name: 'yarn',
label: 'Yarn',
lockFile: 'yarn.lock',
installCommand: 'yarn add',
};
const pnpm: PackageManager = {

export const pnpm: PackageManager = {
name: 'pnpm',
label: 'pnpm',
lockFile: 'pnpm-lock.yaml',
installCommand: 'pnpm install',
};
const npm: PackageManager = {

export const npm: PackageManager = {
name: 'npm',
label: 'npm',
lockFile: 'package-lock.json',
installCommand: 'npm install',
};
const bun: PackageManager = {

export const bun: PackageManager = {
name: 'bun',
label: 'bun',
lockFile: 'bun.lockb',
Expand Down
35 changes: 31 additions & 4 deletions packages/cli/src/utils/modify-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
import { abort, getNextRouter, highlightCodeDifferences } from './utils';
import * as clack from '@clack/prompts';
import {
abort,
abortIfCancelled,
getNextRouter,
highlightCodeDifferences,
} from './utils';
import type { BuildTool } from '../types';

export async function modifyConfigFile(detectedBuildTool: BuildTool) {
Expand Down Expand Up @@ -30,6 +36,7 @@ export async function modifyConfigFile(detectedBuildTool: BuildTool) {
*/
if (detectedModuleType === 'cjs') {
// 1.

const importStatement = `const million = require('million/compiler');\n`;
configFileContent = importStatement + configFileContent;

Expand Down Expand Up @@ -60,6 +67,7 @@ export async function modifyConfigFile(detectedBuildTool: BuildTool) {
} else if (detectedModuleType === 'esm') {
// 1.
const importStatement = `import million from 'million/compiler';\n`;

configFileContent = importStatement + configFileContent;

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

abort(
return abort(
`${chalk.yellow(
`Could not detect module type for ${detectedBuildTool.configFilePath}.`,
)}\nPlease refer docs:${chalk.cyan(
)}\nPlease refer docs to setup manually:${chalk.cyan(
'https://million.dev/docs/install#use-the-compiler',
)} to setup manually.`,
)} `,
);
}

const confirmation = await abortIfCancelled(
clack.confirm({
message: 'Do these changes to your config file cause errors?',
initialValue: false,
}),
);
if (confirmation) {
await fs.promises.writeFile(filePath, oldCode, {
encoding: 'utf-8',
flag: 'w',
});

return abort(
`Reverted the changes. \nPlease refer docs for manual setup: ${chalk.cyan(
`https://million.dev/docs/install#use-the-compiler`,
)}`,
);
}
} catch (e) {
Expand Down
29 changes: 20 additions & 9 deletions packages/cli/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { exec } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { setTimeout as sleep } from 'node:timers/promises';
import { detect } from '@antfu/ni';
import * as clack from '@clack/prompts';
import chalk from 'chalk';
import { abort, abortIfCancelled } from './utils';
import { packageManagers } from './constants';
import { npm, pnpm, yarn, bun, packageManagers } from './constants';
import type { PackageManager } from '../types';

/**
* Detect package manager by checking if the lock file exists.
*/
export function detectPackageManger(): PackageManager | null {
for (const packageManager of packageManagers) {
if (fs.existsSync(path.join(process.cwd(), packageManager.lockFile))) {
return packageManager;
}
export async function detectPackageManger(): Promise<PackageManager | null> {
const packageManager = await detect({
programmatic: true,
cwd: path.join(process.cwd()),
});

switch (packageManager) {
case 'yarn':
return yarn;
case 'pnpm':
return pnpm;
case 'npm':
return npm;
case 'bun':
return bun;
default:
return null;
}
return null;
}

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

const detectedPackageManager = detectPackageManger();
const detectedPackageManager = await detectPackageManger();
await sleep(1000);
s.stop(
`${chalk.bold(
Expand Down
12 changes: 11 additions & 1 deletion pnpm-lock.yaml

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

2 comments on commit ebb8678

@vercel
Copy link

@vercel vercel bot commented on ebb8678 Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sink – ./packages/kitchen-sink

million-kitchen-sink-atit.vercel.app
sink.million.dev
sink-git-main-millionjs.vercel.app
sink-millionjs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ebb8678 Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

million-kitchen-sink – ./packages/kitchen-sink

million-kitchen-sink.vercel.app
million-kitchen-sink-millionjs.vercel.app
million-kitchen-sink-git-main-millionjs.vercel.app

Please sign in to comment.