123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- import * as path from 'path';
- import fs from './less-node/fs';
- import * as os from 'os';
- import * as utils from './less/utils';
- import * as Constants from './less/constants';
- let errno;
- let mkdirp;
- try {
- errno = require('errno');
- } catch (err) {
- errno = null;
- }
- import less from './less-node';
- const pluginManager = new less.PluginManager(less);
- const fileManager = new less.FileManager();
- const plugins = [];
- const queuePlugins = [];
- let args = process.argv.slice(1);
- let silent = false;
- let verbose = false;
- const options = less.options;
- options.plugins = plugins;
- options.reUsePluginManager = true;
- const sourceMapOptions = {};
- let continueProcessing = true;
- const checkArgFunc = (arg, option) => {
- if (!option) {
- console.error(`${arg} option requires a parameter`);
- continueProcessing = false;
- process.exitCode = 1;
- return false;
- }
- return true;
- };
- const checkBooleanArg = arg => {
- const onOff = /^((on|t|true|y|yes)|(off|f|false|n|no))$/i.exec(arg);
- if (!onOff) {
- console.error(` unable to parse ${arg} as a boolean. use one of on/t/true/y/yes/off/f/false/n/no`);
- continueProcessing = false;
- process.exitCode = 1;
- return false;
- }
- return Boolean(onOff[2]);
- };
- const parseVariableOption = (option, variables) => {
- const parts = option.split('=', 2);
- variables[parts[0]] = parts[1];
- };
- let sourceMapFileInline = false;
- function printUsage() {
- less.lesscHelper.printUsage();
- pluginManager.Loader.printUsage(plugins);
- continueProcessing = false;
- }
- function render() {
- if (!continueProcessing) {
- return;
- }
- let input = args[1];
- if (input && input != '-') {
- input = path.resolve(process.cwd(), input);
- }
- let output = args[2];
- const outputbase = args[2];
- if (output) {
- output = path.resolve(process.cwd(), output);
- }
- if (options.sourceMap) {
- sourceMapOptions.sourceMapInputFilename = input;
- if (!sourceMapOptions.sourceMapFullFilename) {
- if (!output && !sourceMapFileInline) {
- console.error('the sourcemap option only has an optional filename if the css filename is given');
- console.error('consider adding --source-map-map-inline which embeds the sourcemap into the css');
- process.exitCode = 1;
- return;
- }
- // its in the same directory, so always just the basename
- if (output) {
- sourceMapOptions.sourceMapOutputFilename = path.basename(output);
- sourceMapOptions.sourceMapFullFilename = `${output}.map`;
- }
- // its in the same directory, so always just the basename
- if ('sourceMapFullFilename' in sourceMapOptions) {
- sourceMapOptions.sourceMapFilename = path.basename(sourceMapOptions.sourceMapFullFilename);
- }
- } else if (options.sourceMap && !sourceMapFileInline) {
- const mapFilename = path.resolve(process.cwd(), sourceMapOptions.sourceMapFullFilename);
- const mapDir = path.dirname(mapFilename);
- const outputDir = path.dirname(output);
- // find the path from the map to the output file
- sourceMapOptions.sourceMapOutputFilename = path.join(
- path.relative(mapDir, outputDir), path.basename(output));
- // make the sourcemap filename point to the sourcemap relative to the css file output directory
- sourceMapOptions.sourceMapFilename = path.join(
- path.relative(outputDir, mapDir), path.basename(sourceMapOptions.sourceMapFullFilename));
- }
- }
- if (sourceMapOptions.sourceMapBasepath === undefined) {
- sourceMapOptions.sourceMapBasepath = input ? path.dirname(input) : process.cwd();
- }
- if (sourceMapOptions.sourceMapRootpath === undefined) {
- const pathToMap = path.dirname((sourceMapFileInline ? output : sourceMapOptions.sourceMapFullFilename) || '.');
- const pathToInput = path.dirname(sourceMapOptions.sourceMapInputFilename || '.');
- sourceMapOptions.sourceMapRootpath = path.relative(pathToMap, pathToInput);
- }
- if (!input) {
- console.error('lessc: no input files');
- console.error('');
- printUsage();
- process.exitCode = 1;
- return;
- }
- const ensureDirectory = filepath => {
- const dir = path.dirname(filepath);
- let cmd;
- const existsSync = fs.existsSync || path.existsSync;
- if (!existsSync(dir)) {
- if (mkdirp === undefined) {
- try {mkdirp = require('mkdirp');}
- catch (e) { mkdirp = null; }
- }
- cmd = mkdirp && mkdirp.sync || fs.mkdirSync;
- cmd(dir);
- }
- };
- if (options.depends) {
- if (!outputbase) {
- console.error('option --depends requires an output path to be specified');
- process.exitCode = 1;
- return;
- }
- process.stdout.write(`${outputbase}: `);
- }
- if (!sourceMapFileInline) {
- var writeSourceMap = (output = '', onDone) => {
- const filename = sourceMapOptions.sourceMapFullFilename;
- ensureDirectory(filename);
- fs.writeFile(filename, output, 'utf8', err => {
- if (err) {
- let description = 'Error: ';
- if (errno && errno.errno[err.errno]) {
- description += errno.errno[err.errno].description;
- } else {
- description += `${err.code} ${err.message}`;
- }
- console.error(`lessc: failed to create file ${filename}`);
- console.error(description);
- process.exitCode = 1;
- } else {
- less.logger.info(`lessc: wrote ${filename}`);
- }
- onDone();
- });
- };
- }
- const writeSourceMapIfNeeded = (output, onDone) => {
- if (options.sourceMap && !sourceMapFileInline) {
- writeSourceMap(output, onDone);
- } else {
- onDone();
- }
- };
- const writeOutput = (output, result, onSuccess) => {
- if (options.depends) {
- onSuccess();
- } else if (output) {
- ensureDirectory(output);
- fs.writeFile(output, result.css, {encoding: 'utf8'}, err => {
- if (err) {
- let description = 'Error: ';
- if (errno && errno.errno[err.errno]) {
- description += errno.errno[err.errno].description;
- } else {
- description += `${err.code} ${err.message}`;
- }
- console.error(`lessc: failed to create file ${output}`);
- console.error(description);
- process.exitCode = 1;
- } else {
- less.logger.info(`lessc: wrote ${output}`);
- onSuccess();
- }
- });
- } else if (!options.depends) {
- process.stdout.write(result.css);
- onSuccess();
- }
- };
- const logDependencies = (options, result) => {
- if (options.depends) {
- let depends = '';
- for (let i = 0; i < result.imports.length; i++) {
- depends += `${result.imports[i]} `;
- }
- console.log(depends);
- }
- };
- const parseLessFile = (e, data) => {
- if (e) {
- console.error(`lessc: ${e.message}`);
- process.exitCode = 1;
- return;
- }
- data = data.replace(/^\uFEFF/, '');
- options.paths = [path.dirname(input)].concat(options.paths);
- options.filename = input;
- if (options.lint) {
- options.sourceMap = false;
- }
- sourceMapOptions.sourceMapFileInline = sourceMapFileInline;
- if (options.sourceMap) {
- options.sourceMap = sourceMapOptions;
- }
- less.logger.addListener({
- info: function(msg) {
- if (verbose) {
- console.log(msg);
- }
- },
- warn: function(msg) {
- // do not show warning if the silent option is used
- if (!silent) {
- console.warn(msg);
- }
- },
- error: function(msg) {
- console.error(msg);
- }
- });
- less.render(data, options)
- .then(result => {
- if (!options.lint) {
- writeOutput(output, result, () => {
- writeSourceMapIfNeeded(result.map, () => {
- logDependencies(options, result);
- });
- });
- }
- },
- err => {
- if (!options.silent) {
- console.error(err.toString({
- stylize: options.color && less.lesscHelper.stylize
- }));
- }
- process.exitCode = 1;
- });
- };
- if (input != '-') {
- fs.readFile(input, 'utf8', parseLessFile);
- } else {
- process.stdin.resume();
- process.stdin.setEncoding('utf8');
- let buffer = '';
- process.stdin.on('data', data => {
- buffer += data;
- });
- process.stdin.on('end', () => {
- parseLessFile(false, buffer);
- });
- }
- }
- function processPluginQueue() {
- let x = 0;
- function pluginError(name) {
- console.error(`Unable to load plugin ${name} please make sure that it is installed under or at the same level as less`);
- process.exitCode = 1;
- }
- function pluginFinished(plugin) {
- x++;
- plugins.push(plugin);
- if (x === queuePlugins.length) {
- render();
- }
- }
- queuePlugins.forEach(queue => {
- const context = utils.clone(options);
- pluginManager.Loader.loadPlugin(queue.name, process.cwd(), context, less.environment, fileManager)
- .then(data => {
- pluginFinished({
- fileContent: data.contents,
- filename: data.filename,
- options: queue.options
- });
- })
- .catch(() => {
- pluginError(queue.name);
- });
- });
- }
- // self executing function so we can return
- (() => {
- args = args.filter(arg => {
- let match;
- match = arg.match(/^-I(.+)$/);
- if (match) {
- options.paths.push(match[1]);
- return false;
- }
- match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=(.*))?$/i);
- if (match) {
- arg = match[1];
- } else {
- return arg;
- }
- switch (arg) {
- case 'v':
- case 'version':
- console.log(`lessc ${less.version.join('.')} (Less Compiler) [JavaScript]`);
- continueProcessing = false;
- break;
- case 'verbose':
- verbose = true;
- break;
- case 's':
- case 'silent':
- silent = true;
- break;
- case 'l':
- case 'lint':
- options.lint = true;
- break;
- case 'strict-imports':
- options.strictImports = true;
- break;
- case 'h':
- case 'help':
- printUsage();
- break;
- case 'x':
- case 'compress':
- options.compress = true;
- break;
- case 'insecure':
- options.insecure = true;
- break;
- case 'M':
- case 'depends':
- options.depends = true;
- break;
- case 'max-line-len':
- if (checkArgFunc(arg, match[2])) {
- options.maxLineLen = parseInt(match[2], 10);
- if (options.maxLineLen <= 0) {
- options.maxLineLen = -1;
- }
- }
- break;
- case 'no-color':
- options.color = false;
- break;
- case 'js':
- options.javascriptEnabled = true;
- break;
- case 'no-js':
- console.error('The "--no-js" argument is deprecated, as inline JavaScript ' +
- 'is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
- break;
- case 'include-path':
- if (checkArgFunc(arg, match[2])) {
- // ; supported on windows.
- // : supported on windows and linux, excluding a drive letter like C:\ so C:\file:D:\file parses to 2
- options.paths = match[2]
- .split(os.type().match(/Windows/) ? /:(?!\\)|;/ : ':')
- .map(p => {
- if (p) {
- return path.resolve(process.cwd(), p);
- }
- });
- }
- break;
- case 'line-numbers':
- if (checkArgFunc(arg, match[2])) {
- options.dumpLineNumbers = match[2];
- }
- break;
- case 'source-map':
- options.sourceMap = true;
- if (match[2]) {
- sourceMapOptions.sourceMapFullFilename = match[2];
- }
- break;
- case 'source-map-rootpath':
- if (checkArgFunc(arg, match[2])) {
- sourceMapOptions.sourceMapRootpath = match[2];
- }
- break;
- case 'source-map-basepath':
- if (checkArgFunc(arg, match[2])) {
- sourceMapOptions.sourceMapBasepath = match[2];
- }
- break;
- case 'source-map-inline':
- case 'source-map-map-inline':
- sourceMapFileInline = true;
- options.sourceMap = true;
- break;
- case 'source-map-include-source':
- case 'source-map-less-inline':
- sourceMapOptions.outputSourceFiles = true;
- break;
- case 'source-map-url':
- if (checkArgFunc(arg, match[2])) {
- sourceMapOptions.sourceMapURL = match[2];
- }
- break;
- case 'rp':
- case 'rootpath':
- if (checkArgFunc(arg, match[2])) {
- options.rootpath = match[2].replace(/\\/g, '/');
- }
- break;
- case 'relative-urls':
- console.warn('The --relative-urls option has been deprecated. Use --rewrite-urls=all.');
- options.rewriteUrls = Constants.RewriteUrls.ALL;
- break;
- case 'ru':
- case 'rewrite-urls':
- const m = match[2];
- if (m) {
- if (m === 'local') {
- options.rewriteUrls = Constants.RewriteUrls.LOCAL;
- } else if (m === 'off') {
- options.rewriteUrls = Constants.RewriteUrls.OFF;
- } else if (m === 'all') {
- options.rewriteUrls = Constants.RewriteUrls.ALL;
- } else {
- console.error(`Unknown rewrite-urls argument ${m}`);
- continueProcessing = false;
- process.exitCode = 1;
- }
- } else {
- options.rewriteUrls = Constants.RewriteUrls.ALL;
- }
- break;
- case 'sm':
- case 'strict-math':
- console.warn('The --strict-math option has been deprecated. Use --math=strict.');
- if (checkArgFunc(arg, match[2])) {
- if (checkBooleanArg(match[2])) {
- options.math = Constants.Math.STRICT_LEGACY;
- }
- }
- break;
- case 'm':
- case 'math':
- if (checkArgFunc(arg, match[2])) {
- options.math = match[2];
- }
- break;
- case 'su':
- case 'strict-units':
- if (checkArgFunc(arg, match[2])) {
- options.strictUnits = checkBooleanArg(match[2]);
- }
- break;
- case 'global-var':
- if (checkArgFunc(arg, match[2])) {
- if (!options.globalVars) {
- options.globalVars = {};
- }
- parseVariableOption(match[2], options.globalVars);
- }
- break;
- case 'modify-var':
- if (checkArgFunc(arg, match[2])) {
- if (!options.modifyVars) {
- options.modifyVars = {};
- }
- parseVariableOption(match[2], options.modifyVars);
- }
- break;
- case 'url-args':
- if (checkArgFunc(arg, match[2])) {
- options.urlArgs = match[2];
- }
- break;
- case 'plugin':
- const splitupArg = match[2].match(/^([^=]+)(=(.*))?/);
- const name = splitupArg[1];
- const pluginOptions = splitupArg[3];
- queuePlugins.push({ name, options: pluginOptions });
- break;
- default:
- queuePlugins.push({ name: arg, options: match[2], default: true });
- break;
- }
- });
- if (queuePlugins.length > 0) {
- processPluginQueue();
- }
- else {
- render();
- }
- })();
|