From c3bdf1e037bc54fa21a5223e2b46cd7d0182c8df Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Wed, 14 Mar 2012 17:50:49 -0400 Subject: [PATCH] adding new find() commands --- external/shelljs/README.md | 42 ++++++++++++++++++- external/shelljs/shell.js | 85 ++++++++++++++++++++++++++++++++++++++ make.js | 20 ++++++--- 3 files changed, 140 insertions(+), 7 deletions(-) diff --git a/external/shelljs/README.md b/external/shelljs/README.md index 01b99a5f8..82f53533b 100644 --- a/external/shelljs/README.md +++ b/external/shelljs/README.md @@ -1,8 +1,10 @@ # ShellJS - Unix shell commands for Node.js [![Build Status](https://secure.travis-ci.org/arturadib/shelljs.png)](http://travis-ci.org/arturadib/shelljs) +_This project is young and experimental. Use at your own risk._ + ShellJS is a **portable** (Windows included) implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your shell script's dependency on Unix while still keeping its familiar and powerful commands. -The project is both [unit-tested](http://travis-ci.org/arturadib/shelljs) and battle-tested at Mozilla's [pdf.js](http://github.com/mozilla/pdf.js). +The project is [unit-tested](http://travis-ci.org/arturadib/shelljs) and is being used at Mozilla's [pdf.js](http://github.com/mozilla/pdf.js). ### Example @@ -12,7 +14,7 @@ require('shelljs/global'); // Copy files to release dir mkdir('-p', 'out/Release'); -cp('-R', 'lib/*.js', 'out/Release'); +cp('-R', 'stuff/*', 'out/Release'); // Replace macros in each .js file cd('lib'); @@ -130,6 +132,27 @@ Returns list of files in the given path, or in current directory if no path prov For convenient iteration via `for (file in ls())`, the format returned is a hash object: `{ 'file1':null, 'dir1/file2':null, ...}`. +#### find(path [,path ...]) +#### find(path_array) +Examples: + +```javascript +find('src', 'lib'); +find(['src', 'lib']); // same as above +for (file in find('.')) { +if (!file.match(/\.js$/)) +continue; +// all files at this point end in '.js' +} +``` + +Returns list of all files (however deep) in the given paths. For convenient iteration +via `for (file in find(...))`, the format returned is a hash object: +`{ 'file1':null, 'dir1/file2':null, ...}`. + +The main difference with respect to `ls('-R', path)` is that the resulting file names +include the base directories, e.g. `lib/resources/file1` instead of just `file1`. + #### cp('[options ,] source [,source ...], dest') #### cp('[options ,] source_array, dest') Available options: @@ -195,6 +218,21 @@ mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above Creates directories. +#### test(expression) +Available expression primaries: + ++ `'-d', 'path'`: true if path is a directory ++ `'-f', 'path'`: true if path is a regular file + +Examples: + +```javascript +if (test('-d', path)) { /* do something with dir */ }; +if (!test('-f', path)) continue; // skip if it's a regular file +``` + +Evaluates expression using the available primaries and returns corresponding value. + #### cat(file [, file ...]) #### cat(file_array) diff --git a/external/shelljs/shell.js b/external/shelljs/shell.js index 2d9a5a787..95ddaa067 100644 --- a/external/shelljs/shell.js +++ b/external/shelljs/shell.js @@ -57,6 +57,7 @@ function _pwd(options) { }; exports.pwd = wrap('pwd', _pwd); + //@ //@ #### ls([options ,] path [,path ...]) //@ #### ls([options ,] path_array) @@ -159,6 +160,54 @@ function _ls(options, paths) { exports.ls = wrap('ls', _ls); +//@ +//@ #### find(path [,path ...]) +//@ #### find(path_array) +//@ Examples: +//@ +//@ ```javascript +//@ find('src', 'lib'); +//@ find(['src', 'lib']); // same as above +//@ for (file in find('.')) { +//@ if (!file.match(/\.js$/)) +//@ continue; +//@ // all files at this point end in '.js' +//@ } +//@ ``` +//@ +//@ Returns list of all files (however deep) in the given paths. For convenient iteration +//@ via `for (file in find(...))`, the format returned is a hash object: +//@ `{ 'file1':null, 'dir1/file2':null, ...}`. +//@ +//@ The main difference from `ls('-R', path)` is that the resulting file names +//@ include the base directories, e.g. `lib/resources/file1` instead of just `file1`. +function _find(options, paths) { + if (!paths) + error('no path specified'); + else if (typeof paths === 'object') + paths = paths; // assume array + else if (typeof paths === 'string') + paths = [].slice.call(arguments, 1); + + var hash = {}; + + // why not simply do ls('-R', paths)? because the output wouldn't give the base dirs + // to get the base dir in the output, we need instead ls('-R', 'dir/*') for every directory + + paths.forEach(function(file){ + hash[file] = null; + + if (fs.statSync(file).isDirectory()) { + for (subfile in _ls('-Ra', file+'/*')) + hash[subfile] = null; + } + }); + + return hash; +} +exports.find = wrap('find', _find); + + //@ //@ #### cp('[options ,] source [,source ...], dest') //@ #### cp('[options ,] source_array, dest') @@ -438,6 +487,42 @@ function _mkdir(options, dirs) { }; // mkdir exports.mkdir = wrap('mkdir', _mkdir); +//@ +//@ #### test(expression) +//@ Available expression primaries: +//@ +//@ + `'-d', 'path'`: true if path is a directory +//@ + `'-f', 'path'`: true if path is a regular file +//@ +//@ Examples: +//@ +//@ ```javascript +//@ if (test('-d', path)) { /* do something with dir */ }; +//@ if (!test('-f', path)) continue; // skip if it's a regular file +//@ ``` +//@ +//@ Evaluates expression using the available primaries and returns corresponding value. +function _test(options, path) { + if (!path) + error('no path given'); + + // hack - only works with unary primaries + options = parseOptions(options, { + 'd': 'directory', + 'f': 'file' + }); + if (!options.directory && !options.file) + error('could not interpret expression'); + + if (options.directory) + return fs.existsSync(path) && fs.statSync(path).isDirectory(); + + if (options.file) + return fs.existsSync(path) && fs.statSync(path).isFile(); +}; // test +exports.test = wrap('test', _test); + + //@ //@ #### cat(file [, file ...]) //@ #### cat(file_array) diff --git a/make.js b/make.js index 84568f778..cb951f49e 100755 --- a/make.js +++ b/make.js @@ -168,7 +168,8 @@ target.pagesrepo = function() { // var EXTENSION_WEB_FILES = - ['web/images', + ['web/debugger.js', + 'web/images', 'web/viewer.css', 'web/viewer.js', 'web/viewer.html', @@ -264,8 +265,11 @@ target.firefox = function() { // We don't need pdf.js anymore since its inlined rm('-Rf', FIREFOX_BUILD_CONTENT_DIR + BUILD_DIR); - // TODO remove '.DS_Store' and other hidden files - // `find $(FIREFOX_BUILD_DIR) -name ".*" -delete` + // Remove '.DS_Store' and other hidden files + for (file in find(FIREFOX_BUILD_DIR)) { + if (file.match(/^\./)) + rm('-f', file); + } // Update the build version number sed('-i', /PDFJSSCRIPT_VERSION/, EXTENSION_VERSION, FIREFOX_BUILD_DIR + '/install.rdf'); @@ -285,8 +289,14 @@ target.firefox = function() { echo('AMO extension created: ' + FIREFOX_AMO_EXTENSION_NAME); cd(ROOT_DIR); - // TODO List all files for mozilla-central - // `@cd $(FIREFOX_BUILD_DIR); find $(FIREFOX_EXTENSION_FILES) -type f > extension-files` + // List all files for mozilla-central + cd(FIREFOX_BUILD_DIR); + var extensionFiles = ''; + for (file in find(FIREFOX_EXTENSION_FILES)) { + if (test('-f', file)) + extensionFiles += file+'\n'; + } + extensionFiles.to('extension-files'); }; //