Skip to content

Commit

Permalink
refactor: refactored the code (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Oct 15, 2023
1 parent 078ade2 commit eb2ac62
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
"vitest": "^0.34.6",
"yargs-parser": "^21.1.1"
}
}
}
37 changes: 0 additions & 37 deletions src/github.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fetch from 'node-fetch'

const api = async (endpoint: string) => {
const response = await fetch(`https://api.github.com/repos/${endpoint}`)
const contents = (await response.json()) as
| { message: string }
| { type: string; path: string }[]
return contents
}

type Options = {
user: string
repository: string
ref: string
directory: string
}

export const viaContentsApi = async ({
user,
repository,
ref = 'HEAD',
directory,
}: Options) => {
const files = []
const contents = await api(
`${user}/${repository}/contents/${directory}?ref=${ref}`
)

if ('message' in contents) {
if (contents.message === 'Not Found') {
return []
}
if (contents.message) {
throw new Error(contents.message)
}
}

if (Array.isArray(contents)) {
for (const item of contents) {
if (item.type === 'file') {
files.push(item.path)
} else if (item.type === 'dir') {
files.push(item.path)
}
}
}

return files
}
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function mkdirp(dir: string) {
try {
fs.mkdirSync(dir, { recursive: true })
} catch (e) {
if (e.code === 'EEXIST') return
if (e instanceof Error) {
if ('code' in e && e.code === 'EEXIST') return
}
throw e
}
}
Expand All @@ -33,7 +35,7 @@ async function main() {
const templateArg = args.template

const templateDirs = await viaContentsApi(config)
const templates = {}
const templates: Record<string, { name: string }> = {}

templateDirs.forEach((dir) => {
let template = dir.replace(`${directoryName}/`, '')
Expand Down Expand Up @@ -129,7 +131,11 @@ async function main() {
directoryPath: path.join(process.cwd(), target),
})
} catch (e) {
throw new Error(`Error running hook for ${templateName}: ${e.message}`)
throw new Error(
`Error running hook for ${templateName}: ${
e instanceof Error ? e.message : e
}`
)
}

console.log(bold(green('✔ Copied project files')))
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"strict": true,
"types": [
"node"
],
"lib": [
"esnext"
],
},
}

0 comments on commit eb2ac62

Please sign in to comment.