2020-05-27 20:47:53 +03:00
|
|
|
const config = require('./core/shared/config');
|
2019-02-11 15:26:06 +03:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// Utility for outputting messages indicating that the admin is building, as it can take a while.
|
2019-02-11 15:26:06 +03:00
|
|
|
let hasBuiltClient = false;
|
|
|
|
const logBuildingClient = function (grunt) {
|
|
|
|
if (!hasBuiltClient) {
|
|
|
|
grunt.log.writeln('Building admin client... (can take ~1min)');
|
|
|
|
setTimeout(logBuildingClient, 5000, grunt);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
module.exports = function (grunt) {
|
|
|
|
// grunt dev - use yarn dev instead!
|
|
|
|
// - Start a server & build assets on the fly whilst developing
|
|
|
|
grunt.registerTask('dev', 'Dev Mode; watch files and restart server on changes', function () {
|
|
|
|
if (grunt.option('client')) {
|
|
|
|
grunt.task.run(['clean:built', 'bgShell:client']);
|
|
|
|
} else if (grunt.option('server')) {
|
|
|
|
grunt.task.run(['express:dev', 'watch']);
|
|
|
|
} else {
|
|
|
|
grunt.task.run(['clean:built', 'bgShell:client', 'express:dev', 'watch']);
|
|
|
|
}
|
|
|
|
});
|
2019-02-11 15:26:06 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt build -- use yarn build instead!
|
|
|
|
// - Builds the client without a watch task
|
|
|
|
grunt.registerTask('build', 'Build client app in development mode',
|
|
|
|
['subgrunt:init', 'clean:tmp', 'ember']);
|
2020-08-07 21:19:25 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// Helpers for common deprecated tasks
|
|
|
|
grunt.registerTask('main', function () {
|
|
|
|
grunt.log.error('@deprecated: Run `yarn main` instead');
|
|
|
|
});
|
|
|
|
|
|
|
|
grunt.registerTask('validate', function () {
|
|
|
|
grunt.log.error('@deprecated: Run `yarn test` instead');
|
|
|
|
});
|
|
|
|
|
|
|
|
// --- Sub Commands
|
|
|
|
// Used to make other commands work
|
|
|
|
|
|
|
|
// Updates submodules, then installs and builds the client for you
|
|
|
|
grunt.registerTask('init', 'Prepare the project for development',
|
|
|
|
['update_submodules:pinned', 'build']);
|
|
|
|
|
|
|
|
// Runs ember dev
|
|
|
|
grunt.registerTask('ember', 'Build JS & templates for development',
|
|
|
|
['subgrunt:dev']);
|
|
|
|
|
|
|
|
// Production asset build
|
|
|
|
grunt.registerTask('prod', 'Build JS & templates for production',
|
|
|
|
['subgrunt:prod', 'postcss:prod']);
|
2020-08-07 21:19:25 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// --- Configuration
|
2020-04-29 18:44:27 +03:00
|
|
|
const cfg = {
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-contrib-watch
|
2019-02-11 15:26:06 +03:00
|
|
|
// Watch files and livereload in the browser during development.
|
2021-07-05 22:02:22 +03:00
|
|
|
// See the grunt dev task for how this is used.
|
2019-02-11 15:26:06 +03:00
|
|
|
watch: grunt.option('no-server-watch') ? {files: []} : {
|
|
|
|
livereload: {
|
|
|
|
files: [
|
|
|
|
'content/themes/casper/assets/css/*.css',
|
|
|
|
'content/themes/casper/assets/js/*.js'
|
|
|
|
],
|
|
|
|
options: {
|
2019-04-01 13:59:48 +03:00
|
|
|
livereload: true,
|
|
|
|
interval: 500
|
2013-08-17 22:39:02 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
express: {
|
2019-02-11 15:26:06 +03:00
|
|
|
files: [
|
|
|
|
'core/server/**/*.js',
|
2020-04-25 22:53:58 +03:00
|
|
|
'core/shared/**/*.js',
|
2019-07-18 17:31:04 +03:00
|
|
|
'core/frontend/**/*.js',
|
2021-11-09 14:07:28 +03:00
|
|
|
'core/frontend/src/**/*.css',
|
2021-05-04 18:46:57 +03:00
|
|
|
'core/*.js',
|
2020-05-01 19:59:38 +03:00
|
|
|
'index.js',
|
2019-02-11 15:26:06 +03:00
|
|
|
'config.*.json',
|
|
|
|
'!config.testing.json'
|
|
|
|
],
|
|
|
|
tasks: ['express:dev'],
|
2013-08-17 22:39:02 +04:00
|
|
|
options: {
|
2019-02-11 15:26:06 +03:00
|
|
|
spawn: false,
|
2019-04-01 13:59:48 +03:00
|
|
|
livereload: true,
|
|
|
|
interval: 500
|
2013-08-17 22:39:02 +04:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-express-server
|
2019-02-11 15:26:06 +03:00
|
|
|
// Start a Ghost express server for use in development and testing
|
|
|
|
express: {
|
|
|
|
dev: {
|
2013-06-25 15:43:15 +04:00
|
|
|
options: {
|
2021-07-05 22:02:22 +03:00
|
|
|
script: 'index.js',
|
|
|
|
output: 'Ghost is running'
|
2015-04-18 15:20:03 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-bg-shell
|
|
|
|
// Tools for building the client
|
2019-02-11 15:26:06 +03:00
|
|
|
bgShell: {
|
|
|
|
client: {
|
|
|
|
cmd: function () {
|
|
|
|
logBuildingClient(grunt);
|
|
|
|
return 'grunt subgrunt:watch';
|
|
|
|
},
|
|
|
|
bg: grunt.option('client') ? false : true,
|
|
|
|
stdout: function (chunk) {
|
|
|
|
// hide certain output to prevent confusion when running alongside server
|
2020-04-29 18:44:27 +03:00
|
|
|
const filter = grunt.option('client') ? false : [
|
2019-02-11 15:26:06 +03:00
|
|
|
/> ghost-admin/,
|
|
|
|
/^Livereload/,
|
|
|
|
/^Serving on/
|
|
|
|
].some(function (regexp) {
|
|
|
|
return regexp.test(chunk);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!filter) {
|
|
|
|
grunt.log.write(chunk);
|
2014-05-27 18:37:44 +04:00
|
|
|
}
|
2015-12-10 17:46:58 +03:00
|
|
|
|
2019-04-04 11:47:54 +03:00
|
|
|
if (chunk.indexOf('Slowest Nodes') !== -1) {
|
2019-02-11 15:26:06 +03:00
|
|
|
hasBuiltClient = true;
|
2015-12-10 17:46:58 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
},
|
|
|
|
stderr: function (chunk) {
|
2020-01-20 12:42:50 +03:00
|
|
|
const skipFilter = grunt.option('client') ? false : [
|
|
|
|
/- building/
|
|
|
|
].some(function (regexp) {
|
|
|
|
return regexp.test(chunk);
|
|
|
|
});
|
|
|
|
|
|
|
|
const errorFilter = grunt.option('client') ? false : [
|
|
|
|
/^>>/
|
|
|
|
].some(function (regexp) {
|
|
|
|
return regexp.test(chunk);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!skipFilter) {
|
|
|
|
hasBuiltClient = errorFilter ? hasBuiltClient : true;
|
|
|
|
grunt.log.error(chunk);
|
|
|
|
}
|
2015-12-10 17:46:58 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
},
|
2016-05-31 16:40:20 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-shell
|
2019-02-11 15:26:06 +03:00
|
|
|
// Command line tools where it's easier to run a command directly than configure a grunt plugin
|
|
|
|
shell: {
|
2021-02-02 21:17:42 +03:00
|
|
|
main: {
|
2019-02-11 15:26:06 +03:00
|
|
|
command: function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const upstream = grunt.option('upstream') || process.env.GHOST_UPSTREAM || 'upstream';
|
2021-02-02 21:17:42 +03:00
|
|
|
grunt.log.writeln('Pulling down the latest main from ' + upstream);
|
2019-02-11 19:25:25 +03:00
|
|
|
return `
|
2021-01-14 15:28:55 +03:00
|
|
|
git submodule sync && \
|
2019-03-06 11:03:01 +03:00
|
|
|
git submodule update
|
|
|
|
|
|
|
|
if ! git diff --exit-code --quiet --ignore-submodules=untracked; then
|
2020-05-28 20:19:13 +03:00
|
|
|
echo "Working directory is not clean, do you have uncommitted changes? Please commit, stash or discard changes to continue."
|
2019-02-11 19:25:25 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-02 21:17:42 +03:00
|
|
|
git checkout main
|
2021-01-14 16:37:06 +03:00
|
|
|
|
|
|
|
if git config remote.${upstream}.url > /dev/null; then
|
2021-02-02 21:17:42 +03:00
|
|
|
git pull ${upstream} main
|
2021-01-14 16:37:06 +03:00
|
|
|
else
|
2021-02-02 21:17:42 +03:00
|
|
|
git pull origin main
|
2021-01-14 16:37:06 +03:00
|
|
|
fi
|
2021-01-14 15:28:55 +03:00
|
|
|
|
|
|
|
yarn && \
|
2019-02-11 19:25:25 +03:00
|
|
|
git submodule foreach "
|
2021-02-02 21:17:42 +03:00
|
|
|
git checkout main
|
2021-01-14 15:28:55 +03:00
|
|
|
|
2021-01-14 16:37:06 +03:00
|
|
|
if git config remote.${upstream}.url > /dev/null; then
|
2021-02-02 21:17:42 +03:00
|
|
|
git pull ${upstream} main
|
2021-01-14 16:37:06 +03:00
|
|
|
else
|
2021-02-02 21:17:42 +03:00
|
|
|
git pull origin main
|
2021-01-14 16:37:06 +03:00
|
|
|
fi
|
2019-02-11 19:25:25 +03:00
|
|
|
"
|
|
|
|
`;
|
2017-04-10 12:30:21 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-contrib-clean
|
2019-02-11 15:26:06 +03:00
|
|
|
// Clean up files as part of other tasks
|
|
|
|
clean: {
|
|
|
|
built: {
|
|
|
|
src: [
|
|
|
|
'core/built/**'
|
|
|
|
]
|
2017-04-10 12:30:21 +03:00
|
|
|
},
|
2019-02-11 15:26:06 +03:00
|
|
|
tmp: {
|
|
|
|
src: ['.tmp/**']
|
|
|
|
}
|
|
|
|
},
|
2017-04-10 12:30:21 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-update-submodules
|
2019-02-11 15:26:06 +03:00
|
|
|
// Grunt task to update git submodules
|
|
|
|
update_submodules: {
|
|
|
|
pinned: {
|
|
|
|
options: {
|
|
|
|
params: '--init'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-08-07 12:53:28 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// @lodder/grunt-postcss
|
|
|
|
// Generate processed, minified css files
|
2020-04-01 15:01:03 +03:00
|
|
|
postcss: {
|
2019-02-11 15:26:06 +03:00
|
|
|
prod: {
|
|
|
|
options: {
|
2020-04-01 15:01:03 +03:00
|
|
|
processors: [
|
|
|
|
require('cssnano')() // minify the result
|
|
|
|
]
|
2016-08-07 12:53:28 +03:00
|
|
|
},
|
2019-02-11 15:26:06 +03:00
|
|
|
files: {
|
2022-03-16 19:22:17 +03:00
|
|
|
'core/frontend/public/ghost.min.css': 'core/frontend/public/ghost.css'
|
2016-05-31 16:40:20 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-subgrunt
|
2019-02-11 15:26:06 +03:00
|
|
|
// Run grunt tasks in submodule Gruntfiles
|
|
|
|
subgrunt: {
|
|
|
|
options: {
|
|
|
|
npmInstall: false,
|
|
|
|
npmPath: 'yarn'
|
2017-04-13 10:26:48 +03:00
|
|
|
},
|
|
|
|
|
2019-02-11 15:26:06 +03:00
|
|
|
init: {
|
|
|
|
options: {
|
|
|
|
npmInstall: true
|
|
|
|
},
|
|
|
|
projects: {
|
2019-05-13 11:11:29 +03:00
|
|
|
'core/client': 'init'
|
2017-04-13 10:26:48 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
},
|
2013-05-13 23:18:20 +04:00
|
|
|
|
2019-02-11 15:26:06 +03:00
|
|
|
dev: {
|
|
|
|
'core/client': 'shell:ember:dev'
|
|
|
|
},
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2019-02-11 15:26:06 +03:00
|
|
|
prod: {
|
2019-05-13 11:11:29 +03:00
|
|
|
'core/client': 'shell:ember:prod'
|
2019-02-11 15:26:06 +03:00
|
|
|
},
|
2013-08-01 11:12:59 +04:00
|
|
|
|
2019-02-11 15:26:06 +03:00
|
|
|
watch: {
|
|
|
|
projects: {
|
2021-06-17 14:15:44 +03:00
|
|
|
'core/client': ['shell:ember:watch', '--live-reload-base-url="' + config.getSubdir() + '/ghost/"']
|
2015-05-28 18:16:09 +03:00
|
|
|
}
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-contrib-symlink
|
2019-02-11 15:26:06 +03:00
|
|
|
// Create symlink for git hooks
|
|
|
|
symlink: {
|
|
|
|
githooks: {
|
|
|
|
// Enable overwrite to delete symlinks before recreating them
|
|
|
|
overwrite: false,
|
|
|
|
// Enable force to overwrite symlinks outside the current working directory
|
|
|
|
force: false,
|
|
|
|
// Expand to all files in /hooks
|
|
|
|
expand: true,
|
|
|
|
cwd: '.github/hooks',
|
|
|
|
src: ['*'],
|
|
|
|
dest: '.git/hooks'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-05-28 18:16:09 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// --- Grunt Initialisation
|
2019-02-11 15:26:06 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// Load all grunt tasks
|
|
|
|
grunt.loadNpmTasks('@lodder/grunt-postcss');
|
|
|
|
grunt.loadNpmTasks('grunt-bg-shell');
|
|
|
|
grunt.loadNpmTasks('grunt-contrib-clean');
|
|
|
|
grunt.loadNpmTasks('grunt-contrib-compress');
|
|
|
|
grunt.loadNpmTasks('grunt-contrib-copy');
|
|
|
|
grunt.loadNpmTasks('grunt-contrib-symlink');
|
|
|
|
grunt.loadNpmTasks('grunt-contrib-watch');
|
|
|
|
grunt.loadNpmTasks('grunt-express-server');
|
|
|
|
grunt.loadNpmTasks('grunt-shell');
|
|
|
|
grunt.loadNpmTasks('grunt-subgrunt');
|
|
|
|
grunt.loadNpmTasks('grunt-update-submodules');
|
2019-02-11 15:26:06 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// This little bit of weirdness gives the express server chance to shutdown properly
|
|
|
|
const waitBeforeExit = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(0);
|
|
|
|
}, 1000);
|
|
|
|
};
|
2019-02-11 15:26:06 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
process.on('SIGINT', waitBeforeExit);
|
|
|
|
process.on('SIGTERM', waitBeforeExit);
|
2019-02-11 15:26:06 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// Load the configuration
|
|
|
|
grunt.initConfig(cfg);
|
2019-02-11 15:26:06 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// --- Release Tooling
|
2021-02-02 21:17:42 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt release
|
|
|
|
// - create a Ghost release zip file.
|
2019-02-11 15:26:06 +03:00
|
|
|
// Uses the files specified by `.npmignore` to know what should and should not be included.
|
|
|
|
// Runs the asset generation tasks for production and duplicates default-prod.html to default.html
|
|
|
|
grunt.registerTask('release',
|
|
|
|
'Release task - creates a final built zip\n' +
|
|
|
|
' - Do our standard build steps \n' +
|
|
|
|
' - Copy files to release-folder/#/#{version} directory\n' +
|
2021-07-05 22:02:22 +03:00
|
|
|
' - Clean out unnecessary files (.git*, etc)\n' +
|
2019-02-11 15:26:06 +03:00
|
|
|
' - Zip files in release-folder to dist-folder/#{version} directory',
|
|
|
|
function () {
|
2021-07-05 22:02:22 +03:00
|
|
|
const escapeChar = process.platform.match(/^win/) ? '^' : '\\';
|
|
|
|
const cwd = process.cwd().replace(/( |\(|\))/g, escapeChar + '$1');
|
|
|
|
const buildDirectory = path.resolve(cwd, '.build');
|
|
|
|
const distDirectory = path.resolve(cwd, '.dist');
|
|
|
|
|
|
|
|
// Common paths used by release
|
|
|
|
grunt.config.set('paths', {
|
|
|
|
build: buildDirectory,
|
|
|
|
releaseBuild: path.join(buildDirectory, 'release'),
|
|
|
|
dist: distDirectory,
|
|
|
|
releaseDist: path.join(distDirectory, 'release')
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load package.json so that we can create correctly versioned releases.
|
|
|
|
grunt.config.set('pkg', grunt.file.readJSON('package.json'));
|
|
|
|
|
|
|
|
// grunt-contrib-copy
|
2019-02-11 15:26:06 +03:00
|
|
|
grunt.config.set('copy.release', {
|
|
|
|
expand: true,
|
|
|
|
// A list of files and patterns to include when creating a release zip.
|
2021-07-05 22:02:22 +03:00
|
|
|
// This is read from the `.npmignore` file and all patterns are inverted as we want to define what to include
|
2019-02-11 15:26:06 +03:00
|
|
|
src: fs.readFileSync('.npmignore', 'utf8').split('\n').filter(Boolean).map(function (pattern) {
|
|
|
|
return pattern[0] === '!' ? pattern.substr(1) : '!' + pattern;
|
|
|
|
}),
|
|
|
|
dest: '<%= paths.releaseBuild %>/'
|
2014-04-16 13:45:49 +04:00
|
|
|
});
|
|
|
|
|
2019-02-11 15:26:06 +03:00
|
|
|
grunt.config.set('copy.admin_html', {
|
|
|
|
files: [{
|
|
|
|
cwd: '.',
|
|
|
|
src: 'core/server/web/admin/views/default-prod.html',
|
|
|
|
dest: 'core/server/web/admin/views/default.html'
|
|
|
|
}]
|
|
|
|
});
|
2020-03-31 18:19:16 +03:00
|
|
|
|
2021-07-05 22:02:22 +03:00
|
|
|
// grunt-contrib-compress
|
|
|
|
grunt.config.set('compress.release', {
|
|
|
|
options: {
|
|
|
|
archive: '<%= paths.releaseDist %>/Ghost-<%= pkg.version %>.zip'
|
|
|
|
},
|
|
|
|
expand: true,
|
|
|
|
cwd: '<%= paths.releaseBuild %>/',
|
|
|
|
src: ['**']
|
|
|
|
});
|
|
|
|
|
|
|
|
// grunt-contrib-clean
|
|
|
|
grunt.config.set('clean.release', {
|
|
|
|
src: ['<%= paths.releaseBuild %>/**']
|
|
|
|
});
|
|
|
|
|
2020-10-28 15:13:46 +03:00
|
|
|
if (!grunt.option('skip-update')) {
|
|
|
|
grunt.task
|
|
|
|
.run('update_submodules:pinned')
|
|
|
|
.run('subgrunt:init');
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:10:08 +03:00
|
|
|
grunt.task
|
|
|
|
.run('clean:built')
|
|
|
|
.run('clean:tmp')
|
|
|
|
.run('prod')
|
|
|
|
.run('clean:release')
|
|
|
|
.run('copy:admin_html')
|
|
|
|
.run('copy:release')
|
|
|
|
.run('compress:release');
|
2019-02-11 15:26:06 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|