Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow hljs to be run from command line #1951

Merged
merged 6 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"url": "git://github.com/highlightjs/highlight.js.git"
},
"main": "./lib/index.js",
"bin": {
"hljs": "./tools/build.js"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing tools/build.js file on npm package. #1984

},
"scripts": {
"mocha": "mocha",
"test": "mocha --globals document test",
Expand All @@ -29,18 +32,20 @@
"engines": {
"node": "*"
},
"devDependencies": {
"bluebird": "^3.5.3",
"dependencies": {
"commander": "^2.19.0",
"bluebird": "^3.5.3",
"del": "^3.0.0",
"gear": "^0.9.7",
"gear-lib": "^0.9.2",
"glob": "^7.1.3",
"js-beautify": "^1.8.9",
"jsdom": "^9.2.1",
"lodash": "^4.17.11",
"mocha": "^5.2.0",
"should": "^13.2.3",
"tiny-worker": "^2.1.2"
},
"devDependencies": {
"mocha": "^5.2.0",
"should": "^13.2.3"
}
}
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