Skip to content

Commit

Permalink
Merge pull request #11 from primno/dev
Browse files Browse the repository at this point in the history
Run the local CLI when using global CLI in a workspace
  • Loading branch information
xaviermonin authored May 14, 2023
2 parents 0efa67a + 120006a commit c17a4ac
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 450 deletions.
8 changes: 8 additions & 0 deletions .changeset/fifty-planes-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@primno/cli": minor
---

Runs the local CLI when the global CLI is running in a Primno workspace.

BREAKING CHANGE:
Does not work with previous versions.
3 changes: 0 additions & 3 deletions bin/bootstrap.mjs

This file was deleted.

3 changes: 3 additions & 0 deletions bin/mn.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

import '../dist/bootstrap.mjs';
605 changes: 191 additions & 414 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@primno/cli",
"version": "0.7.0",
"description": "Command-line interface tool for initializing, building, and deploying Primno workspaces",
"main": "dist/index.mjs",
"main": "dist/cli.mjs",
"files": [
"bin",
"lib",
Expand All @@ -20,8 +20,8 @@
"release": "npm run build && changeset publish"
},
"bin": {
"mn": "./bin/bootstrap.mjs",
"primno": "./bin/bootstrap.mjs"
"mn": "./bin/mn.mjs",
"primno": "./bin/mn.mjs"
},
"engines": {
"node": ">=16",
Expand Down Expand Up @@ -55,6 +55,7 @@
"rollup": "^3.20.2",
"rxjs": "^7.8.0",
"selfsigned": "^2.1.1",
"semver": "^7.5.1",
"terser": "^5.16.8",
"tslib": "^2.5.0",
"typescript": "^4.9.5"
Expand All @@ -75,7 +76,7 @@
"ts-node": "^10.9.1"
},
"peerDependencies": {
"@primno/core": ">= 0.7.0-beta.0 < 1.0.0-beta.0"
"@primno/core": "^0.8.0"
},
"publishConfig": {
"access": "public",
Expand Down
11 changes: 8 additions & 3 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ export default function(
//command: Record<string, unknown>
): RollupOptions[] {
return [
// CJS
{
input: 'src/index.ts',
input: ['src/bootstrap.ts', 'src/cli.ts'],
plugins,
external,
output: { format: 'esm', file: 'dist/mn.mjs', sourcemap }
output: {
format: 'esm',
dir: 'dist/',
chunkFileNames: "shared.mjs",
entryFileNames: "[name].mjs",
sourcemap
}
}
]
}
47 changes: 47 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import chalk from "chalk";
import { getLocalBin, getLocalBinVersion, localBinExists } from "./utils/local-bin";
import { lt as semverLt } from "semver";
import { showPrimnoAsciiArt } from "./utils/display";

async function loadCli(): Promise<typeof import("./cli")> {
if (localBinExists()) {
const localBinVersion = getLocalBinVersion();

if (semverLt(localBinVersion, "0.8.0")) {
throw new Error("Local CLI version is too old. Please update it to 0.8.0 or later.");
}

return await getLocalBin();
}
else {
return await import("./cli");
}
}

async function bootstrap() {
// Set process title
try {
process.title = `mn ${process.argv.slice(2).join(' ')}`;
} catch (_) {
process.title = 'mn';
}

showPrimnoAsciiArt();

let cli: typeof import("./cli") | undefined;

try {
cli = await loadCli();

if (!cli?.default) {
throw new Error("Unable to start CLI.");
}
}
catch (err: any) {
console.error(chalk.red(`Error: ${err.message}`));
}

cli?.default({});
}

bootstrap();
32 changes: 32 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { program } from 'commander';
import { startCommand, newCommand, buildCommand, watchCommand, deployCommand, generateCommand } from './commands';
import { getPackageJson } from "./utils/package";
import { getRootDirName } from "./utils/dir";
import { showError } from "./utils/display";
import process from 'process';

interface Options {

}

const pkg = getPackageJson(getRootDirName());

export const VERSION = pkg.version as string;

export default function run(options: Options) {
program
.name('mn')
.version(pkg.version)
.description(pkg.description)
.addCommand(startCommand)
.addCommand(newCommand)
.addCommand(buildCommand)
.addCommand(watchCommand)
.addCommand(deployCommand)
.addCommand(generateCommand)
.parseAsync(process.argv)
.catch((err) => {
showError(err.message);
process.exitCode = 1;
});
}
2 changes: 1 addition & 1 deletion src/commands/watch-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ async function watchAction() {

export const watchCommand = new Command('watch')
.alias("w")
.description('Build the workspace and rebuild on file changes')
.description('build the workspace and rebuild on file changes')
.action(watchAction);
25 changes: 0 additions & 25 deletions src/index.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/utils/local-bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { existsSync } from 'fs';
import path from 'path';
import { getPackageJson } from './package';

const pathSegments = [process.cwd(), 'node_modules', '@primno', 'cli'];

export function localBinExists() {
return existsSync(path.join(...pathSegments));
}

export function getLocalBinVersion() {
return getPackageJson(path.join(...pathSegments)).version as string;
}

export async function getLocalBin(): Promise<typeof import('../cli')> {
try {
const cliPath = path.posix.join(...pathSegments, 'dist', 'cli.mjs');
return await import(`file://${cliPath}`);
}
catch (except: any) {
throw new Error(`Unable to load local CLI: ${except.message}`);
}
}

0 comments on commit c17a4ac

Please sign in to comment.