diff options
author | 2021-04-27 15:59:23 -0500 | |
---|---|---|
committer | 2021-04-27 15:59:23 -0500 | |
commit | 61ccd59f928ac730c8bfd8c17dc60df021e81906 (patch) | |
tree | 218ecc7bd9cb9675477a7dac3ad00558ac71eb04 /create-astro/index.js | |
parent | 9c980a1017111734c534f1fd0513a14fb1b752ea (diff) | |
download | astro-61ccd59f928ac730c8bfd8c17dc60df021e81906.tar.gz astro-61ccd59f928ac730c8bfd8c17dc60df021e81906.tar.zst astro-61ccd59f928ac730c8bfd8c17dc60df021e81906.zip |
Minimal `create-astro` CLI (#136)
* feat: add minimal create-astro CLI
* docs: update readme
Diffstat (limited to 'create-astro/index.js')
-rw-r--r-- | create-astro/index.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/create-astro/index.js b/create-astro/index.js new file mode 100644 index 000000000..cb3823d2e --- /dev/null +++ b/create-astro/index.js @@ -0,0 +1,47 @@ +import * as fs from 'fs'; +import { resolve } from 'path'; +import decompress from 'decompress'; +import { fileURLToPath, URL } from 'url'; +import { join } from 'node:path'; + +const log = (...args) => console.log(' ', ...args); +export default async function createAstro(argv) { + const [name] = argv.slice(2); + const templateRoot = fileURLToPath(new URL('../create-astro/templates', import.meta.url)); + if (!name) { + log(); + log(`npm init astro <dest>`); + log(`Provide a destination!`); + process.exit(0); + } + + log(); + const dest = resolve(process.cwd(), name); + const relDest = `./${name}`; + if (isEmpty(relDest)) { + await decompress(fs.readFileSync(join(templateRoot, 'starter.tar.gz')), dest); + log(`Your Astro project has been scaffolded at "${relDest}"`); + log(); + log(`Next steps:`); + log(); + log(` cd ${relDest}`); + log(` npm install`); + log(` npm run start`); + } +} + +function isEmpty(path) { + try { + const files = fs.readdirSync(resolve(process.cwd(), path)); + if (files.length > 0) { + log(`It looks like "${path}" isn't empty!`); + return false; + } else { + log(`Scaffolding Astro project at "${path}"`); + return true; + } + } catch (err) { + if (err.code !== 'ENOENT') throw err; + } + return true; +} |