Skip to content

Commit

Permalink
chore: update starter cli outdir option (QwikDev#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley authored Sep 14, 2022
1 parent 0192056 commit fbcf1de
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 50 deletions.
40 changes: 13 additions & 27 deletions packages/create-qwik/create-app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import fs from 'fs';
import { join, resolve } from 'path';
import { cleanPackageJson, panic, toDashCase, writePackageJson } from '../qwik/src/cli/utils/utils';
import { isAbsolute, join, resolve } from 'path';
import { cleanPackageJson, panic, writePackageJson } from '../qwik/src/cli/utils/utils';
import { loadIntegrations } from '../qwik/src/cli/utils/integrations';
import { logCreateAppResult } from '../qwik/src/cli/utils/log';
import { updateApp } from '../qwik/src/cli/add/update-app';
Expand All @@ -12,16 +12,13 @@ import type {
IntegrationPackageJson,
} from '../qwik/src/cli/types';

export async function runCreateCli(starterId: string, projectName: string) {
const outDirName = createOutDirName(projectName);
let outDir: string;

export async function runCreateCli(starterId: string, outDir: string) {
if (writeToCwd()) {
// write to the current working directory
outDir = process.cwd();
} else {
// create a sub directory
outDir = createOutDir(outDirName);
outDir = getOutDir(outDir);
if (fs.existsSync(outDir)) {
panic(
`Directory "${outDir}" already exists. Please either remove this directory, or choose another location.`
Expand All @@ -31,7 +28,6 @@ export async function runCreateCli(starterId: string, projectName: string) {

const opts: CreateAppOptions = {
starterId,
projectName,
outDir,
};

Expand All @@ -43,21 +39,20 @@ export async function runCreateCli(starterId: string, projectName: string) {
}

export async function createApp(opts: CreateAppOptions) {
if (!isValidOption(opts.projectName)) {
throw new Error(`Missing project name`);
}
if (!isValidOption(opts.starterId)) {
throw new Error(`Missing starter id`);
}
if (!isValidOption(opts.outDir)) {
throw new Error(`Missing outDir`);
}
if (!isAbsolute(opts.outDir)) {
throw new Error(`outDir must be an absolute path`);
}
if (!fs.existsSync(opts.outDir)) {
fs.mkdirSync(opts.outDir, { recursive: true });
}

const result: CreateAppResult = {
projectName: opts.projectName,
starterId: opts.starterId,
outDir: opts.outDir,
};
Expand All @@ -83,12 +78,14 @@ async function createFromStarter(
starterApp: IntegrationData
) {
const appPkgJson: IntegrationPackageJson = {
name: toDashCase(result.projectName),
name: `qwik-app`,
description: starterApp.description.trim(),
private: true,
};
await writePackageJson(result.outDir, cleanPackageJson(appPkgJson));
await createReadme(baseApp, result);

const readmePath = join(result.outDir, 'README.md');
await fs.promises.writeFile(readmePath, '');

const baseUpdate = await updateApp({
rootDir: result.outDir,
Expand All @@ -105,23 +102,12 @@ async function createFromStarter(
await starterUpdate.commit(false);
}

async function createReadme(baseApp: IntegrationData, result: CreateAppResult) {
const readmeContent = `# Qwik ${result.projectName} ⚡️`;

const readmePath = join(result.outDir, 'README.md');
await fs.promises.writeFile(readmePath, readmeContent);
}

function isValidOption(value: any) {
return typeof value === 'string' && value.trim().length > 0;
}

export function createOutDirName(projectName: string) {
return projectName.toLocaleLowerCase().replace(/ /g, '-');
}

export function createOutDir(outDirName: string) {
return resolve(process.cwd(), outDirName);
export function getOutDir(outDir: string) {
return resolve(process.cwd(), outDir);
}

export function writeToCwd() {
Expand Down
24 changes: 13 additions & 11 deletions packages/create-qwik/create-interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import prompts from 'prompts';
import color from 'kleur';
import type { CreateAppOptions } from '../qwik/src/cli/types';
import { backgroundInstallDeps } from '../qwik/src/cli/utils/install-deps';
import { createOutDir, createOutDirName, createApp } from './create-app';
import { createApp, getOutDir } from './create-app';
import { getPackageManager } from '../qwik/src/cli/utils/utils';
import { logCreateAppResult } from '../qwik/src/cli/utils/log';
import { loadIntegrations } from '../qwik/src/cli/utils/integrations';
Expand All @@ -14,7 +14,11 @@ export async function runCreateInteractiveCli() {
console.log(``);
console.clear();

console.log(`💫 ${color.cyan(`Let's create a Qwik app`)} 💫`);
console.log(
`💫 ${color.cyan(`Let's create a Qwik app`)} 💫 ${color.dim(
`v${(globalThis as any).QWIK_VERSION}`
)}`
);
console.log(``);

const pkgManager = getPackageManager();
Expand All @@ -29,9 +33,9 @@ export async function runCreateInteractiveCli() {
const projectNameAnswer = await prompts(
{
type: 'text',
name: 'projectName',
message: 'Project name',
initial: 'qwik-app',
name: 'outDir',
message: 'Where would you like to create your new project?',
initial: './qwik-app',
},
{
onCancel: () => {
Expand All @@ -42,9 +46,8 @@ export async function runCreateInteractiveCli() {
);
console.log(``);

const projectName: string = projectNameAnswer.projectName;
const outDirName = createOutDirName(projectName);
const outDir = createOutDir(outDirName);
const outDir: string = getOutDir(projectNameAnswer.outDir);

let removeExistingOutDirPromise: Promise<void> | null = null;

if (fs.existsSync(outDir)) {
Expand All @@ -64,7 +67,7 @@ export async function runCreateInteractiveCli() {
},
{
onCancel: async () => {
console.log('\n' + color.dim(` - Exited without modifying "${outDir}"`) + '\n');
console.log(color.dim(` - Exited without modifying "${outDir}"`) + '\n');
await backgroundInstall.abort();
process.exit(1);
},
Expand All @@ -74,7 +77,7 @@ export async function runCreateInteractiveCli() {
if (existingOutDirAnswer.outDirChoice === 'replace') {
removeExistingOutDirPromise = fs.promises.rm(outDir, { recursive: true });
} else {
console.log('\n' + color.dim(` - Exited without modifying "${outDir}"`) + '\n');
console.log(color.dim(` - Exited without modifying "${outDir}"`) + '\n');
await backgroundInstall.abort();
process.exit(1);
}
Expand Down Expand Up @@ -126,7 +129,6 @@ export async function runCreateInteractiveCli() {

const opts: CreateAppOptions = {
starterId,
projectName,
outDir,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/cli/add/update-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function updateApp(opts: UpdateAppOptions) {

await mergeIntegrationDir(fileUpdates, opts, integration.dir, opts.rootDir);

if ((globalThis as any).codemod) {
if ((globalThis as any).CODE_MOD) {
await updateViteConfigs(fileUpdates, integration, opts.rootDir);
}

Expand Down
1 change: 0 additions & 1 deletion packages/qwik/src/cli/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AppCommand } from './utils/app-command';

export interface CreateAppOptions {
projectName: string;
starterId: string;
outDir: string;
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/create-qwik-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ async function bundleCreateQwikCli(config: BuildConfig, srcCliDir: string, distC
],
external: ['prettier', 'typescript'],
define: {
'globalThis.codemod': 'false',
'globalThis.CODE_MOD': 'false',
'globalThis.QWIK_VERSION': JSON.stringify(config.distVersion),
},
banner: {
js: getBanner(PACKAGE, config.distVersion),
Expand Down
2 changes: 1 addition & 1 deletion scripts/submodule-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export async function submoduleCli(config: BuildConfig) {
],
external: ['prettier', 'typescript'],
define: {
'globalThis.CODE_MOD': 'true',
'globalThis.QWIK_VERSION': JSON.stringify(config.distVersion),
'globalThis.codemod': 'true',
},
});

Expand Down
12 changes: 4 additions & 8 deletions scripts/validate-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,23 @@ async function validateStarter(
app: boolean,
emoji: string
) {
const projectName = starterId;
const appDir = join(distDir, 'e2e-' + projectName);
const appDir = join(distDir, 'e2e-' + starterId);

console.log(`${emoji} ${projectName}: ${appDir}`);
console.log(`${emoji} ${appDir}`);
rmSync(appDir, { force: true, recursive: true });

const result = await api.createApp({
projectName,
starterId,
outDir: appDir,
});

assert.strictEqual(result.projectName, projectName);
assert.strictEqual(result.starterId, starterId);
assert.strictEqual(result.outDir, appDir);

accessSync(result.outDir);

const appPkgJsonPath = join(result.outDir, 'package.json');
const appPkgJson = JSON.parse(readFileSync(appPkgJsonPath, 'utf-8'));
assert.strictEqual(appPkgJson.name, projectName.toLowerCase());

appPkgJson.devDependencies['@builder.io/qwik'] = 'latest';
writeFileSync(appPkgJsonPath, JSON.stringify(appPkgJson, null, 2));
Expand All @@ -90,7 +86,7 @@ async function validateStarter(
accessSync(tsconfigPath);

const { execa } = await import('execa');
console.log(`${emoji} ${projectName}: npm install`);
console.log(`${emoji} ${starterId}: npm install`);
await execa('npm', ['install'], { cwd: appDir, stdout: 'inherit' });

// console.log(`${emoji} ${projectName}: copy @builder.io/qwik distribution`);
Expand Down Expand Up @@ -140,7 +136,7 @@ async function validateStarter(
// accessSync(join(appDir, 'tsconfig.json'));
// accessSync(join(appDir, 'tsconfig.tsbuildinfo'));

console.log(`${emoji} ${projectName} validated\n`);
console.log(`${emoji} ${starterId} validated\n`);
}

function cpSync(src: string, dest: string) {
Expand Down
2 changes: 2 additions & 0 deletions starters/apps/base/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Qwik App ⚡️

- [Qwik Docs](https://qwik.builder.io/)
- [Discord](https://qwik.builder.io/chat)
- [Qwik Github](https://github.com/BuilderIO/qwik)
Expand Down

0 comments on commit fbcf1de

Please sign in to comment.