@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,215 @@
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.detectPackageManager = exports.installCommand = exports.installUpgrade = exports.findPackageJsonDir = exports.isCliGloballyInstalled = exports.findInstalledVersion = exports.warnLatest = exports.getParsedPackageJson = exports.checkEngineStrict = exports.getCliVersion = exports.getParsedCliPackageJson = void 0;
16
+ const child_process_1 = require("child_process");
17
+ const fast_glob_1 = __importDefault(require("fast-glob"));
18
+ const findup_sync_1 = __importDefault(require("findup-sync"));
19
+ const latest_version_1 = __importDefault(require("latest-version"));
20
+ const path_1 = __importDefault(require("path"));
21
+ const semver_1 = __importDefault(require("semver"));
22
+ const deps_1 = require("../deps");
23
+ const file_utils_1 = require("./file-utils");
24
+ const user_utils_1 = require("./user-utils");
25
+ function getParsedCliPackageJson() {
26
+ const packageJson = findup_sync_1.default("package.json", { cwd: __dirname });
27
+ if (!packageJson) {
28
+ throw new Error(`Cannot find package.json in ancestors of ${__dirname}`);
29
+ }
30
+ return parsePackageJson(packageJson);
31
+ }
32
+ exports.getParsedCliPackageJson = getParsedCliPackageJson;
33
+ function getCliVersion() {
34
+ const j = getParsedCliPackageJson();
35
+ return j.version;
36
+ }
37
+ exports.getCliVersion = getCliVersion;
38
+ /**
39
+ * Call this to check if we match the engine policy
40
+ */
41
+ function checkEngineStrict() {
42
+ var _a;
43
+ const pkg = getParsedCliPackageJson();
44
+ const minNodeVersion = (_a = pkg === null || pkg === void 0 ? void 0 : pkg.engines) === null || _a === void 0 ? void 0 : _a.node;
45
+ if (!!minNodeVersion && !semver_1.default.satisfies(process.version, minNodeVersion)) {
46
+ deps_1.logger.warn(`Plasmic only works on Node ${minNodeVersion}`);
47
+ return false;
48
+ }
49
+ return true;
50
+ }
51
+ exports.checkEngineStrict = checkEngineStrict;
52
+ function getParsedPackageJson() {
53
+ const packageJson = findup_sync_1.default("package.json");
54
+ if (!packageJson) {
55
+ throw new Error(`Cannot find package.json`);
56
+ }
57
+ return parsePackageJson(packageJson);
58
+ }
59
+ exports.getParsedPackageJson = getParsedPackageJson;
60
+ // @TODO: is this function still used?
61
+ function warnLatest(context, pkg, baseDir, msgs, yes) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const check = yield checkVersion(context, pkg);
64
+ if (check.type === "up-to-date") {
65
+ return;
66
+ }
67
+ else if (check.type === "wrong-npm-registry") {
68
+ deps_1.logger.warn(`${msgs.requiredMsg()} Unable to find this package in your npm registry. Please update this dependency manually.`);
69
+ return;
70
+ }
71
+ if (yield user_utils_1.confirmWithUser(`${check.type === "not-installed"
72
+ ? msgs.requiredMsg()
73
+ : msgs.updateMsg(check.current, check.latest)} Do you want to ${check.type === "not-installed" ? "add" : "update"} it now?`, yes)) {
74
+ installUpgrade(pkg, baseDir);
75
+ }
76
+ });
77
+ }
78
+ exports.warnLatest = warnLatest;
79
+ function checkVersion(context, pkg) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ // Try to get the latest version from npm
82
+ let last = null;
83
+ try {
84
+ last = yield latest_version_1.default(pkg);
85
+ }
86
+ catch (e) {
87
+ // This is likely because .npmrc is set to a different registry
88
+ return { type: "wrong-npm-registry" };
89
+ }
90
+ const cur = findInstalledVersion(context, pkg);
91
+ if (!cur) {
92
+ return { type: "not-installed" };
93
+ }
94
+ if (semver_1.default.gt(last, cur)) {
95
+ return {
96
+ type: "obsolete",
97
+ latest: last,
98
+ current: cur,
99
+ };
100
+ }
101
+ return { type: "up-to-date" };
102
+ });
103
+ }
104
+ function findInstalledVersion(context, pkg) {
105
+ const filename = findInstalledPackageJsonFile(context, pkg);
106
+ if (filename) {
107
+ const json = parsePackageJson(filename);
108
+ if (json && json.name === pkg) {
109
+ return json.version;
110
+ }
111
+ }
112
+ return undefined;
113
+ }
114
+ exports.findInstalledVersion = findInstalledVersion;
115
+ /**
116
+ * Detects if the cli is globally installed. `rootDir` is the folder
117
+ * where plasmic.json is
118
+ */
119
+ function isCliGloballyInstalled(rootDir) {
120
+ const packageJsonFile = findPackageJsonPath(rootDir);
121
+ if (!packageJsonFile) {
122
+ // We assume global, as instructions state global and we can't really
123
+ // do better
124
+ return true;
125
+ }
126
+ const installedDir = __dirname;
127
+ // Else, we assume it is local if the installedDir is a subfolder of
128
+ // the root project dir
129
+ return !installedDir.startsWith(path_1.default.dirname(packageJsonFile));
130
+ }
131
+ exports.isCliGloballyInstalled = isCliGloballyInstalled;
132
+ function findPackageJsonPath(dir) {
133
+ return file_utils_1.findFile(dir, (f) => f === "package.json", {
134
+ traverseParents: true,
135
+ });
136
+ }
137
+ function findPackageJsonDir(rootDir) {
138
+ const filePath = findPackageJsonPath(rootDir);
139
+ return filePath ? path_1.default.dirname(filePath) : undefined;
140
+ }
141
+ exports.findPackageJsonDir = findPackageJsonDir;
142
+ function findInstalledPackageJsonFile(context, pkg) {
143
+ const packageJsonPath = findPackageJsonPath(context.rootDir);
144
+ const rootDir = packageJsonPath
145
+ ? path_1.default.dirname(packageJsonPath)
146
+ : context.rootDir;
147
+ const files = fast_glob_1.default.sync(`${rootDir}/**/node_modules/${pkg}/package.json`);
148
+ return files.length > 0 ? files[0] : undefined;
149
+ }
150
+ function parsePackageJson(path) {
151
+ try {
152
+ return JSON.parse(file_utils_1.readFileText(path));
153
+ }
154
+ catch (e) {
155
+ return undefined;
156
+ }
157
+ }
158
+ function installUpgrade(pkg, baseDir, opts = {}) {
159
+ const cmd = installCommand(pkg, baseDir, opts);
160
+ if (!process.env.QUIET) {
161
+ deps_1.logger.info(cmd);
162
+ }
163
+ const r = child_process_1.spawnSync(cmd, {
164
+ shell: true,
165
+ stdio: process.env.QUIET ? "ignore" : "inherit",
166
+ cwd: baseDir,
167
+ });
168
+ if (r.status === 0) {
169
+ if (!process.env.QUIET) {
170
+ deps_1.logger.info(`Successfully added ${pkg} dependency.`);
171
+ }
172
+ return true;
173
+ }
174
+ else {
175
+ deps_1.logger.warn(`Cannot add ${pkg} to your project dependencies. Please add it manually.`);
176
+ return false;
177
+ }
178
+ }
179
+ exports.installUpgrade = installUpgrade;
180
+ function installCommand(pkg, baseDir, opts = {}) {
181
+ const mgr = detectPackageManager(baseDir);
182
+ if (mgr === "yarn") {
183
+ if (opts.global) {
184
+ return `yarn global add ${pkg}`;
185
+ }
186
+ else if (opts.dev) {
187
+ return `yarn add --dev --ignore-scripts -W ${pkg}`;
188
+ }
189
+ else {
190
+ return `yarn add --ignore-scripts -W ${pkg}`;
191
+ }
192
+ }
193
+ else {
194
+ if (opts.global) {
195
+ return `npm install -g ${pkg}`;
196
+ }
197
+ else if (opts.dev) {
198
+ return `npm install --save-dev --ignore-scripts ${pkg}`;
199
+ }
200
+ else {
201
+ return `npm install --ignore-scripts ${pkg}`;
202
+ }
203
+ }
204
+ }
205
+ exports.installCommand = installCommand;
206
+ function detectPackageManager(baseDir) {
207
+ const yarnLock = findup_sync_1.default("yarn.lock", { cwd: baseDir });
208
+ if (yarnLock) {
209
+ return "yarn";
210
+ }
211
+ else {
212
+ return "npm";
213
+ }
214
+ }
215
+ exports.detectPackageManager = detectPackageManager;
@@ -0,0 +1,6 @@
1
+ export declare function askChoice<T>(question: {
2
+ message: string;
3
+ choices: T[];
4
+ defaultAnswer: T;
5
+ hidePrompt: boolean;
6
+ }): T | Promise<T>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.askChoice = void 0;
7
+ const inquirer_1 = __importDefault(require("inquirer"));
8
+ function askChoice(question) {
9
+ if (question.hidePrompt) {
10
+ return question.defaultAnswer;
11
+ }
12
+ return inquirer_1.default
13
+ .prompt([
14
+ {
15
+ name: "answer",
16
+ type: "list",
17
+ message: question.message,
18
+ choices: question.choices,
19
+ },
20
+ ])
21
+ .then((answer) => answer.answer);
22
+ }
23
+ exports.askChoice = askChoice;
@@ -0,0 +1,13 @@
1
+ import { SyncArgs } from "../actions/sync";
2
+ import { ProjectVersionMeta, VersionResolution } from "../api";
3
+ import { PlasmicContext } from "./config-utils";
4
+ /**
5
+ * Checks the versionResolution with plasmic.json, plasmic.lock, and user prompts
6
+ * to compute which projects should be synced
7
+ * @param versionResolution
8
+ * @param context
9
+ */
10
+ export declare function checkVersionResolution(versionResolution: VersionResolution, context: PlasmicContext, opts: SyncArgs): Promise<ProjectVersionMeta[]>;
11
+ export declare function getDependencies(projectId: string, version: string, versionResolution: VersionResolution): {
12
+ [projectId: string]: string;
13
+ } | undefined;
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ exports.getDependencies = exports.checkVersionResolution = void 0;
35
+ const lodash_1 = __importDefault(require("lodash"));
36
+ const deps_1 = require("../deps");
37
+ const config_utils_1 = require("./config-utils");
38
+ const error_1 = require("./error");
39
+ const lang_utils_1 = require("./lang-utils");
40
+ const semver = __importStar(require("./semver"));
41
+ const user_utils_1 = require("./user-utils");
42
+ /**
43
+ * Starting at the root, do a BFS of the full dependency tree
44
+ * Because ProjectVersionMeta only stores the (projectId, version),
45
+ * we need to search for the full ProjectVersionMeta of dependencies from `available`
46
+ * @param root
47
+ * @param versionResolution
48
+ */
49
+ function walkDependencyTree(root, available) {
50
+ const queue = [root];
51
+ const result = [];
52
+ const getMeta = (projectId, version) => {
53
+ const meta = available.find((m) => m.projectId === projectId && m.version === version);
54
+ if (!meta) {
55
+ throw new Error(`Cannot find projectId=${projectId}, version=${version} in the sync resolution results.`);
56
+ }
57
+ return meta;
58
+ };
59
+ while (queue.length > 0) {
60
+ const curr = lang_utils_1.ensure(queue.shift());
61
+ result.push(curr);
62
+ queue.push(...lodash_1.default.toPairs(curr.dependencies).map(([projectId, version]) => getMeta(projectId, version)));
63
+ }
64
+ return result;
65
+ }
66
+ /**
67
+ * For a given project, check if its compatible with plasmic.json, plasmic.lock, and user
68
+ * @param meta
69
+ * @param context
70
+ */
71
+ function checkProjectMeta(meta, root, context, opts) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const projectId = meta.projectId;
74
+ const projectName = meta.projectName;
75
+ const newVersion = meta.version;
76
+ // Checks newVersion against plasmic.lock
77
+ const checkVersionLock = () => __awaiter(this, void 0, void 0, function* () {
78
+ const projectLock = context.lock.projects.find((p) => p.projectId === projectId);
79
+ const versionOnDisk = projectLock === null || projectLock === void 0 ? void 0 : projectLock.version;
80
+ if (!versionOnDisk) {
81
+ // Always sync if we haven't seen sync'ed before
82
+ return true;
83
+ }
84
+ if (semver.isLatest(versionOnDisk) &&
85
+ semver.isLatest(newVersion) &&
86
+ meta !== root) {
87
+ // If this is a dependency (not root), and we're dealing with latest dep version
88
+ // just skip, it's confusing
89
+ deps_1.logger.warn(`'${root.projectName}' depends on ${projectName}@${newVersion}. To update this project, explicitly specify this project for sync. Skipping...`);
90
+ return false;
91
+ }
92
+ if (semver.isLatest(newVersion)) {
93
+ // Always sync when version set to "latest"
94
+ return true;
95
+ }
96
+ if (semver.isLatest(versionOnDisk)) {
97
+ // Explicitly allow downgrades from "latest" to published version
98
+ return true;
99
+ }
100
+ // At this point, we can assume newVersion is always X.Y.Z (not latest)
101
+ if (semver.eq(newVersion, versionOnDisk)) {
102
+ if (opts.force) {
103
+ deps_1.logger.info(`Project '${projectName}'@${newVersion} is already up to date, but syncing anyway because --force is used`);
104
+ return true;
105
+ }
106
+ else {
107
+ deps_1.logger.info(`Project '${projectName}'@${newVersion} is already up to date; skipping. (To force an update, run again with "--force")`);
108
+ return false;
109
+ }
110
+ }
111
+ if (semver.lt(newVersion, versionOnDisk)) {
112
+ meta === root
113
+ ? deps_1.logger.warn(`The local version of '${projectName}' (${versionOnDisk}) is higher than requested version @${newVersion}. Plasmic does not support downgrading a project. You should consider updating the version range in ${config_utils_1.CONFIG_FILE_NAME}.`)
114
+ : deps_1.logger.warn(`'${root.projectName}' uses '${projectName}'@${newVersion}, but your code has '${projectName}'@${versionOnDisk}. You should consider upgrading this dependency in Plasmic Studio.`);
115
+ return false;
116
+ }
117
+ if (semver.gt(newVersion, versionOnDisk)) {
118
+ if (meta === root) {
119
+ return true;
120
+ }
121
+ else {
122
+ deps_1.logger.info(`'${root.projectName}' uses '${projectName}'@${newVersion}, but your code has version ${versionOnDisk}`);
123
+ return yield user_utils_1.confirmWithUser(`Do you want to upgrade '${projectName}' to ${newVersion}?`, opts.yes);
124
+ }
125
+ }
126
+ throw new Error(`Error comparing version=${newVersion} with the version found in plasmic.lock (${versionOnDisk}) for '${projectName}'`);
127
+ });
128
+ // Checks newVersion against plasmic.json
129
+ const checkVersionRange = () => __awaiter(this, void 0, void 0, function* () {
130
+ const projectConfig = context.config.projects.find((p) => p.projectId === projectId);
131
+ const versionRange = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.version;
132
+ // If haven't seen this before
133
+ if (!versionRange) {
134
+ // Always sync down dependencies if it's the first time to avoid compile/fix-imports error
135
+ projectId !== root.projectId
136
+ ? deps_1.logger.info(`'${root.projectName}' uses '${projectName}', which has never been synced before. We will also sync '${projectName}'@${newVersion}.`)
137
+ : deps_1.logger.info(`'${projectName} has never been synced before. Syncing...'`);
138
+ return true;
139
+ }
140
+ // If satisfies range in plasmic.json
141
+ if (semver.satisfies(newVersion, versionRange)) {
142
+ deps_1.logger.info(`Updating project '${projectName}' to ${newVersion}`);
143
+ return true;
144
+ }
145
+ deps_1.logger.warn(`${projectName}@${newVersion} falls outside the range specified in ${config_utils_1.CONFIG_FILE_NAME} (${versionRange})\nTip: To avoid this warning in the future, update your ${config_utils_1.CONFIG_FILE_NAME}.`);
146
+ return yield user_utils_1.confirmWithUser("Do you want to force it?", opts.force || opts.yes, "n");
147
+ });
148
+ const projectIds = opts.projects.length > 0
149
+ ? opts.projects
150
+ : context.config.projects.map((p) => p.projectId);
151
+ if (projectIds.includes(projectId) && opts.force) {
152
+ // if --force is used, and this is in the list of projects to sync, then
153
+ // we should always sync it, even if nothing has changed
154
+ return true;
155
+ }
156
+ return (yield checkVersionLock()) && (yield checkVersionRange());
157
+ });
158
+ }
159
+ /**
160
+ * Checks the versionResolution with plasmic.json, plasmic.lock, and user prompts
161
+ * to compute which projects should be synced
162
+ * @param versionResolution
163
+ * @param context
164
+ */
165
+ function checkVersionResolution(versionResolution, context, opts) {
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ // Fail if there's nothing to sync
168
+ if (versionResolution.projects.length <= 0) {
169
+ throw new error_1.HandledError(`Found nothing to sync. Make sure the projectId and version values are valid in ${config_utils_1.CONFIG_FILE_NAME}.`);
170
+ }
171
+ const seen = [];
172
+ const result = [];
173
+ for (const root of versionResolution.projects) {
174
+ const queue = opts.nonRecursive
175
+ ? [root]
176
+ : walkDependencyTree(root, versionResolution.dependencies).reverse();
177
+ for (const m of queue) {
178
+ // If we haven't seen this yet
179
+ if (!seen.find((p) => p.projectId === m.projectId)) {
180
+ if (yield checkProjectMeta(m, root, context, opts)) {
181
+ result.push(m);
182
+ }
183
+ seen.push(m);
184
+ }
185
+ }
186
+ }
187
+ // Ignore repeats
188
+ return result;
189
+ });
190
+ }
191
+ exports.checkVersionResolution = checkVersionResolution;
192
+ function getDependencies(projectId, version, versionResolution) {
193
+ var _a;
194
+ const filterFn = (m) => m.projectId === projectId && m.version === version;
195
+ const meta = (_a = versionResolution.projects.find(filterFn)) !== null && _a !== void 0 ? _a : versionResolution.dependencies.find(filterFn);
196
+ return meta === null || meta === void 0 ? void 0 : meta.dependencies;
197
+ }
198
+ exports.getDependencies = getDependencies;
@@ -0,0 +1,34 @@
1
+ import * as semverlib from "semver";
2
+ /**
3
+ * Wrap `semver` with support for understanding "latest"
4
+ * - "latest" is both a version and a range
5
+ * - "latest" range will match any valid version number
6
+ * - "latest" version will only match "latest" version range
7
+ **/
8
+ export declare type Version = semverlib.SemVer | "latest";
9
+ export declare const latestTag = "latest";
10
+ export declare const isLatest: (v: string) => boolean;
11
+ export declare const valid: (v: string) => string | null;
12
+ export declare const inc: (v: string, release: semverlib.ReleaseType) => string | null;
13
+ export declare const prerelease: (v: string) => readonly string[] | null;
14
+ export declare const major: (v: string) => number | "latest";
15
+ export declare const minor: (v: string) => number | "latest";
16
+ export declare const patch: (v: string) => number | "latest";
17
+ export declare const eq: (v1: string, v2: string) => boolean;
18
+ export declare const gt: (v1: string, v2: string) => boolean;
19
+ export declare const lt: (v1: string, v2: string) => boolean;
20
+ export declare const validRange: (range: string) => string;
21
+ export declare const satisfies: (v: string, range: string) => boolean;
22
+ export declare const toTildeRange: (v: string) => string | null;
23
+ export declare const toCaretRange: (v: string) => string | null;
24
+ export declare const gte: (v1: string, v2: string) => boolean;
25
+ export declare const lte: (v1: string, v2: string) => boolean;
26
+ export declare const neq: (v1: string, v2: string) => boolean;
27
+ export declare const sortAsc: (versions: string[]) => string[];
28
+ export declare const sortDesc: (versions: string[]) => string[];
29
+ export declare const minSatisfying: (versions: string[], range: string) => string | null;
30
+ export declare const maxSatisfying: (versions: string[], range: string) => string | null;
31
+ export declare const coerce: (v: string) => string | undefined;
32
+ export declare const gtr: (version: string, range: string) => boolean;
33
+ export declare const ltr: (version: string, range: string) => boolean;
34
+ export declare const outside: (version: string, range: string, hilo?: ">" | "<" | undefined) => boolean;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.outside = exports.ltr = exports.gtr = exports.coerce = exports.maxSatisfying = exports.minSatisfying = exports.sortDesc = exports.sortAsc = exports.neq = exports.lte = exports.gte = exports.toCaretRange = exports.toTildeRange = exports.satisfies = exports.validRange = exports.lt = exports.gt = exports.eq = exports.patch = exports.minor = exports.major = exports.prerelease = exports.inc = exports.valid = exports.isLatest = exports.latestTag = void 0;
26
+ const lodash_1 = __importDefault(require("lodash"));
27
+ const semverlib = __importStar(require("semver"));
28
+ exports.latestTag = "latest";
29
+ exports.isLatest = (v) => v === exports.latestTag;
30
+ exports.valid = (v) => exports.isLatest(v) ? exports.latestTag : semverlib.valid(v);
31
+ exports.inc = (v, release) => exports.isLatest(v) ? exports.latestTag : semverlib.inc(v, release);
32
+ exports.prerelease = (v) => exports.isLatest(v) ? [] : semverlib.prerelease(v);
33
+ exports.major = (v) => exports.isLatest(v) ? exports.latestTag : semverlib.major(v);
34
+ exports.minor = (v) => exports.isLatest(v) ? exports.latestTag : semverlib.minor(v);
35
+ exports.patch = (v) => exports.isLatest(v) ? exports.latestTag : semverlib.patch(v);
36
+ exports.eq = (v1, v2) => (exports.isLatest(v1) && exports.isLatest(v2)) ||
37
+ (!exports.isLatest(v1) && !exports.isLatest(v2) && semverlib.eq(v1, v2));
38
+ exports.gt = (v1, v2) => (exports.isLatest(v1) && !exports.isLatest(v2)) ||
39
+ (!exports.isLatest(v1) && !exports.isLatest(v2) && semverlib.gt(v1, v2));
40
+ exports.lt = (v1, v2) => (!exports.isLatest(v1) && exports.isLatest(v2)) ||
41
+ (!exports.isLatest(v1) && !exports.isLatest(v2) && semverlib.lt(v1, v2));
42
+ exports.validRange = (range) => exports.isLatest(range) ? exports.latestTag : semverlib.validRange(range);
43
+ exports.satisfies = (v, range) => (exports.isLatest(range) && !!exports.valid(v)) ||
44
+ (!exports.isLatest(v) && !exports.isLatest(range) && semverlib.satisfies(v, range));
45
+ exports.toTildeRange = (v) => exports.isLatest(v) ? exports.latestTag : !!semverlib.valid(v) ? "~" + v : null;
46
+ exports.toCaretRange = (v) => exports.isLatest(v) ? exports.latestTag : !!semverlib.valid(v) ? "^" + v : null;
47
+ exports.gte = (v1, v2) => exports.eq(v1, v2) || exports.gt(v1, v2);
48
+ exports.lte = (v1, v2) => exports.eq(v1, v2) || exports.lt(v1, v2);
49
+ exports.neq = (v1, v2) => !exports.eq(v1, v2);
50
+ exports.sortAsc = (versions) => lodash_1.default.cloneDeep(versions).sort((v1, v2) => exports.gt(v1, v2) ? +1 : exports.eq(v1, v2) ? 0 : -1);
51
+ exports.sortDesc = (versions) => exports.sortAsc(versions).reverse();
52
+ exports.minSatisfying = (versions, range) => { var _a; return (_a = exports.sortAsc(versions).find((v) => exports.satisfies(v, range))) !== null && _a !== void 0 ? _a : null; };
53
+ exports.maxSatisfying = (versions, range) => { var _a; return (_a = exports.sortDesc(versions).find((v) => exports.satisfies(v, range))) !== null && _a !== void 0 ? _a : null; };
54
+ exports.coerce = (v) => { var _a; return exports.isLatest(v) ? exports.latestTag : (_a = semverlib.coerce(v)) === null || _a === void 0 ? void 0 : _a.version; };
55
+ exports.gtr = (version, range) => (exports.isLatest(version) && !exports.isLatest(range)) ||
56
+ (!exports.isLatest(version) && !exports.isLatest(range) && semverlib.gtr(version, range));
57
+ exports.ltr = (version, range) => (!exports.isLatest(version) && exports.isLatest(range)) ||
58
+ (!exports.isLatest(version) && !exports.isLatest(range) && semverlib.ltr(version, range));
59
+ exports.outside = (version, range, hilo) => (hilo === ">" && exports.gtr(version, range)) ||
60
+ (hilo === "<" && exports.ltr(version, range)) ||
61
+ (!hilo && (exports.gtr(version, range) || exports.ltr(version, range)));
@@ -0,0 +1,22 @@
1
+ import * as tmp from "tmp";
2
+ import { AuthConfig, PlasmicConfig, PlasmicLoaderConfig } from "../utils/config-utils";
3
+ export declare class TempRepo {
4
+ tmpDir: tmp.DirResult;
5
+ constructor();
6
+ destroy(): void;
7
+ resolveFile(relativePath: string): string;
8
+ readFile(relativePath: string): string;
9
+ writeFile(relativePath: string, data: string): void;
10
+ deleteFile(relativePath: string): void;
11
+ checkFile(relativePath: string): boolean;
12
+ getComponentFileContents(projectId: string, componentId: string): string | undefined;
13
+ plasmicAuthPath(): string;
14
+ writePlasmicAuth(json: AuthConfig): void;
15
+ deletePlasmicAuth(): void;
16
+ plasmicJsonPath(): string;
17
+ readPlasmicJson(): PlasmicConfig;
18
+ writePlasmicJson(json: PlasmicConfig): void;
19
+ deletePlasmicJson(): void;
20
+ plasmicLoaderJsonPath(): string;
21
+ readPlasmicLoaderJson(): PlasmicLoaderConfig;
22
+ }