banner: function () { cli.output('\n'); cli.output(' ___ ___ ___ ___ ___ ___ ___ ___ __. ___ \n'.gray.bold); cli.output(' |_ -| _| _| -_| -_| | _| . | . | -_| \n'.gray.bold); cli.output(' |___|___|_| |___|___|_|_|___|___|___|___| \n'.gray.bold); cli.output('\n'); cli.output(' by Outright Mental Inc.'.gray + '\n\n'); },
cli.main(function(args, options) { var app = {set: function(v) { }} , c = this , from = options.from , to = options.to , dry = options.dry ; if (!to || !from) { cli.error('"to" and "from" options are required.\n'); cli.output(cli.getUsage()); cli.exit(1); return ; } var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || false; if (!mongoUri) { cli.error('Please set MONGO_URI env.\n'); cli.output(cli.getUsage()); cli.exit(1); return; } mongo.connect(mongoUri); // あー config 読み込み&model読み込み周りを app.js から切り離さないといけないにゃぁ configModel = require('../lib/models/config')(app); async.series([ function (next) { configModel.loadAllConfig(function(err, doc) { return next(); }); }, function (next) { var config = app.set('config'); models = require('../lib/models')(app); models.Config = configModel; return next(); }, function (next) { var limit = 100000; c.spinner('Load revisions..'); models.Revision.find().limit(limit).exec(function(err, revs) { c.spinner('Load revisions.. done!\n', true); var count = Object.keys(revs).length , i = 0 , matched = 0 , matchedWords = 0 ; c.output('Found ' + count + ' revisions.\n'); c.output('Start replacing.\n'); async.each(revs, function(rev, cb) { var regexp = new RegExp(from, 'g'); c.progress(++i/count); var m = rev.body.match(regexp); if (!m) { return cb(); } matched++; matchedWords += m.length; if (dry) { return cb(); } else { rev.body = rev.body.replace(regexp, to); rev.save(function(err, s) { if (err) { c.error('Error on:' + rev.path); } else { } return cb(); }); } }, function(err) { if (dry) { cli.info(matchedWords + ' words in (' + matched + ' of ' + count + ') revisions will be replaced!'); } else { cli.ok(matchedWords + ' words in (' + matched + ' of ' + count + ') revisions replaced!'); } return next(); }); }); } , function (next) { cli.ok('Finished!'); mongo.disconnect(); return next(); } ]); });
cli.main(function(args, options) { function stringToRegExp(value) { // JSON does not support regexes, so, e.g., JSON.parse() will not create // a RegExp from the JSON value `[ "/matchString/" ]`, which is // technically just an array containing a string that begins and end with // a forward slash. To get a RegExp from a JSON string, it must be // constructed explicitly in JavaScript. // // The likelihood of actually wanting to match text that is enclosed in // forward slashes is probably quite rare, so if forward slashes were // included in an argument that requires a regex, the user most likely // thought they were part of the syntax for specifying a regex. // // In the unlikely case that forward slashes are indeed desired in the // search string, the user would need to enclose the expression in a // second set of slashes: // // --customAttrSrround "[\"//matchString//\"]" // if (value) { var stripSlashes = /^\/(.*)\/$/.exec(value); if (stripSlashes) { value = stripSlashes[1]; } return new RegExp(value); } } function parseJSONOption(value, options) { var opts = options || {}; if (value !== null) { var jsonArray; try { jsonArray = JSON.parse(value); if (opts.regexArray) { jsonArray = jsonArray.map(function (regexString) { return stringToRegExp(regexString); }); } } catch (e) { cli.fatal('Could not parse JSON value \'' + value + '\''); } if (jsonArray instanceof Array) { return jsonArray; } else { return [value]; } } } function runMinify(original, output) { var status = 0; var minified = null; try { minified = minify(original, minifyOptions); } catch (e) { status = 3; cli.error('Minification error'); process.stderr.write((e.stack || e).toString()); } if (minifyOptions.lint) { minifyOptions.lint.populate(); } if (minified !== null) { // Write the output try { if (output != null) { fs.writeFileSync(path.resolve(output), minified); } else { process.stdout.write(minified); } } catch (e) { status = 4; console.log(output); cli.error('Cannot write to output'); } } return status; } function createDirectory(path) { try { fs.mkdirSync(path); } catch (e) { if (e.code !== 'EEXIST') { cli.fatal('Can not create directory ' + path); return 3; } } return 0; } function processDirectory(inputDir, outputDir) { var fileList = fs.readdirSync(inputDir); var status = createDirectory(outputDir); for (var i = 0; status === 0 && i < fileList.length; i++) { var fileName = fileList[i]; var inputFilePath = inputDir + '/' + fileName; var outputFilePath = outputDir + '/' + fileName; var stat = fs.statSync(inputFilePath); if (stat.isDirectory()) { status = processDirectory(inputFilePath, outputFilePath); } else { var originalContent = fs.readFileSync(inputFilePath, 'utf8'); status = runMinify(originalContent, outputFilePath); } } return status; } if (options.version) { cli.output(appName + ' v' + appVersion); cli.exit(0); } if (options['config-file']) { var fileOptions; var fileOptionsPath = path.resolve(options['config-file']); try { fileOptions = fs.readFileSync(fileOptionsPath, { encoding: 'utf8' }); } catch (e) { cli.fatal('The specified config file doesn’t exist or is unreadable:\n' + fileOptionsPath); } try { fileOptions = JSON.parse(fileOptions); } catch (je) { try { fileOptions = require(fileOptionsPath); } catch (ne) { cli.fatal('Cannot read the specified config file. \nAs JSON: ' + je.message + '\nAs module: ' + ne.message); } } if (fileOptions && typeof fileOptions === 'object') { minifyOptions = fileOptions; } } mainOptionKeys.forEach(function(key) { var paramKey = changeCase.paramCase(key); var value = options[paramKey]; if (options[paramKey] !== null) { switch (mainOptions[key][1]) { case 'json': minifyOptions[key] = parseJSONOption(value); break; case 'json-regex': minifyOptions[key] = parseJSONOption(value, { regexArray: true }); break; case 'string-regex': minifyOptions[key] = stringToRegExp(value); break; case 'site-url': minifyOptions[key] = { site: value }; break; case true: minifyOptions[key] = value; break; default: minifyOptions[key] = true; } } }); if (minifyOptions.lint === true) { minifyOptions.lint = new HTMLLint(); } if (args.length) { input = args; } if (options['input-dir'] || options['output-dir']) { var inputDir = options['input-dir']; var outputDir = options['output-dir']; if (!inputDir) { cli.error('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.'); cli.exit(2); } try { fs.statSync(inputDir); } catch (e) { cli.error('The input directory does not exist'); cli.exit(2); } if (!outputDir) { cli.error('You need to specify where to write the output files with the option --output-dir'); cli.exit(2); } try { cli.exit(processDirectory(inputDir, outputDir)); } catch (e) { cli.error('Error while processing input files'); cli.error(e); cli.exit(3); } } if (options.output) { output = options.output; } if (input !== null) { // Minifying one or more files specified on the CMD line var original = ''; input.forEach(function(afile) { try { original += fs.readFileSync(afile, 'utf8'); } catch (e) { cli.error('Cannot read file ' + afile); cli.exit(2); } }); cli.exit(runMinify(original, output)); } else { // Minifying input coming from STDIN process.stdin.pipe(concat({ encoding: 'string' }, runMinify)); } });
done: function () { cli.output('\n' + '»'.gray + ' ' + 'Screencode'.bold + ' ' + 'OK'.green.bold + '\n\n'); }
fail: function (message) { cli.error(colors.bold(message)); cli.output('\n'); },
Tramp.translate(options, data, function(err, text) { CLI.output(text + '\n'); process(); });
cli.main(function(args, options) { function stringToRegExp(value) { // JSON does not support regexes, so, e.g., JSON.parse() will not create // a RegExp from the JSON value `[ "/matchString/" ]`, which is // technically just an array containing a string that begins and end with // a forward slash. To get a RegExp from a JSON string, it must be // constructed explicitly in JavaScript. // // The likelihood of actually wanting to match text that is enclosed in // forward slashes is probably quite rare, so if forward slashes were // included in an argument that requires a regex, the user most likely // thought they were part of the syntax for specifying a regex. // // In the unlikely case that forward slashes are indeed desired in the // search string, the user would need to enclose the expression in a // second set of slashes: // // --customAttrSrround "[\"//matchString//\"]" // if (value) { var stripSlashes = /^\/(.*)\/$/.exec(value); if (stripSlashes) { value = stripSlashes[1]; } return new RegExp(value); } } function parseJSONOption(value, options) { var opts = options || {}; if (value !== null) { var jsonArray; try { jsonArray = JSON.parse(value); if (opts.regexArray) { jsonArray = jsonArray.map(function (regexString) { return stringToRegExp(regexString); }); } } catch (e) { cli.fatal('Could not parse JSON value \'' + value + '\''); } if (jsonArray instanceof Array) { return jsonArray; } else { return [value]; } } } function runMinify(original) { var status = 0; var minified = null; try { minified = minify(original, minifyOptions); } catch (e) { status = 3; cli.error('Minification error'); process.stderr.write(e); } if (minifyOptions.lint) { minifyOptions.lint.populate(); } if (minified !== null) { // Write the output try { if (output !== null) { fs.writeFileSync(path.resolve(output), minified); } else { process.stdout.write(minified); } } catch (e) { status = 4; cli.error('Cannot write to output'); } } cli.exit(status); } if (options.version) { cli.output(appName + ' v' + appVersion); cli.exit(0); } if (options['config-file']) { try { var fileOptions = JSON.parse(fs.readFileSync(path.resolve(options['config-file']), 'utf8')); if ((fileOptions !== null) && (typeof fileOptions === 'object')) { minifyOptions = fileOptions; } } catch (e) { cli.fatal('Cannot read the specified config file'); } } mainOptionKeys.forEach(function(key) { var paramKey = changeCase.paramCase(key); var value = options[paramKey]; if (options[paramKey] !== null) { switch (mainOptions[key][1]) { case 'json': minifyOptions[key] = parseJSONOption(value); break; case 'json-regex': minifyOptions[key] = parseJSONOption(value, { regexArray: true }); break; case 'string-regex': minifyOptions[key] = stringToRegExp(value); break; case true: minifyOptions[key] = value; break; default: minifyOptions[key] = true; } } }); if (minifyOptions.lint === true) { minifyOptions.lint = new HTMLLint(); } if (args.length) { input = args; } if (options.output) { output = options.output; } if (input !== null) { // Minifying one or more files specified on the CMD line var original = ''; input.forEach(function(afile) { try { original += fs.readFileSync(afile, 'utf8'); } catch (e) { cli.error('Cannot read file ' + afile); cli.exit(2); } }); runMinify(original); } else { // Minifying input coming from STDIN process.stdin.pipe(concat({ encoding: 'string' }, runMinify)); } });