diff --git a/package.json b/package.json index 9720f15..f8ebe72 100644 --- a/package.json +++ b/package.json @@ -36,4 +36,4 @@ "vitest": "^0.34.6", "yargs-parser": "^21.1.1" } -} +} \ No newline at end of file diff --git a/src/github.js b/src/github.js deleted file mode 100644 index acf55b5..0000000 --- a/src/github.js +++ /dev/null @@ -1,37 +0,0 @@ -import fetch from 'node-fetch' - -async function api(endpoint) { - const response = await fetch(`https://api.github.com/repos/${endpoint}`) - return await response.json() -} - -// Great for downloads with few sub directories on big repos -// Cons: many requests if the repo has a lot of nested dirs -export async function viaContentsApi({ - user, - repository, - ref = 'HEAD', - directory, - getFullData = false, -}) { - const files = [] - const contents = await api(`${user}/${repository}/contents/${directory}?ref=${ref}`) - - if (contents.message === 'Not Found') { - return [] - } - - if (contents.message) { - throw new Error(contents.message) - } - - for (const item of contents) { - if (item.type === 'file') { - files.push(getFullData ? item : item.path) - } else if (item.type === 'dir') { - files.push(getFullData ? item : item.path) - } - } - - return files -} diff --git a/src/github.ts b/src/github.ts new file mode 100644 index 0000000..6093bed --- /dev/null +++ b/src/github.ts @@ -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 +} diff --git a/src/index.ts b/src/index.ts index 363fefc..f95f3d6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } } @@ -33,7 +35,7 @@ async function main() { const templateArg = args.template const templateDirs = await viaContentsApi(config) - const templates = {} + const templates: Record = {} templateDirs.forEach((dir) => { let template = dir.replace(`${directoryName}/`, '') @@ -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'))) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0acf8e8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "node", + "resolveJsonModule": true, + "esModuleInterop": true, + "strict": true, + "types": [ + "node" + ], + "lib": [ + "esnext" + ], + }, +} \ No newline at end of file