Skip to content

Commit

Permalink
Add node version locking tasks + update npm packages to Gulp v4
Browse files Browse the repository at this point in the history
  • Loading branch information
vladolaru committed Jan 31, 2022
1 parent fde4385 commit 135c01b
Show file tree
Hide file tree
Showing 12 changed files with 3,471 additions and 105 deletions.
33 changes: 8 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
assets/.sass-cache
assets/inc/metaboxes/.sass-cache
inc/vendor/twitter-api/cache.tweetcache

.DS_Store
.test.path
nbproject
.sass-cache
.idea
*.sublime-project
*.sublime-workspace
codekit-config.json

node_modules
build
*-ck.js
*.css.map
*.js.mapbower_components/
node_modules/
.idea
.sass-cache
*.css.map
*.js.map
package-lock.json
node-tasks/package-lock.json

.svn
README.md
license_key.txt
admin/css/*.map

admin/js/*.map

*.map
.DS_Store
*.sublime-workspace
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>=14.17.3
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions category-icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: Category Icon
Plugin URI: http://pixelgrade.com
Description: Easily add an icon to a category, tag or any other taxonomy.
Description: Easily attach an icon and/or an image to a category, tag or any other taxonomy term.
Version: 0.8.0
Author: PixelGrade
Author URI: http://pixelgrade.com
Expand Down Expand Up @@ -139,9 +139,9 @@ function plugin_init() {
}

function enqueue_admin_scripts () {
wp_enqueue_style( $this->plugin_slug . '-admin-style', plugins_url( 'assets/css/category-icon.css', __FILE__ ), array( ), $this->version );
wp_enqueue_style( $this->plugin_slug . '-admin-style', plugins_url( 'assets/admin/css/category-icon.css', __FILE__ ), array( ), $this->version );
wp_enqueue_media();
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'assets/js/category-icon.js', __FILE__ ), array( 'jquery' ), $this->version );
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'assets/admin/js/category-icon.js', __FILE__ ), array( 'jquery' ), $this->version );
wp_localize_script( $this->plugin_slug . '-admin-script', 'locals', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
) );
Expand Down
243 changes: 184 additions & 59 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,196 @@
/*
* Load Plugins
*/
var gulp = require( 'gulp' ),
exec = require( 'gulp-exec' ),
clean = require( 'gulp-clean' ),
zip = require( 'gulp-zip' );
const plugin = 'category-icon',

gulp = require('gulp'),
plugins = require('gulp-load-plugins')(),
fs = require('fs'),
del = require('del'),
cp = require('child_process'),
commandExistsSync = require('command-exists').sync,
log = require('fancy-log');

/**
* Copy plugin folder outside in a build folder, recreate styles before that
*/
function copyFolder () {
const dir = process.cwd()
return gulp.src('./*')
.pipe(plugins.exec('rm -Rf ./../build; mkdir -p ./../build/' + plugin + ';', {
silent: true,
continueOnError: true // default: false
}))
.pipe(plugins.rsync({
root: dir,
destination: '../build/' + plugin + '/',
progress: false,
silent: false,
compress: false,
recursive: true,
emptyDirectories: true,
clean: true,
exclude: ['node_modules', 'node-tasks']
}))
}
gulp.task('copy-folder', copyFolder)

/**
* Create a zip archive out of the cleaned folder and delete the folder
* Clean the folder of unneeded files and folders
*/
gulp.task( 'zip', ['build'], function() {
function removeUnneededFiles (done) {

return gulp.src( './' )
.pipe( exec( 'cd ./../; rm -rf category-icon.zip; cd ./build/; zip -r -X ./../category-icon.zip ./category-icon; cd ./../; rm -rf build' ) );
// files that should not be present in build zip
var files_to_remove = [
'**/codekit-config.json',
'node_modules',
'node-tasks',
'bin',
'tests',
'.travis.yml',
'.babelrc',
'.gitignore',
'.codeclimate.yml',
'.csslintrc',
'.eslintignore',
'.eslintrc',
'circle.yml',
'phpunit.xml.dist',
'.sass-cache',
'config.rb',
'gulpfile.js',
'webpack.config.js',
'package.json',
'package-lock.json',
'pxg.json',
'build',
'.idea',
'**/*.css.map',
'**/.git*',
'*.sublime-project',
'.DS_Store',
'**/.DS_Store',
'__MACOSX',
'**/__MACOSX',
'+development.rb',
'+production.rb',
'README.md',
'admin/src',
'admin/scss',
'admin/js/**/*.map',
'admin/css/**/*.map',
'.csscomb',
'.csscomb.json',
'.codeclimate.yml',
'tests',
'circle.yml',
'.circleci',
'.labels',
'.jscsrc',
'.jshintignore',
'browserslist',
'babel.config.js',

} );
'admin/scss',
'admin/src',
'admin/js/vendor/elasticsearch.js'
]

/**
* Copy theme folder outside in a build folder, recreate styles before that
*/
gulp.task( 'copy-folder', function() {
files_to_remove.forEach(function (e, k) {
files_to_remove[k] = '../build/' + plugin + '/' + e
})

return del(files_to_remove, {force: true})
}

gulp.task('remove-files', removeUnneededFiles)

function maybeFixBuildDirPermissions (done) {

cp.execSync('find ./../build -type d -exec chmod 755 {} \\;')

return done()
}

maybeFixBuildDirPermissions.description = 'Make sure that all directories in the build directory have 755 permissions.'
gulp.task('fix-build-dir-permissions', maybeFixBuildDirPermissions)

function maybeFixBuildFilePermissions (done) {

cp.execSync('find ./../build -type f -exec chmod 644 {} \\;')

return done()
}

maybeFixBuildFilePermissions.description = 'Make sure that all files in the build directory have 644 permissions.'
gulp.task('fix-build-file-permissions', maybeFixBuildFilePermissions)

function maybeFixIncorrectLineEndings (done) {
if (!commandExistsSync('dos2unix')) {
log.error('Could not ensure that line endings are correct on the build files since you are missing the "dos2unix" utility! You should install it.')
log.error('However, this is not a very big deal. The build task will continue.')
} else {
cp.execSync('find ./../build -type f -print0 | xargs -0 -n 1 -P 4 dos2unix')
}

return gulp.src( './' )
.pipe( exec( 'rm -Rf ./../build; mkdir -p ./../build/category-icon; cp -Rf ./* ./../build/category-icon/' ) );
} );
return done()
}

maybeFixIncorrectLineEndings.description = 'Make sure that all line endings in the files in the build directory are UNIX line endings.'
gulp.task('fix-line-endings', maybeFixIncorrectLineEndings)

// -----------------------------------------------------------------------------
// Replace the plugin's text domain with the actual text domain
// -----------------------------------------------------------------------------
function replaceTextdomainPlaceholder () {
const textDomain = 'category-icon'

return gulp.src('../build/' + plugin + '/**/*.php')
.pipe(plugins.replace(/['|"]__plugin_txtd['|"]/g, '\'' + textDomain + '\''))
.pipe(gulp.dest('../build/' + plugin))
}

gulp.task('txtdomain-replace', replaceTextdomainPlaceholder)

/**
* Clean the folder of unneeded files and folders
* Create a zip archive out of the cleaned folder and delete the folder
*/
gulp.task( 'build', ['copy-folder'], function() {

// files that should not be present in build zip
files_to_remove = [
'**/codekit-config.json',
'node_modules',
'config.rb',
'gulpfile.js',
'package-lock.json',
'package.json',
'wpgrade-core/vendor/redux2',
'wpgrade-core/features',
'wpgrade-core/tests',
'wpgrade-core/**/*.less',
'wpgrade-core/**/*.scss',
'wpgrade-core/**/*.rb',
'wpgrade-core/**/sass',
'wpgrade-core/**/scss',
'pxg.json',
'build',
'.idea',
'**/*.css.map',
'**/.sass*',
'.sass*',
'**/.git*',
'*.sublime-project',
'.DS_Store',
'**/.DS_Store',
'__MACOSX',
'**/__MACOSX'
];

files_to_remove.forEach( function( e, k ) {
files_to_remove[k] = '../build/category-icon/' + e;
} );

return gulp.src( files_to_remove, {read: false} )
.pipe( clean( {force: true} ) );
} );
function createZipFile () {
var versionString = ''
// get plugin version from the main plugin file
var contents = fs.readFileSync('./' + plugin + '.php', 'utf8')

// split it by lines
var lines = contents.split(/[\r\n]/)

function checkIfVersionLine (value, index, ar) {
var myRegEx = /^[\s\*]*[Vv]ersion:/
if (myRegEx.test(value)) {
return true
}
return false
}

// apply the filter
var versionLine = lines.filter(checkIfVersionLine)

versionString = versionLine[0].replace(/^[\s\*]*[Vv]ersion:/, '').trim()
versionString = '-' + versionString.replace(/\./g, '-')

return gulp.src('./')
.pipe(plugins.exec('cd ./../; rm -rf ' + plugin[0].toUpperCase() + plugin.slice(1) + '*.zip; cd ./build/; zip -r -X ./../' + plugin[0].toUpperCase() + plugin.slice(1) + versionString + '.zip ./; cd ./../; rm -rf build'))

}

gulp.task('make-zip', createZipFile)

function buildSequence (cb) {
return gulp.series('copy-folder', 'remove-files', 'fix-build-dir-permissions', 'fix-build-file-permissions', 'fix-line-endings', 'txtdomain-replace')(cb)
}

buildSequence.description = 'Sets up the build folder'
gulp.task('build', buildSequence)

function zipSequence (cb) {
return gulp.series('build', 'make-zip')(cb)
}

zipSequence.description = 'Creates the zip file'
gulp.task('zip', zipSequence)
31 changes: 31 additions & 0 deletions node-tasks/lock_node_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* This script executes before "npm install"
* Lock the version of Node running based on the one set in the package.json
*/

const fs = require('fs');
const path = require('path');
const semver = require ('semver');

const packageJson = require('../package.json');
const requiredNodeVersion = packageJson.engines.node;

const runningNodeVersion = process.version;

// set .nvmrc and .node_version to have the same version
fs.writeFileSync(path.join(__dirname, '../.node-version'), requiredNodeVersion, 'UTF8');
// This way we could have a bash or zsh script that would automatically use the latest installed version of node from this major release.
// Not ideal, but since nvm doesn't support ranges... :|
fs.writeFileSync(path.join(__dirname, '../.nvmrc'), semver.minVersion(requiredNodeVersion).major.toString(), 'UTF8');

// check that the required version of Node is running
if (!semver.satisfies(runningNodeVersion, requiredNodeVersion)) {
console.error(`
The current node version ${runningNodeVersion} does not satisfy the required version ${requiredNodeVersion} .
If you have NVM and AVN installed, just exit the project folder and cd into it again (learn more about AVN: https://github.com/wbyoung/avn).
Or you may just use NVM to install and use the needed node version.
`);

// kill the process if the required node version is not the one running
process.exit(1);
}
10 changes: 10 additions & 0 deletions node-tasks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "listable-node-tasks",
"description": "Tasks related to the node version required.",
"private": false,
"sideEffects": false,
"license": "GPL-2.0+",
"dependencies": {
"semver": "^7.3.5"
}
}
Loading

0 comments on commit 135c01b

Please sign in to comment.