-
Notifications
You must be signed in to change notification settings - Fork 64
Add utilities for schema management and dependency updates #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98e1e59
Add utilities for API version management and schema updates
davejcameron 8a78179
Document dependency update utilities in README
davejcameron 2737667
Use @shopify/toml-patch for TOML manipulation
davejcameron ba8de3c
Update generate-app.js to use toml-patch library directly
davejcameron b1fba32
Require existing shopify.app.toml in generate-app utility
davejcameron 3eeb22c
Use proper TOML parsing for update-schemas utility
davejcameron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
| import fs from 'fs/promises'; | ||
| import fastGlob from 'fast-glob'; | ||
| import dayjs from 'dayjs'; | ||
| import { updateTomlValues } from '@shopify/toml-patch'; | ||
|
|
||
| const ROOT_DIR = '.'; | ||
| const FILE_PATTERN = '**/shopify.extension.toml.liquid'; | ||
| const LIQUID_PLACEHOLDER = 'LIQUID_PLACEHOLDER'; | ||
|
|
||
| // Method to get the latest API version based on today's date | ||
| function getLatestApiVersion() { | ||
| const date = dayjs(); | ||
| const year = date.year(); | ||
| const month = date.month(); | ||
| const quarter = Math.floor(month / 3); | ||
|
|
||
| const monthNum = quarter * 3 + 1; | ||
| const paddedMonth = String(monthNum).padStart(2, '0'); | ||
|
|
||
| return `${year}-${paddedMonth}`; | ||
| } | ||
|
|
||
| // Method to find all shopify.extension.toml.liquid files | ||
| async function findAllExtensionFiles() { | ||
| return fastGlob(FILE_PATTERN, { cwd: ROOT_DIR, absolute: true }); | ||
| } | ||
|
|
||
| // Function to preprocess liquid syntax | ||
| function preprocessLiquidSyntax(content) { | ||
| const liquidExpressions = []; | ||
| const placeholderContent = content.replace(/\{\{.*?\}\}|\{%\s.*?\s%\}/g, (match) => { | ||
| liquidExpressions.push(match); | ||
| return `{${LIQUID_PLACEHOLDER}:${liquidExpressions.length - 1}}`; | ||
| }); | ||
| return { placeholderContent, liquidExpressions }; | ||
| } | ||
|
|
||
| // Function to restore liquid syntax | ||
| function restoreLiquidSyntax(content, liquidExpressions) { | ||
| return content.replace(new RegExp(`\\{${LIQUID_PLACEHOLDER}:(\\d+)\\}`, 'g'), (match, index) => { | ||
| return liquidExpressions[Number(index)]; | ||
| }); | ||
| } | ||
|
|
||
| // Method to update the API version in the file using toml-patch | ||
| async function updateApiVersion(filePath, latestVersion) { | ||
| try { | ||
| const content = await fs.readFile(filePath, 'utf8'); | ||
|
|
||
| // Handle liquid templates if needed | ||
| const isLiquidFile = filePath.endsWith('.liquid'); | ||
| let liquidExpressions = []; | ||
| let processedContent = content; | ||
|
|
||
| if (isLiquidFile) { | ||
| const processed = preprocessLiquidSyntax(content); | ||
| processedContent = processed.placeholderContent; | ||
| liquidExpressions = processed.liquidExpressions; | ||
| } | ||
|
|
||
| // Use toml-patch to update the API version | ||
| const updates = [ | ||
| [['api_version'], latestVersion] | ||
| ]; | ||
|
|
||
| let updatedContent = updateTomlValues(processedContent, updates); | ||
|
|
||
| // Restore liquid syntax if needed | ||
| if (isLiquidFile) { | ||
| updatedContent = restoreLiquidSyntax(updatedContent, liquidExpressions); | ||
| } | ||
|
|
||
| await fs.writeFile(filePath, updatedContent, 'utf8'); | ||
| console.log(`Updated API version in ${filePath} to ${latestVersion}`); | ||
|
|
||
| } catch (error) { | ||
| console.error(`Error updating API version in ${filePath}:`, error.message); | ||
| } | ||
| } | ||
|
|
||
| // Main method to check and update API versions | ||
| async function checkAndUpdateApiVersions() { | ||
| const latestVersion = getLatestApiVersion(); | ||
| console.log(`Latest API version: ${latestVersion}`); | ||
| const extensionFiles = await findAllExtensionFiles(); | ||
| console.log(`Found ${extensionFiles.length} extension files to check`); | ||
|
|
||
| for (const filePath of extensionFiles) { | ||
| try { | ||
| const content = await fs.readFile(filePath, 'utf8'); | ||
| const match = content.match(/api_version\s*=\s*"(\d{4}-\d{2})"/); | ||
|
|
||
| if (match) { | ||
| const currentVersion = match[1]; | ||
|
|
||
| if (currentVersion !== latestVersion) { | ||
| console.log(`Updating ${filePath} from ${currentVersion} to ${latestVersion}`); | ||
| await updateApiVersion(filePath, latestVersion); | ||
| } else { | ||
| console.log(`API version in ${filePath} is already up to date (${currentVersion}).`); | ||
| } | ||
| } else { | ||
| console.warn(`No API version found in ${filePath}`); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error processing ${filePath}:`, error.message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| checkAndUpdateApiVersions().catch(error => { | ||
| console.error('Error checking and updating API versions:', error); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or 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,58 @@ | ||
| import fs from 'fs/promises'; | ||
| import path from 'path'; | ||
| import fastGlob from 'fast-glob'; | ||
| import { execSync } from 'child_process'; | ||
|
|
||
| const ROOT_DIR = '.'; | ||
| const FILE_PATTERN = '**/package.json.liquid'; | ||
|
|
||
| async function findAllPackageFiles() { | ||
| return fastGlob(FILE_PATTERN, { cwd: ROOT_DIR, absolute: true }); | ||
| } | ||
|
|
||
| async function getLatestVersion(packageName) { | ||
| try { | ||
| // Fetch the latest version of a package from the npm registry | ||
| const output = execSync(`npm show ${packageName} version`, { encoding: 'utf8' }); | ||
| return output.trim(); | ||
| } catch (error) { | ||
| console.warn(`Could not fetch version for package ${packageName}:`, error.message); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async function checkAndUpdateDependencies(filePath) { | ||
| const content = await fs.readFile(filePath, 'utf8'); | ||
| const jsonContent = JSON.parse(content); | ||
|
|
||
| const { dependencies = {}, devDependencies = {} } = jsonContent; | ||
|
|
||
| const updateDependencyVersion = async (dependencies) => { | ||
| for (const [name, currentVersion] of Object.entries(dependencies)) { | ||
| const latestVersion = await getLatestVersion(name); | ||
| if (latestVersion && currentVersion !== `^${latestVersion}`) { | ||
| console.log(`Updating ${name} from ${currentVersion} to ^${latestVersion}`); | ||
| dependencies[name] = `^${latestVersion}`; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await updateDependencyVersion(dependencies); | ||
| await updateDependencyVersion(devDependencies); | ||
|
|
||
| const updatedContent = JSON.stringify(jsonContent, null, 2); | ||
| await fs.writeFile(filePath, updatedContent, 'utf8'); | ||
| console.log(`Updated dependencies in ${filePath}`); | ||
| } | ||
|
|
||
| async function main() { | ||
| const packageFiles = await findAllPackageFiles(); | ||
| for (const filePath of packageFiles) { | ||
| await checkAndUpdateDependencies(filePath); | ||
| } | ||
| } | ||
|
|
||
| main().catch(error => { | ||
| console.error('Error checking and updating dependencies:', error); | ||
| process.exit(1); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we ensure that it's safe to update to the latest version? For example, if there's a major version bump. Are we relying on tests to tell us that something is wrong?