Skip to content

Commit

Permalink
Merge pull request dev-next-release #1113
Browse files Browse the repository at this point in the history
  • Loading branch information
xrutayisire authored Aug 23, 2023
2 parents aed2181 + d823660 commit c8af168
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 81 deletions.
2 changes: 1 addition & 1 deletion e2e-projects/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cimsirp",
"version": "1.9.0",
"version": "1.9.1-dev-next-release.2",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"prettier:fix": "prettier --write .",
"prettier:check": "prettier --check .",
"test": "yarn workspaces foreach --parallel --topological-dev --verbose run test",
"test:e2e": "yarn cypress-setup && start-server-and-test 'npm run dev --prefix e2e-projects/cypress-next-app' http://localhost:3000 'SM_ENV=development ENABLE_SENTRY=false npm run slicemachine --prefix e2e-projects/cypress-next-app -- --no-open' http://localhost:9999 'cypress run'",
"test:e2e:dev": "yarn cypress-setup && start-server-and-test 'npm run dev --prefix e2e-projects/cypress-next-app' http://localhost:3000 'SM_ENV=staging ENABLE_SENTRY=false npm run slicemachine --prefix e2e-projects/cypress-next-app -- --no-open' http://localhost:9999 'cypress open'",
"test:e2e": "yarn cypress-setup && start-server-and-test 'npm run dev --prefix e2e-projects/cypress-next-app' http://localhost:3000 'SM_ENV=development ENABLE_SENTRY=false npm run slicemachine --prefix e2e-projects/cypress-next-app' http://localhost:9999 'cypress run'",
"test:e2e:dev": "yarn cypress-setup && start-server-and-test 'npm run dev --prefix e2e-projects/cypress-next-app' http://localhost:3000 'SM_ENV=staging ENABLE_SENTRY=false npm run slicemachine --prefix e2e-projects/cypress-next-app' http://localhost:9999 'cypress open'",
"cy:open": "cypress open",
"bump:interactive": "lerna version prerelease --preid alpha --no-push --exact",
"bump:alpha": "lerna version prerelease --preid $npm_config_preid --no-changelog --exact --yes",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slicemachine/adapter-next",
"version": "0.3.11",
"version": "0.3.12-dev-next-release.2",
"description": "Slice Machine adapter for Next.js.",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-nuxt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slicemachine/adapter-nuxt",
"version": "0.3.11",
"version": "0.3.12-dev-next-release.2",
"description": "Slice Machine adapter for Nuxt 3.",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-nuxt2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slicemachine/adapter-nuxt2",
"version": "0.3.11",
"version": "0.3.12-dev-next-release.2",
"description": "Slice Machine adapter for Nuxt 2.",
"keywords": [
"typescript",
Expand Down
3 changes: 2 additions & 1 deletion packages/init/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slicemachine/init",
"version": "2.4.3",
"version": "2.4.4-dev-next-release.2",
"description": "Init Prismic Slice Machine in your project",
"keywords": [
"typescript",
Expand Down Expand Up @@ -55,6 +55,7 @@
"@lihbr/listr-update-renderer": "^0.5.3",
"@slicemachine/manager": "workspace:^",
"chalk": "^4.1.2",
"giget": "^1.1.2",
"globby": "^13.1.3",
"listr": "^0.14.3",
"log-symbols": "^4.1.0",
Expand Down
213 changes: 167 additions & 46 deletions packages/init/src/SliceMachineInitProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { execaCommand, type ExecaChildProcess } from "execa";
import open from "open";
import logSymbols from "log-symbols";
import { globby } from "globby";
import { downloadTemplate } from "giget";

import {
createSliceMachineManager,
Expand All @@ -32,11 +33,18 @@ import {
import { listr, listrRun } from "./lib/listr";
import { prompt } from "./lib/prompt";
import { assertExists } from "./lib/assertExists";
import { START_SCRIPT_KEY, START_SCRIPT_VALUE } from "./constants";
import {
GIGET_ORGANIZATION,
GIGET_PROVIDER,
START_SCRIPT_KEY,
START_SCRIPT_VALUE,
} from "./constants";
import { detectStarterId } from "./lib/starters";

export type SliceMachineInitProcessOptions = {
repository?: string;
starter?: string;
directoryName?: string;
push?: boolean;
pushSlices?: boolean;
pushCustomTypes?: boolean;
Expand Down Expand Up @@ -94,30 +102,40 @@ export class SliceMachineInitProcess {
console.log(
`\n${chalk.bgGray(` ${chalk.bold.white("Slice Machine")} `)} ${chalk.dim(
"→",
)} Init command started\n`,
)} Initializing\n`,
);

if (await this.manager.telemetry.checkIsTelemetryEnabled()) {
// We prefer to manually allow console logs despite the app being a CLI to catch wild/unwanted console logs better
// eslint-disable-next-line no-console
console.log(
`${
logSymbols.info
} We collect telemetry data to improve user experience.\n Learn more: ${chalk.cyan(
"https://prismic.dev/slice-machine/telemetry",
)}\n`,
);
}
await this.manager.telemetry.initTelemetry({
appName: pkg.name,
appVersion: pkg.version,
});
await this.manager.telemetry.track({
event: "command:init:start",
repository: this.options.repository,
});

try {
if (this.options.starter) {
await this.copyStarter();
} else if (this.options.directoryName) {
// We prefer to manually allow console logs despite the app being a CLI to catch wild/unwanted console logs better
// eslint-disable-next-line no-console
console.log(
`${logSymbols.warning} --directory-name has no effect because --starter is not specified\n`,
);
}

if (await this.manager.telemetry.checkIsTelemetryEnabled()) {
// We prefer to manually allow console logs despite the app being a CLI to catch wild/unwanted console logs better
// eslint-disable-next-line no-console
console.log(
`${
logSymbols.info
} We collect telemetry data to improve user experience.\n Learn more: ${chalk.cyan(
"https://prismic.dev/slice-machine/telemetry",
)}\n`,
);
}
await this.manager.telemetry.initTelemetry({
appName: pkg.name,
appVersion: pkg.version,
});
await this.manager.telemetry.track({
event: "command:init:start",
repository: this.options.repository,
});

await this.detectEnvironment();

assertExists(
Expand Down Expand Up @@ -174,7 +192,7 @@ export class SliceMachineInitProcess {
console.log(
`\n${chalk.bgGreen(` ${chalk.bold.white("Slice Machine")} `)} ${chalk.dim(
"→",
)} Init command successful!`,
)} Initialization successful!`,
);

try {
Expand All @@ -193,15 +211,49 @@ export class SliceMachineInitProcess {
script: "dev",
});

const apiEndpoints = this.manager.getAPIEndpoints();
const wroomHost = new URL(apiEndpoints.PrismicWroom).host;

const dashboardURL = new URL(
`https://${this.context.repository.domain}.${wroomHost}`,
)
.toString()
.replace(/\/$/, "");
const apiURL = new URL(
"./api/v2",
`https://${this.context.repository.domain}.cdn.${wroomHost}`,
).toString();

// We prefer to manually allow console logs despite the app being a CLI to catch wild/unwanted console logs better
// eslint-disable-next-line no-console
console.log(`
GETTING STARTED
Run Slice Machine ${chalk.cyan(runSmCommand)}
Run your project ${chalk.cyan(runProjectCommand)}
YOUR REPOSITORY
Page Builder ${chalk.cyan(dashboardURL)}
API ${chalk.cyan(apiURL)}
RESOURCES
Documentation ${chalk.cyan(
this.context.framework.prismicDocumentation,
)}
Getting help ${chalk.cyan("https://community.prismic.io")}
GETTING STARTED
Run Slice Machine ${chalk.cyan(runSmCommand)}
Run your project ${chalk.cyan(runProjectCommand)}
`);

if (this.options.startSlicemachine) {
if (this.options.starter && this.options.repository) {
const { openDashboard } = await prompt<boolean, "openDashboard">({
type: "confirm",
name: "openDashboard",
message: "Would you like to open your repository?",
initial: true,
});

if (openDashboard) {
open(dashboardURL);
}
} else if (this.options.startSlicemachine) {
const pkgJSONPath = path.join(this.manager.cwd, "package.json");
const pkg = JSON.parse(await fs.readFile(pkgJSONPath, "utf-8"));
const scripts = pkg.scripts || {};
Expand All @@ -214,13 +266,13 @@ GETTING STARTED
) {
return {
command: runProjectCommand,
message: `Would you like to launch your project + Slicemachine (${runProjectCommand})?`,
message: `Would you like to run your project and Slice Machine concurrently (${runProjectCommand})?`,
};
}

return {
command: runSmCommand,
message: `Would you like to launch Slicemachine (${runSmCommand})?`,
message: `Would you like to run Slice Machine (${runSmCommand})?`,
};
})();
const { startSlicemachine } = await prompt<
Expand Down Expand Up @@ -291,6 +343,74 @@ GETTING STARTED
});
};

protected async copyStarter(): Promise<void> {
const dir = await this.getStarterDirectoryName();

return listrRun([
{
title: "Copying starter...\n",
task: async (_, task) => {
const starter = this.options.starter;

await downloadTemplate(
`${GIGET_PROVIDER}:${GIGET_ORGANIZATION}/${starter}#HEAD`,
{
dir,
},
);

process.chdir(dir);
this.manager.cwd = process.cwd();

task.title = "Starter copied\n";
},
},
]);
}

protected async getStarterDirectoryName(): Promise<string> {
let directoryName: string;
const folderNames = await fs.readdir(process.cwd());

if (
this.options.directoryName &&
!folderNames.includes(this.options.directoryName)
) {
// Use provided directory
directoryName = this.options.directoryName;
} else if (
this.options.repository &&
!folderNames.includes(this.options.repository)
) {
// Use repository name
directoryName = this.options.repository;
} else {
// Use random name
let suggestedName: string;
do {
suggestedName = getRandomRepositoryDomain();
} while (folderNames.includes(suggestedName));

const { selectedDirectory } = await prompt<string, "selectedDirectory">({
type: "text",
name: "selectedDirectory",
message: "Your starter directory name",
initial: suggestedName,
validate: async (rawDirectory: string) => {
if (folderNames.includes(rawDirectory)) {
return "Directory name already exists";
}

return true;
},
});

directoryName = selectedDirectory;
}

return directoryName;
}

protected detectEnvironment(): Promise<void> {
return listrRun([
{
Expand Down Expand Up @@ -473,6 +593,7 @@ GETTING STARTED
await new Promise((resolve) => {
const initialRawMode = !!process.stdin.isRaw;
process.stdin.setRawMode?.(true);
process.stdin.resume();
process.stdin.once("data", (data: Buffer) => {
process.stdin.setRawMode?.(initialRawMode);
process.stdin.pause();
Expand Down Expand Up @@ -654,26 +775,26 @@ GETTING STARTED
}
};

// 1. Try to suggest name after package name
try {
const pkgJSONPath = path.join(this.manager.cwd, "package.json");
const pkg = JSON.parse(await fs.readFile(pkgJSONPath, "utf-8"));

const maybeSuggestion = await trySuggestName(pkg.name);
if (maybeSuggestion) {
suggestedName = maybeSuggestion;
}
} catch {
// Noop
// 1. Try to suggest name after directory name
const maybeSuggestion = await trySuggestName(
path.basename(this.manager.cwd),
);
if (maybeSuggestion) {
suggestedName = maybeSuggestion;
}

// 2. Try to suggest name after directory name
// 2. Try to suggest name after package name
if (!suggestedName) {
const maybeSuggestion = await trySuggestName(
path.basename(this.manager.cwd),
);
if (maybeSuggestion) {
suggestedName = maybeSuggestion;
try {
const pkgJSONPath = path.join(this.manager.cwd, "package.json");
const pkg = JSON.parse(await fs.readFile(pkgJSONPath, "utf-8"));

const maybeSuggestion = await trySuggestName(pkg.name);
if (maybeSuggestion) {
suggestedName = maybeSuggestion;
}
} catch {
// Noop
}
}

Expand Down
Loading

0 comments on commit c8af168

Please sign in to comment.