Skip to content

Commit

Permalink
Move more work into initialise script.
Browse files Browse the repository at this point in the history
  • Loading branch information
lifebeyondfife committed Apr 9, 2016
1 parent 3adadb4 commit ba706c5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# npm rescue

This is an npm-rescue git repository. Each branch in this repo contains a backup of a `node_modules` directory and its corresponding `package.json` file for a given npm project.

See [npm-rescue](http://github.com/lifebeyondfife/npm-rescue) for project home.
29 changes: 19 additions & 10 deletions initialise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ const findNpmPackages = require('./src/findNpmPackages');
const getRepoNames = require('./src/getRepoNames');
const userPrompt = require('./src/userPrompt');
const createRepo = require('./src/createRepo');
const headCommit = require('./src/headCommit');

const initialise = userPrompt().then(directories => {
const searchPath = directories[0];
const gitInitPath = directories[1];
const initialise = userPrompt().then(userProperties => {
const searchPath = userProperties[0];
const gitInitPath = userProperties[1];
const gitUsername = userProperties[2];
const gitEmailAddress = userProperties[3];

Promise.all([findNpmPackages(searchPath).then(packages => {
if (!packages.length) {
Expand All @@ -19,14 +22,20 @@ const initialise = userPrompt().then(directories => {

return getRepoNames(packages);
}),
createRepo(gitInitPath)
createRepo(gitInitPath).then(git => {
return headCommit(git.repo, git.gitDirectory, gitUsername, gitEmailAddress);
})
]).then(values => {
const config = JSON.stringify(
Object.assign({
npmPackages: values[0]
}, {
gitDirectory: path.resolve(values[1])
}),
console.log('Val-you: ' + JSON.stringify(values[1]));
const config = JSON.stringify({
npmPackages: values[0],
gitDirectory: path.resolve(values[1].gitDirectory),
headCommitOid: values[1].headCommitOid,
gitSignature: {
user: gitUsername,
email: gitEmailAddress
}
},
null, 4
);

Expand Down
9 changes: 9 additions & 0 deletions rescue.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const git = require('nodegit');
const path = require('path');
const loadConfig = require('./src/loadConfig');


/* Bit of a mess here now. The nodegit API doesn't allow you to do steps as easily as using git CLI.
- when creating the git repo, make an initial commit.
- This is the OID that all other branches will come from.
- ask for a name and email
put oid, name, email in the config too
*/

const repoConfig = loadConfig.then(config => {
return git.Repository.open(config.gitDirectory).then(repository => {
return {repo: repository, config};
Expand Down
6 changes: 3 additions & 3 deletions src/createRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const gitDirectory = directory => {
};

const createRepo = (gitInitPath) => {
return gitDirectory(gitInitPath).then(path => {
return git.Repository.init(path, 0).then(repository => {
return gitDirectory(gitInitPath).then(gitDirectory => {
return git.Repository.init(gitDirectory, 0).then(repo => {
console.log('Initialised git repository');

return path;
return {gitDirectory, repo};
});
}).catch(error => {
console.log(error.message);
Expand Down
22 changes: 22 additions & 0 deletions src/headCommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const fs = require('fs-extra');
const git = require('nodegit');
const path = require('path');

const headCommit = (repo, gitDirectory, gitUsername, gitEmailAddress) => {
return new Promise((resolve, reject) => {
const readme = path.resolve(gitDirectory, 'README.md');

fs.copySync(path.resolve('./docs', 'README.md'), readme);

const signature = git.Signature.now(gitUsername, gitEmailAddress);

repo.createCommitOnHead([readme], signature, signature, 'Initial npm-rescue commit.').then(oid => {
return resolve({headCommitOid: oid.tostrS(), gitDirectory});
}).catch(error => {
return reject(error);
});
});
};

module.exports = headCommit;
17 changes: 13 additions & 4 deletions src/userPrompt.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
const prompt = require('prompt');

let homeDirectory = process.env[
const homeDirectory = process.env[
(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'
];
let gitUserName = process.env['USER'];
let gitEmailAddress = 'npm@rescue.fake';

const userPrompt = () => {
return new Promise((resolve, reject) => {
Expand All @@ -17,8 +19,13 @@ const userPrompt = () => {
},
gitInitPath: {
description: `Enter where the npm-rescue repository should be created (${homeDirectory})`
}
}
},
gitUserName: {
description: `Enter name for npm-rescue git commits (${gitUserName})`
},
gitEmailAddress: {
description: `Enter email address for npm-rescue git commits (${gitEmailAddress})`
} }
}, (error, result) => {
if (error) {
reject(Error(error));
Expand All @@ -29,9 +36,11 @@ const userPrompt = () => {
}).then(userInput => {
const searchPath = userInput.searchPath || homeDirectory;
const gitInitPath = userInput.gitInitPath || homeDirectory;
gitUserName = userInput.gitUserName || gitUserName;
gitEmailAddress = userInput.gitEmailAddress || gitEmailAddress;
prompt.stop();

return [searchPath, gitInitPath];
return [searchPath, gitInitPath, gitUserName, gitEmailAddress];
}, error => {
console.log(error.message);
process.exit(1);
Expand Down

0 comments on commit ba706c5

Please sign in to comment.