create-plasmic-app 0.0.34 → 0.0.38
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.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -7
- package/dist/lib.js +29 -47
- package/dist/strategies/common.d.ts +8 -0
- package/dist/strategies/common.js +34 -0
- package/dist/strategies/gatsby.js +66 -38
- package/dist/strategies/nextjs.js +52 -14
- package/dist/strategies/react.js +26 -9
- package/dist/strategies/types.d.ts +14 -4
- package/dist/templates/gatsby.d.ts +6 -1
- package/dist/templates/gatsby.js +46 -9
- package/dist/templates/nextjs.d.ts +3 -1
- package/dist/templates/nextjs.js +59 -7
- package/dist/utils/file-utils.d.ts +0 -9
- package/dist/utils/file-utils.js +1 -79
- package/package.json +1 -1
- package/src/index.ts +7 -8
- package/src/lib.ts +33 -63
- package/src/strategies/common.ts +30 -0
- package/src/strategies/gatsby.ts +82 -43
- package/src/strategies/nextjs.ts +85 -23
- package/src/strategies/react.ts +30 -11
- package/src/strategies/types.ts +16 -4
- package/src/templates/gatsby.ts +48 -11
- package/src/templates/nextjs.ts +59 -6
- package/src/utils/file-utils.ts +0 -115
package/dist/templates/nextjs.js
CHANGED
|
@@ -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";
|
|
@@ -86,20 +86,27 @@ export const getStaticPaths${file_utils_1.ifTs(ts, `: GetStaticPaths`)} = async
|
|
|
86
86
|
catchall: mod.path.substring(1).split("/"),
|
|
87
87
|
},
|
|
88
88
|
})),
|
|
89
|
-
|
|
89
|
+
|
|
90
|
+
// Turn on "fallback: 'blocking'" if you would like new paths created
|
|
91
|
+
// in Plasmic to be automatically available
|
|
92
|
+
fallback: false,
|
|
90
93
|
};
|
|
91
94
|
}
|
|
92
95
|
`.trim();
|
|
93
96
|
}
|
|
94
97
|
exports.makeNextjsCatchallPage = makeNextjsCatchallPage;
|
|
95
|
-
function makeNextjsHostPage() {
|
|
96
|
-
|
|
98
|
+
function makeNextjsHostPage(scheme) {
|
|
99
|
+
const commonImports = `
|
|
97
100
|
import * as React from 'react';
|
|
98
|
-
import { PlasmicCanvasHost } from '@plasmicapp/loader-nextjs';
|
|
99
101
|
import Script from 'next/script';
|
|
102
|
+
`.trim();
|
|
103
|
+
if (scheme === "loader") {
|
|
104
|
+
return `
|
|
105
|
+
${commonImports}
|
|
106
|
+
import { PlasmicCanvasHost } from '@plasmicapp/loader-nextjs';
|
|
100
107
|
import { PLASMIC } from '../plasmic-init';
|
|
101
108
|
|
|
102
|
-
export default function
|
|
109
|
+
export default function PlasmicHost() {
|
|
103
110
|
return PLASMIC && (
|
|
104
111
|
<div>
|
|
105
112
|
<Script
|
|
@@ -110,6 +117,51 @@ export default function Host() {
|
|
|
110
117
|
</div>
|
|
111
118
|
);
|
|
112
119
|
}
|
|
113
|
-
|
|
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
|
+
}
|
|
114
149
|
}
|
|
115
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;
|
package/dist/utils/file-utils.js
CHANGED
|
@@ -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.
|
|
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
package/src/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { setMetadataEnv } from "@plasmicapp/cli";
|
|
3
3
|
import * as Sentry from "@sentry/node";
|
|
4
|
-
import chalk from "chalk";
|
|
5
4
|
import * as fs from "fs";
|
|
6
5
|
import inquirer, { DistinctQuestion } from "inquirer";
|
|
7
6
|
import * as path from "upath";
|
|
@@ -14,6 +13,8 @@ if (process.env.CPA_DEBUG_CHDIR) {
|
|
|
14
13
|
process.chdir(process.env.CPA_DEBUG_CHDIR);
|
|
15
14
|
}
|
|
16
15
|
|
|
16
|
+
export type CodeScheme = "codegen" | "loader";
|
|
17
|
+
|
|
17
18
|
// Check for updates
|
|
18
19
|
const createPlasmicAppVersion = updateNotify();
|
|
19
20
|
|
|
@@ -155,7 +156,7 @@ async function run(): Promise<void> {
|
|
|
155
156
|
|
|
156
157
|
// Scheme to use for Plasmic integration
|
|
157
158
|
// - loader only available for gatsby/next.js
|
|
158
|
-
const scheme:
|
|
159
|
+
const scheme: CodeScheme =
|
|
159
160
|
platform === "nextjs" || platform === "gatsby"
|
|
160
161
|
? await maybePrompt({
|
|
161
162
|
name: "scheme",
|
|
@@ -179,16 +180,14 @@ async function run(): Promise<void> {
|
|
|
179
180
|
|
|
180
181
|
// Get the projectId
|
|
181
182
|
console.log();
|
|
182
|
-
console.log(chalk.green.bold("Go to this URL and **clone** the project:"));
|
|
183
|
-
console.log("https://studio.plasmic.app/starters/simple-light");
|
|
184
|
-
console.log();
|
|
185
|
-
console.log(" Note the project ID in the URL redirect");
|
|
186
|
-
console.log(" (e.g. https://studio.plasmic.app/projects/PROJECT_ID)");
|
|
187
183
|
let projectId: string | undefined;
|
|
188
184
|
while (!projectId) {
|
|
189
185
|
const rawProjectId = await maybePrompt({
|
|
190
186
|
name: "projectId",
|
|
191
|
-
message:
|
|
187
|
+
message: `What is the URL of your project?
|
|
188
|
+
(If you don't have a project yet, create one by going to
|
|
189
|
+
https://studio.plasmic.app/starters/simple-light):
|
|
190
|
+
`,
|
|
192
191
|
});
|
|
193
192
|
projectId = rawProjectId
|
|
194
193
|
.replace("https://studio.plasmic.app/projects/", "")
|
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 {
|
|
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
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
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
|
-
|
|
153
|
-
|
|
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
|
+
}
|
package/src/strategies/gatsby.ts
CHANGED
|
@@ -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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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 {
|
|
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(
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
95
|
-
await fs.
|
|
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;
|