|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | + |
| 4 | +import { fileExtension, languageFromExtension, removeBackticks } from './util'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Single step of a recipe. |
| 8 | + */ |
| 9 | +export abstract class Step { //} implements IWriteVisitor { |
| 10 | + constructor(readonly description: string) {} |
| 11 | + |
| 12 | + abstract writeToBash(fd: number, index: number): void; |
| 13 | + |
| 14 | + abstract writeToReadme(fd: number, index: number): void; |
| 15 | + |
| 16 | + protected writeDescriptionBash(fd: number, index: number, suffix: string = ''): void { |
| 17 | + fs.writeSync(fd, `\n# ${index+1}. ${removeBackticks(this.description)}${suffix}\n`); |
| 18 | + } |
| 19 | + |
| 20 | + protected writeDescriptionReadme(fd: number, index: number, suffix: string = ''): void { |
| 21 | + fs.writeSync(fd, `\n${index+1}. ${this.description}${suffix}\n`); |
| 22 | + } |
| 23 | + |
| 24 | + protected spacer = ' '; // Spacer for indented lines in README. |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Step consisting of one or more shell commands. |
| 29 | + */ |
| 30 | +export class CommandStep extends Step { |
| 31 | + constructor( |
| 32 | + readonly description: string, |
| 33 | + readonly commands: string[], |
| 34 | + readonly postscript: string = '', |
| 35 | + readonly ignoreIfBash: boolean = false |
| 36 | + ) { |
| 37 | + super(description); |
| 38 | + } |
| 39 | + |
| 40 | + writeToBash(fd: number, index: number): void { |
| 41 | + this.writeDescriptionBash(fd, index); |
| 42 | + |
| 43 | + const allPrefix = this.ignoreIfBash ? '# ' : ''; |
| 44 | + for (const command of this.commands) { |
| 45 | + const prefix = command === 'npm run serve' ? '# ' : allPrefix; |
| 46 | + fs.writeSync(fd, prefix + command + '\n'); |
| 47 | + } |
| 48 | + if (this.postscript) { |
| 49 | + fs.writeSync(fd, '# ' + this.postscript + '\n'); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + writeToReadme(fd: number, index: number): void { |
| 54 | + this.writeDescriptionReadme(fd, index); |
| 55 | + |
| 56 | + const { spacer } = this; |
| 57 | + fs.writeSync(fd, '\n' + spacer + '```bash\n'); |
| 58 | + for (const command of this.commands) { |
| 59 | + fs.writeSync(fd, spacer + command + '\n'); |
| 60 | + } |
| 61 | + fs.writeSync(fd, spacer + '```\n'); |
| 62 | + |
| 63 | + if (this.postscript) { |
| 64 | + fs.writeSync(fd, '\n' + spacer + this.postscript + '\n'); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Step to create a file. |
| 71 | + */ |
| 72 | +export class CreateFileStep extends Step { |
| 73 | + constructor(readonly description: string, readonly filename: string, readonly contents: string) { |
| 74 | + super(description); |
| 75 | + } |
| 76 | + |
| 77 | + writeToBash(fd: number, index: number): void { |
| 78 | + this.writeDescriptionBash(fd, index); |
| 79 | + |
| 80 | + const dirname = path.dirname(this.filename); |
| 81 | + if (dirname !== '.') { |
| 82 | + fs.writeSync(fd, `mkdir -p ${dirname}\n`); |
| 83 | + } |
| 84 | + |
| 85 | + fs.writeSync(fd, `cat > ${this.filename} << EOF\n`); |
| 86 | + fs.writeSync(fd, this.contents); |
| 87 | + if (this.contents.at(-1) !== '\n') { |
| 88 | + fs.writeSync(fd, '\n'); |
| 89 | + } |
| 90 | + fs.writeSync(fd, 'EOF\n'); |
| 91 | + } |
| 92 | + |
| 93 | + writeToReadme(fd: number, index: number): void { |
| 94 | + this.writeDescriptionReadme(fd, index, ' containing'); |
| 95 | + |
| 96 | + const { spacer } = this; |
| 97 | + const language = languageFromExtension(this.filename); |
| 98 | + fs.writeSync(fd, '\n' + spacer + '```' + language + '\n'); |
| 99 | + for (const line of this.contents.split('\n')) { |
| 100 | + if (line) { |
| 101 | + fs.writeSync(fd, spacer + line + '\n'); |
| 102 | + } else { |
| 103 | + fs.writeSync(fd, '\n'); |
| 104 | + } |
| 105 | + } |
| 106 | + fs.writeSync(fd, spacer + '```\n'); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Step to create a file. |
| 112 | + */ |
| 113 | +export class MergeJsonStep extends Step { |
| 114 | + constructor(readonly description: string, readonly filename: string, readonly toMerge: string) { |
| 115 | + super(description); |
| 116 | + } |
| 117 | + |
| 118 | + writeToBash(fd: number, index: number): void { |
| 119 | + this.writeDescriptionBash(fd, index); |
| 120 | + |
| 121 | + const tempFilename = 'temp' + fileExtension(this.filename); |
| 122 | + fs.writeSync(fd, `cat > ${tempFilename} << EOF\n`); |
| 123 | + fs.writeSync(fd, this.toMerge); |
| 124 | + if (this.toMerge.at(-1) !== '\n') { |
| 125 | + fs.writeSync(fd, '\n'); |
| 126 | + } |
| 127 | + fs.writeSync(fd, 'EOF\n'); |
| 128 | + fs.writeSync(fd, `merge-json ${this.filename} ${tempFilename}\n`); |
| 129 | + fs.writeSync(fd, `rm ${tempFilename}\n`); |
| 130 | + } |
| 131 | + |
| 132 | + writeToReadme(fd: number, index: number): void { |
| 133 | + this.writeDescriptionReadme(fd, index); |
| 134 | + |
| 135 | + const { spacer } = this; |
| 136 | + const language = languageFromExtension(this.filename); |
| 137 | + fs.writeSync(fd, '\n' + spacer + '```' + language + '\n'); |
| 138 | + for (const line of this.toMerge.split('\n')) { |
| 139 | + if (line) { |
| 140 | + fs.writeSync(fd, spacer + line + '\n'); |
| 141 | + } else { |
| 142 | + fs.writeSync(fd, '\n'); |
| 143 | + } |
| 144 | + } |
| 145 | + fs.writeSync(fd, spacer + '```\n'); |
| 146 | + } |
| 147 | +} |
0 commit comments