Skip to content

Commit 0cdb3d3

Browse files
committed
fix package initializer to add the package to devdependency and set global settings to actually work this time.
1 parent 8b34b6c commit 0cdb3d3

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

src/__tests__/unit/global-settings.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ describe('global-settings', () => {
1010

1111
beforeEach(() => {
1212
tempDir = createTempDir();
13+
// Create shops directory for settings
14+
const shopsDir = path.join(tempDir, 'shops');
15+
if (!fs.existsSync(shopsDir)) {
16+
fs.mkdirSync(shopsDir, { recursive: true });
17+
}
1318
});
1419

1520
afterEach(() => {
@@ -39,7 +44,7 @@ describe('global-settings', () => {
3944
version: '1.0.0'
4045
};
4146

42-
const settingsPath = path.join(tempDir, 'settings.json');
47+
const settingsPath = path.join(tempDir, 'shops', 'settings.json');
4348
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
4449

4550
// Act
@@ -54,7 +59,7 @@ describe('global-settings', () => {
5459

5560
test('handles corrupted settings file', async () => {
5661
// Arrange
57-
const settingsPath = path.join(tempDir, 'settings.json');
62+
const settingsPath = path.join(tempDir, 'shops', 'settings.json');
5863
fs.writeFileSync(settingsPath, '{ invalid json }');
5964

6065
// Act
@@ -84,7 +89,7 @@ describe('global-settings', () => {
8489
// Assert
8590
expect(result.success).toBe(true);
8691

87-
const settingsPath = path.join(tempDir, 'settings.json');
92+
const settingsPath = path.join(tempDir, 'shops', 'settings.json');
8893
expect(fs.existsSync(settingsPath)).toBe(true);
8994

9095
const savedSettings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
@@ -119,7 +124,7 @@ describe('global-settings', () => {
119124
// Assert
120125
expect(result.success).toBe(true);
121126

122-
const settingsPath = path.join(tempDir, 'settings.json');
127+
const settingsPath = path.join(tempDir, 'shops', 'settings.json');
123128
const savedSettings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
124129
expect(savedSettings.contentProtection.defaultMode).toBe('strict');
125130
});

src/lib/Initializer.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs";
22
import path from "path";
3+
import { createRequire } from "module";
34
import {
45
select,
56
spinner,
@@ -10,6 +11,10 @@ import {
1011
import { logger } from "./core/logger.js";
1112
import { ShopConfigurationError } from "./errors/ShopError.js";
1213

14+
// Get package version dynamically
15+
const require = createRequire(import.meta.url);
16+
const { version: CLI_VERSION } = require("../../package.json");
17+
1318
/**
1419
* Initializes multi-shop setup in existing Shopify theme projects
1520
* Sets up directory structure, package.json scripts, and GitHub workflows
@@ -156,10 +161,25 @@ export class Initializer {
156161

157162
packageJson.scripts = { ...packageJson.scripts, ...newScripts };
158163

159-
// Add devDependency if not already present
160-
if (!packageJson.devDependencies?.["@shopdevs/multi-shop-cli"]) {
164+
// Manage @shopdevs/multi-shop-cli dependency
165+
const packageName = "@shopdevs/multi-shop-cli";
166+
const existingDepVersion = packageJson.dependencies?.[packageName];
167+
const existingDevDepVersion = packageJson.devDependencies?.[packageName];
168+
169+
// Remove from dependencies if present (should only be in devDependencies)
170+
if (existingDepVersion) {
171+
delete packageJson.dependencies[packageName];
172+
this.logger.debug('Removed from dependencies', { package: packageName, version: existingDepVersion });
173+
}
174+
175+
// Add to devDependencies if not already present, using existing version or current CLI version
176+
if (!existingDevDepVersion) {
161177
packageJson.devDependencies = packageJson.devDependencies || {};
162-
packageJson.devDependencies["@shopdevs/multi-shop-cli"] = "^1.0.0";
178+
const versionToUse = existingDepVersion || `^${CLI_VERSION}`;
179+
packageJson.devDependencies[packageName] = versionToUse;
180+
this.logger.debug('Added to devDependencies', { package: packageName, version: versionToUse });
181+
} else {
182+
this.logger.debug('Already in devDependencies', { package: packageName, version: existingDevDepVersion });
163183
}
164184

165185
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2));

src/lib/core/global-settings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const DEFAULT_SETTINGS: GlobalSettings = {
1818

1919
export const loadGlobalSettings = async (cwd: string): Promise<Result<GlobalSettings>> => {
2020
try {
21-
const settingsPath = path.join(cwd, 'settings.json');
21+
const settingsPath = path.join(cwd, 'shops', 'settings.json');
2222

2323
if (!fs.existsSync(settingsPath)) {
2424
return { success: true, data: DEFAULT_SETTINGS };
@@ -38,7 +38,7 @@ export const loadGlobalSettings = async (cwd: string): Promise<Result<GlobalSett
3838

3939
export const saveGlobalSettings = async (cwd: string, settings: GlobalSettings): Promise<Result<void>> => {
4040
try {
41-
const settingsPath = path.join(cwd, 'settings.json');
41+
const settingsPath = path.join(cwd, 'shops', 'settings.json');
4242
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
4343

4444
return { success: true };

src/lib/core/shop-creation.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import type { ShopConfig } from "../../types/shop.js";
33
import type { CLIContext, Result } from "./types.js";
44
import { collectShopData, type ShopData } from "./shop-input.js";
55
import { setupShopResources } from "./shop-setup.js";
6+
import { getDefaultContentProtection } from "./global-settings.js";
67

78
/**
89
* Shop creation workflow
910
*/
1011

1112
export const createNewShop = async (context: CLIContext): Promise<Result<void>> => {
1213
intro("🆕 Create New Shop");
13-
14+
1415
const shopDataResult = await collectShopData(context);
1516
if (!shopDataResult.success || !shopDataResult.data) return { success: false, error: shopDataResult.error || "Failed to collect shop data" };
1617

17-
const configResult = await buildShopConfig(shopDataResult.data);
18+
const configResult = await buildShopConfig(shopDataResult.data, context.deps.cwd);
1819
if (!configResult.success || !configResult.data) return { success: false, error: configResult.error || "Failed to build config" };
1920

2021
const saveResult = await context.shopOps.saveConfig(shopDataResult.data.shopId, configResult.data);
@@ -27,7 +28,10 @@ export const createNewShop = async (context: CLIContext): Promise<Result<void>>
2728
return { success: true };
2829
};
2930

30-
const buildShopConfig = async (shopData: ShopData): Promise<Result<ShopConfig>> => {
31+
const buildShopConfig = async (shopData: ShopData, cwd: string): Promise<Result<ShopConfig>> => {
32+
// Get default content protection settings from global config
33+
const defaultProtection = await getDefaultContentProtection(cwd);
34+
3135
const config: ShopConfig = {
3236
shopId: shopData.shopId,
3337
name: shopData.shopName,
@@ -45,6 +49,12 @@ const buildShopConfig = async (shopData: ShopData): Promise<Result<ShopConfig>>
4549
authentication: {
4650
method: shopData.authMethod
4751
}
52+
},
53+
// Apply default content protection settings if configured
54+
contentProtection: {
55+
enabled: true,
56+
mode: defaultProtection.defaultMode,
57+
verbosity: defaultProtection.defaultVerbosity
4858
}
4959
};
5060

0 commit comments

Comments
 (0)