-
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 2 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,60 @@ | ||
| import fs from 'fs/promises'; | ||
| import fastGlob from 'fast-glob'; | ||
| import dayjs from 'dayjs'; | ||
|
|
||
| const ROOT_DIR = '.'; | ||
| const FILE_PATTERN = '**/shopify.extension.toml.liquid'; | ||
|
|
||
| // 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 }); | ||
| } | ||
|
|
||
| // Method to update the API version in the file | ||
| async function updateApiVersion(filePath, latestVersion) { | ||
| const content = await fs.readFile(filePath, 'utf8'); | ||
| const updatedContent = content.replace(/api_version\s*=\s*"\d{4}-\d{2}"/, `api_version = "${latestVersion}"`); | ||
|
|
||
| await fs.writeFile(filePath, updatedContent, 'utf8'); | ||
| console.log(`Updated API version in ${filePath}`); | ||
| } | ||
|
|
||
| // Main method to check and update API versions | ||
| async function checkAndUpdateApiVersions() { | ||
| const latestVersion = getLatestApiVersion(); | ||
| const extensionFiles = await findAllExtensionFiles(); | ||
|
|
||
| for (const filePath of extensionFiles) { | ||
| 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) { | ||
| await updateApiVersion(filePath, latestVersion); | ||
| } else { | ||
| console.log(`API version in ${filePath} is already up to date.`); | ||
| } | ||
| } else { | ||
| console.warn(`No API version found in ${filePath}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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); | ||
| }); | ||
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,106 @@ | ||
| import fs from 'fs/promises'; | ||
| import fastGlob from 'fast-glob'; | ||
| import toml from '@iarna/toml'; | ||
|
|
||
| const ROOT_DIR = '.'; | ||
| const FILE_PATTERNS = ['**/Cargo.toml', '**/Cargo.toml.liquid']; | ||
| const LIQUID_PLACEHOLDER = 'LIQUID_PLACEHOLDER'; | ||
|
|
||
| async function findAllCargoFiles() { | ||
| return fastGlob(FILE_PATTERNS, { cwd: ROOT_DIR, absolute: true }); | ||
| } | ||
|
|
||
| async function getLatestVersion(packageName) { | ||
| try { | ||
| // Fetch the latest version of a package from crates.io | ||
| const response = await fetch(`https://crates.io/api/v1/crates/${packageName}`); | ||
| const jsonResponse = await response.json(); | ||
| return jsonResponse.crate.max_version; | ||
| } catch (error) { | ||
| console.warn(`Could not fetch version for package ${packageName}:`, error.message); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async function updateDependencyVersion(name, currentVersion) { | ||
| const latestVersion = await getLatestVersion(name); | ||
|
|
||
| if (latestVersion) { | ||
| if (typeof currentVersion === 'string') { | ||
| if (!currentVersion.includes(latestVersion)) { | ||
| console.log(`Updating ${name} from ${currentVersion} to ${latestVersion}`); | ||
| return latestVersion; | ||
| } | ||
| } else if (typeof currentVersion === 'object' && 'version' in currentVersion) { | ||
| if (!currentVersion.version.includes(latestVersion)) { | ||
| console.log(`Updating ${name} from ${currentVersion.version} to ${latestVersion}`); | ||
| return { ...currentVersion, version: latestVersion }; | ||
| } | ||
| } | ||
| } | ||
| return currentVersion; | ||
| } | ||
|
|
||
| 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 restoreLiquidSyntax(content, liquidExpressions) { | ||
| return content.replace(new RegExp(`\\{${LIQUID_PLACEHOLDER}:(\\d+)\\}`, 'g'), (match, index) => { | ||
| return liquidExpressions[Number(index)]; | ||
| }); | ||
| } | ||
|
|
||
| async function checkAndUpdateDependencies(filePath) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing this, can't we transform the Liquid files to TOML files, run Similarly, could we do that for JS too? |
||
| let content = await fs.readFile(filePath, 'utf8'); | ||
|
|
||
| const isLiquidFile = filePath.endsWith('.liquid'); | ||
| let liquidExpressions = []; | ||
|
|
||
| if (isLiquidFile) { | ||
| const processed = preprocessLiquidSyntax(content); | ||
| content = processed.placeholderContent; | ||
| liquidExpressions = processed.liquidExpressions; | ||
| } | ||
|
|
||
| let tomlData; | ||
| try { | ||
| tomlData = toml.parse(content); | ||
| } catch (error) { | ||
| console.error(`Failed to parse TOML in file: ${filePath}`, error.message); | ||
| return; | ||
| } | ||
|
|
||
| if (tomlData.dependencies) { | ||
| const dependencyNames = Object.keys(tomlData.dependencies); | ||
| for (const name of dependencyNames) { | ||
| const currentVersion = tomlData.dependencies[name]; | ||
| tomlData.dependencies[name] = await updateDependencyVersion(name, currentVersion); | ||
| } | ||
| } | ||
|
|
||
| let updatedContent = toml.stringify(tomlData); | ||
| if (isLiquidFile) { | ||
| updatedContent = restoreLiquidSyntax(updatedContent, liquidExpressions); | ||
| } | ||
|
|
||
| await fs.writeFile(filePath, updatedContent, 'utf8'); | ||
| console.log(`Updated dependencies in ${filePath}`); | ||
| } | ||
|
|
||
| async function main() { | ||
| const cargoFiles = await findAllCargoFiles(); | ||
| for (const filePath of cargoFiles) { | ||
| await checkAndUpdateDependencies(filePath); | ||
| } | ||
| } | ||
|
|
||
| main().catch(error => { | ||
| console.error('Error checking and updating dependencies:', 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
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?