()
| 9 | import { develop } from './util/dev-server'; |
| 10 | |
| 11 | async function main() { |
| 12 | const pkg = ( |
| 13 | await readPackageUp({ |
| 14 | cwd: fileURLToPath(import.meta.url), |
| 15 | }) |
| 16 | )?.packageJson; |
| 17 | if (!pkg) throw new Error('Invalid package'); |
| 18 | |
| 19 | const notifier = updateNotifier({ pkg }); |
| 20 | notifier.notify(); |
| 21 | |
| 22 | program |
| 23 | .version(pkg.version) |
| 24 | .description('Create a markmap from a Markdown input file') |
| 25 | .arguments('<input>') |
| 26 | .option('--no-open', 'Do not open the output file after generation') |
| 27 | .option('--no-toolbar', 'Do not show toolbar') |
| 28 | .option('-o, --output <output>', 'Specify the filename of the output HTML') |
| 29 | .option( |
| 30 | '--offline', |
| 31 | 'Inline all assets to allow the generated HTML to work offline', |
| 32 | ) |
| 33 | .option( |
| 34 | '-w, --watch', |
| 35 | 'Watch the input file and update output on the fly, note that this feature is for development only', |
| 36 | ) |
| 37 | .option('--port <port>', 'Set the port for the devServer to listen') |
| 38 | .action(async (input: string, cmd) => { |
| 39 | let { offline } = cmd; |
| 40 | if (cmd.watch) offline = true; |
| 41 | const content = await readFile(input, 'utf8'); |
| 42 | const output = cmd.output || `${input.replace(/\.\w*$/, '')}.html`; |
| 43 | if (cmd.watch) { |
| 44 | const devServer = await develop({ |
| 45 | toolbar: cmd.toolbar, |
| 46 | offline, |
| 47 | port: +cmd.port || undefined, |
| 48 | }); |
| 49 | const address = devServer.serverInfo!.address; |
| 50 | const provider = devServer.addProvider({ filePath: input }); |
| 51 | const filename = basename(input); |
| 52 | const url = `http://localhost:${address.port}/?key=${provider.key}&filename=${encodeURIComponent(filename)}`; |
| 53 | console.log(`Listening at ${url}`); |
| 54 | if (cmd.open) open(url); |
| 55 | } else { |
| 56 | await createMarkmap({ |
| 57 | content, |
| 58 | output, |
| 59 | open: cmd.open, |
| 60 | toolbar: cmd.toolbar, |
| 61 | offline, |
| 62 | }); |
| 63 | } |
| 64 | }); |
| 65 | program.parse(); |
| 66 | } |
| 67 | |
| 68 | main().catch((err) => { |
no test coverage detected