Skip to content

Commit 3eeb22c

Browse files
committed
Use proper TOML parsing for update-schemas utility
- Replace manual regex-based TOML parsing with @iarna/toml library - Simplify extension directory extraction logic - Add warning for directories specified in config that don't exist
1 parent b1fba32 commit 3eeb22c

2 files changed

Lines changed: 20 additions & 45 deletions

File tree

util/expand-liquid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Liquid } from 'liquidjs';
88
import path from 'path';
99
import fs from 'node:fs/promises';
1010
import { existsSync } from 'fs';
11-
import toml from '@iarna/toml';
11+
import toml from '@iarna/toml'; // For parsing only, no manipulation
1212
import { exec } from 'child_process';
1313

1414
async function expandLiquidTemplates(template, liquidData) {

util/update-schemas.js

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { exec } from 'child_process';
33
import path from 'path';
44
import util from 'util';
55
import { existsSync } from 'fs';
6+
import toml from '@iarna/toml';
67

78
const execPromise = util.promisify(exec);
89
const APP_TOML_FILE = 'shopify.app.toml';
@@ -16,56 +17,30 @@ async function getConfig() {
1617
}
1718

1819
const content = await fs.readFile(APP_TOML_FILE, 'utf8');
19-
const lines = content.split('\n');
20+
21+
// Parse the TOML content
22+
const parsedToml = toml.parse(content);
2023

2124
const config = {
2225
clientId: '',
2326
directories: []
2427
};
25-
26-
let inExtensionDirectories = false;
27-
const dirRegex = /'([^']+)'/g;
28-
const quoteRegex = /"([^"]+)"/g;
29-
30-
for (const line of lines) {
31-
const trimmedLine = line.trim();
32-
33-
// Extract client_id
34-
if (trimmedLine.startsWith('client_id')) {
35-
const match = line.match(quoteRegex);
36-
if (match) {
37-
config.clientId = match[0].replace(/"/g, '');
38-
}
39-
continue;
40-
}
41-
42-
// Check if we're entering the extension_directories section
43-
if (trimmedLine.startsWith('extension_directories')) {
44-
inExtensionDirectories = true;
45-
continue;
46-
}
47-
48-
// Check if we're leaving the extension_directories section
49-
if (inExtensionDirectories && trimmedLine.startsWith(']')) {
50-
inExtensionDirectories = false;
51-
continue;
52-
}
53-
54-
// Extract directories only when in extension_directories section
55-
if (inExtensionDirectories) {
56-
// Try to match with both single and double quotes
57-
let match = trimmedLine.match(dirRegex);
58-
if (!match) {
59-
match = trimmedLine.match(quoteRegex);
60-
}
61-
62-
if (match) {
63-
const cleanDir = match[0].replace(/['"]/g, '').trim();
64-
if (cleanDir && existsSync(cleanDir)) {
65-
config.directories.push(cleanDir);
66-
}
28+
29+
// Extract client_id if it exists
30+
if (parsedToml.client_id) {
31+
config.clientId = parsedToml.client_id;
32+
}
33+
34+
// Extract extension directories if they exist
35+
if (parsedToml.extension_directories && Array.isArray(parsedToml.extension_directories)) {
36+
// Filter the directories to ensure they exist
37+
config.directories = parsedToml.extension_directories.filter(dir => {
38+
const exists = existsSync(dir);
39+
if (!exists) {
40+
console.warn(`Directory specified in config does not exist: ${dir}`);
6741
}
68-
}
42+
return exists;
43+
});
6944
}
7045

7146
return config;

0 commit comments

Comments
 (0)