Skip to content

Commit

Permalink
feat: allow hljs to be run from command line
Browse files Browse the repository at this point in the history
This allows the ability for hljs's build script to be run from
the command line.

This is useful for sofware that requires hljs as part of a build
chain.

It disables generation of docs and demo by default.

The patch adds the following options to "tools/build.js":

 * `--output`: path to write build files to. Defaults to "build".
 * `--docs`: tells builder to include the docs and demo.

Build just for Web, don't include docs:

```Bash
hljs --output "some/path xml" javascript
```

Build for Web, include docs:

```Bash
hljs --docs --output "some/path" xml javascript
```

Build for NodeJS:

```Bash
hljs -t node --output "some/path" xml javascript
```

Build for everything:

```Bash
hljs -t all --output "some/path" xml javascript
```
  • Loading branch information
marcoscaceres authored and Marcos Cáceres committed Jan 24, 2019
1 parent 5b5e907 commit db1924f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
16 changes: 8 additions & 8 deletions tools/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ let path = require('path');
let registry = require('./tasks');
let utility = require('./utility');

let directory;
let directory = {};

function templateAllFunc(blobs) {
const name = path.join('demo', 'index.html');
const name = path.join(directory.root, 'demo/index.html');

blobs = _.compact(blobs);

Expand Down Expand Up @@ -69,29 +69,29 @@ function generateDemo(filterCB, readArgs) {
writeImages: { requires:'readImages', task: ['dest', destArgs] },
readDemoJS: {
requires: 'logStart',
task: ['read', path.join('demo', 'demo.js')]
task: ['read', path.join(directory.root, 'demo/demo.js')]
},
minifyDemoJS: { requires: 'readDemoJS', task: 'jsminify' },
writeDemoJS: { requires: 'minifyDemoJS', task: ['dest', demoRoot] },
readDemoCSS: {
requires: 'logStart',
task: ['read', path.join('demo', 'style.css')]
task: ['read', path.join(directory.root, 'demo/style.css')]
},
minifyDemoCSS: { requires: 'readDemoCSS', task: 'cssminify' },
writeDemoCSS: { requires: 'minifyDemoCSS', task: ['dest', demoRoot] }
};
}

module.exports = function(commander, dir) {
directory = dir;
Object.assign(directory, dir);

let hljsExt, output, requiresTask, tasks,
replace = utility.replace,
regex = utility.regex,
replaceClassNames = utility.replaceClassNames,

coreFile = path.join('src', 'highlight.js'),
languages = utility.glob(path.join('src', 'languages', '*.js')),
coreFile = path.join(directory.root, 'src/highlight.js'),
languages = utility.glob(path.join(directory.root, 'src/languages', '*.js')),
filterCB = utility.buildFilterCallback(commander.args),
replaceArgs = replace(regex.header, ''),
templateArgs =
Expand Down Expand Up @@ -152,7 +152,7 @@ module.exports = function(commander, dir) {
task: ['write', output]
};

tasks = (commander.target === 'browser')
tasks = (commander.target === 'browser' && commander.docs)
? [copyDocs(), generateDemo(filterCB, languages), tasks]
: [tasks];

Expand Down
26 changes: 16 additions & 10 deletions tools/build.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
// For the basic introductions on using this build script, see:
//
// <https://highlightjs.readthedocs.org/en/latest/building-testing.html>
Expand All @@ -12,9 +13,11 @@
//
// The default target. This will package up the core `highlight.js` along
// with all the language definitions into the file `highlight.pack.js` --
// which will be compressed without including the option to disable it. It
// also builds the documentation for our readthedocs page, mentioned
// above, along with a local instance of the demo at:
// which will be compressed without including the option to disable it.
//
// If the --docs options is set, it also builds the documentation for our
// readthedocs page, mentioned above, along with a local instance of the demo
// at:
//
// <https://highlightjs.org/static/demo/>.
//
Expand Down Expand Up @@ -65,25 +68,28 @@ let path = require('path');
let Queue = require('gear').Queue;
let registry = require('./tasks');

let build, dir = {};
const defaultOutPath = path.resolve(__dirname, '../build');

commander
.usage('[options] [<language>...]')
.option('-d, --docs', 'Include documentation and demo (when target "browser")')
.option('-n, --no-compress', 'Disable compression')
.option('-o, --output <path>', 'directory to output to', defaultOutPath)
.option('-t, --target <name>', 'Build for target ' +
'[all, browser, cdn, node]',
/^(browser|cdn|node|all)$/i, 'browser')
.parse(process.argv);

commander.target = commander.target.toLowerCase();

build = require(`./${commander.target}`);
dir.root = path.dirname(__dirname);
dir.build = path.join(dir.root, 'build');
const build = require(`./${commander.target}`);
const dir = {
build: path.resolve(process.cwd(), commander.output),
root: path.resolve(__dirname, "../"),
};

new Queue({ registry: registry })
.clean(dir.build)
.log('Starting build.')
.log(`⚒ Starting build for "${commander.target}."`)
.series(build(commander, dir))
.log('Finished build.')
.log('❤️ Finished build.')
.run();
2 changes: 1 addition & 1 deletion tools/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function copyMetaFiles() {
glob = `{README.md,LICENSE,${docs}}`,

input = utility.glob(path.join(directory.root, glob)),
output = { dir: directory.build, base: '.' };
output = { dir: directory.build, base: directory.build };

return {
startLog: { task: ['log', 'Copying meta files.'] },
Expand Down

0 comments on commit db1924f

Please sign in to comment.