0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Implements templates from external repos (#603)

* Implements templates from external repos

* Adds a changeset
This commit is contained in:
Matthew Phillips 2021-07-06 15:14:22 -04:00 committed by GitHub
parent 2ab625bee8
commit d8ceff5fac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 6 deletions

View file

@ -0,0 +1,11 @@
---
'create-astro': patch
---
Allows using an external repo as a template
You can do this with the `--template` flag:
```bash
npm init astro my-shopify --template cassidoo/shopify-react-astro
```

View file

@ -28,6 +28,12 @@ yarn create astro my-astro-project --template starter
``` ```
[Check out the full list][examples] of example starter templates, available on GitHub. [Check out the full list][examples] of example starter templates, available on GitHub.
You can also use any GitHub repo as a template:
```bash
npm init astro my-astro-project -- --template cassidoo/shopify-react-astro
```
### CLI Flags ### CLI Flags
May be provided in place of prompts May be provided in place of prompts

View file

@ -55,7 +55,12 @@ export async function main() {
]); ]);
const hash = args.commit ? `#${args.commit}` : ''; const hash = args.commit ? `#${args.commit}` : '';
const emitter = degit(`snowpackjs/astro/examples/${options.template}${hash}`, {
const templateTarget = options.template.includes('/') ?
options.template :
`snowpackjs/astro/examples/${options.template}`;
const emitter = degit(`${templateTarget}${hash}`, {
cache: false, cache: false,
force: true, force: true,
verbose: false, verbose: false,

View file

@ -1,15 +1,11 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { fileURLToPath } from 'url';
import http from 'http'; import http from 'http';
import { green, red } from 'kleur/colors'; import { green, red } from 'kleur/colors';
import execa from 'execa'; import execa from 'execa';
import glob from 'tiny-glob'; import glob from 'tiny-glob';
import { TEMPLATES } from '../dist/templates.js'; import { TEMPLATES } from '../dist/templates.js';
import { GITHUB_SHA, FIXTURES_DIR } from './helpers.js';
// config
const GITHUB_SHA = process.env.GITHUB_SHA || execa.sync('git', ['rev-parse', 'HEAD']).stdout; // process.env.GITHUB_SHA will be set in CI; if testing locally execa() will gather this
const FIXTURES_DIR = path.join(fileURLToPath(path.dirname(import.meta.url)), 'fixtures');
// helpers // helpers
async function fetch(url) { async function fetch(url) {

View file

@ -0,0 +1,29 @@
import assert from 'assert';
import execa from 'execa';
import { FIXTURES_URL } from './helpers.js';
import { existsSync } from 'fs';
async function run(outdir, template) {
//--template cassidoo/shopify-react-astro
await execa('../../create-astro.mjs', [outdir, '--template', template, '--force-overwrite'], {
cwd: FIXTURES_URL.pathname,
});
}
const testCases = [
['shopify', 'cassidoo/shopify-react-astro']
];
async function tests() {
for(let [dir, tmpl] of testCases) {
await run(dir, tmpl);
const outPath = new URL('' + dir, FIXTURES_URL);
assert.ok(existsSync(outPath));
}
}
tests().catch(err => {
console.error(err);
process.exit(1);
});

View file

@ -0,0 +1,13 @@
import execa from 'execa';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
const GITHUB_SHA = process.env.GITHUB_SHA || execa.sync('git', ['rev-parse', 'HEAD']).stdout; // process.env.GITHUB_SHA will be set in CI; if testing locally execa() will gather this
const FIXTURES_DIR = path.join(fileURLToPath(path.dirname(import.meta.url)), 'fixtures');
const FIXTURES_URL = pathToFileURL(FIXTURES_DIR + '/');
export {
GITHUB_SHA,
FIXTURES_DIR,
FIXTURES_URL
};