Skip to content

Commit

Permalink
Updated code to be fully async, consistent indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Dec 18, 2012
1 parent 758b362 commit a34aab3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 64 deletions.
35 changes: 28 additions & 7 deletions doc/cli/backfill.md
Expand Up @@ -4,14 +4,35 @@ npm-backfill(1) -- Add installed modules to package.json
## SYNOPSIS

npm backfill
npm backfill <package-name> [ <package-name1> ... <package-nameN> ]
npm backfill --clean

## DESCRIPTION

If you've forgotten to do `npm i <package> --save` when installing packages, or you
are too lazy to look through your installed packages list, then this command will
run through each of your installed packages, extract the version and write them to
your packages.json file.
If you've installed a new package and forgotten to do `npm i --save <package-name>`
then this command will allow you to backfill packages into your packages.json file:

If you include 'insert' (e.g. npm backfill insert) then the existing packages list is
not clobbered. Any packages in the list that exist in your package.json file do have
their version updated however.
```
npm backfill <package-name>
```

You can also backfill several packages at once by doing:

```
npm backfill <package-name1> <package-name2> .... <package-nameN>
```

Additionally by not providing any arguments you can backfill all currently installed
packages:

```
npm backfill
```

Finally, if you've been installing and removing packages and wish to clean your package.json
dependency list you can backfill all currently installed packages clobbering those already
in your package.json as follows:

```
npm backfill --clean
```
95 changes: 39 additions & 56 deletions lib/backfill.js
Expand Up @@ -7,74 +7,57 @@
module.exports = backfill

backfill.usage = "npm backfill"
+ "\nnpm backfill "
+ "\nnpm backfill insert"
+ "\nIf no argument is supplied any existing [dev]Dependencies are clobbered"
+ "\nnpm backfill --clean"
+ "\nnpm backfill <package-name> [... <package-name2>]"
+ "\nnpm backfill --clean <package-name>"
+ "\nIf --clean flag is supplied any existing [dev]Dependencies are clobbered"

backfill.completion = function (opts, cb) { }

var path = require("path")
, fs = require("graceful-fs")
, npm = require("./npm.js")
, semver = require("semver");
, semver = require("semver")
, async = require('async')
, readJson = require("read-package-json");

function backfill(args, silent, cb) {

if (typeof cb !== "function") cb = silent, silent = false;

overwrite = true;

args.forEach(function(arg) {
if (arg == 'insert') {
overwrite = false;
}
});

if (typeof cb !== "function") cb = silent, silent = false;
jsonFile = path.resolve('.') + '/package.json';
if (!fs.existsSync(jsonFile)) cb("No package.json, create manually or run `npm init`", null);
packageJson = JSON.parse(fs.readFileSync(jsonFile, 'utf8'));
if (!packageJson) cb("Error parsing package.json");

getPackageList(writeDependencies, cb);
readJson(jsonFile, function(error, config) {
if (error) cb(error, null);
packageConfig = config;
getPackageList(args, function(error, list) {
if (npm.config.get('clean')) {
packageConfig.dependencies = {};
packageConfig.devDependencies = {};
}
async.map(list, updateDependencies, function(error, results) {
if (error) return cb(error);
fs.writeFile(jsonFile, JSON.stringify(packageConfig, null, 2), function(error) {
if (error) return cb(error, null);
cb(null, null);
});
});
});
});
}

writeDependencies = function(packageList, cb) {
var dependencies = {};
var devDependencies = {};
if (overwrite == true) {
packageJson.dependencies = {};
packageJson.devDependencies = {};
}
if (!packageJson.dependencies) {
packageJson.dependencies = {};
}
if (!packageJson.devDependencies) {
packageJson.devDependencies = {};
}
for (packageName in packageList) {
var version = packageList[packageName];
packageJson.dependencies[packageName] = version;
}
fs.writeFile(jsonFile, JSON.stringify(packageJson, null, 2), function(err) {
if (err) return cb(err, null);
cb(null, null);
});
updateDependencies = function(package, callback) {
readJson(path.resolve('./node_modules/' + package, "package.json"), function(error, json) {
if (error) return callback(error);
packageConfig.dependencies[package] = json.version;
callback(null, true);
});
}

getPackageList = function(done, cb) {
var packageList = {};
var dir = path.resolve(npm.dir);
var files = fs.readdir(dir, function(err, files) {
files.forEach(function(package) {
packageJsonFile = dir + '/' + package + '/package.json';
if (!fs.existsSync(packageJsonFile)) {
console.log("No package.json in " + packageJsonFile);
} else {
data = fs.readFileSync(packageJsonFile, 'utf8');
info = JSON.parse(data);
packageList[info.name] = info.version;
}
});
done(packageList, cb);
});
getPackageList = function(args, callback) {
if (args.length > 0) {
callback(null, args);
return;
}
fs.readdir(path.resolve(npm.dir), function(error, files) {
callback(error, files);
});
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -68,7 +68,8 @@
"retry": "~0.6.0",
"once": "~1.1.1",
"npmconf": "0",
"opener": "~1.3.0"
"opener": "~1.3.0",
"async": "~0.1.22"
},
"bundleDependencies": [
"semver",
Expand Down

0 comments on commit a34aab3

Please sign in to comment.