Skip to content

Commit

Permalink
feat: private repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
pruizlezcano committed Oct 25, 2020
1 parent 2396ed4 commit d6bfd17
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Compare local package.json and remote package.json and if versions don't match,
- `ignore: ['.git',...]` - An array of files for not copiyng when updating.
- `testing: false` - If true, copy update inside ./testing folder.
- `dev: false` - If true, ignore the update.
- `token: 123456...` - If you are using a private repository you should use a personal access token for downloading it

This values are the default values

Expand All @@ -39,7 +40,7 @@ This values are the default values
# Example

```javascript
const AutoUpdate = require('');
const AutoUpdate = require('gh-autoupdater');

const update = new AutoUpdate({
repo: 'https://github.com/user/repo',
Expand Down
35 changes: 26 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AutoUpdate extends EventEmitter {
* @param {Array[String]} ignore - An array of files to not install when updating.
* @param {Boolean} testing - If true, copy update inside ./testing folder
* @param {Boolean} dev - If true, ignore update
* @param {String} token - If you are using a private repository you should use a personal access token for downloading it
*/
if (updateConfig.repo === undefined)
throw new Error('You must incluede a repository link');
Expand All @@ -26,20 +27,16 @@ class AutoUpdate extends EventEmitter {
if (updateConfig.ignore === undefined) updateConfig.ignore = ['.git'];
else updateConfig.ignore.push('.git');
if (updateConfig.testing === undefined) updateConfig.testing = false;
const gitFolder = fs.readdirSync('./').some((file) => file === '.git');
if (updateConfig.dev === undefined && !gitFolder) updateConfig.dev = false;
if (gitFolder) {
updateConfig.dev = true;
}
if (updateConfig.command === undefined) updateConfig.index = false;
if (updateConfig.dev === undefined) updateConfig.dev = false;
if (updateConfig.token === undefined) updateConfig.token = false;
super(
updateConfig.repo,
updateConfig.branch,
updateConfig.temp,
updateConfig.ignore,
updateConfig.testing,
updateConfig.dev,
updateConfig.command
updateConfig.token
);
this.repo = updateConfig.repo;
this.branch = updateConfig.branch;
Expand All @@ -48,6 +45,7 @@ class AutoUpdate extends EventEmitter {
this.testing = updateConfig.testing;
this.dev = updateConfig.dev;
this.command = updateConfig.command;
this.token = updateConfig.token;
}

/**
Expand All @@ -70,7 +68,18 @@ class AutoUpdate extends EventEmitter {
.split('/')
.slice(3)
.join('/')}/${this.branch}/package.json`;
const { data } = await axios.get(pkg);
let data = undefined;
if (this.token) {
const res = await axios.get(pkg, {
headers: {
Authorization: `token ${this.token}`,
},
});
data = res.data;
} else {
const res = await axios.get(pkg);
data = res.data;
}
return { version: data.version, modules: data.dependencies };
}

Expand Down Expand Up @@ -151,7 +160,15 @@ class AutoUpdate extends EventEmitter {
this.emit('download.start', this.repo);
await fs.ensureDir(this.temp);
await fs.emptyDir(this.temp);
await git.clone(this.repo, this.temp);
if (this.token) {
const split = this.repo.split('/');
const user = split[3];
const repo = split[4];
const url = `https://${user}:${this.token}@github.com/${user}/${repo}`;
await git.clone(url, this.temp);
} else {
await git.clone(this.repo, this.temp);
}
return this.emit('download.end', this.repo);
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"author": "Pablo Ruiz Lezcano",
"license": "MIT",
"bugs": {
"url": "https://github.com/pruizlezcano/autoupdater/issues"
"url": "https://github.com/pruizlezcano/gh-autoupdater/issues"
},
"homepage": "https://github.com/pruizlezcano/autoupdater",
"homepage": "https://github.com/pruizlezcano/gh-autoupdater",
"repository": {
"type": "git",
"url": "git+https://github.com/pruizlezcano/autoupdater.git"
"url": "git+https://github.com/pruizlezcano/gh-autoupdater.git"
},
"dependencies": {
"axios": "^0.20.0",
Expand Down

0 comments on commit d6bfd17

Please sign in to comment.