Skip to content

Commit 53e6db6

Browse files
committed
Use markers to insert README stamp
1 parent 4291fb5 commit 53e6db6

File tree

3 files changed

+111
-30
lines changed

3 files changed

+111
-30
lines changed

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ $ npm install -g codestamp
7777
- [`examples/basic`](examples/basic/package.json): Simple stamping via the CLI.
7878
- [`examples/template-python`](examples/template-python/package.json): Use template string to add a Python banner comment via the CLI.
7979
- [`examples/dynamic-json`](examples/dynamic-json/stamp.js): Use the API to programmatically insert the stamp as a JSON field (via `initialStampPlacer`), and ignore insignificant spaces and new lines in JSON (via `fileTransformerForHashing`).
80-
- 🙋 [`scripts/generate-docs.ts`](scripts/generate-docs.ts): The README file you're reading is generated and verified by `codestamp`!
81-
- And here's the stamp: `CodeStamp<<cd21600e1a6bd8f42c99f648986959c1>>`
80+
- 🙋 [`scripts/generate-docs.ts`](scripts/generate-docs.ts): The README file you're reading is generated and verified by `codestamp`! <!-- <CODESTAMP START> -->
81+
- And here's the stamp: `CodeStamp<<d508062897d6b3afb002faedb60e864e>>`
82+
<!-- <CODESTAMP END> -->
8283

8384
### Recommended workflow
8485

@@ -574,6 +575,50 @@ Specify how the initial stamp should be removed and updated.
574575
* },
575576
* }
576577
* ```
578+
*
579+
* @example Recommended: place invisible markers to specify the range
580+
* of insertion and deletion. This is a much more robust approach.
581+
*
582+
* ```typescript
583+
* // First, put START and END markers in your target file, such as:
584+
* // <!-- <CODESTAMP START> -->
585+
* //
586+
* // <!-- <CODESTAMP END> -->
587+
*
588+
* {
589+
* initialStampPlacer: ({ content, stamp }) => {
590+
* const lineList = content.split("\n");
591+
* const startIndex = lineList.findIndex((line) =>
592+
* line.includes(`<!-- <CODESTAMP START> -->`)
593+
* );
594+
* const endIndex = lineList.findIndex((line) =>
595+
* line.includes(`<!-- <CODESTAMP END> -->`)
596+
* );
597+
* // Assert both `startIndex` and `endIndex` !== -1
598+
*
599+
* lineList.splice(
600+
* startIndex + 1,
601+
* 0,
602+
* `here's the stamp: ${stamp}`
603+
* );
604+
*
605+
* return lineList.join("\n");
606+
* },
607+
* initialStampRemover: ({ content, stamp }) => {
608+
* const lineList = content.split("\n");
609+
* const startIndex = lineList.findIndex((line) =>
610+
* line.includes(`<!-- <CODESTAMP START> -->`)
611+
* );
612+
* const endIndex = lineList.findIndex((line) =>
613+
* line.includes(`<!-- <CODESTAMP END> -->`)
614+
* );
615+
* // Assert both `startIndex` and `endIndex` !== -1
616+
*
617+
* lineList.splice(startIndex + 1, endIndex - startIndex - 1);
618+
* return lineList.join("\n");
619+
* },
620+
* }
621+
* ```
577622
*/
578623
export type TStampRemover = TFormatter;
579624
````

scripts/generate-docs.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,37 @@ async function stamp({
6161
dependencyGlobList: dependencyPathList,
6262
shouldWrite,
6363
initialStampPlacer: ({ content, stamp }) => {
64-
const contentLineList = content.split("\n");
65-
const indexOfExamples = contentLineList.findIndex((line) =>
66-
line.startsWith("### More Examples")
64+
const lineList = content.split("\n");
65+
const startIndex = lineList.findIndex((line) =>
66+
line.includes(`<!-- <CODESTAMP START> -->`)
6767
);
68-
69-
invariant(indexOfExamples !== -1, `Couldn't found examples`);
70-
71-
const firstBulletPointIndex = contentLineList.findIndex(
72-
(line, index) => line.startsWith("- ") && index > indexOfExamples
68+
invariant(startIndex !== -1, `Couldn't found start line`);
69+
const endIndex = lineList.findIndex((line) =>
70+
line.includes(`<!-- <CODESTAMP END> -->`)
7371
);
74-
let insertIndex = firstBulletPointIndex;
75-
invariant(insertIndex !== -1, `Couldn't found bullet point`);
76-
77-
while (true) {
78-
if (!contentLineList[insertIndex]!.startsWith("- ")) {
79-
break;
80-
}
81-
insertIndex++;
82-
}
72+
invariant(endIndex !== -1, `Couldn't found end line`);
8373

84-
contentLineList.splice(
85-
insertIndex,
74+
lineList.splice(
75+
startIndex + 1,
8676
0,
87-
`- 🙋 [\`scripts/generate-docs.ts\`](scripts/generate-docs.ts): The README file you're reading is generated and verified by \`codestamp\`!`,
8877
` - And here's the stamp: \`${stamp}\``
8978
);
9079

91-
return contentLineList.join("\n");
80+
return lineList.join("\n");
9281
},
9382
initialStampRemover: ({ content, stamp }) => {
94-
const contentLineList = content.split("\n");
95-
const indexOfStamp = contentLineList.findIndex((line) =>
96-
line.includes(stamp)
83+
const lineList = content.split("\n");
84+
const startIndex = lineList.findIndex((line) =>
85+
line.includes(`<!-- <CODESTAMP START> -->`)
9786
);
87+
invariant(startIndex !== -1, `Couldn't found start line`);
88+
const endIndex = lineList.findIndex((line) =>
89+
line.includes(`<!-- <CODESTAMP END> -->`)
90+
);
91+
invariant(endIndex !== -1, `Couldn't found end line`);
9892

99-
invariant(indexOfStamp !== -1, `Couldn't found stamp`);
100-
101-
contentLineList.splice(indexOfStamp - 1, 2);
102-
return contentLineList.join("\n");
93+
lineList.splice(startIndex + 1, endIndex - startIndex - 1);
94+
return lineList.join("\n");
10395
},
10496
fileTransformerForHashing: (param) => {
10597
if (param.type === "DEPENDENCY") {

src/core.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,50 @@ const defaultInitialStampPlacer: TFormatter = ({ content, stamp }) =>
131131
* },
132132
* }
133133
* ```
134+
*
135+
* @example Recommended: place invisible markers to specify the range
136+
* of insertion and deletion. This is a much more robust approach.
137+
*
138+
* ```typescript
139+
* // First, put START and END markers in your target file, such as:
140+
* // <!-- <CODESTAMP START> -->
141+
* //
142+
* // <!-- <CODESTAMP END> -->
143+
*
144+
* {
145+
* initialStampPlacer: ({ content, stamp }) => {
146+
* const lineList = content.split("\n");
147+
* const startIndex = lineList.findIndex((line) =>
148+
* line.includes(`<!-- <CODESTAMP START> -->`)
149+
* );
150+
* const endIndex = lineList.findIndex((line) =>
151+
* line.includes(`<!-- <CODESTAMP END> -->`)
152+
* );
153+
* // Assert both `startIndex` and `endIndex` !== -1
154+
*
155+
* lineList.splice(
156+
* startIndex + 1,
157+
* 0,
158+
* `here's the stamp: ${stamp}`
159+
* );
160+
*
161+
* return lineList.join("\n");
162+
* },
163+
* initialStampRemover: ({ content, stamp }) => {
164+
* const lineList = content.split("\n");
165+
* const startIndex = lineList.findIndex((line) =>
166+
* line.includes(`<!-- <CODESTAMP START> -->`)
167+
* );
168+
* const endIndex = lineList.findIndex((line) =>
169+
* line.includes(`<!-- <CODESTAMP END> -->`)
170+
* );
171+
* // Assert both `startIndex` and `endIndex` !== -1
172+
*
173+
* lineList.splice(startIndex + 1, endIndex - startIndex - 1);
174+
* return lineList.join("\n");
175+
* },
176+
* }
177+
* ```
134178
*/
135179
export type TStampRemover = TFormatter;
136180
// <DOCEND SOURCE TStampRemover>

0 commit comments

Comments
 (0)