create-plasmic-app 0.0.36 → 0.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeNextjsHostPage = exports.makeNextjsCatchallPage = exports.makeNextjsInitPage = void 0;
3
+ exports.wrapAppRootForCodegen = exports.makeNextjsHostPage = exports.makeNextjsCatchallPage = exports.makeNextjsInitPage = void 0;
4
4
  const file_utils_1 = require("../utils/file-utils");
5
5
  const makeNextjsInitPage = (projectId, projectApiToken) => `
6
6
  import { initPlasmicLoader } from "@plasmicapp/loader-nextjs";
@@ -95,14 +95,18 @@ export const getStaticPaths${file_utils_1.ifTs(ts, `: GetStaticPaths`)} = async
95
95
  `.trim();
96
96
  }
97
97
  exports.makeNextjsCatchallPage = makeNextjsCatchallPage;
98
- function makeNextjsHostPage() {
99
- return `
98
+ function makeNextjsHostPage(scheme) {
99
+ const commonImports = `
100
100
  import * as React from 'react';
101
- import { PlasmicCanvasHost } from '@plasmicapp/loader-nextjs';
102
101
  import Script from 'next/script';
102
+ `.trim();
103
+ if (scheme === "loader") {
104
+ return `
105
+ ${commonImports}
106
+ import { PlasmicCanvasHost } from '@plasmicapp/loader-nextjs';
103
107
  import { PLASMIC } from '../plasmic-init';
104
108
 
105
- export default function Host() {
109
+ export default function PlasmicHost() {
106
110
  return PLASMIC && (
107
111
  <div>
108
112
  <Script
@@ -113,6 +117,51 @@ export default function Host() {
113
117
  </div>
114
118
  );
115
119
  }
116
- `.trim();
120
+ `;
121
+ }
122
+ else {
123
+ return `
124
+ ${commonImports}
125
+ import { PlasmicCanvasHost, registerComponent } from '@plasmicapp/host';
126
+
127
+ // You can register any code components that you want to use here; see
128
+ // https://docs.plasmic.app/learn/code-components-ref/
129
+ // And configure your Plasmic project to use the host url pointing at
130
+ // the /plasmic-host page of your nextjs app (for example,
131
+ // http://localhost:3000/plasmic-host). See
132
+ // https://docs.plasmic.app/learn/app-hosting/#set-a-plasmic-project-to-use-your-app-host
133
+
134
+ // registerComponent(...)
135
+
136
+ export default function PlasmicHost() {
137
+ return (
138
+ <div>
139
+ <Script
140
+ src="https://static1.plasmic.app/preamble.js"
141
+ strategy="beforeInteractive"
142
+ />
143
+ <PlasmicCanvasHost />
144
+ </div>
145
+ );
146
+ }
147
+ `;
148
+ }
117
149
  }
118
150
  exports.makeNextjsHostPage = makeNextjsHostPage;
151
+ function wrapAppRootForCodegen() {
152
+ return `
153
+ import '../styles/globals.css'
154
+ import { PlasmicRootProvider } from "@plasmicapp/react-web";
155
+
156
+ function MyApp({ Component, pageProps }) {
157
+ return (
158
+ <PlasmicRootProvider>
159
+ <Component {...pageProps} />
160
+ </PlasmicRootProvider>
161
+ );
162
+ }
163
+
164
+ export default MyApp
165
+ `;
166
+ }
167
+ exports.wrapAppRootForCodegen = wrapAppRootForCodegen;
@@ -6,14 +6,6 @@ import { PlatformType } from "../lib";
6
6
  */
7
7
  export declare function deleteGlob(searchPattern: string, skipPatterns?: string[]): void;
8
8
  export declare function stripExtension(filename: string, removeComposedPath?: boolean): string;
9
- /**
10
- * create-next-app doesn't create next.config.js,
11
- * so it's safe to just write the file
12
- * @param absPath
13
- * @param projectId
14
- * @returns
15
- */
16
- export declare function writeDefaultNextjsConfig(projectDir: string, projectId: string, loader: boolean, projectApiToken?: string, useTypescript?: boolean): Promise<void>;
17
9
  export declare function writePlasmicLoaderJson(projectDir: string, projectId: string, projectApiToken: string): Promise<void>;
18
10
  /**
19
11
  * - [nextjs|gatsby, loader, '/' page exists] - remove index file
@@ -32,5 +24,4 @@ export declare function overwriteIndex(projectPath: string, platform: string, sc
32
24
  */
33
25
  export declare function overwriteReadme(projectPath: string, platform: PlatformType, buildCommand: string): Promise<void>;
34
26
  export declare function ensureTsconfig(projectPath: string): Promise<void>;
35
- export declare function wrapAppRoot(projectPath: string, platform: string, scheme: string): Promise<void>;
36
27
  export declare function ifTs(ts: boolean, str: string): string;
@@ -31,13 +31,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.ifTs = exports.wrapAppRoot = exports.ensureTsconfig = exports.overwriteReadme = exports.overwriteIndex = exports.writePlasmicLoaderJson = exports.writeDefaultNextjsConfig = exports.stripExtension = exports.deleteGlob = void 0;
34
+ exports.ifTs = exports.ensureTsconfig = exports.overwriteReadme = exports.overwriteIndex = exports.writePlasmicLoaderJson = exports.stripExtension = exports.deleteGlob = void 0;
35
35
  const fs_1 = require("fs");
36
36
  const glob_1 = __importDefault(require("glob"));
37
37
  const lodash_1 = __importDefault(require("lodash"));
38
38
  const path = __importStar(require("upath"));
39
39
  const gatsby_1 = require("../templates/gatsby");
40
- const nextjs_1 = require("../templates/nextjs");
41
40
  const readme_1 = require("../templates/readme");
42
41
  const welcomePage_1 = require("../templates/welcomePage");
43
42
  const lang_utils_1 = require("./lang-utils");
@@ -64,40 +63,6 @@ function stripExtension(filename, removeComposedPath = false) {
64
63
  return filename.substring(0, filename.lastIndexOf(ext));
65
64
  }
66
65
  exports.stripExtension = stripExtension;
67
- /**
68
- * create-next-app doesn't create next.config.js,
69
- * so it's safe to just write the file
70
- * @param absPath
71
- * @param projectId
72
- * @returns
73
- */
74
- function writeDefaultNextjsConfig(projectDir, projectId, loader, projectApiToken, useTypescript) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const nextjsConfigFile = path.join(projectDir, "next.config.js");
77
- if (!loader) {
78
- yield fs_1.promises.writeFile(nextjsConfigFile, `
79
- module.exports = {
80
- eslint: {
81
- ignoreDuringBuilds: true,
82
- },
83
- trailingSlash: true,
84
- // Your NextJS config.
85
- };
86
- `);
87
- return;
88
- }
89
- if (loader && projectApiToken) {
90
- const initFile = path.join(projectDir, `plasmic-init.${useTypescript ? "ts" : "js"}`);
91
- yield fs_1.promises.writeFile(initFile, nextjs_1.makeNextjsInitPage(projectId, projectApiToken));
92
- const pagesFolder = path.join(projectDir, "pages");
93
- const loaderPage = path.join(pagesFolder, `[[...catchall]].${useTypescript ? "tsx" : "jsx"}`);
94
- yield fs_1.promises.writeFile(loaderPage, nextjs_1.makeNextjsCatchallPage(useTypescript ? "ts" : "js"));
95
- const hostPage = path.join(pagesFolder, `plasmic-host.${useTypescript ? "tsx" : "jsx"}`);
96
- yield fs_1.promises.writeFile(hostPage, nextjs_1.makeNextjsHostPage());
97
- }
98
- });
99
- }
100
- exports.writeDefaultNextjsConfig = writeDefaultNextjsConfig;
101
66
  function writePlasmicLoaderJson(projectDir, projectId, projectApiToken) {
102
67
  return __awaiter(this, void 0, void 0, function* () {
103
68
  const plasmicLoaderJson = path.join(projectDir, "plasmic-loader.json");
@@ -283,49 +248,6 @@ function ensureTsconfig(projectPath) {
283
248
  });
284
249
  }
285
250
  exports.ensureTsconfig = ensureTsconfig;
286
- function wrapAppRoot(projectPath, platform, scheme) {
287
- return __awaiter(this, void 0, void 0, function* () {
288
- // with create-plasmic-app v2, isLoader=false
289
- const isLoader = scheme === "loader";
290
- const importPkg = isLoader ? `@plasmicapp/loader` : "@plasmicapp/react-web";
291
- if (platform === "nextjs") {
292
- const appFilePath = path.join(projectPath, "pages", `_app.js`);
293
- yield fs_1.promises.writeFile(appFilePath, `
294
- import '../styles/globals.css'
295
- import { PlasmicRootProvider } from "${importPkg}"
296
-
297
- function MyApp({ Component, pageProps }) {
298
- return (
299
- <PlasmicRootProvider>
300
- <Component {...pageProps} />
301
- </PlasmicRootProvider>
302
- )
303
- }
304
-
305
- export default MyApp
306
- `.trim());
307
- }
308
- else if (platform === "gatsby") {
309
- const wrapperContent = `
310
- import React from "react";
311
- import { PlasmicRootProvider } from "${importPkg}";
312
-
313
- export const wrapRootElement = ({ element }) => {
314
- return (
315
- <PlasmicRootProvider>
316
- {element}
317
- </PlasmicRootProvider>
318
- );
319
- }
320
- `.trim();
321
- const browserFilePath = path.join(projectPath, "gatsby-browser.js");
322
- yield fs_1.promises.writeFile(browserFilePath, wrapperContent);
323
- const ssrFilePath = path.join(projectPath, "gatsby-ssr.js");
324
- yield fs_1.promises.writeFile(ssrFilePath, wrapperContent);
325
- }
326
- });
327
- }
328
- exports.wrapAppRoot = wrapAppRoot;
329
251
  function ifTs(ts, str) {
330
252
  return ts ? str : "";
331
253
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-plasmic-app",
3
- "version": "0.0.36",
3
+ "version": "0.0.39",
4
4
  "description": "Create Plasmic-powered React apps",
5
5
  "main": "./dist/lib.js",
6
6
  "types": "./dist/lib.d.ts",
package/src/index.ts CHANGED
@@ -13,6 +13,8 @@ if (process.env.CPA_DEBUG_CHDIR) {
13
13
  process.chdir(process.env.CPA_DEBUG_CHDIR);
14
14
  }
15
15
 
16
+ export type CodeScheme = "codegen" | "loader";
17
+
16
18
  // Check for updates
17
19
  const createPlasmicAppVersion = updateNotify();
18
20
 
@@ -154,7 +156,7 @@ async function run(): Promise<void> {
154
156
 
155
157
  // Scheme to use for Plasmic integration
156
158
  // - loader only available for gatsby/next.js
157
- const scheme: "codegen" | "loader" =
159
+ const scheme: CodeScheme =
158
160
  platform === "nextjs" || platform === "gatsby"
159
161
  ? await maybePrompt({
160
162
  name: "scheme",
package/src/lib.ts CHANGED
@@ -3,15 +3,8 @@ import chalk from "chalk";
3
3
  import * as path from "upath";
4
4
  import validateProjectName from "validate-npm-package-name";
5
5
  import { getCPAStrategy } from "./strategies";
6
- import { spawnOrFail } from "./utils/cmd-utils";
7
- import {
8
- ensureTsconfig,
9
- overwriteIndex,
10
- overwriteReadme,
11
- wrapAppRoot,
12
- writeDefaultNextjsConfig,
13
- } from "./utils/file-utils";
14
- import { detectPackageManager, installUpgrade } from "./utils/npm-utils";
6
+ import { ensureTsconfig, overwriteReadme } from "./utils/file-utils";
7
+ import { detectPackageManager } from "./utils/npm-utils";
15
8
 
16
9
  export type PlatformType = "nextjs" | "gatsby" | "react";
17
10
  export type SchemeType = "codegen" | "loader";
@@ -91,71 +84,48 @@ export async function create(args: CreatePlasmicAppArgs): Promise<void> {
91
84
  await ensureTsconfig(resolvedProjectPath);
92
85
  }
93
86
 
94
- // Install dependency
95
- banner("INSTALLING THE PLASMIC DEPENDENCY");
96
- if (scheme === "codegen") {
97
- const installResult = await installUpgrade("@plasmicapp/cli", {
98
- workingDir: resolvedProjectPath,
99
- });
100
- if (!installResult) {
101
- throw new Error("Failed to install the Plasmic dependency");
102
- }
103
- }
104
-
105
- // Trigger a sync
106
- const pkgMgr = detectPackageManager(resolvedProjectPath);
107
- const npmRunCmd = pkgMgr === "yarn" ? "yarn" : "npm run";
108
-
109
- if (scheme === "codegen") {
110
- banner("SYNCING PLASMIC COMPONENTS");
111
-
112
- const project = projectApiToken
113
- ? `${projectId}:${projectApiToken}`
114
- : projectId;
115
-
116
- if (platform === "nextjs") {
117
- await writeDefaultNextjsConfig(resolvedProjectPath, projectId, false);
118
- }
119
-
120
- await spawnOrFail(
121
- `npx -p @plasmicapp/cli plasmic sync --yes -p ${project}`,
122
- resolvedProjectPath
123
- );
124
- } else if (scheme === "loader") {
125
- if (!projectApiToken) {
126
- projectApiToken = await getProjectApiToken(projectId);
127
- }
87
+ // Make sure we have an api token for loader
88
+ if (scheme === "loader" && !projectApiToken) {
89
+ projectApiToken = await getProjectApiToken(projectId);
128
90
  if (!projectApiToken) {
129
91
  throw new Error(`Failed to get projectApiToken for ${projectId}`);
130
92
  }
131
-
132
- await cpaStrategy.configLoader({
133
- projectPath: resolvedProjectPath,
134
- projectId,
135
- projectApiToken,
136
- useTypescript,
137
- });
138
93
  }
139
94
 
140
- if (scheme === "loader") {
141
- await cpaStrategy.overwriteFiles({
142
- projectPath: resolvedProjectPath,
143
- useTypescript,
144
- });
145
- } else if (scheme === "codegen") {
146
- // The loader files to be overwritten are handled by cpaStrategy
147
- // but for codegen we still have to run it
148
-
149
- // Overwrite the index file
150
- await overwriteIndex(resolvedProjectPath, platform, scheme);
95
+ // Install dependency
96
+ banner("INSTALLING THE PLASMIC DEPENDENCY");
97
+ const installResult = await cpaStrategy.installDeps({
98
+ scheme,
99
+ projectPath: resolvedProjectPath,
100
+ });
151
101
 
152
- // Overwrite the wrapper files to wrap PlasmicRootProvider
153
- await wrapAppRoot(resolvedProjectPath, platform, scheme);
102
+ if (!installResult) {
103
+ throw new Error("Failed to install the Plasmic dependency");
154
104
  }
155
105
 
106
+ // Configure
107
+ await cpaStrategy.overwriteConfig({
108
+ projectId,
109
+ projectPath: resolvedProjectPath,
110
+ projectApiToken,
111
+ useTypescript,
112
+ scheme,
113
+ });
114
+
115
+ // Generate files
116
+ await cpaStrategy.generateFiles({
117
+ projectPath: resolvedProjectPath,
118
+ useTypescript,
119
+ scheme,
120
+ projectId,
121
+ projectApiToken,
122
+ });
123
+
156
124
  /**
157
125
  * INSTRUCT USER ON NEXT STEPS
158
126
  */
127
+ const pkgMgr = detectPackageManager(resolvedProjectPath);
128
+ const npmRunCmd = pkgMgr === "yarn" ? "yarn" : "npm run";
159
129
  const command =
160
130
  platform === "nextjs"
161
131
  ? `${npmRunCmd} dev`
@@ -0,0 +1,30 @@
1
+ import { banner } from "../lib";
2
+ import { spawnOrFail } from "../utils/cmd-utils";
3
+ import { installUpgrade } from "../utils/npm-utils";
4
+
5
+ export async function installCodegenDeps(opts: { projectPath: string }) {
6
+ const { projectPath } = opts;
7
+ return (
8
+ (await installUpgrade("@plasmicapp/cli", { workingDir: projectPath })) &&
9
+ (await installUpgrade("@plasmicapp/host", { workingDir: projectPath }))
10
+ );
11
+ }
12
+
13
+ export async function runCodegenSync(opts: {
14
+ projectId: string;
15
+ projectApiToken: string | undefined;
16
+ projectPath: string;
17
+ }) {
18
+ const { projectId, projectApiToken, projectPath } = opts;
19
+
20
+ banner("SYNCING PLASMIC COMPONENTS");
21
+
22
+ const project = projectApiToken
23
+ ? `${projectId}:${projectApiToken}`
24
+ : projectId;
25
+
26
+ await spawnOrFail(
27
+ `npx -p @plasmicapp/cli plasmic sync --yes -p ${project}`,
28
+ projectPath
29
+ );
30
+ }
@@ -8,10 +8,13 @@ import {
8
8
  makeGatsbyDefaultPage,
9
9
  makeGatsbyHostPage,
10
10
  makeGatsbyPlasmicInit,
11
+ wrapAppRootForCodegen,
11
12
  } from "../templates/gatsby";
12
13
  import { spawnOrFail } from "../utils/cmd-utils";
13
- import { deleteGlob } from "../utils/file-utils";
14
+ import { deleteGlob, overwriteIndex } from "../utils/file-utils";
15
+ import { ensure } from "../utils/lang-utils";
14
16
  import { installUpgrade } from "../utils/npm-utils";
17
+ import { installCodegenDeps, runCodegenSync } from "./common";
15
18
  import { CPAStrategy } from "./types";
16
19
 
17
20
  const gatsbyStrategy: CPAStrategy = {
@@ -22,41 +25,56 @@ const gatsbyStrategy: CPAStrategy = {
22
25
 
23
26
  await spawnOrFail(`${createCommand}${templateArg}`);
24
27
  },
25
- configLoader: async (args) => {
26
- const { projectId, projectPath, projectApiToken, useTypescript } = args;
27
-
28
- const installResult = await installUpgrade("@plasmicapp/loader-gatsby", {
29
- workingDir: projectPath,
30
- });
31
-
32
- if (!installResult) {
33
- throw new Error("Failed to install the Plasmic dependency");
28
+ installDeps: async ({ projectPath, scheme }) => {
29
+ if (scheme === "loader") {
30
+ return await installUpgrade("@plasmicapp/loader-gatsby", {
31
+ workingDir: projectPath,
32
+ });
33
+ } else {
34
+ return await installCodegenDeps({ projectPath });
34
35
  }
36
+ },
37
+ overwriteConfig: async (args) => {
38
+ const {
39
+ projectId,
40
+ projectPath,
41
+ projectApiToken,
42
+ useTypescript,
43
+ scheme,
44
+ } = args;
35
45
 
36
- // create-gatsby will create a default gatsby-config.js that we need to modify
37
- const gatsbyConfigFile = path.join(projectPath, "gatsby-config.js");
38
- const rl = readline.createInterface({
39
- input: createReadStream(gatsbyConfigFile),
40
- crlfDelay: Infinity,
41
- });
42
- let result = "";
43
- for await (const line of rl) {
44
- result += line + "\n";
45
- // Prepend PlasmicLoader to list of plugins
46
- if (line.includes("plugins:")) {
47
- result += GATSBY_PLUGIN_CONFIG(
48
- projectId,
49
- projectApiToken,
50
- useTypescript
51
- );
46
+ if (scheme === "loader") {
47
+ // create-gatsby will create a default gatsby-config.js that we need to modify
48
+ const gatsbyConfigFile = path.join(projectPath, "gatsby-config.js");
49
+ const rl = readline.createInterface({
50
+ input: createReadStream(gatsbyConfigFile),
51
+ crlfDelay: Infinity,
52
+ });
53
+ let result = "";
54
+ for await (const line of rl) {
55
+ result += line + "\n";
56
+ // Prepend PlasmicLoader to list of plugins
57
+ if (line.includes("plugins:")) {
58
+ result += GATSBY_PLUGIN_CONFIG(
59
+ projectId,
60
+ ensure(projectApiToken),
61
+ useTypescript
62
+ );
63
+ }
52
64
  }
65
+ await fs.writeFile(gatsbyConfigFile, result);
53
66
  }
54
- await fs.writeFile(gatsbyConfigFile, result);
55
67
  },
56
- overwriteFiles: async (args) => {
68
+ generateFiles: async (args) => {
57
69
  // in gatsby we can delete all existing pages/components, since all pages are going
58
70
  // to be handled by templates/defaultPlasmicPage
59
- const { projectPath, useTypescript } = args;
71
+ const {
72
+ projectId,
73
+ projectApiToken,
74
+ projectPath,
75
+ useTypescript,
76
+ scheme,
77
+ } = args;
60
78
 
61
79
  const extension = useTypescript ? "ts" : "js";
62
80
 
@@ -65,15 +83,13 @@ const gatsbyStrategy: CPAStrategy = {
65
83
  // Create a very basic 404 page - `gatsby build` fails without it.
66
84
  await fs.writeFile(path.join(projectPath, `src/pages/404.js`), GATSBY_404);
67
85
 
68
- await fs.writeFile(
69
- path.join(projectPath, `src/plasmic-init.${extension}`),
70
- makeGatsbyPlasmicInit(extension)
71
- );
72
-
73
86
  // Add plasmic-host page
74
87
  await fs.writeFile(
75
88
  path.join(projectPath, `src/pages/plasmic-host.${extension}x`),
76
- makeGatsbyHostPage(extension)
89
+ makeGatsbyHostPage({
90
+ useTypescript,
91
+ scheme,
92
+ })
77
93
  );
78
94
 
79
95
  // Start with an empty gatsby-node.js
@@ -85,16 +101,39 @@ const gatsbyStrategy: CPAStrategy = {
85
101
  GATSBY_SSR_CONFIG
86
102
  );
87
103
 
88
- const templatesFolder = path.join(projectPath, "src/templates");
89
- const defaultPagePath = path.join(
90
- templatesFolder,
91
- `defaultPlasmicPage.${extension}x`
92
- );
104
+ if (scheme === "loader") {
105
+ await fs.writeFile(
106
+ path.join(projectPath, `src/plasmic-init.${extension}`),
107
+ makeGatsbyPlasmicInit(extension)
108
+ );
109
+
110
+ const templatesFolder = path.join(projectPath, "src/templates");
111
+ if (!existsSync(templatesFolder)) {
112
+ await fs.mkdir(templatesFolder);
113
+ }
114
+ const defaultPagePath = path.join(
115
+ templatesFolder,
116
+ `defaultPlasmicPage.${extension}x`
117
+ );
118
+ await fs.writeFile(defaultPagePath, makeGatsbyDefaultPage(extension));
119
+ } else {
120
+ await runCodegenSync({
121
+ projectId,
122
+ projectApiToken,
123
+ projectPath,
124
+ });
125
+
126
+ // Overwrite the index file
127
+ await overwriteIndex(projectPath, "gatsby", scheme);
128
+
129
+ // Overwrite the wrapper files to wrap PlasmicRootProvider
130
+ const wrapperContent = wrapAppRootForCodegen();
131
+ const browserFilePath = path.join(projectPath, "gatsby-browser.js");
132
+ await fs.writeFile(browserFilePath, wrapperContent);
93
133
 
94
- if (!existsSync(templatesFolder)) {
95
- await fs.mkdir(templatesFolder);
134
+ const ssrFilePath = path.join(projectPath, "gatsby-ssr.js");
135
+ await fs.writeFile(ssrFilePath, wrapperContent);
96
136
  }
97
- await fs.writeFile(defaultPagePath, makeGatsbyDefaultPage(extension));
98
137
  },
99
138
  build: async (args) => {
100
139
  const { npmRunCmd, projectPath } = args;