@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.
- package/.eslintrc.js +61 -0
- package/.idea/cli.iml +11 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README +16 -0
- package/README.internal +46 -0
- package/README.md +17 -0
- package/build.sh +8 -0
- package/dist/__mocks__/api.d.ts +16 -0
- package/dist/__mocks__/api.js +297 -0
- package/dist/__tests__/code-utils-spec.d.ts +1 -0
- package/dist/__tests__/code-utils-spec.js +838 -0
- package/dist/__tests__/ftue-spec.d.ts +1 -0
- package/dist/__tests__/ftue-spec.js +39 -0
- package/dist/__tests__/project-api-token-spec.d.ts +1 -0
- package/dist/__tests__/project-api-token-spec.js +147 -0
- package/dist/__tests__/versioned-sync-spec.d.ts +1 -0
- package/dist/__tests__/versioned-sync-spec.js +145 -0
- package/dist/actions/auth.d.ts +8 -0
- package/dist/actions/auth.js +47 -0
- package/dist/actions/fix-imports.d.ts +4 -0
- package/dist/actions/fix-imports.js +25 -0
- package/dist/actions/init.d.ts +62 -0
- package/dist/actions/init.js +460 -0
- package/dist/actions/project-token.d.ts +6 -0
- package/dist/actions/project-token.js +42 -0
- package/dist/actions/sync-components.d.ts +10 -0
- package/dist/actions/sync-components.js +242 -0
- package/dist/actions/sync-global-variants.d.ts +3 -0
- package/dist/actions/sync-global-variants.js +89 -0
- package/dist/actions/sync-icons.d.ts +7 -0
- package/dist/actions/sync-icons.js +92 -0
- package/dist/actions/sync-images.d.ts +6 -0
- package/dist/actions/sync-images.js +137 -0
- package/dist/actions/sync-styles.d.ts +3 -0
- package/dist/actions/sync-styles.js +58 -0
- package/dist/actions/sync.d.ts +25 -0
- package/dist/actions/sync.js +417 -0
- package/dist/actions/upload-bundle.d.ts +15 -0
- package/dist/actions/upload-bundle.js +28 -0
- package/dist/actions/watch.d.ts +14 -0
- package/dist/actions/watch.js +90 -0
- package/dist/api.d.ts +182 -0
- package/dist/api.js +202 -0
- package/dist/deps.d.ts +2 -0
- package/dist/deps.js +20 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +247 -0
- package/dist/lib.d.ts +10 -0
- package/dist/lib.js +23 -0
- package/dist/migrations/0.1.110-fileLocks.d.ts +2 -0
- package/dist/migrations/0.1.110-fileLocks.js +15 -0
- package/dist/migrations/0.1.143-ensureImportModuleType.d.ts +2 -0
- package/dist/migrations/0.1.143-ensureImportModuleType.js +12 -0
- package/dist/migrations/0.1.146-addReactRuntime.d.ts +2 -0
- package/dist/migrations/0.1.146-addReactRuntime.js +10 -0
- package/dist/migrations/0.1.27-migrateInit.d.ts +1 -0
- package/dist/migrations/0.1.27-migrateInit.js +8 -0
- package/dist/migrations/0.1.28-tsToTsx.d.ts +3 -0
- package/dist/migrations/0.1.28-tsToTsx.js +33 -0
- package/dist/migrations/0.1.31-ensureProjectIcons.d.ts +2 -0
- package/dist/migrations/0.1.31-ensureProjectIcons.js +12 -0
- package/dist/migrations/0.1.42-ensureVersion.d.ts +2 -0
- package/dist/migrations/0.1.42-ensureVersion.js +12 -0
- package/dist/migrations/0.1.57-ensureJsBundleThemes.d.ts +2 -0
- package/dist/migrations/0.1.57-ensureJsBundleThemes.js +12 -0
- package/dist/migrations/0.1.64-imageFiles.d.ts +2 -0
- package/dist/migrations/0.1.64-imageFiles.js +17 -0
- package/dist/migrations/0.1.95-componentType.d.ts +2 -0
- package/dist/migrations/0.1.95-componentType.js +16 -0
- package/dist/migrations/migrations.d.ts +10 -0
- package/dist/migrations/migrations.js +119 -0
- package/dist/plasmic.schema.json +463 -0
- package/dist/test-common/fixtures.d.ts +13 -0
- package/dist/test-common/fixtures.js +165 -0
- package/dist/tsconfig-transform.json +68 -0
- package/dist/utils/auth-utils.d.ts +31 -0
- package/dist/utils/auth-utils.js +236 -0
- package/dist/utils/checksum.d.ts +4 -0
- package/dist/utils/checksum.js +63 -0
- package/dist/utils/code-utils.d.ts +46 -0
- package/dist/utils/code-utils.js +457 -0
- package/dist/utils/config-utils.d.ts +271 -0
- package/dist/utils/config-utils.js +178 -0
- package/dist/utils/envdetect.d.ts +4 -0
- package/dist/utils/envdetect.js +42 -0
- package/dist/utils/error.d.ts +14 -0
- package/dist/utils/error.js +42 -0
- package/dist/utils/file-utils.d.ts +71 -0
- package/dist/utils/file-utils.js +433 -0
- package/dist/utils/get-context.d.ts +40 -0
- package/dist/utils/get-context.js +339 -0
- package/dist/utils/help.d.ts +2 -0
- package/dist/utils/help.js +56 -0
- package/dist/utils/lang-utils.d.ts +10 -0
- package/dist/utils/lang-utils.js +52 -0
- package/dist/utils/npm-utils.d.ts +28 -0
- package/dist/utils/npm-utils.js +215 -0
- package/dist/utils/prompts.d.ts +6 -0
- package/dist/utils/prompts.js +23 -0
- package/dist/utils/resolve-utils.d.ts +13 -0
- package/dist/utils/resolve-utils.js +198 -0
- package/dist/utils/semver.d.ts +34 -0
- package/dist/utils/semver.js +61 -0
- package/dist/utils/test-utils.d.ts +22 -0
- package/dist/utils/test-utils.js +106 -0
- package/dist/utils/user-utils.d.ts +7 -0
- package/dist/utils/user-utils.js +48 -0
- package/jest.config.js +6 -0
- package/package.json +80 -0
- package/src/__mocks__/api.ts +394 -0
- package/src/__tests__/code-utils-spec.ts +881 -0
- package/src/__tests__/ftue-spec.ts +43 -0
- package/src/__tests__/project-api-token-spec.ts +208 -0
- package/src/__tests__/versioned-sync-spec.ts +176 -0
- package/src/actions/auth.ts +43 -0
- package/src/actions/fix-imports.ts +13 -0
- package/src/actions/init.ts +638 -0
- package/src/actions/project-token.ts +36 -0
- package/src/actions/sync-components.ts +405 -0
- package/src/actions/sync-global-variants.ts +129 -0
- package/src/actions/sync-icons.ts +135 -0
- package/src/actions/sync-images.ts +191 -0
- package/src/actions/sync-styles.ts +71 -0
- package/src/actions/sync.ts +747 -0
- package/src/actions/upload-bundle.ts +38 -0
- package/src/actions/watch.ts +95 -0
- package/src/api.ts +407 -0
- package/src/deps.ts +18 -0
- package/src/index.ts +300 -0
- package/src/lib.ts +10 -0
- package/src/migrations/0.1.110-fileLocks.ts +16 -0
- package/src/migrations/0.1.146-addReactRuntime.ts +8 -0
- package/src/migrations/0.1.27-migrateInit.ts +4 -0
- package/src/migrations/0.1.28-tsToTsx.ts +37 -0
- package/src/migrations/0.1.31-ensureProjectIcons.ts +10 -0
- package/src/migrations/0.1.42-ensureVersion.ts +10 -0
- package/src/migrations/0.1.57-ensureJsBundleThemes.ts +10 -0
- package/src/migrations/0.1.64-imageFiles.ts +15 -0
- package/src/migrations/0.1.95-componentType.ts +14 -0
- package/src/migrations/migrations.ts +147 -0
- package/src/test-common/fixtures.ts +178 -0
- package/src/utils/auth-utils.ts +276 -0
- package/src/utils/checksum.ts +106 -0
- package/src/utils/code-utils.ts +656 -0
- package/src/utils/config-utils.ts +551 -0
- package/src/utils/envdetect.ts +39 -0
- package/src/utils/error.ts +36 -0
- package/src/utils/file-utils.ts +526 -0
- package/src/utils/get-context.ts +451 -0
- package/src/utils/help.ts +75 -0
- package/src/utils/lang-utils.ts +52 -0
- package/src/utils/npm-utils.ts +223 -0
- package/src/utils/prompts.ts +22 -0
- package/src/utils/resolve-utils.ts +245 -0
- package/src/utils/semver.ts +67 -0
- package/src/utils/test-utils.ts +116 -0
- package/src/utils/user-utils.ts +37 -0
- package/testData/fixImports_plasmic.json +66 -0
- package/tsconfig-transform.json +68 -0
- package/tsconfig.json +67 -0
|
@@ -0,0 +1,457 @@
|
|
|
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.formatScript = exports.maybeConvertTsxToJsx = exports.tsxToJsx = exports.fixAllImportStatements = exports.mkFixImportContext = exports.isLocalModulePath = exports.replaceImports = exports.ensureImportDefaultSpecifier = exports.ensureImportSpecifierWithAlias = exports.formatAsLocal = void 0;
|
|
35
|
+
const babel = __importStar(require("@babel/core"));
|
|
36
|
+
const generator_1 = __importDefault(require("@babel/generator"));
|
|
37
|
+
const parser = __importStar(require("@babel/parser"));
|
|
38
|
+
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
39
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
40
|
+
const Prettier = __importStar(require("prettier"));
|
|
41
|
+
const prettier_1 = require("prettier");
|
|
42
|
+
const ts = __importStar(require("typescript"));
|
|
43
|
+
const upath_1 = __importDefault(require("upath"));
|
|
44
|
+
const sync_images_1 = require("../actions/sync-images");
|
|
45
|
+
const deps_1 = require("../deps");
|
|
46
|
+
const error_1 = require("../utils/error");
|
|
47
|
+
const file_utils_1 = require("./file-utils");
|
|
48
|
+
const lang_utils_1 = require("./lang-utils");
|
|
49
|
+
exports.formatAsLocal = (content, filePath, baseDir, defaultOpts = {}) => {
|
|
50
|
+
const opts = prettier_1.resolveConfig.sync(baseDir) || defaultOpts;
|
|
51
|
+
opts.filepath = filePath;
|
|
52
|
+
// Running Prettier multiple times may actually yield different results!
|
|
53
|
+
// Here we run it twice, just to be safe... :-/
|
|
54
|
+
const res = Prettier.format(content, opts);
|
|
55
|
+
const res2 = Prettier.format(res, opts);
|
|
56
|
+
return res2;
|
|
57
|
+
};
|
|
58
|
+
const nodeToFormattedCode = (n, baseDir, unformatted, commentsToRemove) => {
|
|
59
|
+
const c = generator_1.default(n, {
|
|
60
|
+
retainLines: true,
|
|
61
|
+
shouldPrintComment: (c) => !commentsToRemove || !commentsToRemove.has(c),
|
|
62
|
+
}).code;
|
|
63
|
+
return unformatted
|
|
64
|
+
? c
|
|
65
|
+
: exports.formatAsLocal(c, "/tmp/x.tsx", baseDir, {
|
|
66
|
+
trailingComma: "none",
|
|
67
|
+
arrowParens: "avoid",
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
function findImportSpecifierWithAlias(importDecl, local) {
|
|
71
|
+
for (const spec of importDecl.specifiers) {
|
|
72
|
+
if (spec.type === "ImportSpecifier" && spec.local.name === local) {
|
|
73
|
+
return spec;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
function findImportDefaultSpecifier(importDecl) {
|
|
79
|
+
for (const spec of importDecl.specifiers) {
|
|
80
|
+
if (spec.type === "ImportDefaultSpecifier") {
|
|
81
|
+
return spec;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
function ensureImportSpecifierWithAlias(decl, imported, alias) {
|
|
87
|
+
const existing = findImportSpecifierWithAlias(decl, alias);
|
|
88
|
+
if (existing) {
|
|
89
|
+
if (existing.imported.type === "Identifier") {
|
|
90
|
+
existing.imported.name = imported;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
existing.imported.value = imported;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
decl.specifiers = decl.specifiers.filter((specifier) => {
|
|
98
|
+
if (specifier.type === "ImportDefaultSpecifier" &&
|
|
99
|
+
specifier.local.name === alias) {
|
|
100
|
+
// If we are importing a default for a name that will collide with our
|
|
101
|
+
// desired alias, then the default import is wrong and we skip it.
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
});
|
|
106
|
+
decl.specifiers.push(babel.types.importSpecifier(babel.types.identifier(alias), babel.types.identifier(imported)));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.ensureImportSpecifierWithAlias = ensureImportSpecifierWithAlias;
|
|
110
|
+
function ensureImportDefaultSpecifier(decl, defaultExport) {
|
|
111
|
+
const existing = findImportDefaultSpecifier(decl);
|
|
112
|
+
if (existing) {
|
|
113
|
+
existing.local.name = defaultExport;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
decl.specifiers.splice(0, 0, babel.types.importDefaultSpecifier(babel.types.identifier(defaultExport)));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.ensureImportDefaultSpecifier = ensureImportDefaultSpecifier;
|
|
120
|
+
function tryParsePlasmicImportSpec(node) {
|
|
121
|
+
var _a;
|
|
122
|
+
const c = (_a = node.trailingComments) === null || _a === void 0 ? void 0 : _a[0];
|
|
123
|
+
if (!c) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
const m = c.value.match(/plasmic-import:\s+([\w-]+)(?:\/(component|css|render|globalVariant|projectcss|defaultcss|icon|picture|jsBundle|codeComponent))?/);
|
|
127
|
+
if (m) {
|
|
128
|
+
return { id: m[1], type: m[2] };
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
function filterUnformattedMarker(code, changed) {
|
|
133
|
+
const lines = code.split("\n");
|
|
134
|
+
const isUnformattedMarker = (line) => line.trim() === "// plasmic-unformatted";
|
|
135
|
+
changed = changed || lines.some(isUnformattedMarker);
|
|
136
|
+
code = lines.filter((line) => !isUnformattedMarker(line)).join("\n");
|
|
137
|
+
return [code, changed];
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Given the argument `code` string, for module at `fromPath`, replaces all Plasmic imports
|
|
141
|
+
* for modules found in `compConfigsMap`.
|
|
142
|
+
*/
|
|
143
|
+
function replaceImports(context, code, fromPath, fixImportContext, removeImportDirective, baseDir, changed = false) {
|
|
144
|
+
[code, changed] = filterUnformattedMarker(code, changed);
|
|
145
|
+
const file = parser.parse(code, {
|
|
146
|
+
strictMode: true,
|
|
147
|
+
sourceType: "module",
|
|
148
|
+
plugins: [
|
|
149
|
+
// At a minimum, we need to parse jsx and typescript
|
|
150
|
+
"jsx",
|
|
151
|
+
"typescript",
|
|
152
|
+
// There are also various features that people may have... May just
|
|
153
|
+
// need to add as we encounter them...
|
|
154
|
+
"classProperties",
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
const commentsToRemove = new Set();
|
|
158
|
+
file.program.body.forEach((stmt) => {
|
|
159
|
+
var _a;
|
|
160
|
+
if (stmt.type !== "ImportDeclaration") {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const importStmt = stmt;
|
|
164
|
+
const spec = tryParsePlasmicImportSpec(importStmt);
|
|
165
|
+
if (!spec) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
changed = true;
|
|
169
|
+
if (removeImportDirective) {
|
|
170
|
+
commentsToRemove.add(((_a = stmt.trailingComments) === null || _a === void 0 ? void 0 : _a[0].value) || "");
|
|
171
|
+
}
|
|
172
|
+
const type = spec.type;
|
|
173
|
+
const uuid = spec.id;
|
|
174
|
+
if (type === "component") {
|
|
175
|
+
// instantiation of a mapped or managed component
|
|
176
|
+
const compConfig = fixImportContext.components[uuid];
|
|
177
|
+
if (!compConfig) {
|
|
178
|
+
throwMissingReference(context, "component", uuid, fromPath);
|
|
179
|
+
}
|
|
180
|
+
const { modulePath, exportName } = compConfig.importSpec;
|
|
181
|
+
if (exportName) {
|
|
182
|
+
// ensure import { ${exportName} as ${compConfig.name} }
|
|
183
|
+
ensureImportSpecifierWithAlias(stmt, exportName, compConfig.name);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// Keep the same name as it might be different from compConfig.name due
|
|
187
|
+
// to name collisions.
|
|
188
|
+
// ensureImportDefaultSpecifier(stmt, compConfig.name);
|
|
189
|
+
}
|
|
190
|
+
const realPath = makeImportPath(context, fromPath, modulePath, true);
|
|
191
|
+
stmt.source.value = realPath;
|
|
192
|
+
}
|
|
193
|
+
else if (type === "render") {
|
|
194
|
+
// import of the PP blackbox
|
|
195
|
+
const compConfig = fixImportContext.components[uuid];
|
|
196
|
+
if (!compConfig) {
|
|
197
|
+
throwMissingReference(context, "component", uuid, fromPath);
|
|
198
|
+
}
|
|
199
|
+
const realPath = makeImportPath(context, fromPath, compConfig.renderModuleFilePath, true);
|
|
200
|
+
stmt.source.value = realPath;
|
|
201
|
+
}
|
|
202
|
+
else if (type === "css") {
|
|
203
|
+
// import of the PP css file
|
|
204
|
+
const compConfig = fixImportContext.components[uuid];
|
|
205
|
+
if (!compConfig) {
|
|
206
|
+
throwMissingReference(context, "component", uuid, fromPath);
|
|
207
|
+
}
|
|
208
|
+
const realPath = makeImportPath(context, fromPath, compConfig.cssFilePath, false);
|
|
209
|
+
stmt.source.value = realPath;
|
|
210
|
+
}
|
|
211
|
+
else if (type === "globalVariant") {
|
|
212
|
+
// import of global context
|
|
213
|
+
const variantConfig = fixImportContext.globalVariants[uuid];
|
|
214
|
+
if (!variantConfig) {
|
|
215
|
+
throwMissingReference(context, "global variant", uuid, fromPath);
|
|
216
|
+
}
|
|
217
|
+
const realPath = makeImportPath(context, fromPath, variantConfig.contextFilePath, true);
|
|
218
|
+
stmt.source.value = realPath;
|
|
219
|
+
}
|
|
220
|
+
else if (type === "icon") {
|
|
221
|
+
// import of global context
|
|
222
|
+
const iconConfig = fixImportContext.icons[uuid];
|
|
223
|
+
if (!iconConfig) {
|
|
224
|
+
throwMissingReference(context, "icon", uuid, fromPath);
|
|
225
|
+
}
|
|
226
|
+
const realPath = makeImportPath(context, fromPath, iconConfig.moduleFilePath, true);
|
|
227
|
+
stmt.source.value = realPath;
|
|
228
|
+
}
|
|
229
|
+
else if (type === "picture") {
|
|
230
|
+
const imageConfig = fixImportContext.images[uuid];
|
|
231
|
+
if (!imageConfig) {
|
|
232
|
+
throwMissingReference(context, "image", uuid, fromPath);
|
|
233
|
+
}
|
|
234
|
+
const realPath = makeImportPath(context, fromPath, imageConfig.filePath, false);
|
|
235
|
+
stmt.source.value = realPath;
|
|
236
|
+
}
|
|
237
|
+
else if (type === "projectcss") {
|
|
238
|
+
const projectConfig = fixImportContext.projects[uuid];
|
|
239
|
+
if (!projectConfig) {
|
|
240
|
+
throwMissingReference(context, "project", uuid, fromPath);
|
|
241
|
+
}
|
|
242
|
+
const realPath = makeImportPath(context, fromPath, projectConfig.cssFilePath, false);
|
|
243
|
+
stmt.source.value = realPath;
|
|
244
|
+
}
|
|
245
|
+
else if (type === "defaultcss") {
|
|
246
|
+
const realPath = makeImportPath(context, fromPath, fixImportContext.config.style.defaultStyleCssFilePath, false);
|
|
247
|
+
stmt.source.value = realPath;
|
|
248
|
+
}
|
|
249
|
+
else if (type === "codeComponent") {
|
|
250
|
+
const meta = fixImportContext.codeComponentMetas[uuid];
|
|
251
|
+
if (meta.componentImportPath[0] === ".") {
|
|
252
|
+
// Relative path from the project root
|
|
253
|
+
const toPath = upath_1.default.join(process.cwd(), meta.componentImportPath);
|
|
254
|
+
lang_utils_1.assert(upath_1.default.isAbsolute(toPath));
|
|
255
|
+
const realPath = makeImportPath(context, fromPath, toPath, true, true);
|
|
256
|
+
stmt.source.value = realPath;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
// npm package
|
|
260
|
+
stmt.source.value = meta.componentImportPath;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
if (!changed) {
|
|
265
|
+
return code;
|
|
266
|
+
}
|
|
267
|
+
return nodeToFormattedCode(file, baseDir, !changed, commentsToRemove);
|
|
268
|
+
}
|
|
269
|
+
exports.replaceImports = replaceImports;
|
|
270
|
+
function throwMissingReference(context, itemType, uuid, fromPath) {
|
|
271
|
+
let recFix = `Please make sure projects that "${uuid}" depends on have already been synced.`;
|
|
272
|
+
if (context.cliArgs.nonRecursive) {
|
|
273
|
+
recFix = `Please run "plasmic sync" without the --non-recursive flag to sync dependencies.`;
|
|
274
|
+
}
|
|
275
|
+
else if (!context.cliArgs.force) {
|
|
276
|
+
recFix = `Please run "plasmic sync" with --force flag to force-sync the dependencies.`;
|
|
277
|
+
}
|
|
278
|
+
throw new error_1.HandledError(`Encountered Plasmic ${itemType} "${uuid}" in ${fromPath} that are being used but have not been synced. ${recFix}`);
|
|
279
|
+
}
|
|
280
|
+
function makeImportPath(context, fromPath, toPath, stripExt, forceRelative = false) {
|
|
281
|
+
let result = toPath;
|
|
282
|
+
if (forceRelative || isLocalModulePath(toPath)) {
|
|
283
|
+
result = upath_1.default.relative(file_utils_1.makeFilePath(context, upath_1.default.dirname(fromPath)), file_utils_1.makeFilePath(context, toPath));
|
|
284
|
+
if (!result.startsWith(".")) {
|
|
285
|
+
result = `./${result}`;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (stripExt) {
|
|
289
|
+
result = file_utils_1.stripExtension(result);
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Returns true if the argument ComponentConfig.importSpec.modulePath is referencing
|
|
295
|
+
* a local file
|
|
296
|
+
*/
|
|
297
|
+
function isLocalModulePath(modulePath) {
|
|
298
|
+
// It is a local file reference if the importPath includes the file extension
|
|
299
|
+
return !!upath_1.default.extname(modulePath);
|
|
300
|
+
}
|
|
301
|
+
exports.isLocalModulePath = isLocalModulePath;
|
|
302
|
+
exports.mkFixImportContext = (config) => {
|
|
303
|
+
const allComponents = lang_utils_1.flatMap(config.projects, (p) => p.components);
|
|
304
|
+
const components = lodash_1.default.keyBy(allComponents, (c) => c.id);
|
|
305
|
+
const allCodeComponents = lang_utils_1.flatMap(config.projects, (p) => p.codeComponents || []);
|
|
306
|
+
const codeComponentMetas = lodash_1.default.keyBy(allCodeComponents, (c) => c.id);
|
|
307
|
+
const globalVariants = lodash_1.default.keyBy(config.globalVariants.variantGroups, (c) => c.id);
|
|
308
|
+
const icons = lodash_1.default.keyBy(lang_utils_1.flatMap(config.projects, (p) => p.icons), (c) => c.id);
|
|
309
|
+
const images = lodash_1.default.keyBy(lang_utils_1.flatMap(config.projects, (p) => p.images), (c) => c.id);
|
|
310
|
+
const projects = lodash_1.default.keyBy(config.projects, (p) => p.projectId);
|
|
311
|
+
return {
|
|
312
|
+
config,
|
|
313
|
+
components,
|
|
314
|
+
codeComponentMetas,
|
|
315
|
+
globalVariants,
|
|
316
|
+
icons,
|
|
317
|
+
images,
|
|
318
|
+
projects,
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
/**
|
|
322
|
+
* Assuming that all the files referenced in PlasmicConfig are correct, fixes import statements using PlasmicConfig
|
|
323
|
+
* file locations as the source of truth.
|
|
324
|
+
*/
|
|
325
|
+
function fixAllImportStatements(context, baseDir, summary) {
|
|
326
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
deps_1.logger.info("Fixing import statements...");
|
|
328
|
+
const config = context.config;
|
|
329
|
+
const fixImportContext = exports.mkFixImportContext(config);
|
|
330
|
+
for (const project of config.projects) {
|
|
331
|
+
for (const compConfig of project.components) {
|
|
332
|
+
const compSummary = summary === null || summary === void 0 ? void 0 : summary.get(compConfig.id);
|
|
333
|
+
if (summary && !compSummary) {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
const fixSkeletonModule = compSummary
|
|
337
|
+
? compSummary.skeletonModuleModified
|
|
338
|
+
: true;
|
|
339
|
+
if (!summary || compSummary) {
|
|
340
|
+
yield fixComponentImportStatements(context, compConfig, fixImportContext, fixSkeletonModule, baseDir);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
exports.fixAllImportStatements = fixAllImportStatements;
|
|
347
|
+
function fixComponentImportStatements(context, compConfig, fixImportContext, fixSkeletonModule, baseDir) {
|
|
348
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
349
|
+
// If ComponentConfig.importPath is still a local file, we best-effort also fix up the import statements there.
|
|
350
|
+
if (isLocalModulePath(compConfig.importSpec.modulePath) &&
|
|
351
|
+
fixSkeletonModule) {
|
|
352
|
+
yield fixFileImportStatements(context, compConfig.importSpec.modulePath, fixImportContext, true, baseDir);
|
|
353
|
+
}
|
|
354
|
+
let renderModuleChanged = false;
|
|
355
|
+
if (["files", "public-files"].includes(context.config.images.scheme)) {
|
|
356
|
+
yield sync_images_1.fixComponentCssReferences(context, fixImportContext, compConfig.cssFilePath);
|
|
357
|
+
if (context.config.images.scheme === "public-files") {
|
|
358
|
+
renderModuleChanged = yield sync_images_1.fixComponentImagesReferences(context, fixImportContext, compConfig.renderModuleFilePath);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
// Fix file imports and run prettier just after fixing image references
|
|
362
|
+
yield fixFileImportStatements(context, compConfig.renderModuleFilePath, fixImportContext, false, baseDir, renderModuleChanged);
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
function fixFileImportStatements(context, srcDirFilePath, fixImportContext, removeImportDirective, baseDir, fileHasChanged = false) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
const filePath = file_utils_1.makeFilePath(context, srcDirFilePath);
|
|
368
|
+
if (!file_utils_1.existsBuffered(filePath)) {
|
|
369
|
+
deps_1.logger.warn(`Cannot fix imports in non-existing file ${srcDirFilePath}. Check your plasmic.json file for invalid entries.`);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const prevContent = file_utils_1.readFileText(filePath).toString();
|
|
373
|
+
const newContent = replaceImports(context, prevContent, srcDirFilePath, fixImportContext, removeImportDirective, baseDir, fileHasChanged);
|
|
374
|
+
if (prevContent !== newContent) {
|
|
375
|
+
yield file_utils_1.writeFileContent(context, srcDirFilePath, newContent, {
|
|
376
|
+
force: true,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
class CompilerOptions {
|
|
382
|
+
static getOpts() {
|
|
383
|
+
if (!this.opts) {
|
|
384
|
+
let curDir = __dirname;
|
|
385
|
+
let configPath = "";
|
|
386
|
+
do {
|
|
387
|
+
curDir = upath_1.default.join(curDir, "..");
|
|
388
|
+
configPath = upath_1.default.join(curDir, "tsconfig-transform.json");
|
|
389
|
+
} while (!file_utils_1.existsBuffered(configPath));
|
|
390
|
+
const c = ts.readConfigFile(configPath, (path) => file_utils_1.readFileText(path));
|
|
391
|
+
this.opts = ts.convertCompilerOptionsFromJson(c.config.compilerOptions, ".").options;
|
|
392
|
+
}
|
|
393
|
+
return this.opts;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
CompilerOptions.opts = undefined;
|
|
397
|
+
exports.tsxToJsx = (code) => {
|
|
398
|
+
// when the code has jsx pragma, typescript will remove comments, and remove
|
|
399
|
+
// "import React from 'React'" if React is unused. So we first invalidate it.
|
|
400
|
+
// We also need to add the usageMagic to prevent typescript from remove the
|
|
401
|
+
// import of ncreatePlasmicElementProxy.
|
|
402
|
+
const usageMagic = "\ncreatePlasmicElementProxy();";
|
|
403
|
+
const jsxPragmas = ["jsx", "jsxFrag", "jsxRuntime", "jsxImportSource"];
|
|
404
|
+
function prepForTranspile(str) {
|
|
405
|
+
for (const p of jsxPragmas) {
|
|
406
|
+
str = str.replace(`/** @${p} `, `/** @ ${p} `);
|
|
407
|
+
}
|
|
408
|
+
return str + usageMagic;
|
|
409
|
+
}
|
|
410
|
+
function fixPostTranspile(str) {
|
|
411
|
+
for (const p of jsxPragmas) {
|
|
412
|
+
str = str.replace(`/** @ ${p} `, `/** @${p} `);
|
|
413
|
+
}
|
|
414
|
+
str = str.replace(usageMagic, "");
|
|
415
|
+
return str;
|
|
416
|
+
}
|
|
417
|
+
let result = ts.transpileModule(prepForTranspile(code), {
|
|
418
|
+
compilerOptions: CompilerOptions.getOpts(),
|
|
419
|
+
});
|
|
420
|
+
return fixPostTranspile(result.outputText);
|
|
421
|
+
};
|
|
422
|
+
function maybeConvertTsxToJsx(fileName, content, baseDir) {
|
|
423
|
+
if (fileName.endsWith("tsx")) {
|
|
424
|
+
const jsFileName = file_utils_1.stripExtension(fileName) + ".jsx";
|
|
425
|
+
const jsContent = exports.formatScript(exports.tsxToJsx(content), baseDir);
|
|
426
|
+
return [jsFileName, jsContent];
|
|
427
|
+
}
|
|
428
|
+
return [fileName, content];
|
|
429
|
+
}
|
|
430
|
+
exports.maybeConvertTsxToJsx = maybeConvertTsxToJsx;
|
|
431
|
+
exports.formatScript = (code, baseDir) => {
|
|
432
|
+
const file = parser.parse(code, {
|
|
433
|
+
strictMode: true,
|
|
434
|
+
sourceType: "module",
|
|
435
|
+
plugins: ["jsx", "typescript"],
|
|
436
|
+
});
|
|
437
|
+
let newLineMarker = "THIS_SHALL_BE_NEW_LINE";
|
|
438
|
+
while (code.includes(newLineMarker)) {
|
|
439
|
+
newLineMarker = newLineMarker + "_REALLY";
|
|
440
|
+
}
|
|
441
|
+
traverse_1.default(file, {
|
|
442
|
+
Statement: function (path) {
|
|
443
|
+
if (file.program.body.includes(path.node) &&
|
|
444
|
+
path.node.type !== "ImportDeclaration") {
|
|
445
|
+
path.insertBefore(babel.types.stringLiteral(newLineMarker));
|
|
446
|
+
path.skip();
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
});
|
|
450
|
+
const withmarkers = nodeToFormattedCode(file, baseDir, true);
|
|
451
|
+
const withNewLines = withmarkers.replace(new RegExp(`"${newLineMarker}"`, "g"), "\n");
|
|
452
|
+
return exports.formatAsLocal(withNewLines, "/tmp/x.tsx", baseDir, {
|
|
453
|
+
printWidth: 80,
|
|
454
|
+
tabWidth: 2,
|
|
455
|
+
useTabs: false,
|
|
456
|
+
});
|
|
457
|
+
};
|