Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-flies-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/dev-cli": patch
---

Print error when the `root` property of the configuration file is `null`
5 changes: 5 additions & 0 deletions .changeset/plenty-drinks-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/dev-cli": patch
---

Warn when `publishableKey` or `secretKey` are invalid
5 changes: 5 additions & 0 deletions .changeset/red-bobcats-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/dev-cli": patch
---

Warn if configuration file already exists when running `clerk-dev init`
5 changes: 4 additions & 1 deletion packages/dev-cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@ export default function cli() {
await watch({ js });
});

program.parseAsync();
program.parseAsync().catch(err => {
console.error(err.message);
process.exit(1);
});
}
7 changes: 7 additions & 0 deletions packages/dev-cli/src/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

Expand All @@ -9,6 +10,12 @@ import { getCLIRoot } from '../utils/getMonorepoRoot.js';
* Performs one-time machine-level configuration tasks such as creating a blank config file.
*/
export async function init() {
// If the config file already exists, print a warning and exit.
if (existsSync(CONFIG_FILE)) {
console.warn('Configuration file already exists. Run `clerk-dev config` to open it.');
return;
}

const cliRoot = getCLIRoot();

/** @type import('../utils/getConfiguration.js').Configuration */
Expand Down
7 changes: 6 additions & 1 deletion packages/dev-cli/src/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { join } from 'node:path';
import { parse } from 'dotenv';

import { applyCodemod } from '../codemods/index.js';
import { INVALID_INSTANCE_KEYS_ERROR } from '../utils/errors.js';
import { getClerkPackages } from '../utils/getClerkPackages.js';
import { getConfiguration } from '../utils/getConfiguration.js';
import { getDependencies } from '../utils/getDependencies.js';
Expand Down Expand Up @@ -64,7 +65,11 @@ async function detectFramework() {
*/
async function getInstanceConfiguration(configuration) {
const { activeInstance, instances } = configuration;
return instances[activeInstance];
const activeInstanceConfig = instances[activeInstance];
if (!activeInstanceConfig.publishableKey.startsWith('pk_') || !activeInstanceConfig.secretKey.startsWith('sk_')) {
throw new Error(INVALID_INSTANCE_KEYS_ERROR);
}
return activeInstanceConfig;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/dev-cli/src/commands/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join } from 'node:path';

import concurrently from 'concurrently';

import { NULL_ROOT_ERROR } from '../utils/errors.js';
import { getClerkPackages } from '../utils/getClerkPackages.js';
import { getDependencies } from '../utils/getDependencies.js';
import { getMonorepoRoot } from '../utils/getMonorepoRoot.js';
Expand All @@ -24,6 +25,9 @@ export async function watch({ js }) {
const args = ['watch', 'build', ...filterArgs];

const cwd = await getMonorepoRoot();
if (!cwd) {
throw new Error(NULL_ROOT_ERROR);
}

/** @type {import('concurrently').ConcurrentlyCommandInput} */
const clerkJsCommand = {
Expand Down
5 changes: 5 additions & 0 deletions packages/dev-cli/src/utils/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const NULL_ROOT_ERROR =
'The `root` property of your configuration file is null. Run `clerk-dev set-root` in your local checkout of clerk/javascript to set it.';

export const INVALID_INSTANCE_KEYS_ERROR =
'The publishableKey and secretKey fields of your config are invalid. publishableKey should start with `pk_` and secretKey should start with `sk_`.';
4 changes: 4 additions & 0 deletions packages/dev-cli/src/utils/getClerkPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname, posix } from 'node:path';

import { globby } from 'globby';

import { NULL_ROOT_ERROR } from './errors.js';
import { getMonorepoRoot } from './getMonorepoRoot.js';

/**
Expand All @@ -11,6 +12,9 @@ import { getMonorepoRoot } from './getMonorepoRoot.js';
*/
export async function getClerkPackages() {
const monorepoRoot = await getMonorepoRoot();
if (!monorepoRoot) {
throw new Error(NULL_ROOT_ERROR);
}
/** @type {Record<string, string>} */
const packages = {};
const clerkPackages = await globby([posix.join(monorepoRoot, 'packages', '*', 'package.json'), '!*node_modules*']);
Expand Down
11 changes: 3 additions & 8 deletions packages/dev-cli/src/utils/getMonorepoRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ export function getCLIRoot() {
}

/**
* Gets the `root` property of the clerk-dev configuration file, falling back to the folder containing the source
* for the running instance of clerk-dev.
* @returns {Promise<string>}
* Gets the `root` property of the clerk-dev configuration file.
* @returns {Promise<string | null>}
*/
export async function getMonorepoRoot() {
const config = await getConfiguration();
if (config.root) {
return config.root;
}

return getCLIRoot();
return config.root;
}