-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from primno/dev
Run the local CLI when using global CLI in a workspace
- Loading branch information
Showing
11 changed files
with
318 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
import '../dist/bootstrap.mjs'; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |