Skip to content

Commit 8c5c4cc

Browse files
committed
refactor: dry out toc script with @qui-cli/init
1 parent 69f312e commit 8c5c4cc

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@battis/prettier": "^2.0.8",
3232
"@qui-cli/colors": "workspace:*",
3333
"@qui-cli/core": "workspace:*",
34+
"@qui-cli/init": "workspace:*",
3435
"@qui-cli/log": "workspace:*",
3536
"@qui-cli/plugin": "workspace:*",
3637
"@tsconfig/node-lts": "^24.0.0",

pnpm-lock.yaml

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/toc.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PathString } from '@battis/descriptive-types';
66
import { Colors } from '@qui-cli/colors';
77
import { Log } from '@qui-cli/log';
88
import { IPackageJson } from 'package-json-type';
9+
import * as Confirm from '@qui-cli/init/dist/Init/Confirm/index.js';
910

1011
type Configuration = Plugin.Configuration & {
1112
scanPath?: PathString;
@@ -81,7 +82,7 @@ function init({ values }: Plugin.ExpectedArguments<typeof options>) {
8182
configure({ scanPath: Positionals.get('scanPath'), ...values });
8283
}
8384

84-
function run() {
85+
async function run() {
8586
if (!config.scanPath) {
8687
Log.error(`${Colors.positionalArg('scanPath')} must be defined`);
8788
process.exit(1);
@@ -93,6 +94,7 @@ function run() {
9394
} else {
9495
config.outputPath = path.resolve(process.cwd(), config.outputPath);
9596
}
97+
const outputPath = config.outputPath;
9698

9799
const entries = scan(config.scanPath);
98100
if (entries) {
@@ -105,20 +107,15 @@ function run() {
105107
.replace('{{TOC}}', toc);
106108
}
107109
}
108-
if (fs.existsSync(config.outputPath)) {
109-
if (!config.overwrite) {
110-
Log.error(
111-
`Cannot overwrite existing ${Colors.path(config.outputPath)} (set ${Colors.flagArg('--overwrite')})`
112-
);
113-
Log.info(toc);
114-
process.exit(2);
115-
}
116-
Log.warning(
117-
`Overwriting existing ${Colors.path(path.basename(config.outputPath))}`
118-
);
119-
}
120-
fs.writeFileSync(config.outputPath, toc);
121-
Log.info(`TOC written to ${Colors.path(config.outputPath)}`);
110+
await Confirm.withDiff({
111+
src: toc,
112+
dest: fs.existsSync(config.outputPath)
113+
? fs.readFileSync(outputPath, 'utf8')
114+
: undefined,
115+
identifier: Colors.path(outputPath, Colors.keyword),
116+
action: () => fs.writeFileSync(outputPath, toc),
117+
force: config.overwrite
118+
});
122119
} else {
123120
Log.error(
124121
`No indexable packages and README files found in ${Colors.path(config.scanPath)}`

0 commit comments

Comments
 (0)