Skip to content

Commit

Permalink
feat: support copying from remote http archives
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 25, 2022
1 parent 1748786 commit 02cf9cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -32,6 +32,10 @@ git-glob-copy *.json git@github.com:antongolub/tsc-esm-fix.git/master/json

# Push from repo to repo
ggcp 'git@github.com:antongolub/tsc-esm-fix.git/master/foo/*.txt' git@github.com:antongolub/git-glob-copy.git/master/bar


# Fetch remote archive to local dir
ggcp 'https://registry.npmjs.org/ggcp/-/ggcp-1.5.1.tgz/**/*.js' /private/tmp/ggcp-1.5.1/
```
`ggcp` is an alias for `git-glob-copy`

Expand Down
37 changes: 27 additions & 10 deletions src/main/js/index.js
@@ -1,4 +1,4 @@
import {fs, globby, path, tempy, ctx, $} from 'zx-extra'
import {fs, globby, path, tempy, ctx, $, fetch} from 'zx-extra'
import tar from 'tar'

$.verbose = $.env.DEBUG ? 1 : 0
Expand All @@ -20,12 +20,16 @@ export const copy = async (

if (dst.type === 'archive') throw new Error('archive as dest is not supported yet')

if (src.type === 'git') await fetch(src)
if (src.type === 'git') await gitFetch(src)

if (dst.type === 'git') await fetch(dst, true)
if (dst.type === 'git') await gitFetch(dst, true)

if (src.type === 'archive') {
await tar.x({ file: src.file, cwd: src.base })
if (src.protocol !== 'local') src.file = await download(src.file)

if (fs.statSync(src.file).isFile()) {
await tar.x({ file: src.file, cwd: src.base })
}
}

await copydir({
Expand All @@ -37,10 +41,10 @@ export const copy = async (
ignoreFiles,
})

if (dst.repo) await push(dst, msg)
if (dst.type === 'git') await gitPush(dst, msg)
}

const fetch = (src, nothrow) => ctx(async ($) => {
const gitFetch = (src, nothrow) => ctx(async ($) => {
$.cwd = src.base
try {
await $`git clone --single-branch --branch ${src.branch} --depth 1 ${src.repo} .`
Expand All @@ -52,7 +56,7 @@ const fetch = (src, nothrow) => ctx(async ($) => {
}
})

const push = (dst, msg) => ctx(async ($) => {
const gitPush = (dst, msg) => ctx(async ($) => {
$.cwd = dst.base
await $`git add .`
try {
Expand Down Expand Up @@ -85,12 +89,13 @@ export const parse = (target, {cwd = process.cwd(), temp = tempy.temporaryDirect
}

if (arcref.test(target)) {
const [, file, protocol, format, pattern] = arcref.exec(target)
const [, file, _protocol, format, pattern] = arcref.exec(target)
const protocol = _protocol ? _protocol.replaceAll(/[^a-z]/g, '') : 'local'

return {
type: 'archive',
file: file.startsWith?.('/') ? file : path.resolve(cwd, file),
protocol: protocol ? protocol.replaceAll(/[^a-z]/g, '') : 'local',
file: protocol !== 'local' || file.startsWith?.('/') ? file : path.resolve(cwd, file),
protocol,
format,
pattern,
raw: target,
Expand All @@ -106,6 +111,18 @@ export const parse = (target, {cwd = process.cwd(), temp = tempy.temporaryDirect
}
}

export const download = (async (url, file = tempy.temporaryFile()) => {
const res = await fetch(url)
const fileStream = fs.createWriteStream(file)
await new Promise((resolve, reject) => {
res.body.pipe(fileStream)
res.body.on("error", reject)
fileStream.on("finish", resolve)
})

return file
})

export const copydir = async ({
from,
to,
Expand Down
15 changes: 14 additions & 1 deletion src/test/js/index.test.js
Expand Up @@ -58,7 +58,7 @@ test('copy() throws err on invalid args', () => ctx(async ($) => {
}
}))

test.only('copy() from archive', () => ctx(async ($) => {
test('copy() from local archive', () => ctx(async ($) => {
const temp = tempy.temporaryDirectory()
await fs.outputFile(path.resolve(temp, 'foo.txt'), 'foo')
await fs.outputFile(path.resolve(temp, 'foo.js'), 'foo')
Expand All @@ -77,6 +77,19 @@ test.only('copy() from archive', () => ctx(async ($) => {
assert.is((await fs.readFile(path.resolve(temp, 'unpacked/foo.js'))).toString('utf8'), 'foo')
}))

test('copy() from remote archive', () => ctx(async ($) => {
const temp = tempy.temporaryDirectory()

await copy({
from: 'https://registry.npmjs.org/ggcp/-/ggcp-1.5.1.tgz/**/*.js',
to: './',
cwd: temp
})

assert.ok((!await fs.pathExists(path.resolve(temp, 'package/package.json'))))
assert.ok((await fs.pathExists(path.resolve(temp, 'package/src/main/js/index.js'))))
}))

test('parse()', () => {
const cases = [
[
Expand Down

0 comments on commit 02cf9cd

Please sign in to comment.