From c1fb5014c3c40687332eb14b00f08265aac5999f Mon Sep 17 00:00:00 2001 From: Matt Basta Date: Fri, 26 Jul 2013 15:47:05 -0700 Subject: [PATCH] Version 0.0.3 --- README.md | 31 ++++++++++++++++-- bin/commonplace | 87 ++++++++++++++++++++++++++++++++++++++++++++++++- lib/utils.js | 8 +++-- package.json | 6 ++-- 4 files changed, 124 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7ebd56c..9a4dbbe 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,14 @@ Commonplace is the place for reusable components for the Firefox Marketplace (mozilla/fireplace). -## Installation +## Getting Started -### Getting node/npm +### Install node/npm #### OS X +Use `boxen` to install a node environment, or use `homebrew`: + ```bash brew install node ``` @@ -17,3 +19,28 @@ And make sure that `/usr/local/share/npm/bin` is in your `$PATH`, à la: ```bash export PATH=/usr/local/share/npm/bin:$PATH ``` + +### Setting up your repo + +Create a new repository for your project. In it, create a basic `package.json` file. You can do this very easily by running `npm init`. + +Next, install commonplace by running `npm install commonplace --save`. + +### Creating the commonplace base template + +At this point, simply run `commonplace install`. Running this command will create a `src/` directory in your project containing the minimum files needed to run your code. Other directories will also be created for L10n and other functions. + + +## Updating Commonplace + +Once you've run an `npm update`, you'll be running the latest commonplace scripts, but your modules will be out of date. + +To update your commonplace installation, simply run `commonplace update` from the root of your project. Commonplace will update all of the shared modules in-place. + + +## Things that haven't been built yet + +* `commonplace update` +* `commonplace lint` +* L10n tools +* a bunch of other stuff diff --git a/bin/commonplace b/bin/commonplace index d185ee9..18f31b0 100755 --- a/bin/commonplace +++ b/bin/commonplace @@ -21,14 +21,99 @@ if (!argv.length) { process.exit(); } +var child_process = require('child_process'); +var fs = require('fs'); +var path = require('path'); + +var utils = require('../lib/utils'); + +function copy_dir(src, dest) { + // `dest` is expected to be the path to a directory. + // `src` is expected to be the path to a directory. + + if (!fs.existsSync(src)) { + console.error('Source directory "' + src + '" doesn\'t exist.'); + return; + } + function mkdirRecursive(dir) { + var parent = path.resolve(dir, '../'); + if (!fs.existsSync(parent)) { + mkdirRecursive(parent); + } + fs.mkdirSync(dir); + } + if (!fs.existsSync(dest)) { + mkdirRecursive(dest); + } + + if (src.substr(src.length - 1) !== '/') { + src += '/'; + } + + if (dest.substr(dest.length - 1) !== '/') { + dest += '/'; + } + + var files_copied = 0; + + utils.globSync(src, '*', function(err, files) { + files.forEach(function(file) { + var interim_path = file.substr(src.length); + var dest_file = dest + interim_path; + + var dir = path.dirname(dest_file); + if (!fs.existsSync(dir)) { + console.log(dir); + mkdirRecursive(dir); + } + files_copied++; + fs.readFile(file, function(err, data) { + if (err) {return;} + fs.writeFile(dest_file, data, function(err) { + if (err) { + console.warn('Error copying ' + interim_path, err); + } + }); + }); + }); + }); + + console.log('Copied ' + files_copied + ' files.'); + +} + + switch (argv[0]) { case 'install': + var commonplace_src = path.resolve(__dirname, '../src'); + var local_src = path.resolve(process.cwd(), 'src/'); + console.log('Installing Commonplace...'); + console.log('Source: ' + commonplace_src); + console.log('Destination: ' + local_src); + copy_dir(commonplace_src, local_src); + + console.log('Initializing distributable files...'); + utils.glob(local_src, '.dist', function(err, files) { + files.forEach(function(file) { + fs.readFile(file, function(err, data) { + fs.writeFile(file.substr(file.length - 5), data, function(err) { + if (err) { + console.warn('Error initializing ' + file, err); + } + }); + }); + }); + console.log('Done.'); + }); break; case 'update': + // git clone git://github.com/mozilla/commonplace.git /tmp/commonplace + break; + case 'help': + help(); break; default: console.error('Unrecognized command "' + argv[0] + '"'); help(); - process.exit(); } diff --git a/lib/utils.js b/lib/utils.js index cb00b52..516af28 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -19,6 +19,8 @@ module.exports.opts = function(opts, defaults) { module.exports.glob = function(path_, ext, done) { var results = []; + var wildcard = ext === '*'; + fs.readdir(path_, function(err, list) { if (err) return done(err); var pending = list.length; @@ -33,7 +35,7 @@ module.exports.glob = function(path_, ext, done) { }); } else { // If it's got the right extension, add it to the list. - if(file.substr(file.length - ext.length) == ext) + if(wildcard || file.substr(file.length - ext.length) == ext) results.push(path.normalize(file)); if (!--pending) done(null, results); } @@ -46,6 +48,8 @@ module.exports.globSync = function(path_, ext, done) { var results = []; var list = fs.readdirSync(path_); var pending = list.length; + var wildcard = ext === '*'; + if (!pending) return done(null, results); list.forEach(function(file) { file = path_ + '/' + file; @@ -57,7 +61,7 @@ module.exports.globSync = function(path_, ext, done) { }); } else { // If it's got the right extension, add it to the list. - if(file.substr(file.length - ext.length) == ext) + if(wildcard || file.substr(file.length - ext.length) == ext) results.push(path.normalize(file)); if (!--pending) done(null, results); } diff --git a/package.json b/package.json index 2b5aee6..75e18bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commonplace", - "version": "0.0.2", + "version": "0.0.3", "repository": { "url": "git://github.com/mozilla/commonplace.git", "type": "git" @@ -10,13 +10,13 @@ "npm": ">= 1.1.x" }, "dependencies": { - "bower": "0.10.x", "clean-css": "1.0.x", "nunjucks": "0.1.9", "stylus": "0.32.x", "uglify-js": "2.3.x" }, "bin": { - "damper": "./bin/damper" + "damper": "./bin/damper", + "commonplace": "./bin/commonplace" } }