Skip to content

Commit

Permalink
feat: support downloading a sub folder
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Removed Deno/Go support
  • Loading branch information
egoist committed Dec 23, 2021
1 parent 791a0c4 commit 04bc3e7
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 337 deletions.
17 changes: 2 additions & 15 deletions README.md
Expand Up @@ -10,24 +10,13 @@
## Usage

With Node.js:

```bash
# NPM
npx aho user/repo [destination]
# PNPM
pnpm dlx aho user/repo [destination]
```

With Deno:

```bash
deno install --allow-net --allow-read --allow-write --allow-run \
https://denopkg.com/egoist/aho@latest/aho.ts

aho user/repo [destination]
```

## Flags

### `aho <repo> [destination]`
Expand All @@ -36,14 +25,12 @@ Generate a project from `<repo>` to `[destination]`, destination defaults to cur

`<repo>` is in the format of `user/repo#branch_or_tag`, currently only GitHub repositories are supported. `#branch_or_tag` is optional.

You can also download a sub folder from the repo: `user/repo/sub_folder#dev`.

### `--force`

By default the command would abort if destination is not empty, use `--force` if you want to empty the dir before writing to it.

### `-p, --path <path>`

Extract a sub directory from `<repo>` to `[destination]`.

## Sponsors

[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)
Expand Down
17 changes: 0 additions & 17 deletions aho.code-workspace

This file was deleted.

3 changes: 0 additions & 3 deletions aho.ts

This file was deleted.

4 changes: 0 additions & 4 deletions cli-node.js

This file was deleted.

1 change: 0 additions & 1 deletion deno/README.md

This file was deleted.

71 changes: 0 additions & 71 deletions deno/deno/lib.ts

This file was deleted.

1 change: 0 additions & 1 deletion deno/shared.ts

This file was deleted.

10 changes: 4 additions & 6 deletions package.json
Expand Up @@ -6,16 +6,14 @@
"access": "public"
},
"type": "module",
"bin": "./cli-node.js",
"bin": "./dist/cli.js",
"files": [
"dist",
"/cli-node.js"
"dist"
],
"scripts": {
"build": "rm -rf dist && tsup src/index.ts --format esm && npm run build-deno",
"build": "rm -rf dist && tsup src/cli.ts --format esm --target node14 --minify",
"test": "echo lol",
"prepublishOnly": "npm run build",
"build-deno": "tsno scripts/build-deno.ts"
"prepublishOnly": "npm run build"
},
"license": "MIT",
"devDependencies": {
Expand Down
21 changes: 0 additions & 21 deletions scripts/build-deno.ts

This file was deleted.

61 changes: 15 additions & 46 deletions deno/index.ts → src/cli.ts
@@ -1,22 +1,18 @@
#!/usr/bin/env node
import { cac } from 'cac'
import {
cac,
downloadFile,
ensureDirSync,
existsSync,
getTempDir,
isEmptyDirSync,
resolvePath,
runCommand,
fetchJSON,
args,
exit,
getOwnVersion,
moveSync,
emptyDirSync,
joinPath,
} from './deno/lib.ts'
isEmptyDirSync,
resolvePath,
getTempDir,
} from './lib'
import { PrettyError, parseRepo, getDefaultBranchFromApi, extract } from './'

export const start = async () => {
const start = async () => {
const cli = cac('aho')

cli
Expand All @@ -25,13 +21,13 @@ export const start = async () => {
'-f, --force',
`Force override desitination directory even if it's not empty`,
)
.option('-p, --path <path>', `Only extract a sub path within the repo`)
.action(async (_repo, desitination, flags) => {
if (!_repo) {
throw new PrettyError('No repo provided')
}

const { repo, tag } = parseRepo(_repo)
const { owner, repoName, subpath, tag } = parseRepo(_repo)
const repo = `${owner}/${repoName}`

desitination = resolvePath(desitination || '.')

Expand All @@ -45,7 +41,9 @@ export const start = async () => {
)
}

console.log(`Downloading ${repo}`)
console.log(
`Downloading ${repo} ${subpath ? `(sub folder: ${subpath})` : ''}`,
)

const ref = tag || (await getDefaultBranchFromApi(repo))

Expand All @@ -55,7 +53,7 @@ export const start = async () => {
tempTarFile,
)
console.log(`Generating to ${desitination}`)
await extract(tempTarFile, desitination, { path: flags.path })
await extract(tempTarFile, desitination, { path: subpath })
})

cli.version(getOwnVersion())
Expand All @@ -74,33 +72,4 @@ export const start = async () => {
}
}

class PrettyError extends Error {
constructor(message: string) {
super(message)
this.name = this.constructor.name
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor)
} else {
this.stack = new Error(message).stack
}
}
}

async function extract(from: string, to: string, { path }: { path?: string }) {
const tempDir = getTempDir() + `/aho_temp_${Date.now()}`
ensureDirSync(tempDir)
const cmd = ['tar', 'xvzf', from, '-C', tempDir, '--strip-components', '1']
await runCommand(cmd)
emptyDirSync(to)
moveSync(path ? joinPath(tempDir, path) : tempDir, to)
}

function parseRepo(input: string) {
const [repo, tag] = input.split('#')
return { repo, tag }
}

async function getDefaultBranchFromApi(repo: string): Promise<string> {
const data = await fetchJSON(`https://api.github.com/repos/${repo}`)
return data.default_branch
}
start()
3 changes: 0 additions & 3 deletions src/deno/.vscode/settings.json

This file was deleted.

71 changes: 0 additions & 71 deletions src/deno/lib.ts

This file was deleted.

0 comments on commit 04bc3e7

Please sign in to comment.