Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 24x 24x 1x 28x 26x 2x 1x 28x 28x 28x 2x 26x 26x 26x 24x 2x 1x 72x 72x 216x 72x 1x 44x 1x 48x 1x 24x 1x 24x 1x 24x 1x 12x 1x 24x 12x 6x 4x 6x 6x 4x 2x 1x 24x 12x 6x 6x 4x 6x 6x 24x 24x 24x 24x 4x 1x 28x 24x 24x 1x 1x 1x 24x 24x 24x 1x 28x 26x 1x 28x 28x 28x 24x 24x 24x 24x 1x 48x 1x 1x 1x 24x 24x 24x 24x 24x 4x 4x 24x 22x 24x 1x 24x 24x 30x 30x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 28x 24x 24x 1x 28x 12x 2x 2x 1x | import * as fs from 'fs'; import * as path from 'path'; export interface Info { description?: string; npmName?: string; repoName?: string; githubUser?: string; usage: (command: string) => string | undefined; fileContent?: string; fileName?: string; filePath?: string; heading?: string; npxExecutable?: string; globalExecutable?: string; info: Info; } interface PackageJSON { name?: string; usage?: string; description?: string; bin?: {[key: string]: string} | string; repository?: { type?: string; url?: string; }; } export class Pkg { constructor(public pkg: PackageJSON) { this.pkg = pkg; } static async fileExists(fileName: string) { try { const stat = await fs.promises.lstat(fileName); return stat.isFile(); } catch (e) { return false; } } static async load(process: NodeJS.Process) { const pkgLocation = path.join(process.cwd(), 'package.json'); const found = await Pkg.fileExists(pkgLocation); if (!found) { throw new Error('no package.json found in the root of this directory'); } const pkgContent = await fs.promises.readFile(pkgLocation, 'utf-8'); try { const pkg = JSON.parse(pkgContent); return new Pkg(pkg); } catch (e) { throw new Error('there was an issue parsing the package.json file'); } } parseRepo() { const url = this.pkg.repository?.url || ''; const result = url.match('https?://github.com/(.+)/(.+).git') || []; const [repository, githubUser, repoName] = result; return {githubUser, repoName, repository}; } get npmName(): string | undefined { return this.pkg.name; } get repoName(): string | undefined { return this.parseRepo().repoName; } get githubUser(): string | undefined { return this.parseRepo().githubUser; } get description(): string | undefined { return this.pkg.description; } get heading() { return this.repoName || this.npmName; } get isScoped() { return this.pkg.name && this.pkg.name.split('/').length === 2; } get npxExecutable() { if (typeof this.pkg.bin === 'undefined') return undefined; if (typeof this.pkg.bin === 'string') { if (this.isScoped) return `${this.pkg.name}`; return this.pkg.name; } const primary = Object.keys(this.pkg.bin)[0]; if (this.isScoped) return `-p ${this.pkg.name} ${primary}`; if (this.pkg.name === primary) return this.pkg.name; return `${this.pkg.name} ${primary}`; } get globalExecutable() { if (typeof this.pkg.bin === 'undefined') return undefined; if (typeof this.pkg.bin === 'string' && this.pkg.name) { const split = this.pkg.name.split('/'); if (split.length === 2) return split[1]; return split[0]; } const primary = Object.keys(this.pkg.bin)[0]; return primary; } get usage() { return (command: string) => { const usage = this.pkg.usage; if (command && usage) return usage.replace('CMD', command); return command; }; } static async info(process: NodeJS.Process) { const pkg = await Pkg.load(process); const i: Omit<Info, 'info'> = { npmName: pkg.npmName, repoName: pkg.repoName, githubUser: pkg.githubUser, usage: pkg.usage.bind(pkg), description: pkg.description, heading: pkg.heading, npxExecutable: pkg.npxExecutable, globalExecutable: pkg.globalExecutable, }; return i; } } export class Template { static info<T extends Omit<Info, 'info'>>(info: T): Info { const x: Info = (info as unknown) as Info; x.info = (info as unknown) as Info; return (x as unknown) as Info; } static async readFile(filePath: string) { try { return await fs.promises.readFile(filePath, 'utf-8'); } catch (e) { return ''; } } static async load(fileName: string, process: NodeJS.Process) { const filePath = path.join(process.cwd(), fileName); const fileContent = await Template.readFile(filePath); const pkg = await Pkg.info(process); const info = Template.info({...pkg, fileName, filePath, fileContent}); const update = (content: string) => { return Template.update(filePath, content); }; return {info, update}; } static async update(filePath: string, content: string) { await fs.promises.writeFile(filePath, content, 'utf-8'); } } export class ReadMe { static badges({ repoName, npmName, githubUser, }: Info): {[key: string]: {link: string; image: string}} { const badges: {[key: string]: {link: string; image: string}} = {}; if (githubUser && repoName) { badges['semantic release'] = { image: `https://github.com/${githubUser}/${repoName}/workflows/semantic%20release/badge.svg`, link: `https://github.com/${githubUser}/${repoName}/actions?query=workflow%3A%22semantic+release%22`, }; badges['coverage'] = { image: `https://github.com/${githubUser}/${repoName}/workflows/coverage/badge.svg`, link: `https://${githubUser}.github.io/${repoName}/`, }; } if (npmName) { badges['npm'] = { image: `https://badge.fury.io/js/${encodeURIComponent(npmName)}.svg`, link: `https://www.npmjs.com/package/${npmName}`, }; } return badges; } static badgeMarkdown(info: Info) { const badges = ReadMe.badges(info); return Object.keys(badges).map(alt => { const badge = badges[alt]; return `[](${badge.link})`; }); } static npmInstall({npmName, globalExecutable, usage}: Info) { return npmName ? [ '## Install', '', '```', `npm install ${npmName}${globalExecutable ? ' -g' : ''}`, ...(globalExecutable ? [`${usage(globalExecutable)}`] : []), '```', ] : []; } static npxRun({npxExecutable, usage}: Info) { return npxExecutable ? [ '## Use directly via `npx`', '', '```', `npx ${usage(npxExecutable)}`, '```', ] : []; } static transform({heading, fileContent, info, description}: Info) { const marker = '<!-- anything below this line will be safe from template removal -->'; const keep = (fileContent && fileContent.split(marker)[1]) || ''; const badge = ReadMe.badgeMarkdown(info); const npm = ReadMe.npmInstall(info); const npx = ReadMe.npxRun(info); const template = [ `# ${heading || 'untitled'}`, ...(badge.length ? [badge.join(' ')] : []), ...(description ? [description] : []), ...(npm.length ? [npm.join('\n')] : []), ...(npx.length ? [npx.join('\n')] : []), marker, ]; return template.join('\n\n') + keep; } static async update(process: NodeJS.Process) { const {info, update} = await Template.load('README.md', process); const content = ReadMe.transform(info); return update(content); } static async cli(process: NodeJS.Process) { try { await ReadMe.update(process); process.exit(0); } catch (e) { process.stderr.write(e.message + '\n'); process.exit(1); } } } |