@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,433 @@
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.existsBuffered = exports.deleteFileBuffered = exports.renameFileBuffered = exports.readFileText = exports.writeFileText = exports.withBufferedFs = exports.assertAllPathsInRootDir = exports.fixAllFilePaths = exports.findFile = exports.findSrcDirPath = exports.buildBaseNameToFiles = exports.renameFile = exports.makeFilePath = exports.fileExists = exports.deleteFile = exports.readFileContent = exports.writeFileContent = exports.defaultPagePath = exports.defaultPublicResourcePath = exports.defaultResourcePath = exports.writeFileContentRaw = exports.stripExtension = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const glob_1 = __importDefault(require("glob"));
18
+ const lodash_1 = __importDefault(require("lodash"));
19
+ const upath_1 = __importDefault(require("upath"));
20
+ const deps_1 = require("../deps");
21
+ const error_1 = require("../utils/error");
22
+ const code_utils_1 = require("./code-utils");
23
+ const config_utils_1 = require("./config-utils");
24
+ const lang_utils_1 = require("./lang-utils");
25
+ const user_utils_1 = require("./user-utils");
26
+ function stripExtension(filename, removeComposedPath = false) {
27
+ const ext = removeComposedPath
28
+ ? filename.substring(filename.indexOf("."))
29
+ : upath_1.default.extname(filename);
30
+ if (!ext || filename === ext) {
31
+ return filename;
32
+ }
33
+ return filename.substring(0, filename.lastIndexOf(ext));
34
+ }
35
+ exports.stripExtension = stripExtension;
36
+ function writeFileContentRaw(filePath, content, opts) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ opts = opts || {};
39
+ if (existsBuffered(filePath) && !opts.force) {
40
+ const overwrite = yield user_utils_1.confirmWithUser(`File ${filePath} already exists. Do you want to overwrite?`, opts.yes);
41
+ if (!overwrite) {
42
+ throw new error_1.HandledError(`Cannot write to ${filePath}; file already exists.`);
43
+ }
44
+ }
45
+ fs_1.default.mkdirSync(upath_1.default.dirname(filePath), { recursive: true });
46
+ writeFileText(filePath, content);
47
+ });
48
+ }
49
+ exports.writeFileContentRaw = writeFileContentRaw;
50
+ function defaultResourcePath(context, project, ...subpaths) {
51
+ const projectName = lodash_1.default.isString(project) ? project : project.projectName;
52
+ return upath_1.default.join(context.config.defaultPlasmicDir, lodash_1.default.snakeCase(projectName), ...subpaths);
53
+ }
54
+ exports.defaultResourcePath = defaultResourcePath;
55
+ function defaultPublicResourcePath(context, project, ...subpaths) {
56
+ return upath_1.default.join(context.config.images.publicDir, "plasmic", lodash_1.default.snakeCase(project.projectName), ...subpaths);
57
+ }
58
+ exports.defaultPublicResourcePath = defaultPublicResourcePath;
59
+ function defaultPagePath(context, fileName) {
60
+ var _a, _b;
61
+ if (context.config.platform === "nextjs") {
62
+ return upath_1.default.join(((_a = context.config.nextjsConfig) === null || _a === void 0 ? void 0 : _a.pagesDir) || "", fileName);
63
+ }
64
+ if (context.config.platform === "gatsby") {
65
+ return upath_1.default.join(((_b = context.config.gatsbyConfig) === null || _b === void 0 ? void 0 : _b.pagesDir) || "", fileName);
66
+ }
67
+ return fileName;
68
+ }
69
+ exports.defaultPagePath = defaultPagePath;
70
+ function writeFileContent(context, srcDirFilePath, content, opts = {}) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ const path = makeFilePath(context, srcDirFilePath);
73
+ yield writeFileContentRaw(path, content, Object.assign({ yes: context.cliArgs.yes }, opts));
74
+ });
75
+ }
76
+ exports.writeFileContent = writeFileContent;
77
+ function readFileContent(context, srcDirFilePath) {
78
+ const path = makeFilePath(context, srcDirFilePath);
79
+ return readFileText(path);
80
+ }
81
+ exports.readFileContent = readFileContent;
82
+ function deleteFile(context, srcDirFilePath) {
83
+ const path = makeFilePath(context, srcDirFilePath);
84
+ deleteFileBuffered(path);
85
+ }
86
+ exports.deleteFile = deleteFile;
87
+ function fileExists(context, srcDirFilePath) {
88
+ return existsBuffered(makeFilePath(context, srcDirFilePath));
89
+ }
90
+ exports.fileExists = fileExists;
91
+ function makeFilePath(context, filePath) {
92
+ return upath_1.default.isAbsolute(filePath)
93
+ ? filePath
94
+ : upath_1.default.join(context.absoluteSrcDir, filePath);
95
+ }
96
+ exports.makeFilePath = makeFilePath;
97
+ function renameFile(context, oldPath, newPath) {
98
+ renameFileBuffered(makeFilePath(context, oldPath), makeFilePath(context, newPath));
99
+ }
100
+ exports.renameFile = renameFile;
101
+ /**
102
+ * Returns absolute paths of all Plasmic managed files found, grouped by each basename
103
+ * for example:
104
+ * {
105
+ * "file.txt": [ "/path1/file.txt", "/path2.txt" ]
106
+ * ...
107
+ * }
108
+ * @param {PlasmicContext} context
109
+ * @returns {Record<string, string[]>}
110
+ **/
111
+ function buildBaseNameToFiles(context) {
112
+ const srcDir = context.absoluteSrcDir;
113
+ const scriptFileExt = context.config.code.lang === "ts" ? "tsx" : "jsx";
114
+ const allFiles = glob_1.default.sync(`${srcDir}/**/*.+(ts|css|${scriptFileExt}|json)`, {
115
+ ignore: [`${srcDir}/**/node_modules/**/*`],
116
+ });
117
+ return lodash_1.default.groupBy(allFiles, (f) => upath_1.default.basename(f));
118
+ }
119
+ exports.buildBaseNameToFiles = buildBaseNameToFiles;
120
+ /**
121
+ * Tries to find the file at `srcDir/expectedPath`. If it's not there, tries to detect if it has
122
+ * been moved to a different location. Returns the found location relative to the `srcDir`.
123
+ *
124
+ * If `expectedPath` doesn't exist, but there's more than one file of that name in `baseNameToFiles`, then
125
+ * error and quit. If no file of that name can be found, `expectedPath` is returned.
126
+ */
127
+ function findSrcDirPath(absoluteSrcDir, expectedPath, baseNameToFiles) {
128
+ if (!upath_1.default.isAbsolute(absoluteSrcDir)) {
129
+ throw new error_1.HandledError("Cannot find srcDir. Please check plasmic.json.");
130
+ }
131
+ const fileName = upath_1.default.basename(expectedPath);
132
+ if (existsBuffered(upath_1.default.join(absoluteSrcDir, expectedPath))) {
133
+ return expectedPath;
134
+ }
135
+ else if (!(fileName in baseNameToFiles)) {
136
+ return expectedPath;
137
+ }
138
+ else if (baseNameToFiles[fileName].length === 1) {
139
+ // There's only one file of the same name, so maybe we've been moved there?
140
+ const newPath = upath_1.default.relative(absoluteSrcDir, baseNameToFiles[fileName][0]);
141
+ deps_1.logger.info(`\tDetected file moved from ${expectedPath} to ${newPath}`);
142
+ return newPath;
143
+ }
144
+ else {
145
+ throw new error_1.HandledError(`Cannot find expected file at ${expectedPath}, and found multiple possible matching files ${baseNameToFiles[fileName]}. Please update plasmic.config with the real location for ${fileName}.`);
146
+ }
147
+ }
148
+ exports.findSrcDirPath = findSrcDirPath;
149
+ /**
150
+ * Finds the full path to the first file satisfying `pred` in `dir`. If
151
+ * `opts.traverseParents` is set to true, then will also look in ancestor
152
+ * directories until the plasmic.json file is found. If none is found,
153
+ * returns undefined.
154
+ */
155
+ function findFile(dir, pred, opts) {
156
+ const files = fs_1.default.readdirSync(dir);
157
+ const found = files.find((f) => pred(f));
158
+ if (found) {
159
+ return upath_1.default.join(dir, found);
160
+ }
161
+ if (!opts.traverseParents) {
162
+ return undefined;
163
+ }
164
+ const parent = upath_1.default.dirname(dir);
165
+ if (parent === dir) {
166
+ // We've hit the root dir already
167
+ return undefined;
168
+ }
169
+ return findFile(upath_1.default.dirname(dir), pred, opts);
170
+ }
171
+ exports.findFile = findFile;
172
+ /**
173
+ * Parses a configuration and returns file/dir paths in it (in the format
174
+ * BundleKeyPair to allow the caller to change these paths).
175
+ */
176
+ function getAllPaths(context) {
177
+ const config = context.config;
178
+ const pairs = [];
179
+ const pushPath = (bundle, key) => {
180
+ pairs.push({ bundle, key });
181
+ };
182
+ const pushComponent = (comp) => {
183
+ pushPath(comp, "renderModuleFilePath");
184
+ pushPath(comp, "cssFilePath");
185
+ if (code_utils_1.isLocalModulePath(comp.importSpec.modulePath)) {
186
+ pushPath(comp.importSpec, "modulePath");
187
+ }
188
+ };
189
+ const pushProject = (proj) => {
190
+ pushPath(proj, "cssFilePath");
191
+ for (const component of proj.components) {
192
+ pushComponent(component);
193
+ }
194
+ for (const icon of proj.icons) {
195
+ pushPath(icon, "moduleFilePath");
196
+ }
197
+ for (const image of proj.images) {
198
+ pushPath(image, "filePath");
199
+ }
200
+ };
201
+ for (const project of config.projects) {
202
+ pushProject(project);
203
+ }
204
+ for (const bundle of config.globalVariants.variantGroups) {
205
+ pushPath(bundle, "contextFilePath");
206
+ }
207
+ pushPath(config.tokens, "tokensFilePath");
208
+ pushPath(config.style, "defaultStyleCssFilePath");
209
+ pushPath(config, "defaultPlasmicDir");
210
+ if (config.images.publicDir) {
211
+ pushPath(config.images, "publicDir");
212
+ }
213
+ if (config.gatsbyConfig) {
214
+ pushPath(config.gatsbyConfig, "pagesDir");
215
+ }
216
+ if (config.nextjsConfig) {
217
+ pushPath(config.nextjsConfig, "pagesDir");
218
+ }
219
+ return pairs;
220
+ }
221
+ /**
222
+ * Fixes all src-relative file paths in PlasmicConfig by detecting file
223
+ * movement on disk.
224
+ */
225
+ function fixAllFilePaths(context, baseDir) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ const baseNameToFiles = buildBaseNameToFiles(context);
228
+ let changed = false;
229
+ const paths = getAllPaths(context);
230
+ for (const { bundle, key } of paths) {
231
+ const known = bundle[key];
232
+ // Check null and undefined
233
+ if (known == null) {
234
+ throw new error_1.HandledError(`"${key} is required, but missing in ${config_utils_1.CONFIG_FILE_NAME}. Please restore the file or delete from ${config_utils_1.CONFIG_FILE_NAME} and run plasmic sync: ${bundle}"`);
235
+ }
236
+ // Check falsey values (e.g. "")
237
+ if (!known) {
238
+ continue;
239
+ }
240
+ const found = findSrcDirPath(context.absoluteSrcDir, known, baseNameToFiles);
241
+ if (known !== found) {
242
+ bundle[key] = found;
243
+ changed = true;
244
+ }
245
+ }
246
+ if (changed) {
247
+ yield config_utils_1.updateConfig(context, context.config, baseDir);
248
+ }
249
+ });
250
+ }
251
+ exports.fixAllFilePaths = fixAllFilePaths;
252
+ /**
253
+ * Throws an error if some file in PlasmicConfig is not inside the root
254
+ * directory (i.e., the directory containing plasmic.json).
255
+ */
256
+ function assertAllPathsInRootDir(context) {
257
+ // Do not run this check when running in PlasmicLoader environment
258
+ if (process.env.PLASMIC_LOADER) {
259
+ return;
260
+ }
261
+ if (!context.absoluteSrcDir.startsWith(context.rootDir)) {
262
+ throw new error_1.HandledError(`"srcDir" in ${config_utils_1.CONFIG_FILE_NAME} is outside of ${context.rootDir}`);
263
+ }
264
+ const paths = getAllPaths(context);
265
+ for (const { bundle, key } of paths) {
266
+ const relPath = bundle[key];
267
+ if (!relPath) {
268
+ continue;
269
+ }
270
+ const absPath = upath_1.default.resolve(context.absoluteSrcDir, relPath);
271
+ if (!absPath.startsWith(context.rootDir)) {
272
+ throw new error_1.HandledError(`The path "${relPath}" in ${config_utils_1.CONFIG_FILE_NAME} is outside of ${context.rootDir}`);
273
+ }
274
+ }
275
+ }
276
+ exports.assertAllPathsInRootDir = assertAllPathsInRootDir;
277
+ /** Whether we're currently recording to the buffer. */
278
+ let buffering = false;
279
+ /** List of buffer actions. */
280
+ const buffer = new Map();
281
+ const renamedFiles = new Map();
282
+ /**
283
+ * This turns on buffering of file writes/reads.
284
+ *
285
+ * This is useful for reducing the extent to which our file updates are scattered over time, which can cause webpack
286
+ * dev server to trip up.
287
+ *
288
+ * This also has the side benefit of making our CLI commands more atomic, in case of failure partway through a sync.
289
+ */
290
+ function withBufferedFs(f) {
291
+ return __awaiter(this, void 0, void 0, function* () {
292
+ buffering = true;
293
+ buffer.clear();
294
+ renamedFiles.clear();
295
+ try {
296
+ yield f();
297
+ for (const [filePath, action] of buffer.entries()) {
298
+ switch (action.type) {
299
+ case "create":
300
+ fs_1.default.mkdirSync(upath_1.default.dirname(filePath), { recursive: true });
301
+ // eslint-disable-next-line no-restricted-properties
302
+ fs_1.default.writeFileSync(filePath, action.content);
303
+ break;
304
+ case "rename":
305
+ fs_1.default.mkdirSync(upath_1.default.dirname(action.newPath), { recursive: true });
306
+ // eslint-disable-next-line no-restricted-properties
307
+ fs_1.default.renameSync(filePath, action.newPath);
308
+ break;
309
+ case "delete":
310
+ // eslint-disable-next-line no-restricted-properties
311
+ fs_1.default.unlinkSync(filePath);
312
+ break;
313
+ }
314
+ }
315
+ }
316
+ finally {
317
+ buffering = false;
318
+ }
319
+ });
320
+ }
321
+ exports.withBufferedFs = withBufferedFs;
322
+ function writeFileText(path, content) {
323
+ if (buffering) {
324
+ buffer.set(path, { type: "create", content });
325
+ }
326
+ else {
327
+ // eslint-disable-next-line no-restricted-properties
328
+ fs_1.default.writeFileSync(path, content, "utf8");
329
+ }
330
+ }
331
+ exports.writeFileText = writeFileText;
332
+ function readFileText(path) {
333
+ if (buffering) {
334
+ const action = buffer.get(path);
335
+ if (action) {
336
+ switch (action.type) {
337
+ case "create":
338
+ return lang_utils_1.ensureString(action.content);
339
+ case "rename":
340
+ return readFileText(action.newPath);
341
+ case "delete":
342
+ throw new error_1.HandledError("File does not exists");
343
+ }
344
+ }
345
+ }
346
+ // eslint-disable-next-line no-restricted-properties
347
+ return fs_1.default.readFileSync(path, "utf8");
348
+ }
349
+ exports.readFileText = readFileText;
350
+ function renameFileBuffered(oldPath, newPath) {
351
+ if (oldPath === newPath) {
352
+ return;
353
+ }
354
+ if (buffering) {
355
+ if (!existsBuffered(oldPath)) {
356
+ throw new error_1.HandledError("File does not exists");
357
+ }
358
+ const action = buffer.get(oldPath);
359
+ if (action) {
360
+ switch (action.type) {
361
+ case "create":
362
+ buffer.set(newPath, action);
363
+ buffer.delete(oldPath);
364
+ break;
365
+ case "rename":
366
+ throw new error_1.HandledError("File does not exists");
367
+ case "delete":
368
+ throw new error_1.HandledError("File does not exists");
369
+ }
370
+ }
371
+ const renamedFile = renamedFiles.get(oldPath);
372
+ if (renamedFile !== undefined) {
373
+ oldPath = renamedFile;
374
+ }
375
+ buffer.set(oldPath, { type: "rename", newPath });
376
+ renamedFiles.set(newPath, oldPath);
377
+ }
378
+ else {
379
+ fs_1.default.mkdirSync(upath_1.default.dirname(newPath), { recursive: true });
380
+ // eslint-disable-next-line no-restricted-properties
381
+ fs_1.default.renameSync(oldPath, newPath);
382
+ }
383
+ }
384
+ exports.renameFileBuffered = renameFileBuffered;
385
+ function deleteFileBuffered(path) {
386
+ if (buffering) {
387
+ if (!existsBuffered(path)) {
388
+ throw new error_1.HandledError("File does not exists");
389
+ }
390
+ const action = buffer.get(path);
391
+ if (action) {
392
+ switch (action.type) {
393
+ case "create":
394
+ buffer.delete(path);
395
+ break;
396
+ case "rename":
397
+ throw new error_1.HandledError("File does not exists");
398
+ case "delete":
399
+ throw new error_1.HandledError("File does not exists");
400
+ }
401
+ }
402
+ else {
403
+ buffer.set(path, { type: "delete" });
404
+ }
405
+ renamedFiles.delete(path);
406
+ }
407
+ else {
408
+ // eslint-disable-next-line no-restricted-properties
409
+ fs_1.default.unlinkSync(path);
410
+ }
411
+ }
412
+ exports.deleteFileBuffered = deleteFileBuffered;
413
+ function existsBuffered(path) {
414
+ if (buffering) {
415
+ if (renamedFiles.has(path)) {
416
+ return true;
417
+ }
418
+ const action = buffer.get(path);
419
+ if (action) {
420
+ switch (action.type) {
421
+ case "create":
422
+ return true;
423
+ case "rename":
424
+ return false;
425
+ case "delete":
426
+ return false;
427
+ }
428
+ }
429
+ }
430
+ // eslint-disable-next-line no-restricted-properties
431
+ return fs_1.default.existsSync(path);
432
+ }
433
+ exports.existsBuffered = existsBuffered;
@@ -0,0 +1,40 @@
1
+ import { CommonArgs } from "../index";
2
+ import { PlasmicContext, PlasmicLock } from "./config-utils";
3
+ export declare function readLock(lockFile: string): PlasmicLock;
4
+ export declare function getContext(args: CommonArgs, { enableSkipAuth }?: {
5
+ enableSkipAuth?: boolean;
6
+ }): Promise<PlasmicContext>;
7
+ /**
8
+ * Table of where this metadata will be set
9
+ *
10
+ * Source | via |
11
+ * -------------------------------------------------------------------------
12
+ * cli | defaults | source=cli scheme=codegen command=sync|watch
13
+ * loader | defaults | source=loader scheme=loader command=sync|watch
14
+ * create-plasmic-app | env | source=create-plasmic-app
15
+ * plasmic-action | env | source=plasmic-action
16
+ */
17
+ export interface Metadata {
18
+ platform?: string;
19
+ source?: "cli" | "loader" | "create-plasmic-app" | "plasmic-action";
20
+ scheme?: "codegen" | "loader";
21
+ command?: "sync" | "watch";
22
+ }
23
+ /**
24
+ * Create a metadata bundle
25
+ * This will be used to tag Segment events (e.g. for codegen)
26
+ * Merges in:
27
+ * 1. defaults
28
+ * 2. PLASMIC_METADATA environment variable settings
29
+ * 3. arguments from the command-line
30
+ * to create a single Metadata object for Segment
31
+ * @param defaults
32
+ * @param fromArgs
33
+ */
34
+ export declare function generateMetadata(defaults: Metadata, fromArgs?: string): Metadata;
35
+ /**
36
+ * This is meant to be called from consumers of the CLI to set
37
+ * metadata into the PLASMIC_METADATA environment variable
38
+ * @param metadata
39
+ */
40
+ export declare function setMetadataEnv(metadata: Metadata): void;