File size: 3,913 Bytes
a75217d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | let fs = require('fs');
let path = require('path');
let execSync = require('child_process').execSync;
let exec = function (cmd) {
execSync(cmd, {stdio: 'inherit'});
};
/* global jake, task, desc, publishTask */
const BUILT_EJS_FILES = [
'ejs.js',
'ejs.min.js',
'lib/esm/ejs.js',
'lib/cjs/ejs.js',
];
// Hook into some of the publish lifecycle events
jake.on('finished', function (ev) {
switch (ev.name) {
case 'publish':
console.log('Updating hosted docs...');
console.log('If this fails, run jake docPublish to re-try.');
jake.Task.docPublish.invoke();
break;
default:
// Do nothing
}
});
desc('Builds the EJS library');
task('build', ['lint', 'clean', 'compile', 'browserify', 'minify']);
desc('Compiles ESM to CJS source files');
task('compile', function () {
// Compile CJS version
exec('npx tsc');
let source = fs.readFileSync('lib/cjs/ejs.js', 'utf8').toString();
// Browerify chokes on the 'node:' prefix in require statements
// Added the 'node:' prefix to ESM for Deno compat
['fs', 'path', 'url'].forEach((mod) => {
source = source.replace(`require("node:${mod}")`, `require("${mod}")`);
source = source.replace(new RegExp(`node_${mod}_1`, 'g'), `${mod}_1`);
});
// replace `let` in code-generation strings
source = source.replace(
"var DECLARATION_KEYWORD = 'let';",
"var DECLARATION_KEYWORD = 'var';"
);
fs.writeFileSync('lib/cjs/ejs.js', source);
fs.writeFileSync('lib/cjs/package.json', '{"type":"commonjs"}');
});
desc('Cleans browerified/minified files and package files');
task('clean', ['clobber'], function () {
jake.rmRf('./ejs.js');
jake.rmRf('./ejs.min.js');
jake.rmRf('./lib/cjs');
console.log('Cleaned up compiled files.');
});
desc('Lints the source code');
task('lint', ['clean'], function () {
let epath = path.join('./node_modules/.bin/eslint');
// Handle both ESM and CJS files in project
exec(epath+' --config ./eslint.config_esm.mjs "lib/esm/*.js"');
exec(epath+' --config ./eslint.config_cjs.mjs "test/*.js" "bin/cli.js" "jakefile.js"');
console.log('Linting completed.');
});
task('browserify', function () {
const currentDir = process.cwd();
process.chdir('./lib/cjs');
let epath = path.join('../../node_modules/browserify/bin/cmd.js');
exec(epath+' --standalone ejs ejs.js > ../../ejs.js');
process.chdir(currentDir);
console.log('Browserification completed.');
});
task('minify', function () {
let epath = path.join('./node_modules/uglify-js/bin/uglifyjs');
exec(epath+' ./ejs.js > ejs.min.js');
console.log('Minification completed.');
});
desc('Generates the EJS API docs for the public API');
task('doc', function () {
jake.rmRf('out');
let epath = path.join('./node_modules/.bin/jsdoc');
exec(epath+' --verbose -c jsdoc.json lib/esm/* docs/jsdoc/*');
console.log('Documentation generated in ./out.');
});
desc('Generates the EJS API docs for the public and private API');
task('devdoc', function () {
jake.rmRf('out');
let epath = path.join('./node_modules/.bin/jsdoc');
exec(epath+' --verbose -p -c jsdoc.json lib/esm/* docs/jsdoc/*');
console.log('Documentation generated in ./out.');
});
desc('Publishes the EJS API docs');
task('docPublish', ['doc'], function () {
fs.writeFileSync('out/CNAME', 'api.ejs.co');
console.log('Pushing docs to gh-pages...');
let epath = path.join('./node_modules/.bin/git-directory-deploy');
exec(epath+' --directory out/');
console.log('Docs published to gh-pages.');
});
desc('Runs the EJS test suite');
task('test', ['build', 'testOnly'], function () {});
task('testOnly', function () {
exec(path.join('./node_modules/.bin/mocha --u tdd'));
});
publishTask('ejs', ['build'], function () {
this.packageFiles.include([
'jakefile.js',
'README.md',
'LICENSE',
'package.json',
'ejs.js',
'ejs.min.js',
'lib/**',
'bin/**',
'usage.txt'
]);
});
|