@plasmicapp/cli 0.1.169 → 0.1.170

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.
@@ -249,6 +249,7 @@ class PlasmicApi {
249
249
  iconChecksums: [],
250
250
  globalVariantChecksums: [],
251
251
  projectCssChecksum: "",
252
+ globalContextsChecksum: "",
252
253
  },
253
254
  usedNpmPackages: [],
254
255
  externalCssImports: [],
@@ -281,7 +282,6 @@ class PlasmicApi {
281
282
  return "0.0.1";
282
283
  });
283
284
  }
284
- ;
285
285
  requiredPackages() {
286
286
  return __awaiter(this, void 0, void 0, function* () {
287
287
  return {
@@ -0,0 +1,4 @@
1
+ import { ChecksumBundle, ProjectMetaBundle } from "../api";
2
+ import { PlasmicContext, ProjectConfig, ProjectLock } from "../utils/config-utils";
3
+ export declare function syncGlobalContexts(context: PlasmicContext, projectMeta: ProjectMetaBundle, projectConfig: ProjectConfig, projectLock: ProjectLock, checksums: ChecksumBundle, baseDir: string): Promise<void>;
4
+ export declare function getGlobalContextsResourcePath(context: PlasmicContext, projectConfig: ProjectConfig): string;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getGlobalContextsResourcePath = exports.syncGlobalContexts = void 0;
16
+ const deps_1 = require("../deps");
17
+ const code_utils_1 = require("../utils/code-utils");
18
+ const lodash_1 = __importDefault(require("lodash"));
19
+ const file_utils_1 = require("../utils/file-utils");
20
+ const COMPONENT_NAME = "PlasmicGlobalContextsProvider";
21
+ function syncGlobalContexts(context, projectMeta, projectConfig, projectLock, checksums, baseDir) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const resourcePath = getGlobalContextsResourcePath(context, projectConfig);
24
+ if (checksums.globalContextsChecksum && projectMeta.globalContextBundle) {
25
+ if (context.cliArgs.quiet !== true) {
26
+ deps_1.logger.info(`Syncing component: ${COMPONENT_NAME}@${projectLock.version}\t['${projectConfig.projectName}' ${projectConfig.projectId} ${projectConfig.version}]`);
27
+ }
28
+ if (context.config.code.lang === "js") {
29
+ projectMeta.globalContextBundle.contextModule = code_utils_1.formatScript(code_utils_1.tsxToJsx(projectMeta.globalContextBundle.contextModule), baseDir);
30
+ }
31
+ file_utils_1.writeFileContent(context, resourcePath, projectMeta.globalContextBundle.contextModule, { force: false });
32
+ projectConfig.globalContextsFilePath = resourcePath;
33
+ const fl = projectLock.fileLocks.find((fl) => fl.assetId === projectConfig.projectId && fl.type === "globalContexts");
34
+ if (fl) {
35
+ fl.checksum = checksums.globalContextsChecksum;
36
+ }
37
+ else {
38
+ projectLock.fileLocks.push({
39
+ assetId: projectConfig.projectId,
40
+ checksum: checksums.globalContextsChecksum,
41
+ type: "globalContexts",
42
+ });
43
+ }
44
+ }
45
+ else if (!checksums.globalContextsChecksum &&
46
+ !projectMeta.globalContextBundle) {
47
+ if (file_utils_1.fileExists(context, resourcePath)) {
48
+ file_utils_1.deleteFile(context, resourcePath);
49
+ }
50
+ projectConfig.globalContextsFilePath = "";
51
+ lodash_1.default.remove(projectLock.fileLocks, (fl) => fl.assetId === projectConfig.projectId && fl.type === "globalContexts");
52
+ }
53
+ });
54
+ }
55
+ exports.syncGlobalContexts = syncGlobalContexts;
56
+ function getGlobalContextsResourcePath(context, projectConfig) {
57
+ return projectConfig.globalContextsFilePath !== ""
58
+ ? projectConfig.globalContextsFilePath
59
+ : file_utils_1.defaultResourcePath(context, projectConfig, `${COMPONENT_NAME}.${context.config.code.lang === "ts" ? "tsx" : "jsx"}`);
60
+ }
61
+ exports.getGlobalContextsResourcePath = getGlobalContextsResourcePath;
@@ -54,6 +54,7 @@ const sync_global_variants_1 = require("./sync-global-variants");
54
54
  const sync_icons_1 = require("./sync-icons");
55
55
  const sync_images_1 = require("./sync-images");
56
56
  const sync_styles_1 = require("./sync-styles");
57
+ const sync_global_contexts_1 = require("./sync-global-contexts");
57
58
  function ensureRequiredPackages(context, baseDir, yes) {
58
59
  return __awaiter(this, void 0, void 0, function* () {
59
60
  const requireds = yield context.api.requiredPackages();
@@ -258,8 +259,8 @@ function sync(opts, metadataDefaults) {
258
259
  writeLoaderConfig(opts, config);
259
260
  }
260
261
  const codegenVersion = yield context.api.latestCodegenVersion();
261
- context.lock.projects.forEach(p => {
262
- if (projectsToSync.some(syncedProject => syncedProject.projectId === p.projectId)) {
262
+ context.lock.projects.forEach((p) => {
263
+ if (projectsToSync.some((syncedProject) => syncedProject.projectId === p.projectId)) {
263
264
  p.codegenVersion = codegenVersion;
264
265
  }
265
266
  });
@@ -444,6 +445,7 @@ function syncProjectConfig(context, projectBundle, projectApiToken, version, dep
444
445
  projectConfig.jsBundleThemes.length === 0) {
445
446
  delete projectConfig.jsBundleThemes;
446
447
  }
448
+ yield sync_global_contexts_1.syncGlobalContexts(context, projectBundle, projectConfig, projectLock, checksums, baseDir);
447
449
  // Write out components
448
450
  yield sync_components_1.syncProjectComponents(context, projectConfig, version, componentBundles, forceOverwrite, appendJsxOnMissingBase, summary, pendingMerge, projectLock, checksums, baseDir);
449
451
  });
package/dist/api.d.ts CHANGED
@@ -25,6 +25,10 @@ export interface GlobalVariantBundle {
25
25
  contextModule: string;
26
26
  contextFileName: string;
27
27
  }
28
+ export interface GlobalContextBundle {
29
+ id: string;
30
+ contextModule: string;
31
+ }
28
32
  export interface JsBundleTheme {
29
33
  themeFileName: string;
30
34
  themeModule: string;
@@ -36,6 +40,7 @@ export interface ProjectMetaBundle {
36
40
  cssFileName: string;
37
41
  cssRules: string;
38
42
  jsBundleThemes?: JsBundleTheme[];
43
+ globalContextBundle?: GlobalContextBundle;
39
44
  }
40
45
  export interface IconBundle {
41
46
  id: string;
@@ -111,6 +116,7 @@ export interface ChecksumBundle {
111
116
  iconChecksums: Array<[string, string]>;
112
117
  globalVariantChecksums: Array<[string, string]>;
113
118
  projectCssChecksum: string;
119
+ globalContextsChecksum: string;
114
120
  }
115
121
  export interface CodeComponentMeta {
116
122
  id: string;
package/dist/index.js CHANGED
File without changes
@@ -282,6 +282,10 @@
282
282
  "description": "File location for the project-wide css styles. Relative to srcDir",
283
283
  "type": "string"
284
284
  },
285
+ "globalContextsFilePath": {
286
+ "description": "File location for the project-wide global contexts. Relative to srcDir",
287
+ "type": "string"
288
+ },
285
289
  "icons": {
286
290
  "description": "Metadata for each synced icon in this project",
287
291
  "items": {
@@ -326,6 +330,7 @@
326
330
  "required": [
327
331
  "components",
328
332
  "cssFilePath",
333
+ "globalContextsFilePath",
329
334
  "icons",
330
335
  "images",
331
336
  "indirect",
@@ -139,6 +139,7 @@ exports.project1Config = {
139
139
  images: [],
140
140
  jsBundleThemes: [],
141
141
  indirect: false,
142
+ globalContextsFilePath: "",
142
143
  };
143
144
  function expectProject1PlasmicJson(optional) {
144
145
  const plasmicJson = exports.tmpRepo.readPlasmicJson();
@@ -13,6 +13,7 @@ function getChecksums(context, opts, projectId, componentIds) {
13
13
  cssRulesChecksums: [],
14
14
  globalVariantChecksums: [],
15
15
  projectCssChecksum: "",
16
+ globalContextsChecksum: "",
16
17
  };
17
18
  }
18
19
  const fileLocks = projectLock.fileLocks;
@@ -51,6 +52,11 @@ function getChecksums(context, opts, projectId, componentIds) {
51
52
  const projectCssChecksums = fileLocks.filter((fileLock) => fileLock.type === "projectCss");
52
53
  lang_utils_1.assert(projectCssChecksums.length < 2);
53
54
  const projectCssChecksum = projectCssChecksums.length > 0 ? projectCssChecksums[0].checksum : "";
55
+ const globalContextsChecksums = fileLocks.filter((fileLock) => fileLock.type === "globalContexts" && fileLock.assetId === projectId);
56
+ lang_utils_1.assert(globalContextsChecksums.length < 2);
57
+ const globalContextsChecksum = globalContextsChecksums.length > 0
58
+ ? globalContextsChecksums[0].checksum
59
+ : "";
54
60
  return {
55
61
  imageChecksums,
56
62
  iconChecksums,
@@ -58,6 +64,7 @@ function getChecksums(context, opts, projectId, componentIds) {
58
64
  cssRulesChecksums,
59
65
  globalVariantChecksums,
60
66
  projectCssChecksum,
67
+ globalContextsChecksum,
61
68
  };
62
69
  }
63
70
  exports.getChecksums = getChecksums;
@@ -41,9 +41,11 @@ const Prettier = __importStar(require("prettier"));
41
41
  const prettier_1 = require("prettier");
42
42
  const ts = __importStar(require("typescript"));
43
43
  const upath_1 = __importDefault(require("upath"));
44
+ const sync_global_contexts_1 = require("../actions/sync-global-contexts");
44
45
  const sync_images_1 = require("../actions/sync-images");
45
46
  const deps_1 = require("../deps");
46
47
  const error_1 = require("../utils/error");
48
+ const config_utils_1 = require("./config-utils");
47
49
  const file_utils_1 = require("./file-utils");
48
50
  const lang_utils_1 = require("./lang-utils");
49
51
  exports.formatAsLocal = (content, filePath, baseDir, defaultOpts = {}) => {
@@ -341,6 +343,7 @@ function fixAllImportStatements(context, baseDir, summary) {
341
343
  }
342
344
  }
343
345
  }
346
+ fixGlobalContextImportStatements(context, fixImportContext, baseDir);
344
347
  });
345
348
  }
346
349
  exports.fixAllImportStatements = fixAllImportStatements;
@@ -455,3 +458,26 @@ exports.formatScript = (code, baseDir) => {
455
458
  useTabs: false,
456
459
  });
457
460
  };
461
+ function fixGlobalContextImportStatements(context, fixImportContext, baseDir) {
462
+ return __awaiter(this, void 0, void 0, function* () {
463
+ for (const project of context.config.projects) {
464
+ if (!project.globalContextsFilePath)
465
+ continue;
466
+ const resourcePath = sync_global_contexts_1.getGlobalContextsResourcePath(context, project);
467
+ let prevContent;
468
+ try {
469
+ prevContent = file_utils_1.readFileText(file_utils_1.makeFilePath(context, resourcePath)).toString();
470
+ }
471
+ catch (e) {
472
+ deps_1.logger.warn(`${resourcePath} is missing. If you deleted this component, remember to remove the component from ${config_utils_1.CONFIG_FILE_NAME}`);
473
+ throw e;
474
+ }
475
+ const newContent = replaceImports(context, prevContent, resourcePath, fixImportContext, false, baseDir, true);
476
+ if (prevContent !== newContent) {
477
+ yield file_utils_1.writeFileContent(context, resourcePath, newContent, {
478
+ force: true,
479
+ });
480
+ }
481
+ }
482
+ });
483
+ }
@@ -118,6 +118,8 @@ export interface ProjectConfig {
118
118
  version: string;
119
119
  /** File location for the project-wide css styles. Relative to srcDir */
120
120
  cssFilePath: string;
121
+ /** File location for the project-wide global contexts. Relative to srcDir */
122
+ globalContextsFilePath: string;
121
123
  jsBundleThemes?: JsBundleThemeConfig[];
122
124
  codeComponents?: CodeComponentConfig[];
123
125
  /** Metadata for each synced component in this project. */
@@ -213,7 +215,7 @@ export interface GlobalVariantGroupConfig {
213
215
  contextFilePath: string;
214
216
  }
215
217
  export interface FileLock {
216
- type: "renderModule" | "cssRules" | "icon" | "image" | "projectCss" | "globalVariant";
218
+ type: "renderModule" | "cssRules" | "icon" | "image" | "projectCss" | "globalVariant" | "globalContexts";
217
219
  checksum: string;
218
220
  assetId: string;
219
221
  }
@@ -40,6 +40,7 @@ function createProjectConfig(base) {
40
40
  icons: [],
41
41
  images: [],
42
42
  indirect: base.indirect,
43
+ globalContextsFilePath: "",
43
44
  };
44
45
  }
45
46
  exports.createProjectConfig = createProjectConfig;
@@ -150,6 +151,7 @@ function getOrAddProjectConfig(context, projectId, base // if one doesn't exist,
150
151
  images: [],
151
152
  jsBundleThemes: [],
152
153
  indirect: false,
154
+ globalContextsFilePath: "",
153
155
  };
154
156
  context.config.projects.push(project);
155
157
  }
@@ -188,6 +188,7 @@ function getAllPaths(context) {
188
188
  };
189
189
  const pushProject = (proj) => {
190
190
  pushPath(proj, "cssFilePath");
191
+ pushPath(proj, "globalContextsFilePath");
191
192
  for (const component of proj.components) {
192
193
  pushComponent(component);
193
194
  }
@@ -81,10 +81,7 @@ function removeMissingFilesFromLock(context, config, lock) {
81
81
  image.id,
82
82
  image,
83
83
  ]));
84
- const knownIcons = Object.fromEntries(knownProjects[project.projectId].icons.map((icons) => [
85
- icons.id,
86
- icons,
87
- ]));
84
+ const knownIcons = Object.fromEntries(knownProjects[project.projectId].icons.map((icons) => [icons.id, icons]));
88
85
  project.fileLocks = project.fileLocks.filter((lock) => {
89
86
  switch (lock.type) {
90
87
  default:
@@ -100,6 +97,8 @@ function removeMissingFilesFromLock(context, config, lock) {
100
97
  return knownImages[lock.assetId];
101
98
  case "icon":
102
99
  return knownIcons[lock.assetId];
100
+ case "globalContexts":
101
+ return knownProjects[project.projectId].globalContextsFilePath;
103
102
  }
104
103
  });
105
104
  return project;
@@ -169,6 +168,11 @@ function resolveMissingFilesInConfig(context, config) {
169
168
  for (const project of config.projects) {
170
169
  project.cssFilePath =
171
170
  (yield attemptToRestoreFilePath(context, project.cssFilePath, baseNameToFiles)) || "";
171
+ if (!project.globalContextsFilePath) {
172
+ project.globalContextsFilePath = "";
173
+ }
174
+ project.globalContextsFilePath =
175
+ (yield attemptToRestoreFilePath(context, project.globalContextsFilePath, baseNameToFiles)) || "";
172
176
  project.images = yield filterFiles(project.images, "filePath");
173
177
  project.icons = yield filterFiles(project.icons, "moduleFilePath");
174
178
  project.jsBundleThemes = yield filterFiles(project.jsBundleThemes || [], "themeFilePath");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/cli",
3
- "version": "0.1.169",
3
+ "version": "0.1.170",
4
4
  "description": "plasmic cli for syncing local code with Plasmic designs",
5
5
  "engines": {
6
6
  "node": ">=12"
@@ -339,6 +339,7 @@ class PlasmicApi {
339
339
  iconChecksums: [],
340
340
  globalVariantChecksums: [],
341
341
  projectCssChecksum: "",
342
+ globalContextsChecksum: "",
342
343
  } as ChecksumBundle,
343
344
  usedNpmPackages: [],
344
345
  externalCssImports: [],
@@ -374,7 +375,7 @@ class PlasmicApi {
374
375
 
375
376
  async latestCodegenVersion(): Promise<string> {
376
377
  return "0.0.1";
377
- };
378
+ }
378
379
 
379
380
  async requiredPackages(): Promise<RequiredPackages> {
380
381
  return {
@@ -0,0 +1,87 @@
1
+ import { ChecksumBundle, ProjectMetaBundle } from "../api";
2
+ import { logger } from "../deps";
3
+ import { formatScript, tsxToJsx } from "../utils/code-utils";
4
+ import L from "lodash";
5
+ import {
6
+ PlasmicContext,
7
+ ProjectConfig,
8
+ ProjectLock,
9
+ } from "../utils/config-utils";
10
+ import {
11
+ defaultResourcePath,
12
+ deleteFile,
13
+ fileExists,
14
+ writeFileContent,
15
+ } from "../utils/file-utils";
16
+
17
+ const COMPONENT_NAME = "PlasmicGlobalContextsProvider";
18
+
19
+ export async function syncGlobalContexts(
20
+ context: PlasmicContext,
21
+ projectMeta: ProjectMetaBundle,
22
+ projectConfig: ProjectConfig,
23
+ projectLock: ProjectLock,
24
+ checksums: ChecksumBundle,
25
+ baseDir: string
26
+ ) {
27
+ const resourcePath = getGlobalContextsResourcePath(context, projectConfig);
28
+ if (checksums.globalContextsChecksum && projectMeta.globalContextBundle) {
29
+ if (context.cliArgs.quiet !== true) {
30
+ logger.info(
31
+ `Syncing component: ${COMPONENT_NAME}@${projectLock.version}\t['${projectConfig.projectName}' ${projectConfig.projectId} ${projectConfig.version}]`
32
+ );
33
+ }
34
+ if (context.config.code.lang === "js") {
35
+ projectMeta.globalContextBundle.contextModule = formatScript(
36
+ tsxToJsx(projectMeta.globalContextBundle.contextModule),
37
+ baseDir
38
+ );
39
+ }
40
+ writeFileContent(
41
+ context,
42
+ resourcePath,
43
+ projectMeta.globalContextBundle.contextModule,
44
+ { force: false }
45
+ );
46
+ projectConfig.globalContextsFilePath = resourcePath;
47
+ const fl = projectLock.fileLocks.find(
48
+ (fl) =>
49
+ fl.assetId === projectConfig.projectId && fl.type === "globalContexts"
50
+ );
51
+ if (fl) {
52
+ fl.checksum = checksums.globalContextsChecksum;
53
+ } else {
54
+ projectLock.fileLocks.push({
55
+ assetId: projectConfig.projectId,
56
+ checksum: checksums.globalContextsChecksum,
57
+ type: "globalContexts",
58
+ });
59
+ }
60
+ } else if (
61
+ !checksums.globalContextsChecksum &&
62
+ !projectMeta.globalContextBundle
63
+ ) {
64
+ if (fileExists(context, resourcePath)) {
65
+ deleteFile(context, resourcePath);
66
+ }
67
+ projectConfig.globalContextsFilePath = "";
68
+ L.remove(
69
+ projectLock.fileLocks,
70
+ (fl) =>
71
+ fl.assetId === projectConfig.projectId && fl.type === "globalContexts"
72
+ );
73
+ }
74
+ }
75
+
76
+ export function getGlobalContextsResourcePath(
77
+ context: PlasmicContext,
78
+ projectConfig: ProjectConfig
79
+ ) {
80
+ return projectConfig.globalContextsFilePath !== ""
81
+ ? projectConfig.globalContextsFilePath
82
+ : defaultResourcePath(
83
+ context,
84
+ projectConfig,
85
+ `${COMPONENT_NAME}.${context.config.code.lang === "ts" ? "tsx" : "jsx"}`
86
+ );
87
+ }
@@ -64,6 +64,7 @@ import { syncGlobalVariants } from "./sync-global-variants";
64
64
  import { syncProjectIconAssets } from "./sync-icons";
65
65
  import { syncProjectImageAssets } from "./sync-images";
66
66
  import { upsertStyleTokens } from "./sync-styles";
67
+ import { syncGlobalContexts } from "./sync-global-contexts";
67
68
 
68
69
  export interface SyncArgs extends CommonArgs {
69
70
  projects: readonly string[];
@@ -417,11 +418,15 @@ export async function sync(
417
418
  }
418
419
 
419
420
  const codegenVersion = await context.api.latestCodegenVersion();
420
- context.lock.projects.forEach(p => {
421
- if (projectsToSync.some(syncedProject => syncedProject.projectId === p.projectId)) {
421
+ context.lock.projects.forEach((p) => {
422
+ if (
423
+ projectsToSync.some(
424
+ (syncedProject) => syncedProject.projectId === p.projectId
425
+ )
426
+ ) {
422
427
  p.codegenVersion = codegenVersion;
423
428
  }
424
- })
429
+ });
425
430
  // Write the new ComponentConfigs to disk
426
431
  await updateConfig(context, context.config, baseDir);
427
432
  });
@@ -787,6 +792,15 @@ async function syncProjectConfig(
787
792
  delete projectConfig.jsBundleThemes;
788
793
  }
789
794
 
795
+ await syncGlobalContexts(
796
+ context,
797
+ projectBundle,
798
+ projectConfig,
799
+ projectLock,
800
+ checksums,
801
+ baseDir
802
+ );
803
+
790
804
  // Write out components
791
805
  await syncProjectComponents(
792
806
  context,
package/src/api.ts CHANGED
@@ -39,6 +39,11 @@ export interface GlobalVariantBundle {
39
39
  contextFileName: string;
40
40
  }
41
41
 
42
+ export interface GlobalContextBundle {
43
+ id: string;
44
+ contextModule: string;
45
+ }
46
+
42
47
  export interface JsBundleTheme {
43
48
  themeFileName: string;
44
49
  themeModule: string;
@@ -51,6 +56,7 @@ export interface ProjectMetaBundle {
51
56
  cssFileName: string;
52
57
  cssRules: string;
53
58
  jsBundleThemes?: JsBundleTheme[];
59
+ globalContextBundle?: GlobalContextBundle;
54
60
  }
55
61
 
56
62
  export interface IconBundle {
@@ -142,6 +148,8 @@ export interface ChecksumBundle {
142
148
  globalVariantChecksums: Array<[string, string]>;
143
149
  // Checksum of projectCss file
144
150
  projectCssChecksum: string;
151
+ // Checksum of project global contexts
152
+ globalContextsChecksum: string;
145
153
  }
146
154
 
147
155
  export interface CodeComponentMeta {
@@ -148,6 +148,7 @@ export const project1Config: ProjectConfig = {
148
148
  images: [],
149
149
  jsBundleThemes: [],
150
150
  indirect: false,
151
+ globalContextsFilePath: "",
151
152
  };
152
153
 
153
154
  export function expectProject1PlasmicJson(
@@ -25,6 +25,7 @@ export function getChecksums(
25
25
  cssRulesChecksums: [],
26
26
  globalVariantChecksums: [],
27
27
  projectCssChecksum: "",
28
+ globalContextsChecksum: "",
28
29
  };
29
30
  }
30
31
 
@@ -95,6 +96,16 @@ export function getChecksums(
95
96
  const projectCssChecksum =
96
97
  projectCssChecksums.length > 0 ? projectCssChecksums[0].checksum : "";
97
98
 
99
+ const globalContextsChecksums = fileLocks.filter(
100
+ (fileLock) =>
101
+ fileLock.type === "globalContexts" && fileLock.assetId === projectId
102
+ );
103
+ assert(globalContextsChecksums.length < 2);
104
+ const globalContextsChecksum =
105
+ globalContextsChecksums.length > 0
106
+ ? globalContextsChecksums[0].checksum
107
+ : "";
108
+
98
109
  return {
99
110
  imageChecksums,
100
111
  iconChecksums,
@@ -102,5 +113,6 @@ export function getChecksums(
102
113
  cssRulesChecksums,
103
114
  globalVariantChecksums,
104
115
  projectCssChecksum,
116
+ globalContextsChecksum,
105
117
  };
106
118
  }
@@ -8,6 +8,7 @@ import * as Prettier from "prettier";
8
8
  import { Options, resolveConfig } from "prettier";
9
9
  import * as ts from "typescript";
10
10
  import path from "upath";
11
+ import { getGlobalContextsResourcePath } from "../actions/sync-global-contexts";
11
12
  import {
12
13
  fixComponentCssReferences,
13
14
  fixComponentImagesReferences,
@@ -17,6 +18,7 @@ import { HandledError } from "../utils/error";
17
18
  import {
18
19
  CodeComponentConfig,
19
20
  ComponentConfig,
21
+ CONFIG_FILE_NAME,
20
22
  GlobalVariantGroupConfig,
21
23
  IconConfig,
22
24
  ImageConfig,
@@ -473,6 +475,7 @@ export async function fixAllImportStatements(
473
475
  }
474
476
  }
475
477
  }
478
+ fixGlobalContextImportStatements(context, fixImportContext, baseDir);
476
479
  }
477
480
 
478
481
  async function fixComponentImportStatements(
@@ -654,3 +657,42 @@ export const formatScript = (code: string, baseDir: string) => {
654
657
  useTabs: false,
655
658
  });
656
659
  };
660
+
661
+ async function fixGlobalContextImportStatements(
662
+ context: PlasmicContext,
663
+ fixImportContext: FixImportContext,
664
+ baseDir: string
665
+ ) {
666
+ for (const project of context.config.projects) {
667
+ if (!project.globalContextsFilePath) continue;
668
+ const resourcePath = getGlobalContextsResourcePath(context, project);
669
+
670
+ let prevContent: string;
671
+ try {
672
+ prevContent = readFileText(
673
+ makeFilePath(context, resourcePath)
674
+ ).toString();
675
+ } catch (e) {
676
+ logger.warn(
677
+ `${resourcePath} is missing. If you deleted this component, remember to remove the component from ${CONFIG_FILE_NAME}`
678
+ );
679
+ throw e;
680
+ }
681
+
682
+ const newContent = replaceImports(
683
+ context,
684
+ prevContent,
685
+ resourcePath,
686
+ fixImportContext,
687
+ false,
688
+ baseDir,
689
+ true
690
+ );
691
+
692
+ if (prevContent !== newContent) {
693
+ await writeFileContent(context, resourcePath, newContent, {
694
+ force: true,
695
+ });
696
+ }
697
+ }
698
+ }
@@ -159,6 +159,8 @@ export interface ProjectConfig {
159
159
  version: string;
160
160
  /** File location for the project-wide css styles. Relative to srcDir */
161
161
  cssFilePath: string;
162
+ /** File location for the project-wide global contexts. Relative to srcDir */
163
+ globalContextsFilePath: string;
162
164
 
163
165
  // Code-component-related fields can be treated as optional not to be shown
164
166
  // to the users nor appear to be missing in the documentation.
@@ -197,6 +199,7 @@ export function createProjectConfig(base: {
197
199
  icons: [],
198
200
  images: [],
199
201
  indirect: base.indirect,
202
+ globalContextsFilePath: "",
200
203
  };
201
204
  }
202
205
 
@@ -299,7 +302,8 @@ export interface FileLock {
299
302
  | "icon"
300
303
  | "image"
301
304
  | "projectCss"
302
- | "globalVariant";
305
+ | "globalVariant"
306
+ | "globalContexts";
303
307
  // The checksum value for the file
304
308
  checksum: string;
305
309
  // The component id, or the image asset id
@@ -530,6 +534,7 @@ export function getOrAddProjectConfig(
530
534
  images: [],
531
535
  jsBundleThemes: [],
532
536
  indirect: false,
537
+ globalContextsFilePath: "",
533
538
  };
534
539
  context.config.projects.push(project);
535
540
  }
@@ -242,6 +242,7 @@ function getAllPaths(context: PlasmicContext): BundleKeyPair[] {
242
242
 
243
243
  const pushProject = (proj: ProjectConfig) => {
244
244
  pushPath(proj, "cssFilePath");
245
+ pushPath(proj, "globalContextsFilePath");
245
246
  for (const component of proj.components) {
246
247
  pushComponent(component);
247
248
  }
@@ -282,7 +283,10 @@ function getAllPaths(context: PlasmicContext): BundleKeyPair[] {
282
283
  * Fixes all src-relative file paths in PlasmicConfig by detecting file
283
284
  * movement on disk.
284
285
  */
285
- export async function fixAllFilePaths(context: PlasmicContext, baseDir: string) {
286
+ export async function fixAllFilePaths(
287
+ context: PlasmicContext,
288
+ baseDir: string
289
+ ) {
286
290
  const baseNameToFiles = buildBaseNameToFiles(context);
287
291
  let changed = false;
288
292
 
@@ -79,10 +79,7 @@ function removeMissingFilesFromLock(
79
79
  ])
80
80
  );
81
81
  const knownIcons = Object.fromEntries(
82
- knownProjects[project.projectId].icons.map((icons) => [
83
- icons.id,
84
- icons,
85
- ])
82
+ knownProjects[project.projectId].icons.map((icons) => [icons.id, icons])
86
83
  );
87
84
 
88
85
  project.fileLocks = project.fileLocks.filter((lock) => {
@@ -100,6 +97,8 @@ function removeMissingFilesFromLock(
100
97
  return knownImages[lock.assetId];
101
98
  case "icon":
102
99
  return knownIcons[lock.assetId];
100
+ case "globalContexts":
101
+ return knownProjects[project.projectId].globalContextsFilePath;
103
102
  }
104
103
  });
105
104
 
@@ -209,6 +208,16 @@ async function resolveMissingFilesInConfig(
209
208
  baseNameToFiles
210
209
  )) || "";
211
210
 
211
+ if (!project.globalContextsFilePath) {
212
+ project.globalContextsFilePath = "";
213
+ }
214
+ project.globalContextsFilePath =
215
+ (await attemptToRestoreFilePath(
216
+ context,
217
+ project.globalContextsFilePath,
218
+ baseNameToFiles
219
+ )) || "";
220
+
212
221
  project.images = await filterFiles(project.images, "filePath");
213
222
  project.icons = await filterFiles(project.icons, "moduleFilePath");
214
223
  project.jsBundleThemes = await filterFiles(