Comparing version 0.0.1 to 0.1.0
204
index.js
@@ -1,14 +0,28 @@ | ||
const readPkg = require('read-pkg') | ||
const execa = require('execa') | ||
const curry = require('lodash.curry') | ||
const readPkg = require('read-pkg') | ||
const tmp = require('tmp') | ||
const cpy = require('cpy') | ||
const del = require('del') | ||
const path = require('path') | ||
const pify = require('pify') | ||
const pMap = require('p-map') | ||
const pFilter = require('p-filter') | ||
const fs = pify(require('fs')) | ||
const mkdirp = pify(require('mkdirp')) | ||
// const Observable = require('zen-observable') | ||
const catchAndThrow = curry(function _catchAndThrow (ob, res) { | ||
debugger | ||
ob.error(res.stderr || res) | ||
}) | ||
function readConf (ctx, ob) { | ||
ob.next('Reading package') | ||
const package = readPkg.sync() | ||
const pkg = readPkg.sync() | ||
ob.next('Checking configuration') | ||
ctx.version = package.version | ||
ctx.version = pkg.version | ||
ctx.state = {} | ||
const conf = package.docarchive | ||
const conf = pkg.docarchive | ||
// TODO check properties | ||
@@ -22,4 +36,4 @@ Object.assign(ctx, conf) | ||
ob.next(`Fetching heads at ${ctx.remote}`) | ||
const a = execa('git', ['ls-remote', '--heads', ctx.remote]) | ||
execa('git', ['ls-remote', '--heads', ctx.remote]) | ||
execa('git', ['ls-remote', '--heads', ctx.remote]) | ||
.then(res => { | ||
@@ -32,10 +46,186 @@ ob.next(`Checking if branch '${ctx.branch}' exists`) | ||
}) | ||
.catch(catchAndThrow(ob)) | ||
} | ||
function fetchRemote (ctx, ob) { | ||
if (ctx.localRepo) { | ||
ob.next(`Using ${ctx.localRepo} to fetch the remote`) | ||
const name = ctx.localRepo | ||
ctx.localRepo = { | ||
name, | ||
removeCallback () { | ||
throw new Error('TODO') | ||
} | ||
} | ||
ob.next(`Checking if ${ctx.localRepo.name} is clean`) | ||
const projectDir = process.cwd() | ||
process.chdir(ctx.localRepo.name) | ||
// TODO create branch if it doesn't exist | ||
execa('git', ['checkout', ctx.branch]) | ||
.then(() => execa('git', ['pull'])) | ||
.then(() => { | ||
process.chdir(projectDir) | ||
ob.complete() | ||
}) | ||
.catch(res => { | ||
process.chdir(projectDir) | ||
catchAndThrow(ob, res) | ||
}) | ||
} else { | ||
ob.next('Creating tmp directory to fetch the remote') | ||
ctx.localRepo = tmp.dirSync({ unsafeCleanup: true }) | ||
ctx.localRepo.clean = true | ||
ob.next(`Cloning ${ctx.remote} to ${ctx.localRepo.name}`) | ||
const args = ctx.state.branchExists | ||
? ['clone', '-b', ctx.branch, ctx.remote, '--single-branch', '--depth', 1, ctx.localRepo.name] | ||
: ['clone', ctx.remote, '--depth', 1, ctx.localRepo.name] | ||
execa('git', args) | ||
.then(() => ob.complete()) | ||
.catch(catchAndThrow(ob)) | ||
} | ||
} | ||
function createBranch (ctx, ob) { | ||
const projectDir = process.cwd() | ||
process.chdir(ctx.localRepo.name) | ||
ob.next(`Creating an orphan branch ${ctx.branch}`) | ||
execa('git', ['checkout', '--orphan', ctx.branch]) | ||
.then(() => { | ||
return execa('git', ['reset', '--hard']) | ||
}) | ||
.then(res => { | ||
process.chdir(projectDir) | ||
ob.complete() | ||
}) | ||
.catch(res => { | ||
throw new Error(res.stderr) | ||
process.chdir(projectDir) | ||
catchAndThrow(ob, res) | ||
}) | ||
} | ||
function copyRelease (ctx, ob) { | ||
ctx.releaseDir = path.resolve(ctx.localRepo.name, ctx.version) | ||
ob.next(`Creating ${ctx.releaseDir}`) | ||
mkdirp(ctx.releaseDir) | ||
.then(() => { | ||
ob.next(`Copying ${ctx.distDir} to ${ctx.releaseDir}`) | ||
return cpy(`${path.join(ctx.distDir, '*')}`, ctx.releaseDir) | ||
}) | ||
.then(() => ob.complete()) | ||
.catch(catchAndThrow(ob)) | ||
} | ||
function removeOldSymlinks (ctx, ob) { | ||
// retrieve all symbolic links and delete them | ||
fs.readdir(ctx.localRepo.name) | ||
.then((files) => files.map((file) => path.resolve(ctx.localRepo.name, file))) | ||
.then((files) => { | ||
return pFilter(files, (file) => { | ||
ob.next(`Checking ${file}`) | ||
return fs.lstat(file).then(stats => stats.isSymbolicLink()) | ||
}) | ||
.then((files) => pMap( | ||
files, | ||
file => { | ||
ob.next(`Deleting ${file}`) | ||
return del(file, { force: true }) | ||
} | ||
)) | ||
}) | ||
.then(() => ob.complete()) | ||
.catch(catchAndThrow(ob)) | ||
} | ||
function generateVersionsJson (ctx, ob) { | ||
const projectDir = process.cwd() | ||
const versionMatcher = /[0-9](:?\.[0-9]){2}/ | ||
process.chdir(ctx.localRepo.name) | ||
ob.next(`Creating ${ctx.currentReleaseDirname}`) | ||
fs.readdir('.') | ||
.then((files) => { | ||
ob.next('Check', files) | ||
return pFilter(files, (file) => ( | ||
fs.lstat(file).then(stats => stats.isDirectory()) | ||
)) | ||
}) | ||
.then((dirs) => dirs.filter((dir) => versionMatcher.test(dir))) | ||
.then((versions) => fs.writeFile('versions.json', JSON.stringify(versions))) | ||
.then(() => { | ||
process.chdir(projectDir) | ||
ob.complete() | ||
}) | ||
.catch(err => { | ||
process.chdir(projectDir) | ||
catchAndThrow(ob, err) | ||
}) | ||
} | ||
function createSymlinks (ctx, ob) { | ||
const projectDir = process.cwd() | ||
process.chdir(ctx.localRepo.name) | ||
ob.next(`Creating ${ctx.currentReleaseDirname}`) | ||
fs.symlink(ctx.version, ctx.currentReleaseDirname) | ||
.catch(err => { | ||
process.chdir(projectDir) | ||
catchAndThrow(ob, err) | ||
}) | ||
fs.readdir(ctx.releaseDir) | ||
.then((files) => pMap( | ||
files, | ||
(file) => { | ||
ob.next(`Linking ${file}`) | ||
return fs.symlink(path.join(ctx.version, file), file) | ||
} | ||
)).then(() => { | ||
process.chdir(projectDir) | ||
ob.complete() | ||
}) | ||
.catch(err => { | ||
process.chdir(projectDir) | ||
catchAndThrow(ob, err) | ||
}) | ||
} | ||
function publish (ctx, ob) { | ||
const projectDir = process.cwd() | ||
process.chdir(ctx.localRepo.name) | ||
ob.next('Adding all files') | ||
execa('git', ['add', '.']) | ||
.then(() => { | ||
ob.next('Committing') | ||
return execa('git', ['commit', '-m', ctx.version]) | ||
}) | ||
.then(() => { | ||
ob.next('Pushing') | ||
return execa('git', ['push', 'origin', ctx.branch]) | ||
}) | ||
.then(() => { | ||
process.chdir(projectDir) | ||
ob.complete() | ||
}) | ||
.catch(err => { | ||
process.chdir(projectDir) | ||
catchAndThrow(ob, err) | ||
}) | ||
} | ||
function cleanup (ctx, ob) { | ||
if (ctx.localRepo.clean) { | ||
ob.next(`Cleaning ${ctx.localRepo.name}`) | ||
ctx.localRepo.removeCallback() | ||
} | ||
ob.complete() | ||
} | ||
module.exports = { | ||
readConf: curry(readConf), | ||
checkRemoteBranch: curry(checkRemoteBranch) | ||
checkRemoteBranch: curry(checkRemoteBranch), | ||
fetchRemote: curry(fetchRemote), | ||
createBranch: curry(createBranch), | ||
copyRelease: curry(copyRelease), | ||
removeOldSymlinks: curry(removeOldSymlinks), | ||
createSymlinks: curry(createSymlinks), | ||
generateVersionsJson: curry(generateVersionsJson), | ||
publish: curry(publish), | ||
cleanup: curry(cleanup) | ||
} |
{ | ||
"name": "docarchive", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Manage different documentation versions and host them on Github", | ||
@@ -8,17 +8,14 @@ "main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "ava" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"observable-to-promise": "^0.4.0", | ||
"proxyquire": "^1.7.10", | ||
"sinon": "^1.17.6", | ||
"zen-observable": "^0.4.0" | ||
}, | ||
"dependencies": { | ||
"cpy": "^5.0.0", | ||
"del": "^2.2.2", | ||
"execa": "^0.5.0", | ||
"lodash.curry": "^4.1.1", | ||
"read-pkg": "^2.0.0" | ||
"mkdirp": "^0.5.1", | ||
"p-filter": "^1.0.0", | ||
"p-map": "^1.1.0", | ||
"pify": "^2.3.0", | ||
"read-pkg": "^2.0.0", | ||
"tmp": "^0.0.31" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
215
19838
10
3
2
1
+ Addedcpy@^5.0.0
+ Addeddel@^2.2.2
+ Addedmkdirp@^0.5.1
+ Addedp-filter@^1.0.0
+ Addedp-map@^1.1.0
+ Addedpify@^2.3.0
+ Addedtmp@^0.0.31
+ Addedarray-union@1.0.2(transitive)
+ Addedarray-uniq@1.0.3(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcp-file@4.2.0(transitive)
+ Addedcpy@5.1.0(transitive)
+ Addeddel@2.2.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglobby@5.0.06.1.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-path-cwd@1.0.0(transitive)
+ Addedis-path-in-cwd@1.0.1(transitive)
+ Addedis-path-inside@1.0.1(transitive)
+ Addedmake-dir@1.3.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addednested-error-stacks@2.1.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedp-filter@1.0.0(transitive)
+ Addedp-map@1.2.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-is-inside@1.0.2(transitive)
+ Addedpify@3.0.0(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedtmp@0.0.31(transitive)
+ Addedwrappy@1.0.2(transitive)