| | |
| | | import fs from 'fs' |
| | | import { intro, isCancel, outro, select, text } from '@clack/prompts' |
| | | import { rimraf } from 'rimraf' |
| | | import prettyBytes from 'pretty-bytes' |
| | | |
| | | const cacheFile = "./.quartz-cache/transpiled-build.mjs" |
| | | const fp = "./quartz/build.ts" |
| | |
| | | } |
| | | } |
| | | |
| | | function escapePath(fp) { |
| | | return fp |
| | | .replace(/\\ /g, " ") // unescape spaces |
| | | .replace(/^".*"$/, "$1") |
| | | .replace(/^'.*"$/, "$1") |
| | | .trim() |
| | | } |
| | | |
| | | yargs(hideBin(process.argv)) |
| | | .scriptName("quartz") |
| | | .version(version) |
| | |
| | | } |
| | | |
| | | async function rmContentFolder() { |
| | | if (fs.existsSync(contentFolder)) { |
| | | const contentStat = await fs.promises.lstat(contentFolder) |
| | | const contentStat = await fs.promises.lstat(contentFolder) |
| | | if (contentStat) { |
| | | if (contentStat.isSymbolicLink()) { |
| | | await fs.promises.unlink(contentFolder) |
| | | } else { |
| | | await rimraf(contentFolder) |
| | | } |
| | | } |
| | | |
| | | await fs.promises.mkdir(contentFolder) |
| | | } |
| | | |
| | | if (setupStrategy === 'copy' || setupStrategy === 'symlink') { |
| | | const originalFolder = await text({ |
| | | const originalFolder = escapePath(await text({ |
| | | message: "Enter the full path to existing content folder", |
| | | placeholder: 'On most terminal emulators, you can drag and drop a folder into the window and it will paste the full path', |
| | | validate(fp) { |
| | | if (!fs.existsSync(fp)) { |
| | | const fullPath = escapePath(fp) |
| | | if (!fs.existsSync(fullPath)) { |
| | | return "The given path doesn't exist" |
| | | } else if (!fs.lstatSync(fp).isDirectory()) { |
| | | } else if (!fs.lstatSync(fullPath).isDirectory()) { |
| | | return "The given path is not a folder" |
| | | } |
| | | } |
| | | }) |
| | | })) |
| | | |
| | | if (isCancel(originalFolder)) { |
| | | outro(chalk.red("Exiting")) |
| | |
| | | |
| | | await rmContentFolder() |
| | | if (setupStrategy === 'copy') { |
| | | await fs.promises.cp(originalFolder, contentFolder) |
| | | await fs.promises.cp(originalFolder, contentFolder, { recursive: true }) |
| | | } else if (setupStrategy === 'symlink') { |
| | | await fs.promises.symlink(originalFolder, contentFolder) |
| | | await fs.promises.symlink(originalFolder, contentFolder, 'dir') |
| | | } |
| | | } else if (setupStrategy === 'new') { |
| | | await rmContentFolder() |
| | | await fs.promises.writeFile(path.join(contentFolder, "index.md"), |
| | | `--- |
| | | await fs.promises.mkdir(contentFolder) |
| | | await fs.promises.writeFile(path.join(contentFolder, "index.md"), |
| | | `--- |
| | | title: Welcome to Quartz |
| | | --- |
| | | |
| | |
| | | `) |
| | | }) |
| | | .command('build', 'Build Quartz into a bundle of static HTML files', BuildArgv, async (argv) => { |
| | | await esbuild.build({ |
| | | const result = await esbuild.build({ |
| | | entryPoints: [fp], |
| | | outfile: path.join("quartz", cacheFile), |
| | | bundle: true, |
| | |
| | | jsx: "automatic", |
| | | jsxImportSource: "preact", |
| | | packages: "external", |
| | | metafile: true, |
| | | sourcemap: true, |
| | | plugins: [ |
| | | sassPlugin({ |
| | | type: 'css-text', |
| | |
| | | process.exit(1) |
| | | }) |
| | | |
| | | if (argv.verbose) { |
| | | const outputFileName = 'quartz/.quartz-cache/transpiled-build.mjs' |
| | | const meta = result.metafile.outputs[outputFileName] |
| | | console.log(chalk.gray(`[debug] Successfully transpiled ${Object.keys(meta.inputs).length} files (${prettyBytes(meta.bytes)})`)) |
| | | } |
| | | |
| | | const { default: init } = await import(cacheFile) |
| | | init(argv, version) |
| | | }) |