Skip to content

Instantly share code, notes, and snippets.

@Aiq0
Last active March 16, 2022 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aiq0/790aa5f04209e5b049138445fd79c522 to your computer and use it in GitHub Desktop.
Save Aiq0/790aa5f04209e5b049138445fd79c522 to your computer and use it in GitHub Desktop.
Example usage of triggers in https://github.com/HexRx/simple-ftp-deploy minifing javascript and css files on save

This is example usage of triggers in simple-ftp-deploy plugin for Sublime Text which automatically minifies javascript and css files using css-html-js-minify library, or alternativelly, using some CLI minifier, like minify.

Basically, minifier is called on save of any .css or .js file and .min.css or .min.js file is also automatically uploaded to server. And also, .min.css and .min.js file is deleted from server when you delete origin .css or .js file.

import os
# We get same variables as in minify.py
# Get filename without extension, so we can change the extension to .min.css / .min.js
filenameWithoutExtension, extension = os.path.splitext(filename)
# Continue only if not already minified
if not '.min' in filenameWithoutExtension:
# send2trash is located in default package of Sublime Text
import Default.send2trash as send2trash
minFilename = filenameWithoutExtension + '.min' + extension
# Send .min.css / .min.js file to trash (same as when we delete it from Sublime Text)
send2trash.send2trash(minFilename)
# And finally delete it from FTP server too
ftp.delete(folder, minFilename)
import subprocess, os, time
# When this trigger is executed, we get some variables, which are usefull for us, and they includes:
# folder -> absolute path to project root folder
# filename -> absolute path to file for which this trigger is processed
# __file__ -> absolute path to this file
# ftp -> ftp class from simple-ftp-deploy file (https://github.com/HexRx/simple-ftp-deploy/blob/master/main.py)
# config -> dictionary containing configuration from simple-ftp-deploy.json
# sublime -> see https://www.sublimetext.com/docs/api_reference.html#sublime
# and some function used by simple-ftp-deploy (also see https://github.com/HexRx/simple-ftp-deploy/blob/master/main.py for usage)
# msg -> function used by simple-ftp-deploy to display messages
# error -> function used by simple-ftp-deploy to display errors
# aks -> function used by simple-ftp-deploy to show ok/cancel dialog
# Get filename without extension, so we can change the extension to .min.css / .min.js
filenameWithoutExtension, extension = os.path.splitext(filename)
# Minify on if not already minified
if not '.min' in filenameWithoutExtension:
minFilename = filenameWithoutExtension + '.min' + extension
# Measure time to print statistics
start = time.time()
# Call css-html-js-minify/css-html-js-minify.py located in same folder as this file, with filename and wait for it to finish. New file should be generated with .min.js / .min.css extension
subprocess.Popen([os.path.join(os.path.dirname(__file__), 'css-html-js-minify', 'css-html-js-minify.py'), filename], bufsize=-1, stderr=subprocess.STDOUT).wait()
# Or alternativelly, use some CLI minifier, like https://github.com/tdewolff/minify/tree/master/cmd/minify
# If you want to use other minifier, edit arguments below
# subprocess.Popen(['minify', '-o', minFilename, filename], bufsize=-1, stderr=subprocess.STDOUT).wait()
# Measure time to print statistics
end = time.time()
# Print message that we have minified that file (will print message in console and show in status bar)
msg('[Minified {0}]: {1} to {2} ({3}ms)', filename.replace(folder, ''), minFilename.replace(folder, ''), str(round((end - start) * 1000)))
# And finally upload our minified file
ftp.upload(folder, minFilename)
{
"host": "example.com",
"port": 21,
"user": "admin",
"password": "pass",
"triggers": [
{
"on": "save",
"extensions": [".css", ".js"],
"execute": "minify.py"
},
{
"on": "delete",
"extensions": [".css", ".js"],
"execute": "delete-minified.py"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment