2021-06-08 15:10:56 +00:00
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import { bold, cyan, gray, green, red } from 'kleur/colors';
|
2021-07-07 19:52:44 +00:00
|
|
|
|
import fetch from 'node-fetch';
|
2021-06-08 15:10:56 +00:00
|
|
|
|
import prompts from 'prompts';
|
|
|
|
|
import degit from 'degit';
|
|
|
|
|
import yargs from 'yargs-parser';
|
2021-07-07 19:52:44 +00:00
|
|
|
|
import { FRAMEWORKS, COUNTER_COMPONENTS } from './frameworks.js';
|
2021-06-08 16:56:37 +00:00
|
|
|
|
import { TEMPLATES } from './templates.js';
|
2021-07-07 19:52:44 +00:00
|
|
|
|
import { createConfig } from './config.js';
|
2021-06-08 15:10:56 +00:00
|
|
|
|
const args = yargs(process.argv);
|
|
|
|
|
prompts.override(args);
|
|
|
|
|
|
|
|
|
|
export function mkdirp(dir: string) {
|
2021-06-08 15:12:07 +00:00
|
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e.code === 'EEXIST') return;
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2021-06-08 15:10:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { version } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
|
|
|
|
|
2021-07-07 19:52:44 +00:00
|
|
|
|
const POSTPROCESS_FILES = ['package.json', 'astro.config.mjs', 'CHANGELOG.md']; // some files need processing after copying.
|
2021-06-10 16:30:48 +00:00
|
|
|
|
|
2021-06-08 15:10:56 +00:00
|
|
|
|
export async function main() {
|
2021-06-08 15:12:07 +00:00
|
|
|
|
console.log('\n' + bold('Welcome to Astro!') + gray(` (create-astro v${version})`));
|
2021-06-22 14:06:07 +00:00
|
|
|
|
console.log(`If you encounter a problem, visit ${cyan('https://github.com/snowpackjs/astro/issues')} to search or file a new issue.\n`);
|
2021-06-08 15:12:07 +00:00
|
|
|
|
|
|
|
|
|
console.log(green(`>`) + gray(` Prepare for liftoff.`));
|
|
|
|
|
console.log(green(`>`) + gray(` Gathering mission details...`));
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-06-08 15:12:07 +00:00
|
|
|
|
const cwd = args['_'][2] || '.';
|
|
|
|
|
if (fs.existsSync(cwd)) {
|
|
|
|
|
if (fs.readdirSync(cwd).length > 0) {
|
|
|
|
|
const response = await prompts({
|
|
|
|
|
type: 'confirm',
|
|
|
|
|
name: 'forceOverwrite',
|
|
|
|
|
message: 'Directory not empty. Continue?',
|
|
|
|
|
initial: false,
|
|
|
|
|
});
|
|
|
|
|
if (!response.forceOverwrite) {
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2021-07-07 19:52:44 +00:00
|
|
|
|
|
|
|
|
|
await fs.promises.rm(cwd, { recursive: true });
|
|
|
|
|
mkdirp(cwd);
|
2021-06-08 15:12:07 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mkdirp(cwd);
|
|
|
|
|
}
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-06-08 15:12:07 +00:00
|
|
|
|
const options = /** @type {import('./types/internal').Options} */ await prompts([
|
|
|
|
|
{
|
|
|
|
|
type: 'select',
|
|
|
|
|
name: 'template',
|
|
|
|
|
message: 'Which app template would you like to use?',
|
|
|
|
|
choices: TEMPLATES,
|
|
|
|
|
},
|
|
|
|
|
]);
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-07-07 19:52:44 +00:00
|
|
|
|
if (!options.template) {
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 16:30:48 +00:00
|
|
|
|
const hash = args.commit ? `#${args.commit}` : '';
|
2021-07-06 19:14:22 +00:00
|
|
|
|
|
2021-07-07 20:10:09 +00:00
|
|
|
|
const templateTarget = options.template.includes('/') ? options.template : `snowpackjs/astro/examples/${options.template}`;
|
2021-07-06 19:14:22 +00:00
|
|
|
|
|
|
|
|
|
const emitter = degit(`${templateTarget}${hash}`, {
|
2021-06-08 15:12:07 +00:00
|
|
|
|
cache: false,
|
|
|
|
|
force: true,
|
|
|
|
|
verbose: false,
|
|
|
|
|
});
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-07-07 20:10:09 +00:00
|
|
|
|
const selectedTemplate = TEMPLATES.find((template) => template.value === options.template);
|
2021-07-07 19:52:44 +00:00
|
|
|
|
let renderers: string[] = [];
|
2021-07-07 20:10:09 +00:00
|
|
|
|
|
2021-07-07 19:52:44 +00:00
|
|
|
|
if (selectedTemplate?.renderers === true) {
|
|
|
|
|
const result = /** @type {import('./types/internal').Options} */ await prompts([
|
|
|
|
|
{
|
|
|
|
|
type: 'multiselect',
|
|
|
|
|
name: 'renderers',
|
|
|
|
|
message: 'Which frameworks would you like to use?',
|
|
|
|
|
choices: FRAMEWORKS,
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
renderers = result.renderers;
|
|
|
|
|
} else if (selectedTemplate?.renderers && Array.isArray(selectedTemplate.renderers)) {
|
|
|
|
|
renderers = selectedTemplate.renderers;
|
2021-07-07 20:10:09 +00:00
|
|
|
|
const titles = renderers.map((renderer) => FRAMEWORKS.find((item) => item.value === renderer)?.title).join(', ');
|
2021-07-07 19:52:44 +00:00
|
|
|
|
console.log(green(`✔`) + bold(` Using template's default renderers`) + gray(' › ') + titles);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 16:30:48 +00:00
|
|
|
|
// Copy
|
2021-06-08 15:12:07 +00:00
|
|
|
|
try {
|
|
|
|
|
// emitter.on('info', info => { console.log(info.message) });
|
2021-06-08 16:56:37 +00:00
|
|
|
|
console.log(green(`>`) + gray(` Copying project files...`));
|
2021-06-08 15:12:07 +00:00
|
|
|
|
await emitter.clone(cwd);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// degit is compiled, so the stacktrace is pretty noisy. Just report the message.
|
|
|
|
|
console.error(red(err.message));
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-06-10 16:30:48 +00:00
|
|
|
|
// Post-process in parallel
|
2021-07-07 20:10:09 +00:00
|
|
|
|
await Promise.all(
|
|
|
|
|
POSTPROCESS_FILES.map(async (file) => {
|
|
|
|
|
const fileLoc = path.resolve(path.join(cwd, file));
|
|
|
|
|
|
|
|
|
|
switch (file) {
|
|
|
|
|
case 'CHANGELOG.md': {
|
|
|
|
|
if (fs.existsSync(fileLoc)) {
|
|
|
|
|
await fs.promises.rm(fileLoc);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-07-07 19:52:44 +00:00
|
|
|
|
}
|
2021-07-07 20:10:09 +00:00
|
|
|
|
case 'astro.config.mjs': {
|
|
|
|
|
if (selectedTemplate?.renderers !== true) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
await fs.promises.writeFile(fileLoc, createConfig({ renderers }));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'package.json': {
|
|
|
|
|
const packageJSON = JSON.parse(await fs.promises.readFile(fileLoc, 'utf8'));
|
|
|
|
|
delete packageJSON.snowpack; // delete snowpack config only needed in monorepo (can mess up projects)
|
|
|
|
|
// Fetch latest versions of selected renderers
|
|
|
|
|
const rendererEntries = (await Promise.all(
|
|
|
|
|
['astro', ...renderers].map((renderer: string) =>
|
|
|
|
|
fetch(`https://registry.npmjs.org/${renderer}/latest`)
|
|
|
|
|
.then((res: any) => res.json())
|
|
|
|
|
.then((res: any) => [renderer, `^${res['version']}`])
|
|
|
|
|
)
|
|
|
|
|
)) as any;
|
|
|
|
|
packageJSON.devDependencies = { ...(packageJSON.devDependencies ?? {}), ...Object.fromEntries(rendererEntries) };
|
|
|
|
|
await fs.promises.writeFile(fileLoc, JSON.stringify(packageJSON, undefined, 2));
|
2021-06-10 16:30:48 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-07-07 19:52:44 +00:00
|
|
|
|
}
|
2021-07-07 20:10:09 +00:00
|
|
|
|
})
|
|
|
|
|
);
|
2021-07-07 19:52:44 +00:00
|
|
|
|
|
|
|
|
|
// Inject framework components into starter template
|
|
|
|
|
if (selectedTemplate?.value === 'starter') {
|
|
|
|
|
let importStatements: string[] = [];
|
|
|
|
|
let components: string[] = [];
|
2021-07-07 20:10:09 +00:00
|
|
|
|
await Promise.all(
|
|
|
|
|
renderers.map(async (renderer) => {
|
|
|
|
|
const component = COUNTER_COMPONENTS[renderer as keyof typeof COUNTER_COMPONENTS];
|
|
|
|
|
const componentName = path.basename(component.filename, path.extname(component.filename));
|
|
|
|
|
const absFileLoc = path.resolve(cwd, component.filename);
|
|
|
|
|
importStatements.push(`import ${componentName} from '${component.filename.replace(/^src/, '..')}';`);
|
2021-07-08 18:31:05 +00:00
|
|
|
|
components.push(`<${componentName} client:visible />`);
|
2021-07-07 20:10:09 +00:00
|
|
|
|
await fs.promises.writeFile(absFileLoc, component.content);
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-07-07 19:52:44 +00:00
|
|
|
|
|
|
|
|
|
const pageFileLoc = path.resolve(path.join(cwd, 'src', 'pages', 'index.astro'));
|
|
|
|
|
const content = (await fs.promises.readFile(pageFileLoc)).toString();
|
2021-07-07 21:31:42 +00:00
|
|
|
|
const newContent = content
|
|
|
|
|
.replace(/^(\s*)\/\* ASTRO\:COMPONENT_IMPORTS \*\//gm, (_, indent) => {
|
|
|
|
|
return indent + importStatements.join('\n');
|
|
|
|
|
})
|
|
|
|
|
.replace(/^(\s*)<!-- ASTRO:COMPONENT_MARKUP -->/gm, (_, indent) => {
|
2021-07-07 21:32:45 +00:00
|
|
|
|
return components.map((ln) => indent + ln).join('\n');
|
2021-07-07 21:31:42 +00:00
|
|
|
|
});
|
|
|
|
|
await fs.promises.writeFile(pageFileLoc, newContent);
|
2021-07-07 19:52:44 +00:00
|
|
|
|
}
|
2021-06-10 16:30:48 +00:00
|
|
|
|
|
2021-06-22 14:06:07 +00:00
|
|
|
|
console.log(bold(green('✔') + ' Done!'));
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-06-08 15:12:07 +00:00
|
|
|
|
console.log('\nNext steps:');
|
|
|
|
|
let i = 1;
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-06-08 15:12:07 +00:00
|
|
|
|
const relative = path.relative(process.cwd(), cwd);
|
|
|
|
|
if (relative !== '') {
|
|
|
|
|
console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`);
|
|
|
|
|
}
|
2021-06-08 15:10:56 +00:00
|
|
|
|
|
2021-06-08 15:12:07 +00:00
|
|
|
|
console.log(` ${i++}: ${bold(cyan('npm install'))} (or pnpm install, yarn, etc)`);
|
|
|
|
|
console.log(` ${i++}: ${bold(cyan('git init && git add -A && git commit -m "Initial commit"'))} (optional step)`);
|
|
|
|
|
console.log(` ${i++}: ${bold(cyan('npm start'))} (or pnpm, yarn, etc)`);
|
|
|
|
|
|
|
|
|
|
console.log(`\nTo close the dev server, hit ${bold(cyan('Ctrl-C'))}`);
|
|
|
|
|
console.log('\nStuck? Visit us at https://astro.build/chat\n');
|
|
|
|
|
}
|