Skip to content
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

v 2.0.3 #2

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
timezone: Europe/Oslo
time: '09:00'
open-pull-requests-limit: 30
labels:
- '[Dependency] GitHub Action'
- '[Type] Dependency'
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: weekly
timezone: Europe/Oslo
time: '09:00'
open-pull-requests-limit: 30
rebase-strategy: disabled
labels:
- '[Dependency] JavaScript'
- '[Type] Dependency'
- package-ecosystem: 'composer'
directory: '/'
schedule:
interval: weekly
timezone: Europe/Oslo
time: '09:00'
open-pull-requests-limit: 30
labels:
- '[Dependency] php'
- '[Type] Dependency'
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
# wp-project-version-sync

A CLI command to update your WordPress plugin or theme's [file header](https://codex.wordpress.org/File_Header) version based off of your `package.json` version.
A CLI command to update your WordPress plugin, theme, readme.txt and json file header version based off of your `package.json` version.

wp-update-project-version is a CLI command that is most useful when added to your build script in your `package.json`, like so:
`wp-update-project-version` is a CLI command that is most useful when added to your build script in your `package.json`.

```
## Usage

Only `package.json` is the supported `<source>` reference for a version.

You can update one or more files, WordPress header files (plugin and `style.css`), `readme.txt` and `*.json` (eg block.json) are supported.

Sync version number in `readme.txt`, `src/block.json` and `plugin.php` with the version number in `package.json`:


```json
{
"scripts": {
"build": "webpack -p && wp-project-version-sync -p style.css"
"build": "npm run bump && wp-scripts build",
"bump": "wp-update-project-version -s package.json -p readme.txt src/block.json plugin.php",
}
}
```

## Options

```
Usage: wp-update-project-version [options]

Options:
-s, --source package.json Optional, only package.json is supported
-p, --path <path> Path to file(s) to update
```

## Installation

```
npm i @masonite/wp-project-version-sync
npm i @soderlind/wp-project-version-sync
```

## Usage
## Credits

Currently, only `package.json` is the only supported `<source>` reference for a version. You can update one or more files, though at the moment, only WordPress header files are supported.
This is a fork of [wp-project-version-sync v1.1.0](https://github.com/masonitedoors/wp-project-version-sync) by @masonitedoors, see changelog for my [changes](https://github.com/soderlind/wp-project-version-sync/commit/8a175ab024ccb1a6ae2e21c8e958c373a42d41f5).

```
// wp-update-project-version -s <source> -p <path> [paths...]
wp-update-project-version -s package.json -p style.css index.php
```

## Changelog

### 2.0.0

- Update version number in more file types:
- `Stable tag` in readme.txt
- `"version"` in *.json (eg block.json)
- Update dependencies
- Update jest tests
- Add tests for readme.txt and block.json.
- Make it compatible with the updated dependencies.
10 changes: 10 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs"
}
]
]
}
137 changes: 73 additions & 64 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,98 @@
#! /usr/bin/env node
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const program = require("commander");
const chalk = require("chalk");
const wpThemeVersionPattern = /^((?:<\?php\s+)?\/\*\*?[\s\S]*?Version:)(\s*)([\d.]*)/;
// Import required modules
const fs = require('fs');
const path = require('path');
const { program } = require('commander');
const chalk = require('chalk');

// Regular expression pattern to match WordPress version numbers in file headers
const wpVersionPattern = /((?:<\?php\s+)?Stable tag:|Version:|"version":)(\s+)(["']?)(\d+\.\d+\.\d+)(["']?)/i;

// Run the program.
run();

// Main function to run the program
function run() {
let paths = [];
let errors = [];
let srcFileDefault = "package.json";
let srcFileVal = srcFileDefault;

program
.option(
"-s, --src [srcFile]",
"Source of version. Default is your project's package.json"
)
.option(
"-p, --path <path...> [paths]",
"Relative path to your plugin or theme's file header."
)
.parse(process.argv);
// Define command line options
program
.option('-s, --src [srcFile]', "Source of version. Default is your project's package.json")
.option('-p, --path <path...>', "Relative path to your plugin or theme's file header.")
.parse(process.argv);

srcFileVal = program.srcFile || srcFileDefault;
pathVal = program.path || null;
pathsVal = program.args || null;
// Get values of command line options
const srcFileVal = program.srcFile || 'package.json';
const pathVal = program.opts().path || null;
const pathsVal = program.args || null;

if (srcFileVal !== srcFileDefault) {
console.error(
chalk.red("package.json is the only accepted source at the moment.")
);
}
// Check if source file is package.json
if (srcFileVal !== 'package.json') {
console.error(chalk.red('package.json is the only accepted source at the moment.'));
return;
}

if (!pathVal) {
console.error(chalk.red("No paths were specified."));
return false;
}
// Check if any paths were specified
if (!pathVal) {
console.error(chalk.red('No paths were specified.'));
return;
}

console.log(
`Source: ${srcFileVal} ${
srcFileDefault === srcFileVal ? chalk.green("(Default)") : ""
}\n`
);
// Print source file and default message
console.log(
`Source: ${srcFileVal} ${srcFileVal === 'package.json' ? chalk.green('(Default)') : ''}\n`
);

paths = paths.concat(pathVal, pathsVal);
// Combine path values into a single array
const paths = [...pathVal, ...pathsVal];

paths.forEach(p => {
if (fs.existsSync(p)) {
updateFileHeader(pathVal);
} else {
errors.push(`${p} could not be found.`)
}
});
// Loop through each path and update file header
let errors = false;
paths.forEach((p) => {
if (fs.existsSync(p)) {
updateFileHeader(p);
} else {
console.error(chalk.red(`${p} could not be found.`));
errors = true;
}
});

if (errors.length > 0) {
console.error(chalk.red(errors.join('\n')));
console.log(chalk.yellow('Done with errors.'))
} else {
console.log(chalk.green('Done without errors.'))
}
// Print success or error message
if (errors) {
console.log(chalk.yellow('Done with errors.'));
} else {
console.log(chalk.green('Done without errors.'));
}
}

// Function to update the version number in a file header
function updateFileHeader(pathToFile, version = getPackageJsonVersion()) {
let newContent = replaceFileHeaderVersion(pathToFile, version);
fs.writeFileSync(pathToFile, newContent, "utf8");
// Replace old version number with new version number
const newContent = replaceFileHeaderVersion(pathToFile, version);
// Write new content to file
fs.writeFileSync(pathToFile, newContent, 'utf8');
}

// Function to get the version number from package.json
function getPackageJsonVersion() {
const packageJson = require(path.resolve(process.cwd(), "package.json"));
return packageJson.version;
const packageJson = require(path.resolve(process.cwd(), 'package.json'));
return packageJson.version;
}

// Function to replace the version number in a file header
function replaceFileHeaderVersion(pathToFile, version) {
const replaceOutput = function(match, before, whitespace, oldVersion) {
return before + whitespace + version;
};
const file = fs.readFileSync(pathToFile, "utf8");
return file.replace(wpThemeVersionPattern, replaceOutput);
// Replace old version number with new version number
const replaceOutput = function (match, before, whitespace, maybeQuote, oldVersion) {
return before + whitespace + maybeQuote + version + maybeQuote;
};
// Read file contents
const file = fs.readFileSync(pathToFile, 'utf8');
// Replace version number in file contents
return file.replace(wpVersionPattern, replaceOutput);
}

// Export functions for testing
module.exports = {
updateFileHeader,
getPackageJsonVersion,
replaceFileHeaderVersion
updateFileHeader,
getPackageJsonVersion,
replaceFileHeaderVersion,
};
Loading