Vivid-Casper/gulpfile.js

177 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-04-15 04:55:37 +00:00
const {series, watch, src, dest, parallel} = require('gulp');
const pump = require('pump');
const path = require('path');
const releaseUtils = require('@tryghost/release-utils');
const inquirer = require('inquirer');
2017-05-15 20:03:47 +00:00
// gulp plugins and utils
2019-03-14 16:07:40 +00:00
const livereload = require('gulp-livereload');
const postcss = require('gulp-postcss');
const zip = require('gulp-zip');
const concat = require('gulp-concat');
2019-03-14 16:07:40 +00:00
const uglify = require('gulp-uglify');
const beeper = require('beeper');
const fs = require('fs');
2017-05-15 20:03:47 +00:00
// postcss plugins
2019-03-14 16:07:40 +00:00
const autoprefixer = require('autoprefixer');
const colorFunction = require('postcss-color-mod-function');
2019-03-14 16:07:40 +00:00
const cssnano = require('cssnano');
const easyimport = require('postcss-easy-import');
2017-05-15 20:03:47 +00:00
const REPO = 'TryGhost/Casper';
const REPO_READONLY = 'TryGhost/Casper';
const CHANGELOG_PATH = path.join(process.cwd(), '.', 'changelog.md');
function serve(done) {
livereload.listen();
done();
}
2017-05-15 20:03:47 +00:00
const handleError = (done) => {
return function (err) {
if (err) {
beeper();
}
return done(err);
};
};
2019-04-15 04:55:37 +00:00
function hbs(done) {
pump([
2019-08-21 05:51:43 +00:00
src(['*.hbs', 'partials/**/*.hbs']),
2019-04-15 04:55:37 +00:00
livereload()
], handleError(done));
}
function css(done) {
pump([
src('assets/css/*.css', {sourcemaps: true}),
postcss([
easyimport,
colorFunction(),
autoprefixer(),
cssnano()
]),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
}
function js(done) {
pump([
src([
// pull in lib files first so our own code can depend on it
'assets/js/lib/*.js',
'assets/js/*.js'
], {sourcemaps: true}),
concat('casper.js'),
uglify(),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
}
2017-05-15 20:03:47 +00:00
function zipper(done) {
const filename = require('./package.json').name + '.zip';
pump([
src([
'**',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!yarn-error.log',
'!yarn.lock',
'!gulpfile.js'
]),
zip(filename),
dest('dist/')
], handleError(done));
}
2019-04-15 04:55:37 +00:00
const cssWatcher = () => watch('assets/css/**', css);
2022-11-11 03:30:32 +00:00
const jsWatcher = () => watch('assets/js/**', js);
2019-08-21 05:51:43 +00:00
const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], hbs);
2022-11-11 03:30:32 +00:00
const watcher = parallel(cssWatcher, jsWatcher, hbsWatcher);
const build = series(css, js);
exports.build = build;
exports.zip = series(build, zipper);
exports.default = series(build, serve, watcher);
exports.release = async () => {
// @NOTE: https://yarnpkg.com/lang/en/docs/cli/version/
// require(./package.json) can run into caching issues, this re-reads from file everytime on release
let packageJSON = JSON.parse(fs.readFileSync('./package.json'));
const newVersion = packageJSON.version;
if (!newVersion || newVersion === '') {
console.log(`Invalid version: ${newVersion}`);
return;
}
console.log(`\nCreating release for ${newVersion}...`);
const githubToken = process.env.GST_TOKEN;
if (!githubToken) {
console.log('Please configure your environment with a GitHub token located in GST_TOKEN');
return;
}
try {
const result = await inquirer.prompt([{
type: 'input',
name: 'compatibleWithGhost',
message: 'Which version of Ghost is it compatible with?',
default: '5.0.0'
}]);
const compatibleWithGhost = result.compatibleWithGhost;
const releasesResponse = await releaseUtils.releases.get({
userAgent: 'Casper',
uri: `https://api.github.com/repos/${REPO_READONLY}/releases`
});
if (!releasesResponse || !releasesResponse) {
2020-04-03 15:16:26 +00:00
console.log('No releases found. Skipping...');
return;
}
let previousVersion = releasesResponse[0].tag_name || releasesResponse[0].name;
2020-04-03 15:16:26 +00:00
console.log(`Previous version: ${previousVersion}`);
2020-04-03 15:16:26 +00:00
const changelog = new releaseUtils.Changelog({
changelogPath: CHANGELOG_PATH,
folder: path.join(process.cwd(), '.')
});
2020-04-03 15:16:26 +00:00
changelog
.write({
githubRepoPath: `https://github.com/${REPO}`,
lastVersion: previousVersion
})
.sort()
.clean();
const newReleaseResponse = await releaseUtils.releases.create({
draft: true,
preRelease: false,
tagName: 'v' + newVersion,
releaseName: newVersion,
userAgent: 'Casper',
uri: `https://api.github.com/repos/${REPO}/releases`,
github: {
token: githubToken
},
content: [`**Compatible with Ghost ≥ ${compatibleWithGhost}**\n\n`],
changelogPath: CHANGELOG_PATH
});
console.log(`\nRelease draft generated: ${newReleaseResponse.releaseUrl}\n`);
} catch (err) {
2020-04-03 15:16:26 +00:00
console.error(err);
process.exit(1);
}
};