|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// @flow |
| 4 | +/* eslint-disable no-console */ |
| 5 | + |
| 6 | +// $FlowFixMe |
| 7 | +import fs from 'fs/promises'; |
| 8 | +import {readdirSync} from 'fs'; |
| 9 | +import path from 'path'; |
| 10 | +import {spawn as _spawn} from 'child_process'; |
| 11 | +// $FlowFixMe |
| 12 | +import {parseArgs, styleText} from 'util'; |
| 13 | + |
| 14 | +const supportsEmoji = isUnicodeSupported(); |
| 15 | + |
| 16 | +// Fallback symbols for Windows from https://en.wikipedia.org/wiki/Code_page_437 |
| 17 | +const success: string = supportsEmoji ? '✨' : '√'; |
| 18 | +const error: string = supportsEmoji ? '🚨' : '×'; |
| 19 | + |
| 20 | +const {positionals} = parseArgs({ |
| 21 | + allowPositionals: true, |
| 22 | + options: {}, |
| 23 | +}); |
| 24 | + |
| 25 | +let template = positionals[0]; |
| 26 | +if (!template) { |
| 27 | + let packageManager = getCurrentPackageManager()?.name; |
| 28 | + console.error( |
| 29 | + `Usage: ${packageManager ?? 'npm'} create <template> [directory]\n`, |
| 30 | + ); |
| 31 | + printAvailableTemplates(); |
| 32 | + console.log(''); |
| 33 | + process.exit(1); |
| 34 | +} |
| 35 | + |
| 36 | +let name = positionals[1]; |
| 37 | +if (!name) { |
| 38 | + name = '.'; |
| 39 | +} |
| 40 | + |
| 41 | +install(template, name).then( |
| 42 | + () => { |
| 43 | + process.exit(0); |
| 44 | + }, |
| 45 | + err => { |
| 46 | + console.error(err); |
| 47 | + process.exit(1); |
| 48 | + }, |
| 49 | +); |
| 50 | + |
| 51 | +async function install(template: string, name: string) { |
| 52 | + let templateDir = path.join(__dirname, '..', 'templates', template); |
| 53 | + try { |
| 54 | + await fs.stat(templateDir); |
| 55 | + } catch { |
| 56 | + console.error( |
| 57 | + style(['red', 'bold'], `${error} Unknown template ${template}.\n`), |
| 58 | + ); |
| 59 | + printAvailableTemplates(); |
| 60 | + console.log(''); |
| 61 | + process.exit(1); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (name === '.') { |
| 66 | + if ((await fs.readdir(name)).length !== 0) { |
| 67 | + console.error(style(['red', 'bold'], `${error} Directory is not empty.`)); |
| 68 | + process.exit(1); |
| 69 | + return; |
| 70 | + } |
| 71 | + } else { |
| 72 | + try { |
| 73 | + await fs.stat(name); |
| 74 | + console.error(style(['red', 'bold'], `${error} ${name} already exists.`)); |
| 75 | + process.exit(1); |
| 76 | + return; |
| 77 | + } catch { |
| 78 | + // ignore |
| 79 | + } |
| 80 | + await fs.mkdir(name, {recursive: true}); |
| 81 | + } |
| 82 | + |
| 83 | + await spawn('git', ['init'], { |
| 84 | + stdio: 'inherit', |
| 85 | + cwd: name, |
| 86 | + }); |
| 87 | + |
| 88 | + await fs.cp(templateDir, name, { |
| 89 | + recursive: true, |
| 90 | + }); |
| 91 | + |
| 92 | + let packageManager = getCurrentPackageManager()?.name; |
| 93 | + switch (packageManager) { |
| 94 | + case 'yarn': |
| 95 | + await spawn('yarn', [], {cwd: name, stdio: 'inherit'}); |
| 96 | + break; |
| 97 | + case 'pnpm': |
| 98 | + await spawn('pnpm', ['install'], {cwd: name, stdio: 'inherit'}); |
| 99 | + break; |
| 100 | + case 'npm': |
| 101 | + default: |
| 102 | + await spawn( |
| 103 | + 'npm', |
| 104 | + ['install', '--legacy-peer-deps', '--no-audit', '--no-fund'], |
| 105 | + {cwd: name, stdio: 'inherit'}, |
| 106 | + ); |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + await spawn('git', ['add', '-A'], {cwd: name}); |
| 111 | + await spawn( |
| 112 | + 'git', |
| 113 | + ['commit', '--quiet', '-a', '-m', 'Initial commit from create-parcel'], |
| 114 | + { |
| 115 | + stdio: 'inherit', |
| 116 | + cwd: name, |
| 117 | + }, |
| 118 | + ); |
| 119 | + |
| 120 | + console.log(''); |
| 121 | + console.log(style(['green', 'bold'], `${success} Your new app is ready!\n`)); |
| 122 | + console.log('To get started, run the following commands:'); |
| 123 | + console.log(''); |
| 124 | + if (name !== '.') { |
| 125 | + console.log(` cd ${name}`); |
| 126 | + } |
| 127 | + console.log(` ${packageManager ?? 'npm'} start`); |
| 128 | + console.log(''); |
| 129 | +} |
| 130 | + |
| 131 | +function spawn(cmd, args, opts) { |
| 132 | + return new Promise((resolve, reject) => { |
| 133 | + let p = _spawn(cmd, args, opts); |
| 134 | + p.on('close', (code, signal) => { |
| 135 | + if (code || signal) { |
| 136 | + reject(new Error(`${cmd} failed with exit code ${code}`)); |
| 137 | + } else { |
| 138 | + resolve(); |
| 139 | + } |
| 140 | + }); |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +function getCurrentPackageManager( |
| 145 | + userAgent: ?string = process.env.npm_config_user_agent, |
| 146 | +): ?{|name: string, version: string|} { |
| 147 | + if (!userAgent) { |
| 148 | + return undefined; |
| 149 | + } |
| 150 | + |
| 151 | + const pmSpec = userAgent.split(' ')[0]; |
| 152 | + const separatorPos = pmSpec.lastIndexOf('/'); |
| 153 | + const name = pmSpec.substring(0, separatorPos); |
| 154 | + return { |
| 155 | + name: name, |
| 156 | + version: pmSpec.substring(separatorPos + 1), |
| 157 | + }; |
| 158 | +} |
| 159 | + |
| 160 | +function printAvailableTemplates() { |
| 161 | + console.error('Available templates:\n'); |
| 162 | + for (let dir of readdirSync(path.join(__dirname, '..', 'templates'))) { |
| 163 | + console.error(` • ${dir}`); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// From https://github.com/sindresorhus/is-unicode-supported/blob/8f123916d5c25a87c4f966dcc248b7ca5df2b4ca/index.js |
| 168 | +// This package is ESM-only so it has to be vendored |
| 169 | +function isUnicodeSupported() { |
| 170 | + if (process.platform !== 'win32') { |
| 171 | + return process.env.TERM !== 'linux'; // Linux console (kernel) |
| 172 | + } |
| 173 | + |
| 174 | + return ( |
| 175 | + Boolean(process.env.CI) || |
| 176 | + Boolean(process.env.WT_SESSION) || // Windows Terminal |
| 177 | + process.env.ConEmuTask === '{cmd::Cmder}' || // ConEmu and cmder |
| 178 | + process.env.TERM_PROGRAM === 'vscode' || |
| 179 | + process.env.TERM === 'xterm-256color' || |
| 180 | + process.env.TERM === 'alacritty' |
| 181 | + ); |
| 182 | +} |
| 183 | + |
| 184 | +function style(format, text) { |
| 185 | + if (styleText) { |
| 186 | + return styleText(format, text); |
| 187 | + } else { |
| 188 | + return text; |
| 189 | + } |
| 190 | +} |
0 commit comments