Skip to content

Commit d79b9ee

Browse files
committed
feat: introduce COMMANDKIT_CWD constant for consistent working directory reference
- Added COMMANDKIT_CWD constant to centralize the current working directory across the codebase. - Updated various modules to use COMMANDKIT_CWD instead of process.cwd() for improved consistency and maintainability.
1 parent 2fa864d commit d79b9ee

File tree

15 files changed

+65
-53
lines changed

15 files changed

+65
-53
lines changed

packages/commandkit/env.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ const {
33
COMMANDKIT_IS_CLI,
44
COMMANDKIT_IS_DEV,
55
COMMANDKIT_IS_TEST,
6-
} = require('commandkit');
6+
COMMANDKIT_CWD,
7+
} = require('./dist/utils/constants.js');
78

89
module.exports = {
910
COMMANDKIT_BOOTSTRAP_MODE,
1011
COMMANDKIT_IS_CLI,
1112
COMMANDKIT_IS_DEV,
1213
COMMANDKIT_IS_TEST,
14+
COMMANDKIT_CWD,
1315
};

packages/commandkit/env.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export {
33
COMMANDKIT_IS_CLI,
44
COMMANDKIT_IS_DEV,
55
COMMANDKIT_IS_TEST,
6-
} from 'commandkit';
6+
COMMANDKIT_CWD,
7+
} from './dist/utils/constants';

packages/commandkit/src/cli/build.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { rimraf } from 'rimraf';
88
import { performTypeCheck } from './type-checker';
99
import { copyLocaleFiles } from './common';
1010
import { MaybeArray } from '../components';
11+
import { COMMANDKIT_CWD } from '../utils/constants';
1112

1213
/**
1314
* @private
@@ -51,7 +52,7 @@ export async function buildApplication({
5152
const config = await loadConfigFile(configPath);
5253

5354
if (!isDev && !config?.typescript?.ignoreBuildErrors) {
54-
await performTypeCheck(configPath || process.cwd());
55+
await performTypeCheck(configPath || COMMANDKIT_CWD);
5556
}
5657

5758
const pluginRuntime = new CompilerPluginRuntime(
@@ -121,7 +122,11 @@ export async function buildApplication({
121122
});
122123

123124
await copyLocaleFiles('src', dest);
124-
await injectEntryFile(configPath || process.cwd(), !!isDev, config.distDir);
125+
await injectEntryFile(
126+
configPath || COMMANDKIT_CWD,
127+
!!isDev,
128+
config.distDir,
129+
);
125130
} catch (error) {
126131
console.error('Build failed:', error);
127132
if (error instanceof Error) {

packages/commandkit/src/cli/common.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import colors from '../utils/colors';
55
import { ResolvedCommandKitConfig } from '../config/utils';
66
import { generateTypesPackage } from '../utils/types-package';
77
import { execSync } from 'node:child_process';
8+
import { COMMANDKIT_CWD } from '../utils/constants';
89

910
let ts: typeof import('typescript') | undefined;
1011

@@ -40,21 +41,6 @@ export function panic(message: any): never {
4041
process.exit(1);
4142
}
4243

43-
/**
44-
* @private
45-
* @internal
46-
*/
47-
export function findPackageJSON() {
48-
const cwd = process.cwd();
49-
const target = join(cwd, 'package.json');
50-
51-
if (!fs.existsSync(target)) {
52-
panic('Could not find package.json in current directory.');
53-
}
54-
55-
return JSON.parse(fs.readFileSync(target, 'utf8'));
56-
}
57-
5844
/**
5945
* @private
6046
* @internal
@@ -89,7 +75,7 @@ function detectPackageManager() {
8975
};
9076

9177
for (const [lockfile, manager] of Object.entries(lockfiles)) {
92-
if (fs.existsSync(join(process.cwd(), lockfile))) {
78+
if (fs.existsSync(join(COMMANDKIT_CWD, lockfile))) {
9379
packageManager = manager;
9480
break;
9581
}
@@ -118,7 +104,7 @@ export async function loadTypeScript(e?: string) {
118104
const prefix = packageManager === 'deno' ? 'npm:' : '';
119105
execSync(`${packageManager} add -D ${prefix}typescript`, {
120106
stdio: 'inherit',
121-
cwd: process.cwd(),
107+
cwd: COMMANDKIT_CWD,
122108
});
123109

124110
console.log(
@@ -169,7 +155,7 @@ export async function loadConfigFileFromPath(
169155

170156
await generateTypesPackage();
171157

172-
const nodeModulesPath = process.cwd();
158+
const nodeModulesPath = COMMANDKIT_CWD;
173159

174160
const tmpFile = join(nodeModulesPath, 'compiled-commandkit.config.mjs');
175161

@@ -223,8 +209,8 @@ export function erase(dir: string) {
223209
* @internal
224210
*/
225211
export async function copyLocaleFiles(_from: string, _to: string) {
226-
const resolvedFrom = join(process.cwd(), _from);
227-
const resolvedTo = join(process.cwd(), _to);
212+
const resolvedFrom = join(COMMANDKIT_CWD, _from);
213+
const resolvedTo = join(COMMANDKIT_CWD, _to);
228214

229215
const localePaths = ['app/locales'];
230216
const srcLocalePaths = localePaths.map((path) => join(resolvedFrom, path));

packages/commandkit/src/cli/development.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import colors from '../utils/colors';
99
import { ChildProcess } from 'node:child_process';
1010
import { setTimeout as sleep } from 'node:timers/promises';
1111
import { randomUUID } from 'node:crypto';
12-
import { HMREventType } from '../utils/constants';
12+
import { COMMANDKIT_CWD, HMREventType } from '../utils/constants';
1313
import { findEntrypoint } from './common';
1414

1515
/**
@@ -55,7 +55,7 @@ const isEventSource = (p: string) =>
5555
export async function bootstrapDevelopmentServer(configPath?: string) {
5656
process.env.COMMANDKIT_BOOTSTRAP_MODE = 'development';
5757
const start = performance.now();
58-
const cwd = configPath || process.cwd();
58+
const cwd = configPath || COMMANDKIT_CWD;
5959
const configPaths = getPossibleConfigPaths(cwd);
6060

6161
const watcher = watch([join(cwd, 'src'), ...configPaths], {

packages/commandkit/src/cli/generators.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { join } from 'path';
33
import { panic } from './common';
44
import { existsSync } from 'fs';
55
import colors from '../utils/colors';
6+
import { COMMANDKIT_CWD } from '../utils/constants';
67

7-
const BASE_PATH = process.cwd();
8+
const BASE_PATH = COMMANDKIT_CWD;
89
const COMMANDS_DIR = join(BASE_PATH, 'src/app/commands');
910
const EVENTS_DIR = join(BASE_PATH, 'src/app/events');
1011

@@ -13,7 +14,7 @@ const EVENTS_DIR = join(BASE_PATH, 'src/app/events');
1314
* @internal
1415
*/
1516
const formatPath = (path: string) =>
16-
path.replace(process.cwd(), '.').replace(/\\/g, '/');
17+
path.replace(COMMANDKIT_CWD, '.').replace(/\\/g, '/');
1718

1819
/**
1920
* @private

packages/commandkit/src/cli/information.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { execSync } from 'node:child_process';
33
import { version as commandkitVersion } from '../version';
44
import fs from 'node:fs';
55
import path from 'node:path';
6+
import { COMMANDKIT_CWD } from '../utils/constants';
67

78
/**
89
* @private
@@ -65,11 +66,11 @@ function findPackageVersion(packageName: string) {
6566
} catch (e) {
6667
try {
6768
const basePaths = [
68-
path.join(process.cwd(), 'node_modules', packageName),
69-
path.join(process.cwd(), '..', '..', 'node_modules', packageName),
70-
path.join(process.cwd(), '..', '..', '.pnpm', packageName),
69+
path.join(COMMANDKIT_CWD, 'node_modules', packageName),
70+
path.join(COMMANDKIT_CWD, '..', '..', 'node_modules', packageName),
71+
path.join(COMMANDKIT_CWD, '..', '..', '.pnpm', packageName),
7172
path.join(
72-
process.cwd(),
73+
COMMANDKIT_CWD,
7374
'..',
7475
'..',
7576
'node_modules',
@@ -89,7 +90,7 @@ function findPackageVersion(packageName: string) {
8990
}
9091

9192
const nodeModulesPath = path.join(
92-
process.cwd(),
93+
COMMANDKIT_CWD,
9394
'..',
9495
'..',
9596
'node_modules',

packages/commandkit/src/cli/init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isCompilerPlugin,
1010
} from '../plugins';
1111
import { panic } from './common';
12+
import { COMMANDKIT_CWD } from '../utils/constants';
1213

1314
/**
1415
* Creates a command line interface for CommandKit.
@@ -142,7 +143,7 @@ export async function bootstrapCommandkitCLI(
142143
}
143144
});
144145

145-
const types = join(process.cwd(), 'node_modules', 'commandkit-types');
146+
const types = join(COMMANDKIT_CWD, 'node_modules', 'commandkit-types');
146147

147148
if (!existsSync(types)) {
148149
await mkdir(types, { recursive: true }).catch(() => {});

packages/commandkit/src/cli/production.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { join } from 'path';
21
import { loadConfigFile } from '../config/loader';
32
import { createAppProcess } from './app-process';
43
import { existsSync } from 'fs';
54
import { findEntrypoint, panic } from './common';
65
import { buildApplication } from './build';
76
import { CompilerPlugin, isCompilerPlugin } from '../plugins';
87
import { createSpinner } from './utils';
8+
import { COMMANDKIT_CWD } from '../utils/constants';
99

1010
/**
1111
* @private
1212
* @internal
1313
*/
1414
export async function bootstrapProductionServer(configPath?: string) {
1515
process.env.COMMANDKIT_BOOTSTRAP_MODE = 'production';
16-
const cwd = configPath || process.cwd();
16+
const cwd = configPath || COMMANDKIT_CWD;
1717
const config = await loadConfigFile(cwd);
1818
const mainFile = findEntrypoint(config.distDir);
1919

@@ -32,7 +32,7 @@ export async function bootstrapProductionServer(configPath?: string) {
3232
*/
3333
export async function createProductionBuild(configPath?: string) {
3434
process.env.COMMANDKIT_BOOTSTRAP_MODE = 'production';
35-
const cwd = configPath || process.cwd();
35+
const cwd = configPath || COMMANDKIT_CWD;
3636
const config = await loadConfigFile(cwd);
3737

3838
const spinner = await createSpinner(

packages/commandkit/src/config/loader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { existsSync } from 'node:fs';
22
import { join } from 'node:path';
33
import { loadConfigFileFromPath } from '../cli/common';
44
import { getConfig } from './config';
5+
import { COMMANDKIT_CWD } from '../utils/constants';
56

67
const CONFIG_FILE_NAMES = [
78
'commandkit.config.js',
@@ -41,7 +42,7 @@ let loadedConfig: ReturnType<typeof getConfig> | null = null;
4142
* Load the configuration file from the given entrypoint.
4243
* @param entrypoint The entrypoint to load the configuration file from. Defaults to the current working directory.
4344
*/
44-
export async function loadConfigFile(entrypoint = process.cwd()) {
45+
export async function loadConfigFile(entrypoint = COMMANDKIT_CWD) {
4546
if (loadedConfig) return loadedConfig;
4647
const filePath = findConfigFile(entrypoint);
4748
if (!filePath) return getConfig();

0 commit comments

Comments
 (0)