Skip to content

Commit c17a4ac

Browse files
authored
Merge pull request #11 from primno/dev
Run the local CLI when using global CLI in a workspace
2 parents 0efa67a + 120006a commit c17a4ac

11 files changed

Lines changed: 318 additions & 450 deletions

File tree

.changeset/fifty-planes-fly.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@primno/cli": minor
3+
---
4+
5+
Runs the local CLI when the global CLI is running in a Primno workspace.
6+
7+
BREAKING CHANGE:
8+
Does not work with previous versions.

bin/bootstrap.mjs

Lines changed: 0 additions & 3 deletions
This file was deleted.

bin/mn.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
import '../dist/bootstrap.mjs';

package-lock.json

Lines changed: 191 additions & 414 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@primno/cli",
33
"version": "0.7.0",
44
"description": "Command-line interface tool for initializing, building, and deploying Primno workspaces",
5-
"main": "dist/index.mjs",
5+
"main": "dist/cli.mjs",
66
"files": [
77
"bin",
88
"lib",
@@ -20,8 +20,8 @@
2020
"release": "npm run build && changeset publish"
2121
},
2222
"bin": {
23-
"mn": "./bin/bootstrap.mjs",
24-
"primno": "./bin/bootstrap.mjs"
23+
"mn": "./bin/mn.mjs",
24+
"primno": "./bin/mn.mjs"
2525
},
2626
"engines": {
2727
"node": ">=16",
@@ -55,6 +55,7 @@
5555
"rollup": "^3.20.2",
5656
"rxjs": "^7.8.0",
5757
"selfsigned": "^2.1.1",
58+
"semver": "^7.5.1",
5859
"terser": "^5.16.8",
5960
"tslib": "^2.5.0",
6061
"typescript": "^4.9.5"
@@ -75,7 +76,7 @@
7576
"ts-node": "^10.9.1"
7677
},
7778
"peerDependencies": {
78-
"@primno/core": ">= 0.7.0-beta.0 < 1.0.0-beta.0"
79+
"@primno/core": "^0.8.0"
7980
},
8081
"publishConfig": {
8182
"access": "public",

rollup.config.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ export default function(
3030
//command: Record<string, unknown>
3131
): RollupOptions[] {
3232
return [
33-
// CJS
3433
{
35-
input: 'src/index.ts',
34+
input: ['src/bootstrap.ts', 'src/cli.ts'],
3635
plugins,
3736
external,
38-
output: { format: 'esm', file: 'dist/mn.mjs', sourcemap }
37+
output: {
38+
format: 'esm',
39+
dir: 'dist/',
40+
chunkFileNames: "shared.mjs",
41+
entryFileNames: "[name].mjs",
42+
sourcemap
43+
}
3944
}
4045
]
4146
}

src/bootstrap.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import chalk from "chalk";
2+
import { getLocalBin, getLocalBinVersion, localBinExists } from "./utils/local-bin";
3+
import { lt as semverLt } from "semver";
4+
import { showPrimnoAsciiArt } from "./utils/display";
5+
6+
async function loadCli(): Promise<typeof import("./cli")> {
7+
if (localBinExists()) {
8+
const localBinVersion = getLocalBinVersion();
9+
10+
if (semverLt(localBinVersion, "0.8.0")) {
11+
throw new Error("Local CLI version is too old. Please update it to 0.8.0 or later.");
12+
}
13+
14+
return await getLocalBin();
15+
}
16+
else {
17+
return await import("./cli");
18+
}
19+
}
20+
21+
async function bootstrap() {
22+
// Set process title
23+
try {
24+
process.title = `mn ${process.argv.slice(2).join(' ')}`;
25+
} catch (_) {
26+
process.title = 'mn';
27+
}
28+
29+
showPrimnoAsciiArt();
30+
31+
let cli: typeof import("./cli") | undefined;
32+
33+
try {
34+
cli = await loadCli();
35+
36+
if (!cli?.default) {
37+
throw new Error("Unable to start CLI.");
38+
}
39+
}
40+
catch (err: any) {
41+
console.error(chalk.red(`Error: ${err.message}`));
42+
}
43+
44+
cli?.default({});
45+
}
46+
47+
bootstrap();

src/cli.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { program } from 'commander';
2+
import { startCommand, newCommand, buildCommand, watchCommand, deployCommand, generateCommand } from './commands';
3+
import { getPackageJson } from "./utils/package";
4+
import { getRootDirName } from "./utils/dir";
5+
import { showError } from "./utils/display";
6+
import process from 'process';
7+
8+
interface Options {
9+
10+
}
11+
12+
const pkg = getPackageJson(getRootDirName());
13+
14+
export const VERSION = pkg.version as string;
15+
16+
export default function run(options: Options) {
17+
program
18+
.name('mn')
19+
.version(pkg.version)
20+
.description(pkg.description)
21+
.addCommand(startCommand)
22+
.addCommand(newCommand)
23+
.addCommand(buildCommand)
24+
.addCommand(watchCommand)
25+
.addCommand(deployCommand)
26+
.addCommand(generateCommand)
27+
.parseAsync(process.argv)
28+
.catch((err) => {
29+
showError(err.message);
30+
process.exitCode = 1;
31+
});
32+
}

src/commands/watch-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ async function watchAction() {
1515

1616
export const watchCommand = new Command('watch')
1717
.alias("w")
18-
.description('Build the workspace and rebuild on file changes')
18+
.description('build the workspace and rebuild on file changes')
1919
.action(watchAction);

src/index.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)