@plasmicapp/cli 0.1.162

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.
Files changed (162) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.idea/cli.iml +11 -0
  3. package/.idea/misc.xml +6 -0
  4. package/.idea/modules.xml +8 -0
  5. package/.idea/vcs.xml +6 -0
  6. package/README +16 -0
  7. package/README.internal +46 -0
  8. package/README.md +17 -0
  9. package/build.sh +8 -0
  10. package/dist/__mocks__/api.d.ts +16 -0
  11. package/dist/__mocks__/api.js +297 -0
  12. package/dist/__tests__/code-utils-spec.d.ts +1 -0
  13. package/dist/__tests__/code-utils-spec.js +838 -0
  14. package/dist/__tests__/ftue-spec.d.ts +1 -0
  15. package/dist/__tests__/ftue-spec.js +39 -0
  16. package/dist/__tests__/project-api-token-spec.d.ts +1 -0
  17. package/dist/__tests__/project-api-token-spec.js +147 -0
  18. package/dist/__tests__/versioned-sync-spec.d.ts +1 -0
  19. package/dist/__tests__/versioned-sync-spec.js +145 -0
  20. package/dist/actions/auth.d.ts +8 -0
  21. package/dist/actions/auth.js +47 -0
  22. package/dist/actions/fix-imports.d.ts +4 -0
  23. package/dist/actions/fix-imports.js +25 -0
  24. package/dist/actions/init.d.ts +62 -0
  25. package/dist/actions/init.js +460 -0
  26. package/dist/actions/project-token.d.ts +6 -0
  27. package/dist/actions/project-token.js +42 -0
  28. package/dist/actions/sync-components.d.ts +10 -0
  29. package/dist/actions/sync-components.js +242 -0
  30. package/dist/actions/sync-global-variants.d.ts +3 -0
  31. package/dist/actions/sync-global-variants.js +89 -0
  32. package/dist/actions/sync-icons.d.ts +7 -0
  33. package/dist/actions/sync-icons.js +92 -0
  34. package/dist/actions/sync-images.d.ts +6 -0
  35. package/dist/actions/sync-images.js +137 -0
  36. package/dist/actions/sync-styles.d.ts +3 -0
  37. package/dist/actions/sync-styles.js +58 -0
  38. package/dist/actions/sync.d.ts +25 -0
  39. package/dist/actions/sync.js +417 -0
  40. package/dist/actions/upload-bundle.d.ts +15 -0
  41. package/dist/actions/upload-bundle.js +28 -0
  42. package/dist/actions/watch.d.ts +14 -0
  43. package/dist/actions/watch.js +90 -0
  44. package/dist/api.d.ts +182 -0
  45. package/dist/api.js +202 -0
  46. package/dist/deps.d.ts +2 -0
  47. package/dist/deps.js +20 -0
  48. package/dist/index.d.ts +7 -0
  49. package/dist/index.js +247 -0
  50. package/dist/lib.d.ts +10 -0
  51. package/dist/lib.js +23 -0
  52. package/dist/migrations/0.1.110-fileLocks.d.ts +2 -0
  53. package/dist/migrations/0.1.110-fileLocks.js +15 -0
  54. package/dist/migrations/0.1.143-ensureImportModuleType.d.ts +2 -0
  55. package/dist/migrations/0.1.143-ensureImportModuleType.js +12 -0
  56. package/dist/migrations/0.1.146-addReactRuntime.d.ts +2 -0
  57. package/dist/migrations/0.1.146-addReactRuntime.js +10 -0
  58. package/dist/migrations/0.1.27-migrateInit.d.ts +1 -0
  59. package/dist/migrations/0.1.27-migrateInit.js +8 -0
  60. package/dist/migrations/0.1.28-tsToTsx.d.ts +3 -0
  61. package/dist/migrations/0.1.28-tsToTsx.js +33 -0
  62. package/dist/migrations/0.1.31-ensureProjectIcons.d.ts +2 -0
  63. package/dist/migrations/0.1.31-ensureProjectIcons.js +12 -0
  64. package/dist/migrations/0.1.42-ensureVersion.d.ts +2 -0
  65. package/dist/migrations/0.1.42-ensureVersion.js +12 -0
  66. package/dist/migrations/0.1.57-ensureJsBundleThemes.d.ts +2 -0
  67. package/dist/migrations/0.1.57-ensureJsBundleThemes.js +12 -0
  68. package/dist/migrations/0.1.64-imageFiles.d.ts +2 -0
  69. package/dist/migrations/0.1.64-imageFiles.js +17 -0
  70. package/dist/migrations/0.1.95-componentType.d.ts +2 -0
  71. package/dist/migrations/0.1.95-componentType.js +16 -0
  72. package/dist/migrations/migrations.d.ts +10 -0
  73. package/dist/migrations/migrations.js +119 -0
  74. package/dist/plasmic.schema.json +463 -0
  75. package/dist/test-common/fixtures.d.ts +13 -0
  76. package/dist/test-common/fixtures.js +165 -0
  77. package/dist/tsconfig-transform.json +68 -0
  78. package/dist/utils/auth-utils.d.ts +31 -0
  79. package/dist/utils/auth-utils.js +236 -0
  80. package/dist/utils/checksum.d.ts +4 -0
  81. package/dist/utils/checksum.js +63 -0
  82. package/dist/utils/code-utils.d.ts +46 -0
  83. package/dist/utils/code-utils.js +457 -0
  84. package/dist/utils/config-utils.d.ts +271 -0
  85. package/dist/utils/config-utils.js +178 -0
  86. package/dist/utils/envdetect.d.ts +4 -0
  87. package/dist/utils/envdetect.js +42 -0
  88. package/dist/utils/error.d.ts +14 -0
  89. package/dist/utils/error.js +42 -0
  90. package/dist/utils/file-utils.d.ts +71 -0
  91. package/dist/utils/file-utils.js +433 -0
  92. package/dist/utils/get-context.d.ts +40 -0
  93. package/dist/utils/get-context.js +339 -0
  94. package/dist/utils/help.d.ts +2 -0
  95. package/dist/utils/help.js +56 -0
  96. package/dist/utils/lang-utils.d.ts +10 -0
  97. package/dist/utils/lang-utils.js +52 -0
  98. package/dist/utils/npm-utils.d.ts +28 -0
  99. package/dist/utils/npm-utils.js +215 -0
  100. package/dist/utils/prompts.d.ts +6 -0
  101. package/dist/utils/prompts.js +23 -0
  102. package/dist/utils/resolve-utils.d.ts +13 -0
  103. package/dist/utils/resolve-utils.js +198 -0
  104. package/dist/utils/semver.d.ts +34 -0
  105. package/dist/utils/semver.js +61 -0
  106. package/dist/utils/test-utils.d.ts +22 -0
  107. package/dist/utils/test-utils.js +106 -0
  108. package/dist/utils/user-utils.d.ts +7 -0
  109. package/dist/utils/user-utils.js +48 -0
  110. package/jest.config.js +6 -0
  111. package/package.json +80 -0
  112. package/src/__mocks__/api.ts +394 -0
  113. package/src/__tests__/code-utils-spec.ts +881 -0
  114. package/src/__tests__/ftue-spec.ts +43 -0
  115. package/src/__tests__/project-api-token-spec.ts +208 -0
  116. package/src/__tests__/versioned-sync-spec.ts +176 -0
  117. package/src/actions/auth.ts +43 -0
  118. package/src/actions/fix-imports.ts +13 -0
  119. package/src/actions/init.ts +638 -0
  120. package/src/actions/project-token.ts +36 -0
  121. package/src/actions/sync-components.ts +405 -0
  122. package/src/actions/sync-global-variants.ts +129 -0
  123. package/src/actions/sync-icons.ts +135 -0
  124. package/src/actions/sync-images.ts +191 -0
  125. package/src/actions/sync-styles.ts +71 -0
  126. package/src/actions/sync.ts +747 -0
  127. package/src/actions/upload-bundle.ts +38 -0
  128. package/src/actions/watch.ts +95 -0
  129. package/src/api.ts +407 -0
  130. package/src/deps.ts +18 -0
  131. package/src/index.ts +300 -0
  132. package/src/lib.ts +10 -0
  133. package/src/migrations/0.1.110-fileLocks.ts +16 -0
  134. package/src/migrations/0.1.146-addReactRuntime.ts +8 -0
  135. package/src/migrations/0.1.27-migrateInit.ts +4 -0
  136. package/src/migrations/0.1.28-tsToTsx.ts +37 -0
  137. package/src/migrations/0.1.31-ensureProjectIcons.ts +10 -0
  138. package/src/migrations/0.1.42-ensureVersion.ts +10 -0
  139. package/src/migrations/0.1.57-ensureJsBundleThemes.ts +10 -0
  140. package/src/migrations/0.1.64-imageFiles.ts +15 -0
  141. package/src/migrations/0.1.95-componentType.ts +14 -0
  142. package/src/migrations/migrations.ts +147 -0
  143. package/src/test-common/fixtures.ts +178 -0
  144. package/src/utils/auth-utils.ts +276 -0
  145. package/src/utils/checksum.ts +106 -0
  146. package/src/utils/code-utils.ts +656 -0
  147. package/src/utils/config-utils.ts +551 -0
  148. package/src/utils/envdetect.ts +39 -0
  149. package/src/utils/error.ts +36 -0
  150. package/src/utils/file-utils.ts +526 -0
  151. package/src/utils/get-context.ts +451 -0
  152. package/src/utils/help.ts +75 -0
  153. package/src/utils/lang-utils.ts +52 -0
  154. package/src/utils/npm-utils.ts +223 -0
  155. package/src/utils/prompts.ts +22 -0
  156. package/src/utils/resolve-utils.ts +245 -0
  157. package/src/utils/semver.ts +67 -0
  158. package/src/utils/test-utils.ts +116 -0
  159. package/src/utils/user-utils.ts +37 -0
  160. package/testData/fixImports_plasmic.json +66 -0
  161. package/tsconfig-transform.json +68 -0
  162. package/tsconfig.json +67 -0
@@ -0,0 +1,191 @@
1
+ import L from "lodash";
2
+ import path from "upath";
3
+ import { ChecksumBundle, ImageBundle } from "../api";
4
+ import { logger } from "../deps";
5
+ import { FixImportContext } from "../utils/code-utils";
6
+ import {
7
+ getOrAddProjectConfig,
8
+ getOrAddProjectLock,
9
+ PlasmicContext,
10
+ } from "../utils/config-utils";
11
+ import {
12
+ defaultPublicResourcePath,
13
+ defaultResourcePath,
14
+ deleteFile,
15
+ fileExists,
16
+ readFileContent,
17
+ renameFile,
18
+ writeFileContent,
19
+ } from "../utils/file-utils";
20
+ import { ensure } from "../utils/lang-utils";
21
+
22
+ export async function syncProjectImageAssets(
23
+ context: PlasmicContext,
24
+ projectId: string,
25
+ version: string,
26
+ imageBundles: ImageBundle[],
27
+ checksums: ChecksumBundle
28
+ ) {
29
+ const project = getOrAddProjectConfig(context, projectId);
30
+ const projectLock = getOrAddProjectLock(context, projectId);
31
+ const knownImageConfigs = L.keyBy(project.images, (i) => i.id);
32
+ const imageBundleIds = L.keyBy(imageBundles, (i) => i.id);
33
+ const imageFileLocks = L.keyBy(
34
+ projectLock.fileLocks.filter((fileLock) => fileLock.type === "image"),
35
+ (fl) => fl.assetId
36
+ );
37
+ const id2ImageChecksum = new Map(checksums.imageChecksums);
38
+
39
+ const deletedImages = L.filter(
40
+ knownImageConfigs,
41
+ (i) => !imageBundleIds[i.id] && !id2ImageChecksum.has(i.id)
42
+ );
43
+
44
+ for (const bundle of imageBundles) {
45
+ if (context.cliArgs.quiet !== true) {
46
+ logger.info(
47
+ `Syncing image: ${bundle.name}@${version}\t['${project.projectName}' ${project.projectId}/${bundle.id} ${project.version}]`
48
+ );
49
+ }
50
+ let imageConfig = knownImageConfigs[bundle.id];
51
+ const isNew = !imageConfig;
52
+ const defaultFilePath =
53
+ context.config.images.scheme === "public-files"
54
+ ? defaultPublicResourcePath(context, project, "images", bundle.fileName)
55
+ : defaultResourcePath(context, project, "images", bundle.fileName);
56
+ if (isNew) {
57
+ imageConfig = {
58
+ id: bundle.id,
59
+ name: bundle.name,
60
+ filePath: defaultFilePath,
61
+ };
62
+ project.images.push(imageConfig);
63
+ } else {
64
+ const filePath = path.join(
65
+ path.dirname(imageConfig.filePath),
66
+ path.basename(defaultFilePath)
67
+ );
68
+ if (
69
+ imageConfig.filePath !== filePath &&
70
+ fileExists(context, imageConfig.filePath)
71
+ ) {
72
+ if (context.cliArgs.quiet !== true) {
73
+ logger.info(
74
+ `Renaming image: ${imageConfig.name}@${version}\t['${project.projectName}' ${project.projectId}/${bundle.id} ${project.version}]`
75
+ );
76
+ }
77
+ renameFile(context, imageConfig.filePath, filePath);
78
+ imageConfig.filePath = filePath;
79
+ }
80
+ imageConfig.name = bundle.name;
81
+ }
82
+
83
+ // Update FileLocks
84
+ if (imageFileLocks[bundle.id]) {
85
+ imageFileLocks[bundle.id].checksum = ensure(
86
+ id2ImageChecksum.get(bundle.id)
87
+ );
88
+ } else {
89
+ projectLock.fileLocks.push({
90
+ type: "image",
91
+ assetId: bundle.id,
92
+ checksum: ensure(id2ImageChecksum.get(bundle.id)),
93
+ });
94
+ }
95
+
96
+ await writeFileContent(
97
+ context,
98
+ imageConfig.filePath,
99
+ Buffer.from(bundle.blob, "base64"),
100
+ {
101
+ force: !isNew,
102
+ }
103
+ );
104
+ }
105
+
106
+ const deletedImageFiles = new Set<string>();
107
+ for (const deletedImage of deletedImages) {
108
+ const imageConfig = knownImageConfigs[deletedImage.id];
109
+ if (fileExists(context, imageConfig.filePath)) {
110
+ logger.info(
111
+ `Deleting image: ${imageConfig.name}@${version}\t['${project.projectName}' ${project.projectId}/${deletedImage.id} ${project.version}]`
112
+ );
113
+ deleteFile(context, imageConfig.filePath);
114
+ deletedImageFiles.add(deletedImage.id);
115
+ }
116
+ }
117
+ project.images = project.images.filter((i) => !deletedImageFiles.has(i.id));
118
+
119
+ const deletedImageIds = new Set(deletedImages.map((i) => i.id));
120
+ projectLock.fileLocks = projectLock.fileLocks.filter(
121
+ (fileLock) =>
122
+ fileLock.type !== "image" || !deletedImageIds.has(fileLock.assetId)
123
+ );
124
+ }
125
+
126
+ const RE_ASSETCSSREF_ALL = /var\(--image-([^\)]+)\)/g;
127
+ export async function fixComponentCssReferences(
128
+ context: PlasmicContext,
129
+ fixImportContext: FixImportContext,
130
+ cssFilePath: string
131
+ ) {
132
+ if (!fileExists(context, cssFilePath)) {
133
+ return;
134
+ }
135
+
136
+ const prevContent = readFileContent(context, cssFilePath);
137
+ const newContent = prevContent.replace(RE_ASSETCSSREF_ALL, (sub, assetId) => {
138
+ const asset = fixImportContext.images[assetId];
139
+ if (asset) {
140
+ return context.config.images.scheme === "public-files"
141
+ ? `url("${path.join(
142
+ "/",
143
+ ensure(context.config.images.publicUrlPrefix),
144
+ path.relative(
145
+ ensure(context.config.images.publicDir),
146
+ asset.filePath
147
+ )
148
+ )}")`
149
+ : `url("./${path.relative(
150
+ path.dirname(cssFilePath),
151
+ asset.filePath
152
+ )}")`;
153
+ } else {
154
+ return sub;
155
+ }
156
+ });
157
+
158
+ if (prevContent !== newContent) {
159
+ await writeFileContent(context, cssFilePath, newContent, { force: true });
160
+ }
161
+ }
162
+
163
+ const RE_ASSETTSXREF_ALL = /Plasmic_Image_([^\)\s]+)__Ref/g;
164
+ export async function fixComponentImagesReferences(
165
+ context: PlasmicContext,
166
+ fixImportContext: FixImportContext,
167
+ renderModuleFilePath: string
168
+ ) {
169
+ const prevContent = readFileContent(context, renderModuleFilePath);
170
+ const newContent = prevContent.replace(RE_ASSETTSXREF_ALL, (sub, assetId) => {
171
+ const asset = fixImportContext.images[assetId];
172
+ if (asset) {
173
+ return path.join(
174
+ "/",
175
+ ensure(context.config.images.publicUrlPrefix),
176
+ path.relative(ensure(context.config.images.publicDir), asset.filePath)
177
+ );
178
+ } else {
179
+ return sub;
180
+ }
181
+ });
182
+
183
+ if (prevContent !== newContent) {
184
+ await writeFileContent(context, renderModuleFilePath, newContent, {
185
+ force: true,
186
+ });
187
+ // Returns true if the content changed
188
+ return true;
189
+ }
190
+ return false;
191
+ }
@@ -0,0 +1,71 @@
1
+ import { StyleTokensMap } from "../api";
2
+ import { PlasmicContext } from "../utils/config-utils";
3
+ import { HandledError } from "../utils/error";
4
+ import {
5
+ fileExists,
6
+ readFileContent,
7
+ writeFileContent,
8
+ } from "../utils/file-utils";
9
+
10
+ export async function upsertStyleTokens(
11
+ context: PlasmicContext,
12
+ newStyleMap: StyleTokensMap
13
+ ) {
14
+ const curStyleMap = await readCurStyleMap(context);
15
+ for (const prop of newStyleMap.props) {
16
+ const index = curStyleMap.props.findIndex(
17
+ (p) => p.meta.id === prop.meta.id
18
+ );
19
+ if (index >= 0) {
20
+ curStyleMap.props[index] = prop;
21
+ } else {
22
+ curStyleMap.props.push(prop);
23
+ }
24
+ }
25
+ curStyleMap.props.sort((prop1, prop2) =>
26
+ prop1.name === prop2.name ? 0 : prop1.name < prop2.name ? -1 : 1
27
+ );
28
+ await writeFileContent(
29
+ context,
30
+ context.config.tokens.tokensFilePath,
31
+
32
+ JSON.stringify(curStyleMap, undefined, 2),
33
+
34
+ { force: true }
35
+ );
36
+ }
37
+
38
+ async function readCurStyleMap(
39
+ context: PlasmicContext
40
+ ): Promise<StyleTokensMap> {
41
+ const filePath = context.config.tokens.tokensFilePath;
42
+ if (fileExists(context, filePath)) {
43
+ try {
44
+ return JSON.parse(
45
+ readFileContent(context, context.config.tokens.tokensFilePath)
46
+ );
47
+ } catch (e) {
48
+ throw new HandledError(
49
+ `Error encountered reading ${context.config.tokens.tokensFilePath}: ${e}`
50
+ );
51
+ }
52
+ } else {
53
+ const defaultMap = {
54
+ props: [],
55
+ global: {
56
+ meta: {
57
+ source: "plasmic.app",
58
+ },
59
+ },
60
+ } as StyleTokensMap;
61
+ await writeFileContent(
62
+ context,
63
+ context.config.tokens.tokensFilePath,
64
+ JSON.stringify(defaultMap, undefined, 2),
65
+ {
66
+ force: false,
67
+ }
68
+ );
69
+ return defaultMap;
70
+ }
71
+ }